drm/amd/display: Drop CONFIG_DRM_AMD_DC_DCN2_0 and DSC_SUPPORTED
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / display / dc / dce110 / dce110_hw_sequencer.c
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25
26 #include <linux/delay.h>
27
28 #include "dm_services.h"
29 #include "dc.h"
30 #include "dc_bios_types.h"
31 #include "core_types.h"
32 #include "core_status.h"
33 #include "resource.h"
34 #include "dm_helpers.h"
35 #include "dce110_hw_sequencer.h"
36 #include "dce110_timing_generator.h"
37 #include "dce/dce_hwseq.h"
38 #include "gpio_service_interface.h"
39
40 #include "dce110_compressor.h"
41
42 #include "bios/bios_parser_helper.h"
43 #include "timing_generator.h"
44 #include "mem_input.h"
45 #include "opp.h"
46 #include "ipp.h"
47 #include "transform.h"
48 #include "stream_encoder.h"
49 #include "link_encoder.h"
50 #include "link_hwss.h"
51 #include "clock_source.h"
52 #include "clk_mgr.h"
53 #include "abm.h"
54 #include "audio.h"
55 #include "reg_helper.h"
56
57 /* include DCE11 register header files */
58 #include "dce/dce_11_0_d.h"
59 #include "dce/dce_11_0_sh_mask.h"
60 #include "custom_float.h"
61
62 #include "atomfirmware.h"
63
64 /*
65  * All values are in milliseconds;
66  * For eDP, after power-up/power/down,
67  * 300/500 msec max. delay from LCDVCC to black video generation
68  */
69 #define PANEL_POWER_UP_TIMEOUT 300
70 #define PANEL_POWER_DOWN_TIMEOUT 500
71 #define HPD_CHECK_INTERVAL 10
72
73 #define CTX \
74         hws->ctx
75
76 #define DC_LOGGER_INIT()
77
78 #define REG(reg)\
79         hws->regs->reg
80
81 #undef FN
82 #define FN(reg_name, field_name) \
83         hws->shifts->field_name, hws->masks->field_name
84
85 struct dce110_hw_seq_reg_offsets {
86         uint32_t crtc;
87 };
88
89 static const struct dce110_hw_seq_reg_offsets reg_offsets[] = {
90 {
91         .crtc = (mmCRTC0_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
92 },
93 {
94         .crtc = (mmCRTC1_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
95 },
96 {
97         .crtc = (mmCRTC2_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
98 },
99 {
100         .crtc = (mmCRTCV_GSL_CONTROL - mmCRTC_GSL_CONTROL),
101 }
102 };
103
104 #define HW_REG_BLND(reg, id)\
105         (reg + reg_offsets[id].blnd)
106
107 #define HW_REG_CRTC(reg, id)\
108         (reg + reg_offsets[id].crtc)
109
110 #define MAX_WATERMARK 0xFFFF
111 #define SAFE_NBP_MARK 0x7FFF
112
113 /*******************************************************************************
114  * Private definitions
115  ******************************************************************************/
116 /***************************PIPE_CONTROL***********************************/
117 static void dce110_init_pte(struct dc_context *ctx)
118 {
119         uint32_t addr;
120         uint32_t value = 0;
121         uint32_t chunk_int = 0;
122         uint32_t chunk_mul = 0;
123
124         addr = mmUNP_DVMM_PTE_CONTROL;
125         value = dm_read_reg(ctx, addr);
126
127         set_reg_field_value(
128                 value,
129                 0,
130                 DVMM_PTE_CONTROL,
131                 DVMM_USE_SINGLE_PTE);
132
133         set_reg_field_value(
134                 value,
135                 1,
136                 DVMM_PTE_CONTROL,
137                 DVMM_PTE_BUFFER_MODE0);
138
139         set_reg_field_value(
140                 value,
141                 1,
142                 DVMM_PTE_CONTROL,
143                 DVMM_PTE_BUFFER_MODE1);
144
145         dm_write_reg(ctx, addr, value);
146
147         addr = mmDVMM_PTE_REQ;
148         value = dm_read_reg(ctx, addr);
149
150         chunk_int = get_reg_field_value(
151                 value,
152                 DVMM_PTE_REQ,
153                 HFLIP_PTEREQ_PER_CHUNK_INT);
154
155         chunk_mul = get_reg_field_value(
156                 value,
157                 DVMM_PTE_REQ,
158                 HFLIP_PTEREQ_PER_CHUNK_MULTIPLIER);
159
160         if (chunk_int != 0x4 || chunk_mul != 0x4) {
161
162                 set_reg_field_value(
163                         value,
164                         255,
165                         DVMM_PTE_REQ,
166                         MAX_PTEREQ_TO_ISSUE);
167
168                 set_reg_field_value(
169                         value,
170                         4,
171                         DVMM_PTE_REQ,
172                         HFLIP_PTEREQ_PER_CHUNK_INT);
173
174                 set_reg_field_value(
175                         value,
176                         4,
177                         DVMM_PTE_REQ,
178                         HFLIP_PTEREQ_PER_CHUNK_MULTIPLIER);
179
180                 dm_write_reg(ctx, addr, value);
181         }
182 }
183 /**************************************************************************/
184
185 static void enable_display_pipe_clock_gating(
186         struct dc_context *ctx,
187         bool clock_gating)
188 {
189         /*TODO*/
190 }
191
192 static bool dce110_enable_display_power_gating(
193         struct dc *dc,
194         uint8_t controller_id,
195         struct dc_bios *dcb,
196         enum pipe_gating_control power_gating)
197 {
198         enum bp_result bp_result = BP_RESULT_OK;
199         enum bp_pipe_control_action cntl;
200         struct dc_context *ctx = dc->ctx;
201         unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
202
203         if (IS_FPGA_MAXIMUS_DC(ctx->dce_environment))
204                 return true;
205
206         if (power_gating == PIPE_GATING_CONTROL_INIT)
207                 cntl = ASIC_PIPE_INIT;
208         else if (power_gating == PIPE_GATING_CONTROL_ENABLE)
209                 cntl = ASIC_PIPE_ENABLE;
210         else
211                 cntl = ASIC_PIPE_DISABLE;
212
213         if (controller_id == underlay_idx)
214                 controller_id = CONTROLLER_ID_UNDERLAY0 - 1;
215
216         if (power_gating != PIPE_GATING_CONTROL_INIT || controller_id == 0){
217
218                 bp_result = dcb->funcs->enable_disp_power_gating(
219                                                 dcb, controller_id + 1, cntl);
220
221                 /* Revert MASTER_UPDATE_MODE to 0 because bios sets it 2
222                  * by default when command table is called
223                  *
224                  * Bios parser accepts controller_id = 6 as indicative of
225                  * underlay pipe in dce110. But we do not support more
226                  * than 3.
227                  */
228                 if (controller_id < CONTROLLER_ID_MAX - 1)
229                         dm_write_reg(ctx,
230                                 HW_REG_CRTC(mmCRTC_MASTER_UPDATE_MODE, controller_id),
231                                 0);
232         }
233
234         if (power_gating != PIPE_GATING_CONTROL_ENABLE)
235                 dce110_init_pte(ctx);
236
237         if (bp_result == BP_RESULT_OK)
238                 return true;
239         else
240                 return false;
241 }
242
243 static void build_prescale_params(struct ipp_prescale_params *prescale_params,
244                 const struct dc_plane_state *plane_state)
245 {
246         prescale_params->mode = IPP_PRESCALE_MODE_FIXED_UNSIGNED;
247
248         switch (plane_state->format) {
249         case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
250                 prescale_params->scale = 0x2082;
251                 break;
252         case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
253         case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
254                 prescale_params->scale = 0x2020;
255                 break;
256         case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
257         case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
258                 prescale_params->scale = 0x2008;
259                 break;
260         case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
261         case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
262                 prescale_params->scale = 0x2000;
263                 break;
264         default:
265                 ASSERT(false);
266                 break;
267         }
268 }
269
270 static bool
271 dce110_set_input_transfer_func(struct pipe_ctx *pipe_ctx,
272                                const struct dc_plane_state *plane_state)
273 {
274         struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
275         const struct dc_transfer_func *tf = NULL;
276         struct ipp_prescale_params prescale_params = { 0 };
277         bool result = true;
278
279         if (ipp == NULL)
280                 return false;
281
282         if (plane_state->in_transfer_func)
283                 tf = plane_state->in_transfer_func;
284
285         build_prescale_params(&prescale_params, plane_state);
286         ipp->funcs->ipp_program_prescale(ipp, &prescale_params);
287
288         if (plane_state->gamma_correction &&
289                         !plane_state->gamma_correction->is_identity &&
290                         dce_use_lut(plane_state->format))
291                 ipp->funcs->ipp_program_input_lut(ipp, plane_state->gamma_correction);
292
293         if (tf == NULL) {
294                 /* Default case if no input transfer function specified */
295                 ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_sRGB);
296         } else if (tf->type == TF_TYPE_PREDEFINED) {
297                 switch (tf->tf) {
298                 case TRANSFER_FUNCTION_SRGB:
299                         ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_sRGB);
300                         break;
301                 case TRANSFER_FUNCTION_BT709:
302                         ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_xvYCC);
303                         break;
304                 case TRANSFER_FUNCTION_LINEAR:
305                         ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_BYPASS);
306                         break;
307                 case TRANSFER_FUNCTION_PQ:
308                 default:
309                         result = false;
310                         break;
311                 }
312         } else if (tf->type == TF_TYPE_BYPASS) {
313                 ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_BYPASS);
314         } else {
315                 /*TF_TYPE_DISTRIBUTED_POINTS - Not supported in DCE 11*/
316                 result = false;
317         }
318
319         return result;
320 }
321
322 static bool convert_to_custom_float(struct pwl_result_data *rgb_resulted,
323                                     struct curve_points *arr_points,
324                                     uint32_t hw_points_num)
325 {
326         struct custom_float_format fmt;
327
328         struct pwl_result_data *rgb = rgb_resulted;
329
330         uint32_t i = 0;
331
332         fmt.exponenta_bits = 6;
333         fmt.mantissa_bits = 12;
334         fmt.sign = true;
335
336         if (!convert_to_custom_float_format(arr_points[0].x, &fmt,
337                                             &arr_points[0].custom_float_x)) {
338                 BREAK_TO_DEBUGGER();
339                 return false;
340         }
341
342         if (!convert_to_custom_float_format(arr_points[0].offset, &fmt,
343                                             &arr_points[0].custom_float_offset)) {
344                 BREAK_TO_DEBUGGER();
345                 return false;
346         }
347
348         if (!convert_to_custom_float_format(arr_points[0].slope, &fmt,
349                                             &arr_points[0].custom_float_slope)) {
350                 BREAK_TO_DEBUGGER();
351                 return false;
352         }
353
354         fmt.mantissa_bits = 10;
355         fmt.sign = false;
356
357         if (!convert_to_custom_float_format(arr_points[1].x, &fmt,
358                                             &arr_points[1].custom_float_x)) {
359                 BREAK_TO_DEBUGGER();
360                 return false;
361         }
362
363         if (!convert_to_custom_float_format(arr_points[1].y, &fmt,
364                                             &arr_points[1].custom_float_y)) {
365                 BREAK_TO_DEBUGGER();
366                 return false;
367         }
368
369         if (!convert_to_custom_float_format(arr_points[1].slope, &fmt,
370                                             &arr_points[1].custom_float_slope)) {
371                 BREAK_TO_DEBUGGER();
372                 return false;
373         }
374
375         fmt.mantissa_bits = 12;
376         fmt.sign = true;
377
378         while (i != hw_points_num) {
379                 if (!convert_to_custom_float_format(rgb->red, &fmt,
380                                                     &rgb->red_reg)) {
381                         BREAK_TO_DEBUGGER();
382                         return false;
383                 }
384
385                 if (!convert_to_custom_float_format(rgb->green, &fmt,
386                                                     &rgb->green_reg)) {
387                         BREAK_TO_DEBUGGER();
388                         return false;
389                 }
390
391                 if (!convert_to_custom_float_format(rgb->blue, &fmt,
392                                                     &rgb->blue_reg)) {
393                         BREAK_TO_DEBUGGER();
394                         return false;
395                 }
396
397                 if (!convert_to_custom_float_format(rgb->delta_red, &fmt,
398                                                     &rgb->delta_red_reg)) {
399                         BREAK_TO_DEBUGGER();
400                         return false;
401                 }
402
403                 if (!convert_to_custom_float_format(rgb->delta_green, &fmt,
404                                                     &rgb->delta_green_reg)) {
405                         BREAK_TO_DEBUGGER();
406                         return false;
407                 }
408
409                 if (!convert_to_custom_float_format(rgb->delta_blue, &fmt,
410                                                     &rgb->delta_blue_reg)) {
411                         BREAK_TO_DEBUGGER();
412                         return false;
413                 }
414
415                 ++rgb;
416                 ++i;
417         }
418
419         return true;
420 }
421
422 #define MAX_LOW_POINT      25
423 #define NUMBER_REGIONS     16
424 #define NUMBER_SW_SEGMENTS 16
425
426 static bool
427 dce110_translate_regamma_to_hw_format(const struct dc_transfer_func *output_tf,
428                                       struct pwl_params *regamma_params)
429 {
430         struct curve_points *arr_points;
431         struct pwl_result_data *rgb_resulted;
432         struct pwl_result_data *rgb;
433         struct pwl_result_data *rgb_plus_1;
434         struct fixed31_32 y_r;
435         struct fixed31_32 y_g;
436         struct fixed31_32 y_b;
437         struct fixed31_32 y1_min;
438         struct fixed31_32 y3_max;
439
440         int32_t region_start, region_end;
441         uint32_t i, j, k, seg_distr[NUMBER_REGIONS], increment, start_index, hw_points;
442
443         if (output_tf == NULL || regamma_params == NULL || output_tf->type == TF_TYPE_BYPASS)
444                 return false;
445
446         arr_points = regamma_params->arr_points;
447         rgb_resulted = regamma_params->rgb_resulted;
448         hw_points = 0;
449
450         memset(regamma_params, 0, sizeof(struct pwl_params));
451
452         if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
453                 /* 16 segments
454                  * segments are from 2^-11 to 2^5
455                  */
456                 region_start = -11;
457                 region_end = region_start + NUMBER_REGIONS;
458
459                 for (i = 0; i < NUMBER_REGIONS; i++)
460                         seg_distr[i] = 4;
461
462         } else {
463                 /* 10 segments
464                  * segment is from 2^-10 to 2^1
465                  * We include an extra segment for range [2^0, 2^1). This is to
466                  * ensure that colors with normalized values of 1 don't miss the
467                  * LUT.
468                  */
469                 region_start = -10;
470                 region_end = 1;
471
472                 seg_distr[0] = 4;
473                 seg_distr[1] = 4;
474                 seg_distr[2] = 4;
475                 seg_distr[3] = 4;
476                 seg_distr[4] = 4;
477                 seg_distr[5] = 4;
478                 seg_distr[6] = 4;
479                 seg_distr[7] = 4;
480                 seg_distr[8] = 4;
481                 seg_distr[9] = 4;
482                 seg_distr[10] = 0;
483                 seg_distr[11] = -1;
484                 seg_distr[12] = -1;
485                 seg_distr[13] = -1;
486                 seg_distr[14] = -1;
487                 seg_distr[15] = -1;
488         }
489
490         for (k = 0; k < 16; k++) {
491                 if (seg_distr[k] != -1)
492                         hw_points += (1 << seg_distr[k]);
493         }
494
495         j = 0;
496         for (k = 0; k < (region_end - region_start); k++) {
497                 increment = NUMBER_SW_SEGMENTS / (1 << seg_distr[k]);
498                 start_index = (region_start + k + MAX_LOW_POINT) *
499                                 NUMBER_SW_SEGMENTS;
500                 for (i = start_index; i < start_index + NUMBER_SW_SEGMENTS;
501                                 i += increment) {
502                         if (j == hw_points - 1)
503                                 break;
504                         rgb_resulted[j].red = output_tf->tf_pts.red[i];
505                         rgb_resulted[j].green = output_tf->tf_pts.green[i];
506                         rgb_resulted[j].blue = output_tf->tf_pts.blue[i];
507                         j++;
508                 }
509         }
510
511         /* last point */
512         start_index = (region_end + MAX_LOW_POINT) * NUMBER_SW_SEGMENTS;
513         rgb_resulted[hw_points - 1].red = output_tf->tf_pts.red[start_index];
514         rgb_resulted[hw_points - 1].green = output_tf->tf_pts.green[start_index];
515         rgb_resulted[hw_points - 1].blue = output_tf->tf_pts.blue[start_index];
516
517         arr_points[0].x = dc_fixpt_pow(dc_fixpt_from_int(2),
518                                              dc_fixpt_from_int(region_start));
519         arr_points[1].x = dc_fixpt_pow(dc_fixpt_from_int(2),
520                                              dc_fixpt_from_int(region_end));
521
522         y_r = rgb_resulted[0].red;
523         y_g = rgb_resulted[0].green;
524         y_b = rgb_resulted[0].blue;
525
526         y1_min = dc_fixpt_min(y_r, dc_fixpt_min(y_g, y_b));
527
528         arr_points[0].y = y1_min;
529         arr_points[0].slope = dc_fixpt_div(arr_points[0].y,
530                                                  arr_points[0].x);
531
532         y_r = rgb_resulted[hw_points - 1].red;
533         y_g = rgb_resulted[hw_points - 1].green;
534         y_b = rgb_resulted[hw_points - 1].blue;
535
536         /* see comment above, m_arrPoints[1].y should be the Y value for the
537          * region end (m_numOfHwPoints), not last HW point(m_numOfHwPoints - 1)
538          */
539         y3_max = dc_fixpt_max(y_r, dc_fixpt_max(y_g, y_b));
540
541         arr_points[1].y = y3_max;
542
543         arr_points[1].slope = dc_fixpt_zero;
544
545         if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
546                 /* for PQ, we want to have a straight line from last HW X point,
547                  * and the slope to be such that we hit 1.0 at 10000 nits.
548                  */
549                 const struct fixed31_32 end_value = dc_fixpt_from_int(125);
550
551                 arr_points[1].slope = dc_fixpt_div(
552                                 dc_fixpt_sub(dc_fixpt_one, arr_points[1].y),
553                                 dc_fixpt_sub(end_value, arr_points[1].x));
554         }
555
556         regamma_params->hw_points_num = hw_points;
557
558         k = 0;
559         for (i = 1; i < 16; i++) {
560                 if (seg_distr[k] != -1) {
561                         regamma_params->arr_curve_points[k].segments_num = seg_distr[k];
562                         regamma_params->arr_curve_points[i].offset =
563                                         regamma_params->arr_curve_points[k].offset + (1 << seg_distr[k]);
564                 }
565                 k++;
566         }
567
568         if (seg_distr[k] != -1)
569                 regamma_params->arr_curve_points[k].segments_num = seg_distr[k];
570
571         rgb = rgb_resulted;
572         rgb_plus_1 = rgb_resulted + 1;
573
574         i = 1;
575
576         while (i != hw_points + 1) {
577                 if (dc_fixpt_lt(rgb_plus_1->red, rgb->red))
578                         rgb_plus_1->red = rgb->red;
579                 if (dc_fixpt_lt(rgb_plus_1->green, rgb->green))
580                         rgb_plus_1->green = rgb->green;
581                 if (dc_fixpt_lt(rgb_plus_1->blue, rgb->blue))
582                         rgb_plus_1->blue = rgb->blue;
583
584                 rgb->delta_red = dc_fixpt_sub(rgb_plus_1->red, rgb->red);
585                 rgb->delta_green = dc_fixpt_sub(rgb_plus_1->green, rgb->green);
586                 rgb->delta_blue = dc_fixpt_sub(rgb_plus_1->blue, rgb->blue);
587
588                 ++rgb_plus_1;
589                 ++rgb;
590                 ++i;
591         }
592
593         convert_to_custom_float(rgb_resulted, arr_points, hw_points);
594
595         return true;
596 }
597
598 static bool
599 dce110_set_output_transfer_func(struct pipe_ctx *pipe_ctx,
600                                 const struct dc_stream_state *stream)
601 {
602         struct transform *xfm = pipe_ctx->plane_res.xfm;
603
604         xfm->funcs->opp_power_on_regamma_lut(xfm, true);
605         xfm->regamma_params.hw_points_num = GAMMA_HW_POINTS_NUM;
606
607         if (stream->out_transfer_func &&
608             stream->out_transfer_func->type == TF_TYPE_PREDEFINED &&
609             stream->out_transfer_func->tf == TRANSFER_FUNCTION_SRGB) {
610                 xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_SRGB);
611         } else if (dce110_translate_regamma_to_hw_format(stream->out_transfer_func,
612                                                          &xfm->regamma_params)) {
613                 xfm->funcs->opp_program_regamma_pwl(xfm, &xfm->regamma_params);
614                 xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_USER);
615         } else {
616                 xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_BYPASS);
617         }
618
619         xfm->funcs->opp_power_on_regamma_lut(xfm, false);
620
621         return true;
622 }
623
624 void dce110_update_info_frame(struct pipe_ctx *pipe_ctx)
625 {
626         bool is_hdmi_tmds;
627         bool is_dp;
628
629         ASSERT(pipe_ctx->stream);
630
631         if (pipe_ctx->stream_res.stream_enc == NULL)
632                 return;  /* this is not root pipe */
633
634         is_hdmi_tmds = dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal);
635         is_dp = dc_is_dp_signal(pipe_ctx->stream->signal);
636
637         if (!is_hdmi_tmds && !is_dp)
638                 return;
639
640         if (is_hdmi_tmds)
641                 pipe_ctx->stream_res.stream_enc->funcs->update_hdmi_info_packets(
642                         pipe_ctx->stream_res.stream_enc,
643                         &pipe_ctx->stream_res.encoder_info_frame);
644         else
645                 pipe_ctx->stream_res.stream_enc->funcs->update_dp_info_packets(
646                         pipe_ctx->stream_res.stream_enc,
647                         &pipe_ctx->stream_res.encoder_info_frame);
648 }
649
650 void dce110_enable_stream(struct pipe_ctx *pipe_ctx)
651 {
652         enum dc_lane_count lane_count =
653                 pipe_ctx->stream->link->cur_link_settings.lane_count;
654
655         struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
656         struct dc_link *link = pipe_ctx->stream->link;
657
658
659         uint32_t active_total_with_borders;
660         uint32_t early_control = 0;
661         struct timing_generator *tg = pipe_ctx->stream_res.tg;
662
663         /* For MST, there are multiply stream go to only one link.
664          * connect DIG back_end to front_end while enable_stream and
665          * disconnect them during disable_stream
666          * BY this, it is logic clean to separate stream and link */
667         link->link_enc->funcs->connect_dig_be_to_fe(link->link_enc,
668                                                     pipe_ctx->stream_res.stream_enc->id, true);
669
670         link->dc->hwss.update_info_frame(pipe_ctx);
671
672         /* enable early control to avoid corruption on DP monitor*/
673         active_total_with_borders =
674                         timing->h_addressable
675                                 + timing->h_border_left
676                                 + timing->h_border_right;
677
678         if (lane_count != 0)
679                 early_control = active_total_with_borders % lane_count;
680
681         if (early_control == 0)
682                 early_control = lane_count;
683
684         tg->funcs->set_early_control(tg, early_control);
685
686         /* enable audio only within mode set */
687         if (pipe_ctx->stream_res.audio != NULL) {
688                 if (dc_is_dp_signal(pipe_ctx->stream->signal))
689                         pipe_ctx->stream_res.stream_enc->funcs->dp_audio_enable(pipe_ctx->stream_res.stream_enc);
690         }
691
692
693
694
695 }
696
697 /*todo: cloned in stream enc, fix*/
698 static bool is_panel_backlight_on(struct dce_hwseq *hws)
699 {
700         uint32_t value;
701
702         REG_GET(LVTMA_PWRSEQ_CNTL, LVTMA_BLON, &value);
703
704         return value;
705 }
706
707 static bool is_panel_powered_on(struct dce_hwseq *hws)
708 {
709         uint32_t pwr_seq_state, dig_on, dig_on_ovrd;
710
711
712         REG_GET(LVTMA_PWRSEQ_STATE, LVTMA_PWRSEQ_TARGET_STATE_R, &pwr_seq_state);
713
714         REG_GET_2(LVTMA_PWRSEQ_CNTL, LVTMA_DIGON, &dig_on, LVTMA_DIGON_OVRD, &dig_on_ovrd);
715
716         return (pwr_seq_state == 1) || (dig_on == 1 && dig_on_ovrd == 1);
717 }
718
719 static enum bp_result link_transmitter_control(
720                 struct dc_bios *bios,
721         struct bp_transmitter_control *cntl)
722 {
723         enum bp_result result;
724
725         result = bios->funcs->transmitter_control(bios, cntl);
726
727         return result;
728 }
729
730 /*
731  * @brief
732  * eDP only.
733  */
734 void dce110_edp_wait_for_hpd_ready(
735                 struct dc_link *link,
736                 bool power_up)
737 {
738         struct dc_context *ctx = link->ctx;
739         struct graphics_object_id connector = link->link_enc->connector;
740         struct gpio *hpd;
741         bool edp_hpd_high = false;
742         uint32_t time_elapsed = 0;
743         uint32_t timeout = power_up ?
744                 PANEL_POWER_UP_TIMEOUT : PANEL_POWER_DOWN_TIMEOUT;
745
746         if (dal_graphics_object_id_get_connector_id(connector)
747                         != CONNECTOR_ID_EDP) {
748                 BREAK_TO_DEBUGGER();
749                 return;
750         }
751
752         if (!power_up)
753                 /*
754                  * From KV, we will not HPD low after turning off VCC -
755                  * instead, we will check the SW timer in power_up().
756                  */
757                 return;
758
759         /*
760          * When we power on/off the eDP panel,
761          * we need to wait until SENSE bit is high/low.
762          */
763
764         /* obtain HPD */
765         /* TODO what to do with this? */
766         hpd = get_hpd_gpio(ctx->dc_bios, connector, ctx->gpio_service);
767
768         if (!hpd) {
769                 BREAK_TO_DEBUGGER();
770                 return;
771         }
772
773         dal_gpio_open(hpd, GPIO_MODE_INTERRUPT);
774
775         /* wait until timeout or panel detected */
776
777         do {
778                 uint32_t detected = 0;
779
780                 dal_gpio_get_value(hpd, &detected);
781
782                 if (!(detected ^ power_up)) {
783                         edp_hpd_high = true;
784                         break;
785                 }
786
787                 msleep(HPD_CHECK_INTERVAL);
788
789                 time_elapsed += HPD_CHECK_INTERVAL;
790         } while (time_elapsed < timeout);
791
792         dal_gpio_close(hpd);
793
794         dal_gpio_destroy_irq(&hpd);
795
796         if (false == edp_hpd_high) {
797                 DC_LOG_ERROR(
798                                 "%s: wait timed out!\n", __func__);
799         }
800 }
801
802 void dce110_edp_power_control(
803                 struct dc_link *link,
804                 bool power_up)
805 {
806         struct dc_context *ctx = link->ctx;
807         struct dce_hwseq *hwseq = ctx->dc->hwseq;
808         struct bp_transmitter_control cntl = { 0 };
809         enum bp_result bp_result;
810
811
812         if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
813                         != CONNECTOR_ID_EDP) {
814                 BREAK_TO_DEBUGGER();
815                 return;
816         }
817
818         if (power_up != is_panel_powered_on(hwseq)) {
819                 /* Send VBIOS command to prompt eDP panel power */
820                 if (power_up) {
821                         unsigned long long current_ts = dm_get_timestamp(ctx);
822                         unsigned long long duration_in_ms =
823                                         div64_u64(dm_get_elapse_time_in_ns(
824                                                         ctx,
825                                                         current_ts,
826                                                         link->link_trace.time_stamp.edp_poweroff), 1000000);
827                         unsigned long long wait_time_ms = 0;
828
829                         /* max 500ms from LCDVDD off to on */
830                         unsigned long long edp_poweroff_time_ms = 500;
831
832                         if (link->local_sink != NULL)
833                                 edp_poweroff_time_ms =
834                                                 500 + link->local_sink->edid_caps.panel_patch.extra_t12_ms;
835                         if (link->link_trace.time_stamp.edp_poweroff == 0)
836                                 wait_time_ms = edp_poweroff_time_ms;
837                         else if (duration_in_ms < edp_poweroff_time_ms)
838                                 wait_time_ms = edp_poweroff_time_ms - duration_in_ms;
839
840                         if (wait_time_ms) {
841                                 msleep(wait_time_ms);
842                                 dm_output_to_console("%s: wait %lld ms to power on eDP.\n",
843                                                 __func__, wait_time_ms);
844                         }
845
846                 }
847
848                 DC_LOG_HW_RESUME_S3(
849                                 "%s: Panel Power action: %s\n",
850                                 __func__, (power_up ? "On":"Off"));
851
852                 cntl.action = power_up ?
853                         TRANSMITTER_CONTROL_POWER_ON :
854                         TRANSMITTER_CONTROL_POWER_OFF;
855                 cntl.transmitter = link->link_enc->transmitter;
856                 cntl.connector_obj_id = link->link_enc->connector;
857                 cntl.coherent = false;
858                 cntl.lanes_number = LANE_COUNT_FOUR;
859                 cntl.hpd_sel = link->link_enc->hpd_source;
860                 bp_result = link_transmitter_control(ctx->dc_bios, &cntl);
861
862                 if (!power_up)
863                         /*save driver power off time stamp*/
864                         link->link_trace.time_stamp.edp_poweroff = dm_get_timestamp(ctx);
865                 else
866                         link->link_trace.time_stamp.edp_poweron = dm_get_timestamp(ctx);
867
868                 if (bp_result != BP_RESULT_OK)
869                         DC_LOG_ERROR(
870                                         "%s: Panel Power bp_result: %d\n",
871                                         __func__, bp_result);
872         } else {
873                 DC_LOG_HW_RESUME_S3(
874                                 "%s: Skipping Panel Power action: %s\n",
875                                 __func__, (power_up ? "On":"Off"));
876         }
877 }
878
879 /*todo: cloned in stream enc, fix*/
880 /*
881  * @brief
882  * eDP only. Control the backlight of the eDP panel
883  */
884 void dce110_edp_backlight_control(
885                 struct dc_link *link,
886                 bool enable)
887 {
888         struct dc_context *ctx = link->ctx;
889         struct dce_hwseq *hws = ctx->dc->hwseq;
890         struct bp_transmitter_control cntl = { 0 };
891
892         if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
893                 != CONNECTOR_ID_EDP) {
894                 BREAK_TO_DEBUGGER();
895                 return;
896         }
897
898         if (enable && is_panel_backlight_on(hws)) {
899                 DC_LOG_HW_RESUME_S3(
900                                 "%s: panel already powered up. Do nothing.\n",
901                                 __func__);
902                 return;
903         }
904
905         /* Send VBIOS command to control eDP panel backlight */
906
907         DC_LOG_HW_RESUME_S3(
908                         "%s: backlight action: %s\n",
909                         __func__, (enable ? "On":"Off"));
910
911         cntl.action = enable ?
912                 TRANSMITTER_CONTROL_BACKLIGHT_ON :
913                 TRANSMITTER_CONTROL_BACKLIGHT_OFF;
914
915         /*cntl.engine_id = ctx->engine;*/
916         cntl.transmitter = link->link_enc->transmitter;
917         cntl.connector_obj_id = link->link_enc->connector;
918         /*todo: unhardcode*/
919         cntl.lanes_number = LANE_COUNT_FOUR;
920         cntl.hpd_sel = link->link_enc->hpd_source;
921         cntl.signal = SIGNAL_TYPE_EDP;
922
923         /* For eDP, the following delays might need to be considered
924          * after link training completed:
925          * idle period - min. accounts for required BS-Idle pattern,
926          * max. allows for source frame synchronization);
927          * 50 msec max. delay from valid video data from source
928          * to video on dislpay or backlight enable.
929          *
930          * Disable the delay for now.
931          * Enable it in the future if necessary.
932          */
933         /* dc_service_sleep_in_milliseconds(50); */
934                 /*edp 1.2*/
935         if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_ON)
936                 edp_receiver_ready_T7(link);
937         link_transmitter_control(ctx->dc_bios, &cntl);
938         /*edp 1.2*/
939         if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_OFF)
940                 edp_receiver_ready_T9(link);
941 }
942
943 void dce110_enable_audio_stream(struct pipe_ctx *pipe_ctx)
944 {
945         /* notify audio driver for audio modes of monitor */
946         struct dc *core_dc;
947         struct clk_mgr *clk_mgr;
948         unsigned int i, num_audio = 1;
949
950         if (!pipe_ctx->stream)
951                 return;
952
953         core_dc = pipe_ctx->stream->ctx->dc;
954         clk_mgr = core_dc->clk_mgr;
955
956         if (pipe_ctx->stream_res.audio && pipe_ctx->stream_res.audio->enabled == true)
957                 return;
958
959         if (pipe_ctx->stream_res.audio) {
960                 for (i = 0; i < MAX_PIPES; i++) {
961                         /*current_state not updated yet*/
962                         if (core_dc->current_state->res_ctx.pipe_ctx[i].stream_res.audio != NULL)
963                                 num_audio++;
964                 }
965
966                 pipe_ctx->stream_res.audio->funcs->az_enable(pipe_ctx->stream_res.audio);
967
968                 if (num_audio >= 1 && clk_mgr->funcs->enable_pme_wa)
969                         /*this is the first audio. apply the PME w/a in order to wake AZ from D3*/
970                         clk_mgr->funcs->enable_pme_wa(clk_mgr);
971                 /* un-mute audio */
972                 /* TODO: audio should be per stream rather than per link */
973                 pipe_ctx->stream_res.stream_enc->funcs->audio_mute_control(
974                                         pipe_ctx->stream_res.stream_enc, false);
975                 if (pipe_ctx->stream_res.audio)
976                         pipe_ctx->stream_res.audio->enabled = true;
977         }
978 }
979
980 void dce110_disable_audio_stream(struct pipe_ctx *pipe_ctx)
981 {
982         struct dc *dc;
983         struct clk_mgr *clk_mgr;
984
985         if (!pipe_ctx || !pipe_ctx->stream)
986                 return;
987
988         dc = pipe_ctx->stream->ctx->dc;
989         clk_mgr = dc->clk_mgr;
990
991         if (pipe_ctx->stream_res.audio && pipe_ctx->stream_res.audio->enabled == false)
992                 return;
993
994         pipe_ctx->stream_res.stream_enc->funcs->audio_mute_control(
995                         pipe_ctx->stream_res.stream_enc, true);
996         if (pipe_ctx->stream_res.audio) {
997                 pipe_ctx->stream_res.audio->enabled = false;
998
999                 if (dc_is_dp_signal(pipe_ctx->stream->signal))
1000                         pipe_ctx->stream_res.stream_enc->funcs->dp_audio_disable(
1001                                         pipe_ctx->stream_res.stream_enc);
1002                 else
1003                         pipe_ctx->stream_res.stream_enc->funcs->hdmi_audio_disable(
1004                                         pipe_ctx->stream_res.stream_enc);
1005
1006                 if (clk_mgr->funcs->enable_pme_wa)
1007                         /*this is the first audio. apply the PME w/a in order to wake AZ from D3*/
1008                         clk_mgr->funcs->enable_pme_wa(clk_mgr);
1009
1010                 /* TODO: notify audio driver for if audio modes list changed
1011                  * add audio mode list change flag */
1012                 /* dal_audio_disable_azalia_audio_jack_presence(stream->audio,
1013                  * stream->stream_engine_id);
1014                  */
1015         }
1016 }
1017
1018 void dce110_disable_stream(struct pipe_ctx *pipe_ctx)
1019 {
1020         struct dc_stream_state *stream = pipe_ctx->stream;
1021         struct dc_link *link = stream->link;
1022         struct dc *dc = pipe_ctx->stream->ctx->dc;
1023
1024         if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal)) {
1025                 pipe_ctx->stream_res.stream_enc->funcs->stop_hdmi_info_packets(
1026                         pipe_ctx->stream_res.stream_enc);
1027                 pipe_ctx->stream_res.stream_enc->funcs->hdmi_reset_stream_attribute(
1028                         pipe_ctx->stream_res.stream_enc);
1029         }
1030
1031         if (dc_is_dp_signal(pipe_ctx->stream->signal))
1032                 pipe_ctx->stream_res.stream_enc->funcs->stop_dp_info_packets(
1033                         pipe_ctx->stream_res.stream_enc);
1034
1035         dc->hwss.disable_audio_stream(pipe_ctx);
1036
1037         link->link_enc->funcs->connect_dig_be_to_fe(
1038                         link->link_enc,
1039                         pipe_ctx->stream_res.stream_enc->id,
1040                         false);
1041
1042 }
1043
1044 void dce110_unblank_stream(struct pipe_ctx *pipe_ctx,
1045                 struct dc_link_settings *link_settings)
1046 {
1047         struct encoder_unblank_param params = { { 0 } };
1048         struct dc_stream_state *stream = pipe_ctx->stream;
1049         struct dc_link *link = stream->link;
1050
1051         /* only 3 items below are used by unblank */
1052         params.timing = pipe_ctx->stream->timing;
1053         params.link_settings.link_rate = link_settings->link_rate;
1054
1055         if (dc_is_dp_signal(pipe_ctx->stream->signal))
1056                 pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(pipe_ctx->stream_res.stream_enc, &params);
1057
1058         if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1059                 link->dc->hwss.edp_backlight_control(link, true);
1060         }
1061 }
1062
1063 void dce110_blank_stream(struct pipe_ctx *pipe_ctx)
1064 {
1065         struct dc_stream_state *stream = pipe_ctx->stream;
1066         struct dc_link *link = stream->link;
1067
1068         if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1069                 link->dc->hwss.edp_backlight_control(link, false);
1070                 dc_link_set_abm_disable(link);
1071         }
1072
1073         if (dc_is_dp_signal(pipe_ctx->stream->signal))
1074                 pipe_ctx->stream_res.stream_enc->funcs->dp_blank(pipe_ctx->stream_res.stream_enc);
1075 }
1076
1077
1078 void dce110_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
1079 {
1080         if (pipe_ctx != NULL && pipe_ctx->stream_res.stream_enc != NULL)
1081                 pipe_ctx->stream_res.stream_enc->funcs->set_avmute(pipe_ctx->stream_res.stream_enc, enable);
1082 }
1083
1084 static enum audio_dto_source translate_to_dto_source(enum controller_id crtc_id)
1085 {
1086         switch (crtc_id) {
1087         case CONTROLLER_ID_D0:
1088                 return DTO_SOURCE_ID0;
1089         case CONTROLLER_ID_D1:
1090                 return DTO_SOURCE_ID1;
1091         case CONTROLLER_ID_D2:
1092                 return DTO_SOURCE_ID2;
1093         case CONTROLLER_ID_D3:
1094                 return DTO_SOURCE_ID3;
1095         case CONTROLLER_ID_D4:
1096                 return DTO_SOURCE_ID4;
1097         case CONTROLLER_ID_D5:
1098                 return DTO_SOURCE_ID5;
1099         default:
1100                 return DTO_SOURCE_UNKNOWN;
1101         }
1102 }
1103
1104 static void build_audio_output(
1105         struct dc_state *state,
1106         const struct pipe_ctx *pipe_ctx,
1107         struct audio_output *audio_output)
1108 {
1109         const struct dc_stream_state *stream = pipe_ctx->stream;
1110         audio_output->engine_id = pipe_ctx->stream_res.stream_enc->id;
1111
1112         audio_output->signal = pipe_ctx->stream->signal;
1113
1114         /* audio_crtc_info  */
1115
1116         audio_output->crtc_info.h_total =
1117                 stream->timing.h_total;
1118
1119         /*
1120          * Audio packets are sent during actual CRTC blank physical signal, we
1121          * need to specify actual active signal portion
1122          */
1123         audio_output->crtc_info.h_active =
1124                         stream->timing.h_addressable
1125                         + stream->timing.h_border_left
1126                         + stream->timing.h_border_right;
1127
1128         audio_output->crtc_info.v_active =
1129                         stream->timing.v_addressable
1130                         + stream->timing.v_border_top
1131                         + stream->timing.v_border_bottom;
1132
1133         audio_output->crtc_info.pixel_repetition = 1;
1134
1135         audio_output->crtc_info.interlaced =
1136                         stream->timing.flags.INTERLACE;
1137
1138         audio_output->crtc_info.refresh_rate =
1139                 (stream->timing.pix_clk_100hz*100)/
1140                 (stream->timing.h_total*stream->timing.v_total);
1141
1142         audio_output->crtc_info.color_depth =
1143                 stream->timing.display_color_depth;
1144
1145         audio_output->crtc_info.requested_pixel_clock_100Hz =
1146                         pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz;
1147
1148         audio_output->crtc_info.calculated_pixel_clock_100Hz =
1149                         pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz;
1150
1151 /*for HDMI, audio ACR is with deep color ratio factor*/
1152         if (dc_is_hdmi_signal(pipe_ctx->stream->signal) &&
1153                 audio_output->crtc_info.requested_pixel_clock_100Hz ==
1154                                 (stream->timing.pix_clk_100hz)) {
1155                 if (pipe_ctx->stream_res.pix_clk_params.pixel_encoding == PIXEL_ENCODING_YCBCR420) {
1156                         audio_output->crtc_info.requested_pixel_clock_100Hz =
1157                                         audio_output->crtc_info.requested_pixel_clock_100Hz/2;
1158                         audio_output->crtc_info.calculated_pixel_clock_100Hz =
1159                                         pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz/2;
1160
1161                 }
1162         }
1163
1164         if (state->clk_mgr &&
1165                 (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
1166                         pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)) {
1167                 audio_output->pll_info.dp_dto_source_clock_in_khz =
1168                                 state->clk_mgr->funcs->get_dp_ref_clk_frequency(
1169                                                 state->clk_mgr);
1170         }
1171
1172         audio_output->pll_info.feed_back_divider =
1173                         pipe_ctx->pll_settings.feedback_divider;
1174
1175         audio_output->pll_info.dto_source =
1176                 translate_to_dto_source(
1177                         pipe_ctx->stream_res.tg->inst + 1);
1178
1179         /* TODO hard code to enable for now. Need get from stream */
1180         audio_output->pll_info.ss_enabled = true;
1181
1182         audio_output->pll_info.ss_percentage =
1183                         pipe_ctx->pll_settings.ss_percentage;
1184 }
1185
1186 static void get_surface_visual_confirm_color(const struct pipe_ctx *pipe_ctx,
1187                 struct tg_color *color)
1188 {
1189         uint32_t color_value = MAX_TG_COLOR_VALUE * (4 - pipe_ctx->stream_res.tg->inst) / 4;
1190
1191         switch (pipe_ctx->plane_res.scl_data.format) {
1192         case PIXEL_FORMAT_ARGB8888:
1193                 /* set boarder color to red */
1194                 color->color_r_cr = color_value;
1195                 break;
1196
1197         case PIXEL_FORMAT_ARGB2101010:
1198                 /* set boarder color to blue */
1199                 color->color_b_cb = color_value;
1200                 break;
1201         case PIXEL_FORMAT_420BPP8:
1202                 /* set boarder color to green */
1203                 color->color_g_y = color_value;
1204                 break;
1205         case PIXEL_FORMAT_420BPP10:
1206                 /* set boarder color to yellow */
1207                 color->color_g_y = color_value;
1208                 color->color_r_cr = color_value;
1209                 break;
1210         case PIXEL_FORMAT_FP16:
1211                 /* set boarder color to white */
1212                 color->color_r_cr = color_value;
1213                 color->color_b_cb = color_value;
1214                 color->color_g_y = color_value;
1215                 break;
1216         default:
1217                 break;
1218         }
1219 }
1220
1221 static void program_scaler(const struct dc *dc,
1222                 const struct pipe_ctx *pipe_ctx)
1223 {
1224         struct tg_color color = {0};
1225
1226 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1227         /* TOFPGA */
1228         if (pipe_ctx->plane_res.xfm->funcs->transform_set_pixel_storage_depth == NULL)
1229                 return;
1230 #endif
1231
1232         if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE)
1233                 get_surface_visual_confirm_color(pipe_ctx, &color);
1234         else
1235                 color_space_to_black_color(dc,
1236                                 pipe_ctx->stream->output_color_space,
1237                                 &color);
1238
1239         pipe_ctx->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
1240                 pipe_ctx->plane_res.xfm,
1241                 pipe_ctx->plane_res.scl_data.lb_params.depth,
1242                 &pipe_ctx->stream->bit_depth_params);
1243
1244         if (pipe_ctx->stream_res.tg->funcs->set_overscan_blank_color) {
1245                 /*
1246                  * The way 420 is packed, 2 channels carry Y component, 1 channel
1247                  * alternate between Cb and Cr, so both channels need the pixel
1248                  * value for Y
1249                  */
1250                 if (pipe_ctx->stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
1251                         color.color_r_cr = color.color_g_y;
1252
1253                 pipe_ctx->stream_res.tg->funcs->set_overscan_blank_color(
1254                                 pipe_ctx->stream_res.tg,
1255                                 &color);
1256         }
1257
1258         pipe_ctx->plane_res.xfm->funcs->transform_set_scaler(pipe_ctx->plane_res.xfm,
1259                 &pipe_ctx->plane_res.scl_data);
1260 }
1261
1262 static enum dc_status dce110_enable_stream_timing(
1263                 struct pipe_ctx *pipe_ctx,
1264                 struct dc_state *context,
1265                 struct dc *dc)
1266 {
1267         struct dc_stream_state *stream = pipe_ctx->stream;
1268         struct pipe_ctx *pipe_ctx_old = &dc->current_state->res_ctx.
1269                         pipe_ctx[pipe_ctx->pipe_idx];
1270         struct tg_color black_color = {0};
1271
1272         if (!pipe_ctx_old->stream) {
1273
1274                 /* program blank color */
1275                 color_space_to_black_color(dc,
1276                                 stream->output_color_space, &black_color);
1277                 pipe_ctx->stream_res.tg->funcs->set_blank_color(
1278                                 pipe_ctx->stream_res.tg,
1279                                 &black_color);
1280
1281                 /*
1282                  * Must blank CRTC after disabling power gating and before any
1283                  * programming, otherwise CRTC will be hung in bad state
1284                  */
1285                 pipe_ctx->stream_res.tg->funcs->set_blank(pipe_ctx->stream_res.tg, true);
1286
1287                 if (false == pipe_ctx->clock_source->funcs->program_pix_clk(
1288                                 pipe_ctx->clock_source,
1289                                 &pipe_ctx->stream_res.pix_clk_params,
1290                                 &pipe_ctx->pll_settings)) {
1291                         BREAK_TO_DEBUGGER();
1292                         return DC_ERROR_UNEXPECTED;
1293                 }
1294
1295                 pipe_ctx->stream_res.tg->funcs->program_timing(
1296                                 pipe_ctx->stream_res.tg,
1297                                 &stream->timing,
1298                                 0,
1299                                 0,
1300                                 0,
1301                                 0,
1302                                 pipe_ctx->stream->signal,
1303                                 true);
1304         }
1305
1306         if (!pipe_ctx_old->stream) {
1307                 if (false == pipe_ctx->stream_res.tg->funcs->enable_crtc(
1308                                 pipe_ctx->stream_res.tg)) {
1309                         BREAK_TO_DEBUGGER();
1310                         return DC_ERROR_UNEXPECTED;
1311                 }
1312         }
1313
1314         return DC_OK;
1315 }
1316
1317 static enum dc_status apply_single_controller_ctx_to_hw(
1318                 struct pipe_ctx *pipe_ctx,
1319                 struct dc_state *context,
1320                 struct dc *dc)
1321 {
1322         struct dc_stream_state *stream = pipe_ctx->stream;
1323         struct drr_params params = {0};
1324         unsigned int event_triggers = 0;
1325         struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
1326
1327         if (dc->hwss.disable_stream_gating) {
1328                 dc->hwss.disable_stream_gating(dc, pipe_ctx);
1329         }
1330
1331         if (pipe_ctx->stream_res.audio != NULL) {
1332                 struct audio_output audio_output;
1333
1334                 build_audio_output(context, pipe_ctx, &audio_output);
1335
1336                 if (dc_is_dp_signal(pipe_ctx->stream->signal))
1337                         pipe_ctx->stream_res.stream_enc->funcs->dp_audio_setup(
1338                                         pipe_ctx->stream_res.stream_enc,
1339                                         pipe_ctx->stream_res.audio->inst,
1340                                         &pipe_ctx->stream->audio_info);
1341                 else
1342                         pipe_ctx->stream_res.stream_enc->funcs->hdmi_audio_setup(
1343                                         pipe_ctx->stream_res.stream_enc,
1344                                         pipe_ctx->stream_res.audio->inst,
1345                                         &pipe_ctx->stream->audio_info,
1346                                         &audio_output.crtc_info);
1347
1348                 pipe_ctx->stream_res.audio->funcs->az_configure(
1349                                 pipe_ctx->stream_res.audio,
1350                                 pipe_ctx->stream->signal,
1351                                 &audio_output.crtc_info,
1352                                 &pipe_ctx->stream->audio_info);
1353         }
1354
1355         /*  */
1356         /* Do not touch stream timing on seamless boot optimization. */
1357         if (!pipe_ctx->stream->apply_seamless_boot_optimization)
1358                 dc->hwss.enable_stream_timing(pipe_ctx, context, dc);
1359
1360         if (dc->hwss.setup_vupdate_interrupt)
1361                 dc->hwss.setup_vupdate_interrupt(pipe_ctx);
1362
1363         params.vertical_total_min = stream->adjust.v_total_min;
1364         params.vertical_total_max = stream->adjust.v_total_max;
1365         if (pipe_ctx->stream_res.tg->funcs->set_drr)
1366                 pipe_ctx->stream_res.tg->funcs->set_drr(
1367                         pipe_ctx->stream_res.tg, &params);
1368
1369         // DRR should set trigger event to monitor surface update event
1370         if (stream->adjust.v_total_min != 0 && stream->adjust.v_total_max != 0)
1371                 event_triggers = 0x80;
1372         if (pipe_ctx->stream_res.tg->funcs->set_static_screen_control)
1373                 pipe_ctx->stream_res.tg->funcs->set_static_screen_control(
1374                                 pipe_ctx->stream_res.tg, event_triggers);
1375
1376         if (!dc_is_virtual_signal(pipe_ctx->stream->signal))
1377                 pipe_ctx->stream_res.stream_enc->funcs->dig_connect_to_otg(
1378                         pipe_ctx->stream_res.stream_enc,
1379                         pipe_ctx->stream_res.tg->inst);
1380
1381         pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
1382                         pipe_ctx->stream_res.opp,
1383                         COLOR_SPACE_YCBCR601,
1384                         stream->timing.display_color_depth,
1385                         stream->signal);
1386
1387         pipe_ctx->stream_res.opp->funcs->opp_program_fmt(
1388                 pipe_ctx->stream_res.opp,
1389                 &stream->bit_depth_params,
1390                 &stream->clamping);
1391         while (odm_pipe) {
1392                 odm_pipe->stream_res.opp->funcs->opp_set_dyn_expansion(
1393                                 odm_pipe->stream_res.opp,
1394                                 COLOR_SPACE_YCBCR601,
1395                                 stream->timing.display_color_depth,
1396                                 stream->signal);
1397
1398                 odm_pipe->stream_res.opp->funcs->opp_program_fmt(
1399                                 odm_pipe->stream_res.opp,
1400                                 &stream->bit_depth_params,
1401                                 &stream->clamping);
1402                 odm_pipe = odm_pipe->next_odm_pipe;
1403         }
1404
1405         if (!stream->dpms_off)
1406                 core_link_enable_stream(context, pipe_ctx);
1407
1408         pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->bottom_pipe != 0;
1409
1410         pipe_ctx->stream->link->psr_feature_enabled = false;
1411
1412         return DC_OK;
1413 }
1414
1415 /******************************************************************************/
1416
1417 static void power_down_encoders(struct dc *dc)
1418 {
1419         int i;
1420
1421         /* do not know BIOS back-front mapping, simply blank all. It will not
1422          * hurt for non-DP
1423          */
1424         for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1425                 dc->res_pool->stream_enc[i]->funcs->dp_blank(
1426                                         dc->res_pool->stream_enc[i]);
1427         }
1428
1429         for (i = 0; i < dc->link_count; i++) {
1430                 enum signal_type signal = dc->links[i]->connector_signal;
1431
1432                 if ((signal == SIGNAL_TYPE_EDP) ||
1433                         (signal == SIGNAL_TYPE_DISPLAY_PORT))
1434                         if (!dc->links[i]->wa_flags.dp_keep_receiver_powered)
1435                                 dp_receiver_power_ctrl(dc->links[i], false);
1436
1437                 if (signal != SIGNAL_TYPE_EDP)
1438                         signal = SIGNAL_TYPE_NONE;
1439
1440                 dc->links[i]->link_enc->funcs->disable_output(
1441                                 dc->links[i]->link_enc, signal);
1442         }
1443 }
1444
1445 static void power_down_controllers(struct dc *dc)
1446 {
1447         int i;
1448
1449         for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
1450                 dc->res_pool->timing_generators[i]->funcs->disable_crtc(
1451                                 dc->res_pool->timing_generators[i]);
1452         }
1453 }
1454
1455 static void power_down_clock_sources(struct dc *dc)
1456 {
1457         int i;
1458
1459         if (dc->res_pool->dp_clock_source->funcs->cs_power_down(
1460                 dc->res_pool->dp_clock_source) == false)
1461                 dm_error("Failed to power down pll! (dp clk src)\n");
1462
1463         for (i = 0; i < dc->res_pool->clk_src_count; i++) {
1464                 if (dc->res_pool->clock_sources[i]->funcs->cs_power_down(
1465                                 dc->res_pool->clock_sources[i]) == false)
1466                         dm_error("Failed to power down pll! (clk src index=%d)\n", i);
1467         }
1468 }
1469
1470 static void power_down_all_hw_blocks(struct dc *dc)
1471 {
1472         power_down_encoders(dc);
1473
1474         power_down_controllers(dc);
1475
1476         power_down_clock_sources(dc);
1477
1478         if (dc->fbc_compressor)
1479                 dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
1480 }
1481
1482 static void disable_vga_and_power_gate_all_controllers(
1483                 struct dc *dc)
1484 {
1485         int i;
1486         struct timing_generator *tg;
1487         struct dc_context *ctx = dc->ctx;
1488
1489         for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
1490                 tg = dc->res_pool->timing_generators[i];
1491
1492                 if (tg->funcs->disable_vga)
1493                         tg->funcs->disable_vga(tg);
1494         }
1495         for (i = 0; i < dc->res_pool->pipe_count; i++) {
1496                 /* Enable CLOCK gating for each pipe BEFORE controller
1497                  * powergating. */
1498                 enable_display_pipe_clock_gating(ctx,
1499                                 true);
1500
1501                 dc->current_state->res_ctx.pipe_ctx[i].pipe_idx = i;
1502                 dc->hwss.disable_plane(dc,
1503                         &dc->current_state->res_ctx.pipe_ctx[i]);
1504         }
1505 }
1506
1507
1508 static struct dc_stream_state *get_edp_stream(struct dc_state *context)
1509 {
1510         int i;
1511
1512         for (i = 0; i < context->stream_count; i++) {
1513                 if (context->streams[i]->signal == SIGNAL_TYPE_EDP)
1514                         return context->streams[i];
1515         }
1516         return NULL;
1517 }
1518
1519 static struct dc_link *get_edp_link_with_sink(
1520                 struct dc *dc,
1521                 struct dc_state *context)
1522 {
1523         int i;
1524         struct dc_link *link = NULL;
1525
1526         /* check if there is an eDP panel not in use */
1527         for (i = 0; i < dc->link_count; i++) {
1528                 if (dc->links[i]->local_sink &&
1529                         dc->links[i]->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1530                         link = dc->links[i];
1531                         break;
1532                 }
1533         }
1534
1535         return link;
1536 }
1537
1538 /**
1539  * When ASIC goes from VBIOS/VGA mode to driver/accelerated mode we need:
1540  *  1. Power down all DC HW blocks
1541  *  2. Disable VGA engine on all controllers
1542  *  3. Enable power gating for controller
1543  *  4. Set acc_mode_change bit (VBIOS will clear this bit when going to FSDOS)
1544  */
1545 void dce110_enable_accelerated_mode(struct dc *dc, struct dc_state *context)
1546 {
1547         int i;
1548         struct dc_link *edp_link_with_sink = get_edp_link_with_sink(dc, context);
1549         struct dc_link *edp_link = get_edp_link(dc);
1550         struct dc_stream_state *edp_stream = NULL;
1551         bool can_apply_edp_fast_boot = false;
1552         bool can_apply_seamless_boot = false;
1553         bool keep_edp_vdd_on = false;
1554
1555         if (dc->hwss.init_pipes)
1556                 dc->hwss.init_pipes(dc, context);
1557
1558         edp_stream = get_edp_stream(context);
1559
1560         // Check fastboot support, disable on DCE8 because of blank screens
1561         if (edp_link && dc->ctx->dce_version != DCE_VERSION_8_0 &&
1562                     dc->ctx->dce_version != DCE_VERSION_8_1 &&
1563                     dc->ctx->dce_version != DCE_VERSION_8_3) {
1564
1565                 // enable fastboot if backend is enabled on eDP
1566                 if (edp_link->link_enc->funcs->is_dig_enabled(edp_link->link_enc)) {
1567                         /* Set optimization flag on eDP stream*/
1568                         if (edp_stream) {
1569                                 edp_stream->apply_edp_fast_boot_optimization = true;
1570                                 can_apply_edp_fast_boot = true;
1571                         }
1572                 }
1573
1574                 // We are trying to enable eDP, don't power down VDD
1575                 if (edp_stream)
1576                         keep_edp_vdd_on = true;
1577         }
1578
1579         // Check seamless boot support
1580         for (i = 0; i < context->stream_count; i++) {
1581                 if (context->streams[i]->apply_seamless_boot_optimization) {
1582                         can_apply_seamless_boot = true;
1583                         break;
1584                 }
1585         }
1586
1587         /* eDP should not have stream in resume from S4 and so even with VBios post
1588          * it should get turned off
1589          */
1590         if (!can_apply_edp_fast_boot && !can_apply_seamless_boot) {
1591                 if (edp_link_with_sink && !keep_edp_vdd_on) {
1592                         /*turn off backlight before DP_blank and encoder powered down*/
1593                         dc->hwss.edp_backlight_control(edp_link_with_sink, false);
1594                 }
1595                 /*resume from S3, no vbios posting, no need to power down again*/
1596                 power_down_all_hw_blocks(dc);
1597                 disable_vga_and_power_gate_all_controllers(dc);
1598                 if (edp_link_with_sink && !keep_edp_vdd_on)
1599                         dc->hwss.edp_power_control(edp_link_with_sink, false);
1600         }
1601         bios_set_scratch_acc_mode_change(dc->ctx->dc_bios);
1602 }
1603
1604 static uint32_t compute_pstate_blackout_duration(
1605         struct bw_fixed blackout_duration,
1606         const struct dc_stream_state *stream)
1607 {
1608         uint32_t total_dest_line_time_ns;
1609         uint32_t pstate_blackout_duration_ns;
1610
1611         pstate_blackout_duration_ns = 1000 * blackout_duration.value >> 24;
1612
1613         total_dest_line_time_ns = 1000000UL *
1614                 (stream->timing.h_total * 10) /
1615                 stream->timing.pix_clk_100hz +
1616                 pstate_blackout_duration_ns;
1617
1618         return total_dest_line_time_ns;
1619 }
1620
1621 static void dce110_set_displaymarks(
1622         const struct dc *dc,
1623         struct dc_state *context)
1624 {
1625         uint8_t i, num_pipes;
1626         unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
1627
1628         for (i = 0, num_pipes = 0; i < MAX_PIPES; i++) {
1629                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1630                 uint32_t total_dest_line_time_ns;
1631
1632                 if (pipe_ctx->stream == NULL)
1633                         continue;
1634
1635                 total_dest_line_time_ns = compute_pstate_blackout_duration(
1636                         dc->bw_vbios->blackout_duration, pipe_ctx->stream);
1637                 pipe_ctx->plane_res.mi->funcs->mem_input_program_display_marks(
1638                         pipe_ctx->plane_res.mi,
1639                         context->bw_ctx.bw.dce.nbp_state_change_wm_ns[num_pipes],
1640                         context->bw_ctx.bw.dce.stutter_exit_wm_ns[num_pipes],
1641                         context->bw_ctx.bw.dce.stutter_entry_wm_ns[num_pipes],
1642                         context->bw_ctx.bw.dce.urgent_wm_ns[num_pipes],
1643                         total_dest_line_time_ns);
1644                 if (i == underlay_idx) {
1645                         num_pipes++;
1646                         pipe_ctx->plane_res.mi->funcs->mem_input_program_chroma_display_marks(
1647                                 pipe_ctx->plane_res.mi,
1648                                 context->bw_ctx.bw.dce.nbp_state_change_wm_ns[num_pipes],
1649                                 context->bw_ctx.bw.dce.stutter_exit_wm_ns[num_pipes],
1650                                 context->bw_ctx.bw.dce.urgent_wm_ns[num_pipes],
1651                                 total_dest_line_time_ns);
1652                 }
1653                 num_pipes++;
1654         }
1655 }
1656
1657 void dce110_set_safe_displaymarks(
1658                 struct resource_context *res_ctx,
1659                 const struct resource_pool *pool)
1660 {
1661         int i;
1662         int underlay_idx = pool->underlay_pipe_index;
1663         struct dce_watermarks max_marks = {
1664                 MAX_WATERMARK, MAX_WATERMARK, MAX_WATERMARK, MAX_WATERMARK };
1665         struct dce_watermarks nbp_marks = {
1666                 SAFE_NBP_MARK, SAFE_NBP_MARK, SAFE_NBP_MARK, SAFE_NBP_MARK };
1667         struct dce_watermarks min_marks = { 0, 0, 0, 0};
1668
1669         for (i = 0; i < MAX_PIPES; i++) {
1670                 if (res_ctx->pipe_ctx[i].stream == NULL || res_ctx->pipe_ctx[i].plane_res.mi == NULL)
1671                         continue;
1672
1673                 res_ctx->pipe_ctx[i].plane_res.mi->funcs->mem_input_program_display_marks(
1674                                 res_ctx->pipe_ctx[i].plane_res.mi,
1675                                 nbp_marks,
1676                                 max_marks,
1677                                 min_marks,
1678                                 max_marks,
1679                                 MAX_WATERMARK);
1680
1681                 if (i == underlay_idx)
1682                         res_ctx->pipe_ctx[i].plane_res.mi->funcs->mem_input_program_chroma_display_marks(
1683                                 res_ctx->pipe_ctx[i].plane_res.mi,
1684                                 nbp_marks,
1685                                 max_marks,
1686                                 max_marks,
1687                                 MAX_WATERMARK);
1688
1689         }
1690 }
1691
1692 /*******************************************************************************
1693  * Public functions
1694  ******************************************************************************/
1695
1696 static void set_drr(struct pipe_ctx **pipe_ctx,
1697                 int num_pipes, unsigned int vmin, unsigned int vmax,
1698                 unsigned int vmid, unsigned int vmid_frame_number)
1699 {
1700         int i = 0;
1701         struct drr_params params = {0};
1702         // DRR should set trigger event to monitor surface update event
1703         unsigned int event_triggers = 0x80;
1704
1705         params.vertical_total_max = vmax;
1706         params.vertical_total_min = vmin;
1707
1708         /* TODO: If multiple pipes are to be supported, you need
1709          * some GSL stuff. Static screen triggers may be programmed differently
1710          * as well.
1711          */
1712         for (i = 0; i < num_pipes; i++) {
1713                 pipe_ctx[i]->stream_res.tg->funcs->set_drr(
1714                         pipe_ctx[i]->stream_res.tg, &params);
1715
1716                 if (vmax != 0 && vmin != 0)
1717                         pipe_ctx[i]->stream_res.tg->funcs->set_static_screen_control(
1718                                         pipe_ctx[i]->stream_res.tg,
1719                                         event_triggers);
1720         }
1721 }
1722
1723 static void get_position(struct pipe_ctx **pipe_ctx,
1724                 int num_pipes,
1725                 struct crtc_position *position)
1726 {
1727         int i = 0;
1728
1729         /* TODO: handle pipes > 1
1730          */
1731         for (i = 0; i < num_pipes; i++)
1732                 pipe_ctx[i]->stream_res.tg->funcs->get_position(pipe_ctx[i]->stream_res.tg, position);
1733 }
1734
1735 static void set_static_screen_control(struct pipe_ctx **pipe_ctx,
1736                 int num_pipes, const struct dc_static_screen_events *events)
1737 {
1738         unsigned int i;
1739         unsigned int value = 0;
1740
1741         if (events->overlay_update)
1742                 value |= 0x100;
1743         if (events->surface_update)
1744                 value |= 0x80;
1745         if (events->cursor_update)
1746                 value |= 0x2;
1747         if (events->force_trigger)
1748                 value |= 0x1;
1749
1750         if (num_pipes) {
1751                 struct dc *dc = pipe_ctx[0]->stream->ctx->dc;
1752
1753                 if (dc->fbc_compressor)
1754                         value |= 0x84;
1755         }
1756
1757         for (i = 0; i < num_pipes; i++)
1758                 pipe_ctx[i]->stream_res.tg->funcs->
1759                         set_static_screen_control(pipe_ctx[i]->stream_res.tg, value);
1760 }
1761
1762 /*
1763  *  Check if FBC can be enabled
1764  */
1765 static bool should_enable_fbc(struct dc *dc,
1766                 struct dc_state *context,
1767                 uint32_t *pipe_idx)
1768 {
1769         uint32_t i;
1770         struct pipe_ctx *pipe_ctx = NULL;
1771         struct resource_context *res_ctx = &context->res_ctx;
1772         unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
1773
1774
1775         ASSERT(dc->fbc_compressor);
1776
1777         /* FBC memory should be allocated */
1778         if (!dc->ctx->fbc_gpu_addr)
1779                 return false;
1780
1781         /* Only supports single display */
1782         if (context->stream_count != 1)
1783                 return false;
1784
1785         for (i = 0; i < dc->res_pool->pipe_count; i++) {
1786                 if (res_ctx->pipe_ctx[i].stream) {
1787
1788                         pipe_ctx = &res_ctx->pipe_ctx[i];
1789
1790                         if (!pipe_ctx)
1791                                 continue;
1792
1793                         /* fbc not applicable on underlay pipe */
1794                         if (pipe_ctx->pipe_idx != underlay_idx) {
1795                                 *pipe_idx = i;
1796                                 break;
1797                         }
1798                 }
1799         }
1800
1801         if (i == dc->res_pool->pipe_count)
1802                 return false;
1803
1804         if (!pipe_ctx->stream->link)
1805                 return false;
1806
1807         /* Only supports eDP */
1808         if (pipe_ctx->stream->link->connector_signal != SIGNAL_TYPE_EDP)
1809                 return false;
1810
1811         /* PSR should not be enabled */
1812         if (pipe_ctx->stream->link->psr_feature_enabled)
1813                 return false;
1814
1815         /* Nothing to compress */
1816         if (!pipe_ctx->plane_state)
1817                 return false;
1818
1819         /* Only for non-linear tiling */
1820         if (pipe_ctx->plane_state->tiling_info.gfx8.array_mode == DC_ARRAY_LINEAR_GENERAL)
1821                 return false;
1822
1823         return true;
1824 }
1825
1826 /*
1827  *  Enable FBC
1828  */
1829 static void enable_fbc(
1830                 struct dc *dc,
1831                 struct dc_state *context)
1832 {
1833         uint32_t pipe_idx = 0;
1834
1835         if (should_enable_fbc(dc, context, &pipe_idx)) {
1836                 /* Program GRPH COMPRESSED ADDRESS and PITCH */
1837                 struct compr_addr_and_pitch_params params = {0, 0, 0};
1838                 struct compressor *compr = dc->fbc_compressor;
1839                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
1840
1841                 params.source_view_width = pipe_ctx->stream->timing.h_addressable;
1842                 params.source_view_height = pipe_ctx->stream->timing.v_addressable;
1843                 params.inst = pipe_ctx->stream_res.tg->inst;
1844                 compr->compr_surface_address.quad_part = dc->ctx->fbc_gpu_addr;
1845
1846                 compr->funcs->surface_address_and_pitch(compr, &params);
1847                 compr->funcs->set_fbc_invalidation_triggers(compr, 1);
1848
1849                 compr->funcs->enable_fbc(compr, &params);
1850         }
1851 }
1852
1853 static void dce110_reset_hw_ctx_wrap(
1854                 struct dc *dc,
1855                 struct dc_state *context)
1856 {
1857         int i;
1858
1859         /* Reset old context */
1860         /* look up the targets that have been removed since last commit */
1861         for (i = 0; i < MAX_PIPES; i++) {
1862                 struct pipe_ctx *pipe_ctx_old =
1863                         &dc->current_state->res_ctx.pipe_ctx[i];
1864                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1865
1866                 /* Note: We need to disable output if clock sources change,
1867                  * since bios does optimization and doesn't apply if changing
1868                  * PHY when not already disabled.
1869                  */
1870
1871                 /* Skip underlay pipe since it will be handled in commit surface*/
1872                 if (!pipe_ctx_old->stream || pipe_ctx_old->top_pipe)
1873                         continue;
1874
1875                 if (!pipe_ctx->stream ||
1876                                 pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
1877                         struct clock_source *old_clk = pipe_ctx_old->clock_source;
1878
1879                         /* Disable if new stream is null. O/w, if stream is
1880                          * disabled already, no need to disable again.
1881                          */
1882                         if (!pipe_ctx->stream || !pipe_ctx->stream->dpms_off) {
1883                                 core_link_disable_stream(pipe_ctx_old);
1884
1885                                 /* free acquired resources*/
1886                                 if (pipe_ctx_old->stream_res.audio) {
1887                                         /*disable az_endpoint*/
1888                                         pipe_ctx_old->stream_res.audio->funcs->
1889                                                         az_disable(pipe_ctx_old->stream_res.audio);
1890
1891                                         /*free audio*/
1892                                         if (dc->caps.dynamic_audio == true) {
1893                                                 /*we have to dynamic arbitrate the audio endpoints*/
1894                                                 /*we free the resource, need reset is_audio_acquired*/
1895                                                 update_audio_usage(&dc->current_state->res_ctx, dc->res_pool,
1896                                                                 pipe_ctx_old->stream_res.audio, false);
1897                                                 pipe_ctx_old->stream_res.audio = NULL;
1898                                         }
1899                                 }
1900                         }
1901
1902                         pipe_ctx_old->stream_res.tg->funcs->set_blank(pipe_ctx_old->stream_res.tg, true);
1903                         if (!hwss_wait_for_blank_complete(pipe_ctx_old->stream_res.tg)) {
1904                                 dm_error("DC: failed to blank crtc!\n");
1905                                 BREAK_TO_DEBUGGER();
1906                         }
1907                         pipe_ctx_old->stream_res.tg->funcs->disable_crtc(pipe_ctx_old->stream_res.tg);
1908                         pipe_ctx_old->plane_res.mi->funcs->free_mem_input(
1909                                         pipe_ctx_old->plane_res.mi, dc->current_state->stream_count);
1910
1911                         if (old_clk && 0 == resource_get_clock_source_reference(&context->res_ctx,
1912                                                                                 dc->res_pool,
1913                                                                                 old_clk))
1914                                 old_clk->funcs->cs_power_down(old_clk);
1915
1916                         dc->hwss.disable_plane(dc, pipe_ctx_old);
1917
1918                         pipe_ctx_old->stream = NULL;
1919                 }
1920         }
1921 }
1922
1923 static void dce110_setup_audio_dto(
1924                 struct dc *dc,
1925                 struct dc_state *context)
1926 {
1927         int i;
1928
1929         /* program audio wall clock. use HDMI as clock source if HDMI
1930          * audio active. Otherwise, use DP as clock source
1931          * first, loop to find any HDMI audio, if not, loop find DP audio
1932          */
1933         /* Setup audio rate clock source */
1934         /* Issue:
1935         * Audio lag happened on DP monitor when unplug a HDMI monitor
1936         *
1937         * Cause:
1938         * In case of DP and HDMI connected or HDMI only, DCCG_AUDIO_DTO_SEL
1939         * is set to either dto0 or dto1, audio should work fine.
1940         * In case of DP connected only, DCCG_AUDIO_DTO_SEL should be dto1,
1941         * set to dto0 will cause audio lag.
1942         *
1943         * Solution:
1944         * Not optimized audio wall dto setup. When mode set, iterate pipe_ctx,
1945         * find first available pipe with audio, setup audio wall DTO per topology
1946         * instead of per pipe.
1947         */
1948         for (i = 0; i < dc->res_pool->pipe_count; i++) {
1949                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1950
1951                 if (pipe_ctx->stream == NULL)
1952                         continue;
1953
1954                 if (pipe_ctx->top_pipe)
1955                         continue;
1956
1957                 if (pipe_ctx->stream->signal != SIGNAL_TYPE_HDMI_TYPE_A)
1958                         continue;
1959
1960                 if (pipe_ctx->stream_res.audio != NULL) {
1961                         struct audio_output audio_output;
1962
1963                         build_audio_output(context, pipe_ctx, &audio_output);
1964
1965                         pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
1966                                 pipe_ctx->stream_res.audio,
1967                                 pipe_ctx->stream->signal,
1968                                 &audio_output.crtc_info,
1969                                 &audio_output.pll_info);
1970                         break;
1971                 }
1972         }
1973
1974         /* no HDMI audio is found, try DP audio */
1975         if (i == dc->res_pool->pipe_count) {
1976                 for (i = 0; i < dc->res_pool->pipe_count; i++) {
1977                         struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1978
1979                         if (pipe_ctx->stream == NULL)
1980                                 continue;
1981
1982                         if (pipe_ctx->top_pipe)
1983                                 continue;
1984
1985                         if (!dc_is_dp_signal(pipe_ctx->stream->signal))
1986                                 continue;
1987
1988                         if (pipe_ctx->stream_res.audio != NULL) {
1989                                 struct audio_output audio_output;
1990
1991                                 build_audio_output(context, pipe_ctx, &audio_output);
1992
1993                                 pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
1994                                         pipe_ctx->stream_res.audio,
1995                                         pipe_ctx->stream->signal,
1996                                         &audio_output.crtc_info,
1997                                         &audio_output.pll_info);
1998                                 break;
1999                         }
2000                 }
2001         }
2002 }
2003
2004 enum dc_status dce110_apply_ctx_to_hw(
2005                 struct dc *dc,
2006                 struct dc_state *context)
2007 {
2008         struct dc_bios *dcb = dc->ctx->dc_bios;
2009         enum dc_status status;
2010         int i;
2011
2012         /* Reset old context */
2013         /* look up the targets that have been removed since last commit */
2014         dc->hwss.reset_hw_ctx_wrap(dc, context);
2015
2016         /* Skip applying if no targets */
2017         if (context->stream_count <= 0)
2018                 return DC_OK;
2019
2020         /* Apply new context */
2021         dcb->funcs->set_scratch_critical_state(dcb, true);
2022
2023         /* below is for real asic only */
2024         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2025                 struct pipe_ctx *pipe_ctx_old =
2026                                         &dc->current_state->res_ctx.pipe_ctx[i];
2027                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2028
2029                 if (pipe_ctx->stream == NULL || pipe_ctx->top_pipe)
2030                         continue;
2031
2032                 if (pipe_ctx->stream == pipe_ctx_old->stream) {
2033                         if (pipe_ctx_old->clock_source != pipe_ctx->clock_source)
2034                                 dce_crtc_switch_to_clk_src(dc->hwseq,
2035                                                 pipe_ctx->clock_source, i);
2036                         continue;
2037                 }
2038
2039                 dc->hwss.enable_display_power_gating(
2040                                 dc, i, dc->ctx->dc_bios,
2041                                 PIPE_GATING_CONTROL_DISABLE);
2042         }
2043
2044         if (dc->fbc_compressor)
2045                 dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
2046
2047         dce110_setup_audio_dto(dc, context);
2048
2049         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2050                 struct pipe_ctx *pipe_ctx_old =
2051                                         &dc->current_state->res_ctx.pipe_ctx[i];
2052                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2053
2054                 if (pipe_ctx->stream == NULL)
2055                         continue;
2056
2057                 if (pipe_ctx->stream == pipe_ctx_old->stream &&
2058                         pipe_ctx->stream->link->link_state_valid) {
2059                         continue;
2060                 }
2061
2062                 if (pipe_ctx_old->stream && !pipe_need_reprogram(pipe_ctx_old, pipe_ctx))
2063                         continue;
2064
2065                 if (pipe_ctx->top_pipe || pipe_ctx->prev_odm_pipe)
2066                         continue;
2067
2068                 status = apply_single_controller_ctx_to_hw(
2069                                 pipe_ctx,
2070                                 context,
2071                                 dc);
2072
2073                 if (DC_OK != status)
2074                         return status;
2075         }
2076
2077         if (dc->fbc_compressor)
2078                 enable_fbc(dc, dc->current_state);
2079
2080         dcb->funcs->set_scratch_critical_state(dcb, false);
2081
2082         return DC_OK;
2083 }
2084
2085 /*******************************************************************************
2086  * Front End programming
2087  ******************************************************************************/
2088 static void set_default_colors(struct pipe_ctx *pipe_ctx)
2089 {
2090         struct default_adjustment default_adjust = { 0 };
2091
2092         default_adjust.force_hw_default = false;
2093         default_adjust.in_color_space = pipe_ctx->plane_state->color_space;
2094         default_adjust.out_color_space = pipe_ctx->stream->output_color_space;
2095         default_adjust.csc_adjust_type = GRAPHICS_CSC_ADJUST_TYPE_SW;
2096         default_adjust.surface_pixel_format = pipe_ctx->plane_res.scl_data.format;
2097
2098         /* display color depth */
2099         default_adjust.color_depth =
2100                 pipe_ctx->stream->timing.display_color_depth;
2101
2102         /* Lb color depth */
2103         default_adjust.lb_color_depth = pipe_ctx->plane_res.scl_data.lb_params.depth;
2104
2105         pipe_ctx->plane_res.xfm->funcs->opp_set_csc_default(
2106                                         pipe_ctx->plane_res.xfm, &default_adjust);
2107 }
2108
2109
2110 /*******************************************************************************
2111  * In order to turn on/off specific surface we will program
2112  * Blender + CRTC
2113  *
2114  * In case that we have two surfaces and they have a different visibility
2115  * we can't turn off the CRTC since it will turn off the entire display
2116  *
2117  * |----------------------------------------------- |
2118  * |bottom pipe|curr pipe  |              |         |
2119  * |Surface    |Surface    | Blender      |  CRCT   |
2120  * |visibility |visibility | Configuration|         |
2121  * |------------------------------------------------|
2122  * |   off     |    off    | CURRENT_PIPE | blank   |
2123  * |   off     |    on     | CURRENT_PIPE | unblank |
2124  * |   on      |    off    | OTHER_PIPE   | unblank |
2125  * |   on      |    on     | BLENDING     | unblank |
2126  * -------------------------------------------------|
2127  *
2128  ******************************************************************************/
2129 static void program_surface_visibility(const struct dc *dc,
2130                 struct pipe_ctx *pipe_ctx)
2131 {
2132         enum blnd_mode blender_mode = BLND_MODE_CURRENT_PIPE;
2133         bool blank_target = false;
2134
2135         if (pipe_ctx->bottom_pipe) {
2136
2137                 /* For now we are supporting only two pipes */
2138                 ASSERT(pipe_ctx->bottom_pipe->bottom_pipe == NULL);
2139
2140                 if (pipe_ctx->bottom_pipe->plane_state->visible) {
2141                         if (pipe_ctx->plane_state->visible)
2142                                 blender_mode = BLND_MODE_BLENDING;
2143                         else
2144                                 blender_mode = BLND_MODE_OTHER_PIPE;
2145
2146                 } else if (!pipe_ctx->plane_state->visible)
2147                         blank_target = true;
2148
2149         } else if (!pipe_ctx->plane_state->visible)
2150                 blank_target = true;
2151
2152         dce_set_blender_mode(dc->hwseq, pipe_ctx->stream_res.tg->inst, blender_mode);
2153         pipe_ctx->stream_res.tg->funcs->set_blank(pipe_ctx->stream_res.tg, blank_target);
2154
2155 }
2156
2157 static void program_gamut_remap(struct pipe_ctx *pipe_ctx)
2158 {
2159         int i = 0;
2160         struct xfm_grph_csc_adjustment adjust;
2161         memset(&adjust, 0, sizeof(adjust));
2162         adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_BYPASS;
2163
2164
2165         if (pipe_ctx->stream->gamut_remap_matrix.enable_remap == true) {
2166                 adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_SW;
2167
2168                 for (i = 0; i < CSC_TEMPERATURE_MATRIX_SIZE; i++)
2169                         adjust.temperature_matrix[i] =
2170                                 pipe_ctx->stream->gamut_remap_matrix.matrix[i];
2171         }
2172
2173         pipe_ctx->plane_res.xfm->funcs->transform_set_gamut_remap(pipe_ctx->plane_res.xfm, &adjust);
2174 }
2175 static void update_plane_addr(const struct dc *dc,
2176                 struct pipe_ctx *pipe_ctx)
2177 {
2178         struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2179
2180         if (plane_state == NULL)
2181                 return;
2182
2183         pipe_ctx->plane_res.mi->funcs->mem_input_program_surface_flip_and_addr(
2184                         pipe_ctx->plane_res.mi,
2185                         &plane_state->address,
2186                         plane_state->flip_immediate);
2187
2188         plane_state->status.requested_address = plane_state->address;
2189 }
2190
2191 static void dce110_update_pending_status(struct pipe_ctx *pipe_ctx)
2192 {
2193         struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2194
2195         if (plane_state == NULL)
2196                 return;
2197
2198         plane_state->status.is_flip_pending =
2199                         pipe_ctx->plane_res.mi->funcs->mem_input_is_flip_pending(
2200                                         pipe_ctx->plane_res.mi);
2201
2202         if (plane_state->status.is_flip_pending && !plane_state->visible)
2203                 pipe_ctx->plane_res.mi->current_address = pipe_ctx->plane_res.mi->request_address;
2204
2205         plane_state->status.current_address = pipe_ctx->plane_res.mi->current_address;
2206         if (pipe_ctx->plane_res.mi->current_address.type == PLN_ADDR_TYPE_GRPH_STEREO &&
2207                         pipe_ctx->stream_res.tg->funcs->is_stereo_left_eye) {
2208                 plane_state->status.is_right_eye =\
2209                                 !pipe_ctx->stream_res.tg->funcs->is_stereo_left_eye(pipe_ctx->stream_res.tg);
2210         }
2211 }
2212
2213 void dce110_power_down(struct dc *dc)
2214 {
2215         power_down_all_hw_blocks(dc);
2216         disable_vga_and_power_gate_all_controllers(dc);
2217 }
2218
2219 static bool wait_for_reset_trigger_to_occur(
2220         struct dc_context *dc_ctx,
2221         struct timing_generator *tg)
2222 {
2223         bool rc = false;
2224
2225         /* To avoid endless loop we wait at most
2226          * frames_to_wait_on_triggered_reset frames for the reset to occur. */
2227         const uint32_t frames_to_wait_on_triggered_reset = 10;
2228         uint32_t i;
2229
2230         for (i = 0; i < frames_to_wait_on_triggered_reset; i++) {
2231
2232                 if (!tg->funcs->is_counter_moving(tg)) {
2233                         DC_ERROR("TG counter is not moving!\n");
2234                         break;
2235                 }
2236
2237                 if (tg->funcs->did_triggered_reset_occur(tg)) {
2238                         rc = true;
2239                         /* usually occurs at i=1 */
2240                         DC_SYNC_INFO("GSL: reset occurred at wait count: %d\n",
2241                                         i);
2242                         break;
2243                 }
2244
2245                 /* Wait for one frame. */
2246                 tg->funcs->wait_for_state(tg, CRTC_STATE_VACTIVE);
2247                 tg->funcs->wait_for_state(tg, CRTC_STATE_VBLANK);
2248         }
2249
2250         if (false == rc)
2251                 DC_ERROR("GSL: Timeout on reset trigger!\n");
2252
2253         return rc;
2254 }
2255
2256 /* Enable timing synchronization for a group of Timing Generators. */
2257 static void dce110_enable_timing_synchronization(
2258                 struct dc *dc,
2259                 int group_index,
2260                 int group_size,
2261                 struct pipe_ctx *grouped_pipes[])
2262 {
2263         struct dc_context *dc_ctx = dc->ctx;
2264         struct dcp_gsl_params gsl_params = { 0 };
2265         int i;
2266
2267         DC_SYNC_INFO("GSL: Setting-up...\n");
2268
2269         /* Designate a single TG in the group as a master.
2270          * Since HW doesn't care which one, we always assign
2271          * the 1st one in the group. */
2272         gsl_params.gsl_group = 0;
2273         gsl_params.gsl_master = grouped_pipes[0]->stream_res.tg->inst;
2274
2275         for (i = 0; i < group_size; i++)
2276                 grouped_pipes[i]->stream_res.tg->funcs->setup_global_swap_lock(
2277                                         grouped_pipes[i]->stream_res.tg, &gsl_params);
2278
2279         /* Reset slave controllers on master VSync */
2280         DC_SYNC_INFO("GSL: enabling trigger-reset\n");
2281
2282         for (i = 1 /* skip the master */; i < group_size; i++)
2283                 grouped_pipes[i]->stream_res.tg->funcs->enable_reset_trigger(
2284                                 grouped_pipes[i]->stream_res.tg,
2285                                 gsl_params.gsl_group);
2286
2287         for (i = 1 /* skip the master */; i < group_size; i++) {
2288                 DC_SYNC_INFO("GSL: waiting for reset to occur.\n");
2289                 wait_for_reset_trigger_to_occur(dc_ctx, grouped_pipes[i]->stream_res.tg);
2290                 grouped_pipes[i]->stream_res.tg->funcs->disable_reset_trigger(
2291                                 grouped_pipes[i]->stream_res.tg);
2292         }
2293
2294         /* GSL Vblank synchronization is a one time sync mechanism, assumption
2295          * is that the sync'ed displays will not drift out of sync over time*/
2296         DC_SYNC_INFO("GSL: Restoring register states.\n");
2297         for (i = 0; i < group_size; i++)
2298                 grouped_pipes[i]->stream_res.tg->funcs->tear_down_global_swap_lock(grouped_pipes[i]->stream_res.tg);
2299
2300         DC_SYNC_INFO("GSL: Set-up complete.\n");
2301 }
2302
2303 static void dce110_enable_per_frame_crtc_position_reset(
2304                 struct dc *dc,
2305                 int group_size,
2306                 struct pipe_ctx *grouped_pipes[])
2307 {
2308         struct dc_context *dc_ctx = dc->ctx;
2309         struct dcp_gsl_params gsl_params = { 0 };
2310         int i;
2311
2312         gsl_params.gsl_group = 0;
2313         gsl_params.gsl_master = 0;
2314
2315         for (i = 0; i < group_size; i++)
2316                 grouped_pipes[i]->stream_res.tg->funcs->setup_global_swap_lock(
2317                                         grouped_pipes[i]->stream_res.tg, &gsl_params);
2318
2319         DC_SYNC_INFO("GSL: enabling trigger-reset\n");
2320
2321         for (i = 1; i < group_size; i++)
2322                 grouped_pipes[i]->stream_res.tg->funcs->enable_crtc_reset(
2323                                 grouped_pipes[i]->stream_res.tg,
2324                                 gsl_params.gsl_master,
2325                                 &grouped_pipes[i]->stream->triggered_crtc_reset);
2326
2327         DC_SYNC_INFO("GSL: waiting for reset to occur.\n");
2328         for (i = 1; i < group_size; i++)
2329                 wait_for_reset_trigger_to_occur(dc_ctx, grouped_pipes[i]->stream_res.tg);
2330
2331         for (i = 0; i < group_size; i++)
2332                 grouped_pipes[i]->stream_res.tg->funcs->tear_down_global_swap_lock(grouped_pipes[i]->stream_res.tg);
2333
2334 }
2335
2336 static void init_pipes(struct dc *dc, struct dc_state *context)
2337 {
2338         // Do nothing
2339 }
2340
2341 static void init_hw(struct dc *dc)
2342 {
2343         int i;
2344         struct dc_bios *bp;
2345         struct transform *xfm;
2346         struct abm *abm;
2347         struct dmcu *dmcu;
2348
2349         bp = dc->ctx->dc_bios;
2350         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2351                 xfm = dc->res_pool->transforms[i];
2352                 xfm->funcs->transform_reset(xfm);
2353
2354                 dc->hwss.enable_display_power_gating(
2355                                 dc, i, bp,
2356                                 PIPE_GATING_CONTROL_INIT);
2357                 dc->hwss.enable_display_power_gating(
2358                                 dc, i, bp,
2359                                 PIPE_GATING_CONTROL_DISABLE);
2360                 dc->hwss.enable_display_pipe_clock_gating(
2361                         dc->ctx,
2362                         true);
2363         }
2364
2365         dce_clock_gating_power_up(dc->hwseq, false);
2366         /***************************************/
2367
2368         for (i = 0; i < dc->link_count; i++) {
2369                 /****************************************/
2370                 /* Power up AND update implementation according to the
2371                  * required signal (which may be different from the
2372                  * default signal on connector). */
2373                 struct dc_link *link = dc->links[i];
2374
2375                 link->link_enc->funcs->hw_init(link->link_enc);
2376         }
2377
2378         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2379                 struct timing_generator *tg = dc->res_pool->timing_generators[i];
2380
2381                 tg->funcs->disable_vga(tg);
2382
2383                 /* Blank controller using driver code instead of
2384                  * command table. */
2385                 tg->funcs->set_blank(tg, true);
2386                 hwss_wait_for_blank_complete(tg);
2387         }
2388
2389         for (i = 0; i < dc->res_pool->audio_count; i++) {
2390                 struct audio *audio = dc->res_pool->audios[i];
2391                 audio->funcs->hw_init(audio);
2392         }
2393
2394         abm = dc->res_pool->abm;
2395         if (abm != NULL) {
2396                 abm->funcs->init_backlight(abm);
2397                 abm->funcs->abm_init(abm);
2398         }
2399
2400         dmcu = dc->res_pool->dmcu;
2401         if (dmcu != NULL && abm != NULL)
2402                 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
2403
2404         if (dc->fbc_compressor)
2405                 dc->fbc_compressor->funcs->power_up_fbc(dc->fbc_compressor);
2406
2407 }
2408
2409
2410 void dce110_prepare_bandwidth(
2411                 struct dc *dc,
2412                 struct dc_state *context)
2413 {
2414         struct clk_mgr *dccg = dc->clk_mgr;
2415
2416         dce110_set_safe_displaymarks(&context->res_ctx, dc->res_pool);
2417
2418         dccg->funcs->update_clocks(
2419                         dccg,
2420                         context,
2421                         false);
2422 }
2423
2424 void dce110_optimize_bandwidth(
2425                 struct dc *dc,
2426                 struct dc_state *context)
2427 {
2428         struct clk_mgr *dccg = dc->clk_mgr;
2429
2430         dce110_set_displaymarks(dc, context);
2431
2432         dccg->funcs->update_clocks(
2433                         dccg,
2434                         context,
2435                         true);
2436 }
2437
2438 static void dce110_program_front_end_for_pipe(
2439                 struct dc *dc, struct pipe_ctx *pipe_ctx)
2440 {
2441         struct mem_input *mi = pipe_ctx->plane_res.mi;
2442         struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2443         struct xfm_grph_csc_adjustment adjust;
2444         struct out_csc_color_matrix tbl_entry;
2445         unsigned int i;
2446         DC_LOGGER_INIT();
2447         memset(&tbl_entry, 0, sizeof(tbl_entry));
2448
2449         memset(&adjust, 0, sizeof(adjust));
2450         adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_BYPASS;
2451
2452         dce_enable_fe_clock(dc->hwseq, mi->inst, true);
2453
2454         set_default_colors(pipe_ctx);
2455         if (pipe_ctx->stream->csc_color_matrix.enable_adjustment
2456                         == true) {
2457                 tbl_entry.color_space =
2458                         pipe_ctx->stream->output_color_space;
2459
2460                 for (i = 0; i < 12; i++)
2461                         tbl_entry.regval[i] =
2462                         pipe_ctx->stream->csc_color_matrix.matrix[i];
2463
2464                 pipe_ctx->plane_res.xfm->funcs->opp_set_csc_adjustment
2465                                 (pipe_ctx->plane_res.xfm, &tbl_entry);
2466         }
2467
2468         if (pipe_ctx->stream->gamut_remap_matrix.enable_remap == true) {
2469                 adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_SW;
2470
2471                 for (i = 0; i < CSC_TEMPERATURE_MATRIX_SIZE; i++)
2472                         adjust.temperature_matrix[i] =
2473                                 pipe_ctx->stream->gamut_remap_matrix.matrix[i];
2474         }
2475
2476         pipe_ctx->plane_res.xfm->funcs->transform_set_gamut_remap(pipe_ctx->plane_res.xfm, &adjust);
2477
2478         pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->bottom_pipe != 0;
2479
2480         program_scaler(dc, pipe_ctx);
2481
2482         mi->funcs->mem_input_program_surface_config(
2483                         mi,
2484                         plane_state->format,
2485                         &plane_state->tiling_info,
2486                         &plane_state->plane_size,
2487                         plane_state->rotation,
2488                         NULL,
2489                         false);
2490         if (mi->funcs->set_blank)
2491                 mi->funcs->set_blank(mi, pipe_ctx->plane_state->visible);
2492
2493         if (dc->config.gpu_vm_support)
2494                 mi->funcs->mem_input_program_pte_vm(
2495                                 pipe_ctx->plane_res.mi,
2496                                 plane_state->format,
2497                                 &plane_state->tiling_info,
2498                                 plane_state->rotation);
2499
2500         /* Moved programming gamma from dc to hwss */
2501         if (pipe_ctx->plane_state->update_flags.bits.full_update ||
2502                         pipe_ctx->plane_state->update_flags.bits.in_transfer_func_change ||
2503                         pipe_ctx->plane_state->update_flags.bits.gamma_change)
2504                 dc->hwss.set_input_transfer_func(pipe_ctx, pipe_ctx->plane_state);
2505
2506         if (pipe_ctx->plane_state->update_flags.bits.full_update)
2507                 dc->hwss.set_output_transfer_func(pipe_ctx, pipe_ctx->stream);
2508
2509         DC_LOG_SURFACE(
2510                         "Pipe:%d %p: addr hi:0x%x, "
2511                         "addr low:0x%x, "
2512                         "src: %d, %d, %d,"
2513                         " %d; dst: %d, %d, %d, %d;"
2514                         "clip: %d, %d, %d, %d\n",
2515                         pipe_ctx->pipe_idx,
2516                         (void *) pipe_ctx->plane_state,
2517                         pipe_ctx->plane_state->address.grph.addr.high_part,
2518                         pipe_ctx->plane_state->address.grph.addr.low_part,
2519                         pipe_ctx->plane_state->src_rect.x,
2520                         pipe_ctx->plane_state->src_rect.y,
2521                         pipe_ctx->plane_state->src_rect.width,
2522                         pipe_ctx->plane_state->src_rect.height,
2523                         pipe_ctx->plane_state->dst_rect.x,
2524                         pipe_ctx->plane_state->dst_rect.y,
2525                         pipe_ctx->plane_state->dst_rect.width,
2526                         pipe_ctx->plane_state->dst_rect.height,
2527                         pipe_ctx->plane_state->clip_rect.x,
2528                         pipe_ctx->plane_state->clip_rect.y,
2529                         pipe_ctx->plane_state->clip_rect.width,
2530                         pipe_ctx->plane_state->clip_rect.height);
2531
2532         DC_LOG_SURFACE(
2533                         "Pipe %d: width, height, x, y\n"
2534                         "viewport:%d, %d, %d, %d\n"
2535                         "recout:  %d, %d, %d, %d\n",
2536                         pipe_ctx->pipe_idx,
2537                         pipe_ctx->plane_res.scl_data.viewport.width,
2538                         pipe_ctx->plane_res.scl_data.viewport.height,
2539                         pipe_ctx->plane_res.scl_data.viewport.x,
2540                         pipe_ctx->plane_res.scl_data.viewport.y,
2541                         pipe_ctx->plane_res.scl_data.recout.width,
2542                         pipe_ctx->plane_res.scl_data.recout.height,
2543                         pipe_ctx->plane_res.scl_data.recout.x,
2544                         pipe_ctx->plane_res.scl_data.recout.y);
2545 }
2546
2547 static void dce110_apply_ctx_for_surface(
2548                 struct dc *dc,
2549                 const struct dc_stream_state *stream,
2550                 int num_planes,
2551                 struct dc_state *context)
2552 {
2553         int i;
2554
2555         if (num_planes == 0)
2556                 return;
2557
2558         if (dc->fbc_compressor)
2559                 dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
2560
2561         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2562                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2563                 struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
2564
2565                 if (stream == pipe_ctx->stream) {
2566                         if (!pipe_ctx->top_pipe &&
2567                                 (pipe_ctx->plane_state || old_pipe_ctx->plane_state))
2568                                 dc->hwss.pipe_control_lock(dc, pipe_ctx, true);
2569                 }
2570         }
2571
2572         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2573                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2574
2575                 if (pipe_ctx->stream != stream)
2576                         continue;
2577
2578                 /* Need to allocate mem before program front end for Fiji */
2579                 pipe_ctx->plane_res.mi->funcs->allocate_mem_input(
2580                                 pipe_ctx->plane_res.mi,
2581                                 pipe_ctx->stream->timing.h_total,
2582                                 pipe_ctx->stream->timing.v_total,
2583                                 pipe_ctx->stream->timing.pix_clk_100hz / 10,
2584                                 context->stream_count);
2585
2586                 dce110_program_front_end_for_pipe(dc, pipe_ctx);
2587
2588                 dc->hwss.update_plane_addr(dc, pipe_ctx);
2589
2590                 program_surface_visibility(dc, pipe_ctx);
2591
2592         }
2593
2594         for (i = 0; i < dc->res_pool->pipe_count; i++) {
2595                 struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2596                 struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
2597
2598                 if ((stream == pipe_ctx->stream) &&
2599                         (!pipe_ctx->top_pipe) &&
2600                         (pipe_ctx->plane_state || old_pipe_ctx->plane_state))
2601                         dc->hwss.pipe_control_lock(dc, pipe_ctx, false);
2602         }
2603
2604         if (dc->fbc_compressor)
2605                 enable_fbc(dc, context);
2606 }
2607
2608 static void dce110_power_down_fe(struct dc *dc, struct pipe_ctx *pipe_ctx)
2609 {
2610         int fe_idx = pipe_ctx->plane_res.mi ?
2611                 pipe_ctx->plane_res.mi->inst : pipe_ctx->pipe_idx;
2612
2613         /* Do not power down fe when stream is active on dce*/
2614         if (dc->current_state->res_ctx.pipe_ctx[fe_idx].stream)
2615                 return;
2616
2617         dc->hwss.enable_display_power_gating(
2618                 dc, fe_idx, dc->ctx->dc_bios, PIPE_GATING_CONTROL_ENABLE);
2619
2620         dc->res_pool->transforms[fe_idx]->funcs->transform_reset(
2621                                 dc->res_pool->transforms[fe_idx]);
2622 }
2623
2624 static void dce110_wait_for_mpcc_disconnect(
2625                 struct dc *dc,
2626                 struct resource_pool *res_pool,
2627                 struct pipe_ctx *pipe_ctx)
2628 {
2629         /* do nothing*/
2630 }
2631
2632 static void program_output_csc(struct dc *dc,
2633                 struct pipe_ctx *pipe_ctx,
2634                 enum dc_color_space colorspace,
2635                 uint16_t *matrix,
2636                 int opp_id)
2637 {
2638         int i;
2639         struct out_csc_color_matrix tbl_entry;
2640
2641         if (pipe_ctx->stream->csc_color_matrix.enable_adjustment == true) {
2642                 enum dc_color_space color_space = pipe_ctx->stream->output_color_space;
2643
2644                 for (i = 0; i < 12; i++)
2645                         tbl_entry.regval[i] = pipe_ctx->stream->csc_color_matrix.matrix[i];
2646
2647                 tbl_entry.color_space = color_space;
2648
2649                 pipe_ctx->plane_res.xfm->funcs->opp_set_csc_adjustment(
2650                                 pipe_ctx->plane_res.xfm, &tbl_entry);
2651         }
2652 }
2653
2654 void dce110_set_cursor_position(struct pipe_ctx *pipe_ctx)
2655 {
2656         struct dc_cursor_position pos_cpy = pipe_ctx->stream->cursor_position;
2657         struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
2658         struct mem_input *mi = pipe_ctx->plane_res.mi;
2659         struct dc_cursor_mi_param param = {
2660                 .pixel_clk_khz = pipe_ctx->stream->timing.pix_clk_100hz / 10,
2661                 .ref_clk_khz = pipe_ctx->stream->ctx->dc->res_pool->ref_clocks.xtalin_clock_inKhz,
2662                 .viewport = pipe_ctx->plane_res.scl_data.viewport,
2663                 .h_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.horz,
2664                 .v_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.vert,
2665                 .rotation = pipe_ctx->plane_state->rotation,
2666                 .mirror = pipe_ctx->plane_state->horizontal_mirror
2667         };
2668
2669         if (pipe_ctx->plane_state->address.type
2670                         == PLN_ADDR_TYPE_VIDEO_PROGRESSIVE)
2671                 pos_cpy.enable = false;
2672
2673         if (pipe_ctx->top_pipe && pipe_ctx->plane_state != pipe_ctx->top_pipe->plane_state)
2674                 pos_cpy.enable = false;
2675
2676         if (ipp->funcs->ipp_cursor_set_position)
2677                 ipp->funcs->ipp_cursor_set_position(ipp, &pos_cpy, &param);
2678         if (mi->funcs->set_cursor_position)
2679                 mi->funcs->set_cursor_position(mi, &pos_cpy, &param);
2680 }
2681
2682 void dce110_set_cursor_attribute(struct pipe_ctx *pipe_ctx)
2683 {
2684         struct dc_cursor_attributes *attributes = &pipe_ctx->stream->cursor_attributes;
2685
2686         if (pipe_ctx->plane_res.ipp &&
2687             pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes)
2688                 pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes(
2689                                 pipe_ctx->plane_res.ipp, attributes);
2690
2691         if (pipe_ctx->plane_res.mi &&
2692             pipe_ctx->plane_res.mi->funcs->set_cursor_attributes)
2693                 pipe_ctx->plane_res.mi->funcs->set_cursor_attributes(
2694                                 pipe_ctx->plane_res.mi, attributes);
2695
2696         if (pipe_ctx->plane_res.xfm &&
2697             pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes)
2698                 pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes(
2699                                 pipe_ctx->plane_res.xfm, attributes);
2700 }
2701
2702 static const struct hw_sequencer_funcs dce110_funcs = {
2703         .program_gamut_remap = program_gamut_remap,
2704         .program_output_csc = program_output_csc,
2705         .init_hw = init_hw,
2706         .init_pipes = init_pipes,
2707         .apply_ctx_to_hw = dce110_apply_ctx_to_hw,
2708         .apply_ctx_for_surface = dce110_apply_ctx_for_surface,
2709         .update_plane_addr = update_plane_addr,
2710         .update_pending_status = dce110_update_pending_status,
2711         .set_input_transfer_func = dce110_set_input_transfer_func,
2712         .set_output_transfer_func = dce110_set_output_transfer_func,
2713         .power_down = dce110_power_down,
2714         .enable_accelerated_mode = dce110_enable_accelerated_mode,
2715         .enable_timing_synchronization = dce110_enable_timing_synchronization,
2716         .enable_per_frame_crtc_position_reset = dce110_enable_per_frame_crtc_position_reset,
2717         .update_info_frame = dce110_update_info_frame,
2718         .enable_stream = dce110_enable_stream,
2719         .disable_stream = dce110_disable_stream,
2720         .unblank_stream = dce110_unblank_stream,
2721         .blank_stream = dce110_blank_stream,
2722         .enable_audio_stream = dce110_enable_audio_stream,
2723         .disable_audio_stream = dce110_disable_audio_stream,
2724         .enable_display_pipe_clock_gating = enable_display_pipe_clock_gating,
2725         .enable_display_power_gating = dce110_enable_display_power_gating,
2726         .disable_plane = dce110_power_down_fe,
2727         .pipe_control_lock = dce_pipe_control_lock,
2728         .prepare_bandwidth = dce110_prepare_bandwidth,
2729         .optimize_bandwidth = dce110_optimize_bandwidth,
2730         .set_drr = set_drr,
2731         .get_position = get_position,
2732         .set_static_screen_control = set_static_screen_control,
2733         .reset_hw_ctx_wrap = dce110_reset_hw_ctx_wrap,
2734         .enable_stream_timing = dce110_enable_stream_timing,
2735         .disable_stream_gating = NULL,
2736         .enable_stream_gating = NULL,
2737         .setup_stereo = NULL,
2738         .set_avmute = dce110_set_avmute,
2739         .wait_for_mpcc_disconnect = dce110_wait_for_mpcc_disconnect,
2740         .edp_backlight_control = dce110_edp_backlight_control,
2741         .edp_power_control = dce110_edp_power_control,
2742         .edp_wait_for_hpd_ready = dce110_edp_wait_for_hpd_ready,
2743         .set_cursor_position = dce110_set_cursor_position,
2744         .set_cursor_attribute = dce110_set_cursor_attribute
2745 };
2746
2747 void dce110_hw_sequencer_construct(struct dc *dc)
2748 {
2749         dc->hwss = dce110_funcs;
2750 }
2751