xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc.c (revision 7ee983c850b40043ac4751836fbd9a2b4d0c5937)
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  #include "dm_services.h"
26  
27  #include "amdgpu.h"
28  
29  #include "dc.h"
30  
31  #include "core_status.h"
32  #include "core_types.h"
33  #include "hw_sequencer.h"
34  #include "dce/dce_hwseq.h"
35  
36  #include "resource.h"
37  #include "dc_state.h"
38  #include "dc_state_priv.h"
39  #include "dc_plane_priv.h"
40  
41  #include "gpio_service_interface.h"
42  #include "clk_mgr.h"
43  #include "clock_source.h"
44  #include "dc_bios_types.h"
45  
46  #include "bios_parser_interface.h"
47  #include "bios/bios_parser_helper.h"
48  #include "include/irq_service_interface.h"
49  #include "transform.h"
50  #include "dmcu.h"
51  #include "dpp.h"
52  #include "timing_generator.h"
53  #include "abm.h"
54  #include "virtual/virtual_link_encoder.h"
55  #include "hubp.h"
56  
57  #include "link_hwss.h"
58  #include "link_encoder.h"
59  #include "link_enc_cfg.h"
60  
61  #include "link.h"
62  #include "dm_helpers.h"
63  #include "mem_input.h"
64  
65  #include "dc_dmub_srv.h"
66  
67  #include "dsc.h"
68  
69  #include "vm_helper.h"
70  
71  #include "dce/dce_i2c.h"
72  
73  #include "dmub/dmub_srv.h"
74  
75  #include "dce/dmub_psr.h"
76  
77  #include "dce/dmub_hw_lock_mgr.h"
78  
79  #include "dc_trace.h"
80  
81  #include "hw_sequencer_private.h"
82  
83  #if defined(CONFIG_DRM_AMD_DC_FP)
84  #include "dml2/dml2_internal_types.h"
85  #endif
86  
87  #include "dce/dmub_outbox.h"
88  
89  #define CTX \
90  	dc->ctx
91  
92  #define DC_LOGGER \
93  	dc->ctx->logger
94  
95  static const char DC_BUILD_ID[] = "production-build";
96  
97  /**
98   * DOC: Overview
99   *
100   * DC is the OS-agnostic component of the amdgpu DC driver.
101   *
102   * DC maintains and validates a set of structs representing the state of the
103   * driver and writes that state to AMD hardware
104   *
105   * Main DC HW structs:
106   *
107   * struct dc - The central struct.  One per driver.  Created on driver load,
108   * destroyed on driver unload.
109   *
110   * struct dc_context - One per driver.
111   * Used as a backpointer by most other structs in dc.
112   *
113   * struct dc_link - One per connector (the physical DP, HDMI, miniDP, or eDP
114   * plugpoints).  Created on driver load, destroyed on driver unload.
115   *
116   * struct dc_sink - One per display.  Created on boot or hotplug.
117   * Destroyed on shutdown or hotunplug.  A dc_link can have a local sink
118   * (the display directly attached).  It may also have one or more remote
119   * sinks (in the Multi-Stream Transport case)
120   *
121   * struct resource_pool - One per driver.  Represents the hw blocks not in the
122   * main pipeline.  Not directly accessible by dm.
123   *
124   * Main dc state structs:
125   *
126   * These structs can be created and destroyed as needed.  There is a full set of
127   * these structs in dc->current_state representing the currently programmed state.
128   *
129   * struct dc_state - The global DC state to track global state information,
130   * such as bandwidth values.
131   *
132   * struct dc_stream_state - Represents the hw configuration for the pipeline from
133   * a framebuffer to a display.  Maps one-to-one with dc_sink.
134   *
135   * struct dc_plane_state - Represents a framebuffer.  Each stream has at least one,
136   * and may have more in the Multi-Plane Overlay case.
137   *
138   * struct resource_context - Represents the programmable state of everything in
139   * the resource_pool.  Not directly accessible by dm.
140   *
141   * struct pipe_ctx - A member of struct resource_context.  Represents the
142   * internal hardware pipeline components.  Each dc_plane_state has either
143   * one or two (in the pipe-split case).
144   */
145  
146  /* Private functions */
147  
elevate_update_type(enum surface_update_type * original,enum surface_update_type new)148  static inline void elevate_update_type(enum surface_update_type *original, enum surface_update_type new)
149  {
150  	if (new > *original)
151  		*original = new;
152  }
153  
destroy_links(struct dc * dc)154  static void destroy_links(struct dc *dc)
155  {
156  	uint32_t i;
157  
158  	for (i = 0; i < dc->link_count; i++) {
159  		if (NULL != dc->links[i])
160  			dc->link_srv->destroy_link(&dc->links[i]);
161  	}
162  }
163  
get_num_of_internal_disp(struct dc_link ** links,uint32_t num_links)164  static uint32_t get_num_of_internal_disp(struct dc_link **links, uint32_t num_links)
165  {
166  	int i;
167  	uint32_t count = 0;
168  
169  	for (i = 0; i < num_links; i++) {
170  		if (links[i]->connector_signal == SIGNAL_TYPE_EDP ||
171  				links[i]->is_internal_display)
172  			count++;
173  	}
174  
175  	return count;
176  }
177  
get_seamless_boot_stream_count(struct dc_state * ctx)178  static int get_seamless_boot_stream_count(struct dc_state *ctx)
179  {
180  	uint8_t i;
181  	uint8_t seamless_boot_stream_count = 0;
182  
183  	for (i = 0; i < ctx->stream_count; i++)
184  		if (ctx->streams[i]->apply_seamless_boot_optimization)
185  			seamless_boot_stream_count++;
186  
187  	return seamless_boot_stream_count;
188  }
189  
create_links(struct dc * dc,uint32_t num_virtual_links)190  static bool create_links(
191  		struct dc *dc,
192  		uint32_t num_virtual_links)
193  {
194  	int i;
195  	int connectors_num;
196  	struct dc_bios *bios = dc->ctx->dc_bios;
197  
198  	dc->link_count = 0;
199  
200  	connectors_num = bios->funcs->get_connectors_number(bios);
201  
202  	DC_LOG_DC("BIOS object table - number of connectors: %d", connectors_num);
203  
204  	if (connectors_num > ENUM_ID_COUNT) {
205  		dm_error(
206  			"DC: Number of connectors %d exceeds maximum of %d!\n",
207  			connectors_num,
208  			ENUM_ID_COUNT);
209  		return false;
210  	}
211  
212  	dm_output_to_console(
213  		"DC: %s: connectors_num: physical:%d, virtual:%d\n",
214  		__func__,
215  		connectors_num,
216  		num_virtual_links);
217  
218  	// condition loop on link_count to allow skipping invalid indices
219  	for (i = 0; dc->link_count < connectors_num && i < MAX_LINKS; i++) {
220  		struct link_init_data link_init_params = {0};
221  		struct dc_link *link;
222  
223  		DC_LOG_DC("BIOS object table - printing link object info for connector number: %d, link_index: %d", i, dc->link_count);
224  
225  		link_init_params.ctx = dc->ctx;
226  		/* next BIOS object table connector */
227  		link_init_params.connector_index = i;
228  		link_init_params.link_index = dc->link_count;
229  		link_init_params.dc = dc;
230  		link = dc->link_srv->create_link(&link_init_params);
231  
232  		if (link) {
233  			dc->links[dc->link_count] = link;
234  			link->dc = dc;
235  			++dc->link_count;
236  		}
237  	}
238  
239  	DC_LOG_DC("BIOS object table - end");
240  
241  	/* Create a link for each usb4 dpia port */
242  	for (i = 0; i < dc->res_pool->usb4_dpia_count; i++) {
243  		struct link_init_data link_init_params = {0};
244  		struct dc_link *link;
245  
246  		link_init_params.ctx = dc->ctx;
247  		link_init_params.connector_index = i;
248  		link_init_params.link_index = dc->link_count;
249  		link_init_params.dc = dc;
250  		link_init_params.is_dpia_link = true;
251  
252  		link = dc->link_srv->create_link(&link_init_params);
253  		if (link) {
254  			dc->links[dc->link_count] = link;
255  			link->dc = dc;
256  			++dc->link_count;
257  		}
258  	}
259  
260  	for (i = 0; i < num_virtual_links; i++) {
261  		struct dc_link *link = kzalloc(sizeof(*link), GFP_KERNEL);
262  		struct encoder_init_data enc_init = {0};
263  
264  		if (link == NULL) {
265  			BREAK_TO_DEBUGGER();
266  			goto failed_alloc;
267  		}
268  
269  		link->link_index = dc->link_count;
270  		dc->links[dc->link_count] = link;
271  		dc->link_count++;
272  
273  		link->ctx = dc->ctx;
274  		link->dc = dc;
275  		link->connector_signal = SIGNAL_TYPE_VIRTUAL;
276  		link->link_id.type = OBJECT_TYPE_CONNECTOR;
277  		link->link_id.id = CONNECTOR_ID_VIRTUAL;
278  		link->link_id.enum_id = ENUM_ID_1;
279  		link->link_enc = kzalloc(sizeof(*link->link_enc), GFP_KERNEL);
280  
281  		if (!link->link_enc) {
282  			BREAK_TO_DEBUGGER();
283  			goto failed_alloc;
284  		}
285  
286  		link->link_status.dpcd_caps = &link->dpcd_caps;
287  
288  		enc_init.ctx = dc->ctx;
289  		enc_init.channel = CHANNEL_ID_UNKNOWN;
290  		enc_init.hpd_source = HPD_SOURCEID_UNKNOWN;
291  		enc_init.transmitter = TRANSMITTER_UNKNOWN;
292  		enc_init.connector = link->link_id;
293  		enc_init.encoder.type = OBJECT_TYPE_ENCODER;
294  		enc_init.encoder.id = ENCODER_ID_INTERNAL_VIRTUAL;
295  		enc_init.encoder.enum_id = ENUM_ID_1;
296  		virtual_link_encoder_construct(link->link_enc, &enc_init);
297  	}
298  
299  	dc->caps.num_of_internal_disp = get_num_of_internal_disp(dc->links, dc->link_count);
300  
301  	return true;
302  
303  failed_alloc:
304  	return false;
305  }
306  
307  /* Create additional DIG link encoder objects if fewer than the platform
308   * supports were created during link construction. This can happen if the
309   * number of physical connectors is less than the number of DIGs.
310   */
create_link_encoders(struct dc * dc)311  static bool create_link_encoders(struct dc *dc)
312  {
313  	bool res = true;
314  	unsigned int num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
315  	unsigned int num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
316  	int i;
317  
318  	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
319  	 * link encoders and physical display endpoints and does not require
320  	 * additional link encoder objects.
321  	 */
322  	if (num_usb4_dpia == 0)
323  		return res;
324  
325  	/* Create as many link encoder objects as the platform supports. DPIA
326  	 * endpoints can be programmably mapped to any DIG.
327  	 */
328  	if (num_dig_link_enc > dc->res_pool->dig_link_enc_count) {
329  		for (i = 0; i < num_dig_link_enc; i++) {
330  			struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
331  
332  			if (!link_enc && dc->res_pool->funcs->link_enc_create_minimal) {
333  				link_enc = dc->res_pool->funcs->link_enc_create_minimal(dc->ctx,
334  						(enum engine_id)(ENGINE_ID_DIGA + i));
335  				if (link_enc) {
336  					dc->res_pool->link_encoders[i] = link_enc;
337  					dc->res_pool->dig_link_enc_count++;
338  				} else {
339  					res = false;
340  				}
341  			}
342  		}
343  	}
344  
345  	return res;
346  }
347  
348  /* Destroy any additional DIG link encoder objects created by
349   * create_link_encoders().
350   * NB: Must only be called after destroy_links().
351   */
destroy_link_encoders(struct dc * dc)352  static void destroy_link_encoders(struct dc *dc)
353  {
354  	unsigned int num_usb4_dpia;
355  	unsigned int num_dig_link_enc;
356  	int i;
357  
358  	if (!dc->res_pool)
359  		return;
360  
361  	num_usb4_dpia = dc->res_pool->res_cap->num_usb4_dpia;
362  	num_dig_link_enc = dc->res_pool->res_cap->num_dig_link_enc;
363  
364  	/* A platform without USB4 DPIA endpoints has a fixed mapping between DIG
365  	 * link encoders and physical display endpoints and does not require
366  	 * additional link encoder objects.
367  	 */
368  	if (num_usb4_dpia == 0)
369  		return;
370  
371  	for (i = 0; i < num_dig_link_enc; i++) {
372  		struct link_encoder *link_enc = dc->res_pool->link_encoders[i];
373  
374  		if (link_enc) {
375  			link_enc->funcs->destroy(&link_enc);
376  			dc->res_pool->link_encoders[i] = NULL;
377  			dc->res_pool->dig_link_enc_count--;
378  		}
379  	}
380  }
381  
dc_perf_trace_create(void)382  static struct dc_perf_trace *dc_perf_trace_create(void)
383  {
384  	return kzalloc(sizeof(struct dc_perf_trace), GFP_KERNEL);
385  }
386  
dc_perf_trace_destroy(struct dc_perf_trace ** perf_trace)387  static void dc_perf_trace_destroy(struct dc_perf_trace **perf_trace)
388  {
389  	kfree(*perf_trace);
390  	*perf_trace = NULL;
391  }
392  
set_long_vtotal(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)393  static bool set_long_vtotal(struct dc *dc, struct dc_stream_state *stream, struct dc_crtc_timing_adjust *adjust)
394  {
395  	if (!dc || !stream || !adjust)
396  		return false;
397  
398  	if (!dc->current_state)
399  		return false;
400  
401  	int i;
402  
403  	for (i = 0; i < MAX_PIPES; i++) {
404  		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
405  
406  		if (pipe->stream == stream && pipe->stream_res.tg) {
407  			if (dc->hwss.set_long_vtotal)
408  				dc->hwss.set_long_vtotal(&pipe, 1, adjust->v_total_min, adjust->v_total_max);
409  
410  			return true;
411  		}
412  	}
413  
414  	return false;
415  }
416  
417  /**
418   *  dc_stream_adjust_vmin_vmax - look up pipe context & update parts of DRR
419   *  @dc:     dc reference
420   *  @stream: Initial dc stream state
421   *  @adjust: Updated parameters for vertical_total_min and vertical_total_max
422   *
423   *  Looks up the pipe context of dc_stream_state and updates the
424   *  vertical_total_min and vertical_total_max of the DRR, Dynamic Refresh
425   *  Rate, which is a power-saving feature that targets reducing panel
426   *  refresh rate while the screen is static
427   *
428   *  Return: %true if the pipe context is found and adjusted;
429   *          %false if the pipe context is not found.
430   */
dc_stream_adjust_vmin_vmax(struct dc * dc,struct dc_stream_state * stream,struct dc_crtc_timing_adjust * adjust)431  bool dc_stream_adjust_vmin_vmax(struct dc *dc,
432  		struct dc_stream_state *stream,
433  		struct dc_crtc_timing_adjust *adjust)
434  {
435  	int i;
436  
437  	/*
438  	 * Don't adjust DRR while there's bandwidth optimizations pending to
439  	 * avoid conflicting with firmware updates.
440  	 */
441  	if (dc->ctx->dce_version > DCE_VERSION_MAX)
442  		if (dc->optimized_required || dc->wm_optimized_required)
443  			return false;
444  
445  	dc_exit_ips_for_hw_access(dc);
446  
447  	stream->adjust.v_total_max = adjust->v_total_max;
448  	stream->adjust.v_total_mid = adjust->v_total_mid;
449  	stream->adjust.v_total_mid_frame_num = adjust->v_total_mid_frame_num;
450  	stream->adjust.v_total_min = adjust->v_total_min;
451  	stream->adjust.allow_otg_v_count_halt = adjust->allow_otg_v_count_halt;
452  
453  	if (dc->caps.max_v_total != 0 &&
454  		(adjust->v_total_max > dc->caps.max_v_total || adjust->v_total_min > dc->caps.max_v_total)) {
455  		if (adjust->allow_otg_v_count_halt)
456  			return set_long_vtotal(dc, stream, adjust);
457  		else
458  			return false;
459  	}
460  
461  	for (i = 0; i < MAX_PIPES; i++) {
462  		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
463  
464  		if (pipe->stream == stream && pipe->stream_res.tg) {
465  			dc->hwss.set_drr(&pipe,
466  					1,
467  					*adjust);
468  
469  			return true;
470  		}
471  	}
472  	return false;
473  }
474  
475  /**
476   * dc_stream_get_last_used_drr_vtotal - Looks up the pipe context of
477   * dc_stream_state and gets the last VTOTAL used by DRR (Dynamic Refresh Rate)
478   *
479   * @dc: [in] dc reference
480   * @stream: [in] Initial dc stream state
481   * @refresh_rate: [in] new refresh_rate
482   *
483   * Return: %true if the pipe context is found and there is an associated
484   *         timing_generator for the DC;
485   *         %false if the pipe context is not found or there is no
486   *         timing_generator for the DC.
487   */
dc_stream_get_last_used_drr_vtotal(struct dc * dc,struct dc_stream_state * stream,uint32_t * refresh_rate)488  bool dc_stream_get_last_used_drr_vtotal(struct dc *dc,
489  		struct dc_stream_state *stream,
490  		uint32_t *refresh_rate)
491  {
492  	bool status = false;
493  
494  	int i = 0;
495  
496  	dc_exit_ips_for_hw_access(dc);
497  
498  	for (i = 0; i < MAX_PIPES; i++) {
499  		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
500  
501  		if (pipe->stream == stream && pipe->stream_res.tg) {
502  			/* Only execute if a function pointer has been defined for
503  			 * the DC version in question
504  			 */
505  			if (pipe->stream_res.tg->funcs->get_last_used_drr_vtotal) {
506  				pipe->stream_res.tg->funcs->get_last_used_drr_vtotal(pipe->stream_res.tg, refresh_rate);
507  
508  				status = true;
509  
510  				break;
511  			}
512  		}
513  	}
514  
515  	return status;
516  }
517  
dc_stream_get_crtc_position(struct dc * dc,struct dc_stream_state ** streams,int num_streams,unsigned int * v_pos,unsigned int * nom_v_pos)518  bool dc_stream_get_crtc_position(struct dc *dc,
519  		struct dc_stream_state **streams, int num_streams,
520  		unsigned int *v_pos, unsigned int *nom_v_pos)
521  {
522  	/* TODO: Support multiple streams */
523  	const struct dc_stream_state *stream = streams[0];
524  	int i;
525  	bool ret = false;
526  	struct crtc_position position;
527  
528  	dc_exit_ips_for_hw_access(dc);
529  
530  	for (i = 0; i < MAX_PIPES; i++) {
531  		struct pipe_ctx *pipe =
532  				&dc->current_state->res_ctx.pipe_ctx[i];
533  
534  		if (pipe->stream == stream && pipe->stream_res.stream_enc) {
535  			dc->hwss.get_position(&pipe, 1, &position);
536  
537  			*v_pos = position.vertical_count;
538  			*nom_v_pos = position.nominal_vcount;
539  			ret = true;
540  		}
541  	}
542  	return ret;
543  }
544  
545  #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
546  static inline void
dc_stream_forward_dmub_crc_window(struct dc_dmub_srv * dmub_srv,struct rect * rect,struct otg_phy_mux * mux_mapping,bool is_stop)547  dc_stream_forward_dmub_crc_window(struct dc_dmub_srv *dmub_srv,
548  		struct rect *rect, struct otg_phy_mux *mux_mapping, bool is_stop)
549  {
550  	union dmub_rb_cmd cmd = {0};
551  
552  	cmd.secure_display.roi_info.phy_id = mux_mapping->phy_output_num;
553  	cmd.secure_display.roi_info.otg_id = mux_mapping->otg_output_num;
554  
555  	if (is_stop) {
556  		cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
557  		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_CRC_STOP_UPDATE;
558  	} else {
559  		cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
560  		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_CRC_WIN_NOTIFY;
561  		cmd.secure_display.roi_info.x_start = rect->x;
562  		cmd.secure_display.roi_info.y_start = rect->y;
563  		cmd.secure_display.roi_info.x_end = rect->x + rect->width;
564  		cmd.secure_display.roi_info.y_end = rect->y + rect->height;
565  	}
566  
567  	dc_wake_and_execute_dmub_cmd(dmub_srv->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
568  }
569  
570  static inline void
dc_stream_forward_dmcu_crc_window(struct dmcu * dmcu,struct rect * rect,struct otg_phy_mux * mux_mapping,bool is_stop)571  dc_stream_forward_dmcu_crc_window(struct dmcu *dmcu,
572  		struct rect *rect, struct otg_phy_mux *mux_mapping, bool is_stop)
573  {
574  	if (is_stop)
575  		dmcu->funcs->stop_crc_win_update(dmcu, mux_mapping);
576  	else
577  		dmcu->funcs->forward_crc_window(dmcu, rect, mux_mapping);
578  }
579  
580  bool
dc_stream_forward_crc_window(struct dc_stream_state * stream,struct rect * rect,uint8_t phy_id,bool is_stop)581  dc_stream_forward_crc_window(struct dc_stream_state *stream,
582  		struct rect *rect, uint8_t phy_id, bool is_stop)
583  {
584  	struct dmcu *dmcu;
585  	struct dc_dmub_srv *dmub_srv;
586  	struct otg_phy_mux mux_mapping;
587  	struct pipe_ctx *pipe;
588  	int i;
589  	struct dc *dc = stream->ctx->dc;
590  
591  	for (i = 0; i < MAX_PIPES; i++) {
592  		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
593  		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
594  			break;
595  	}
596  
597  	/* Stream not found */
598  	if (i == MAX_PIPES)
599  		return false;
600  
601  	mux_mapping.phy_output_num = phy_id;
602  	mux_mapping.otg_output_num = pipe->stream_res.tg->inst;
603  
604  	dmcu = dc->res_pool->dmcu;
605  	dmub_srv = dc->ctx->dmub_srv;
606  
607  	/* forward to dmub */
608  	if (dmub_srv)
609  		dc_stream_forward_dmub_crc_window(dmub_srv, rect, &mux_mapping, is_stop);
610  	/* forward to dmcu */
611  	else if (dmcu && dmcu->funcs->is_dmcu_initialized(dmcu))
612  		dc_stream_forward_dmcu_crc_window(dmcu, rect, &mux_mapping, is_stop);
613  	else
614  		return false;
615  
616  	return true;
617  }
618  
619  static void
dc_stream_forward_dmub_multiple_crc_window(struct dc_dmub_srv * dmub_srv,struct crc_window * window,struct otg_phy_mux * mux_mapping,bool stop)620  dc_stream_forward_dmub_multiple_crc_window(struct dc_dmub_srv *dmub_srv,
621  		struct crc_window *window, struct otg_phy_mux *mux_mapping, bool stop)
622  {
623  	int i;
624  	union dmub_rb_cmd cmd = {0};
625  
626  	cmd.secure_display.mul_roi_ctl.phy_id = mux_mapping->phy_output_num;
627  	cmd.secure_display.mul_roi_ctl.otg_id = mux_mapping->otg_output_num;
628  
629  	cmd.secure_display.header.type = DMUB_CMD__SECURE_DISPLAY;
630  
631  	if (stop) {
632  		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_MULTIPLE_CRC_STOP_UPDATE;
633  	} else {
634  		cmd.secure_display.header.sub_type = DMUB_CMD__SECURE_DISPLAY_MULTIPLE_CRC_WIN_NOTIFY;
635  		for (i = 0; i < MAX_CRC_WINDOW_NUM; i++) {
636  			cmd.secure_display.mul_roi_ctl.roi_ctl[i].x_start = window[i].rect.x;
637  			cmd.secure_display.mul_roi_ctl.roi_ctl[i].y_start = window[i].rect.y;
638  			cmd.secure_display.mul_roi_ctl.roi_ctl[i].x_end = window[i].rect.x + window[i].rect.width;
639  			cmd.secure_display.mul_roi_ctl.roi_ctl[i].y_end = window[i].rect.y + window[i].rect.height;
640  			cmd.secure_display.mul_roi_ctl.roi_ctl[i].enable = window[i].enable;
641  		}
642  	}
643  
644  	dc_wake_and_execute_dmub_cmd(dmub_srv->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
645  }
646  
647  bool
dc_stream_forward_multiple_crc_window(struct dc_stream_state * stream,struct crc_window * window,uint8_t phy_id,bool stop)648  dc_stream_forward_multiple_crc_window(struct dc_stream_state *stream,
649  		struct crc_window *window, uint8_t phy_id, bool stop)
650  {
651  	struct dc_dmub_srv *dmub_srv;
652  	struct otg_phy_mux mux_mapping;
653  	struct pipe_ctx *pipe;
654  	int i;
655  	struct dc *dc = stream->ctx->dc;
656  
657  	for (i = 0; i < MAX_PIPES; i++) {
658  		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
659  		if (pipe->stream == stream && !pipe->top_pipe && !pipe->prev_odm_pipe)
660  			break;
661  	}
662  
663  	/* Stream not found */
664  	if (i == MAX_PIPES)
665  		return false;
666  
667  	mux_mapping.phy_output_num = phy_id;
668  	mux_mapping.otg_output_num = pipe->stream_res.tg->inst;
669  
670  	dmub_srv = dc->ctx->dmub_srv;
671  
672  	/* forward to dmub only. no dmcu support*/
673  	if (dmub_srv)
674  		dc_stream_forward_dmub_multiple_crc_window(dmub_srv, window, &mux_mapping, stop);
675  	else
676  		return false;
677  
678  	return true;
679  }
680  #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
681  
682  /**
683   * dc_stream_configure_crc() - Configure CRC capture for the given stream.
684   * @dc: DC Object
685   * @stream: The stream to configure CRC on.
686   * @crc_window: CRC window (x/y start/end) information
687   * @enable: Enable CRC if true, disable otherwise.
688   * @continuous: Capture CRC on every frame if true. Otherwise, only capture
689   *              once.
690   * @idx: Capture CRC on which CRC engine instance
691   * @reset: Reset CRC engine before the configuration
692   *
693   * By default, the entire frame is used to calculate the CRC.
694   *
695   * Return: %false if the stream is not found or CRC capture is not supported;
696   *         %true if the stream has been configured.
697   */
dc_stream_configure_crc(struct dc * dc,struct dc_stream_state * stream,struct crc_params * crc_window,bool enable,bool continuous,uint8_t idx,bool reset)698  bool dc_stream_configure_crc(struct dc *dc, struct dc_stream_state *stream,
699  			     struct crc_params *crc_window, bool enable, bool continuous,
700  			     uint8_t idx, bool reset)
701  {
702  	struct pipe_ctx *pipe;
703  	struct crc_params param;
704  	struct timing_generator *tg;
705  
706  	pipe = resource_get_otg_master_for_stream(
707  			&dc->current_state->res_ctx, stream);
708  
709  	/* Stream not found */
710  	if (pipe == NULL)
711  		return false;
712  
713  	dc_exit_ips_for_hw_access(dc);
714  
715  	/* By default, capture the full frame */
716  	param.windowa_x_start = 0;
717  	param.windowa_y_start = 0;
718  	param.windowa_x_end = pipe->stream->timing.h_addressable;
719  	param.windowa_y_end = pipe->stream->timing.v_addressable;
720  	param.windowb_x_start = 0;
721  	param.windowb_y_start = 0;
722  	param.windowb_x_end = pipe->stream->timing.h_addressable;
723  	param.windowb_y_end = pipe->stream->timing.v_addressable;
724  
725  	if (crc_window) {
726  		param.windowa_x_start = crc_window->windowa_x_start;
727  		param.windowa_y_start = crc_window->windowa_y_start;
728  		param.windowa_x_end = crc_window->windowa_x_end;
729  		param.windowa_y_end = crc_window->windowa_y_end;
730  		param.windowb_x_start = crc_window->windowb_x_start;
731  		param.windowb_y_start = crc_window->windowb_y_start;
732  		param.windowb_x_end = crc_window->windowb_x_end;
733  		param.windowb_y_end = crc_window->windowb_y_end;
734  	}
735  
736  	param.dsc_mode = pipe->stream->timing.flags.DSC ? 1:0;
737  	param.odm_mode = pipe->next_odm_pipe ? 1:0;
738  
739  	/* Default to the union of both windows */
740  	param.selection = UNION_WINDOW_A_B;
741  	param.continuous_mode = continuous;
742  	param.enable = enable;
743  
744  	param.crc_eng_inst = idx;
745  	param.reset = reset;
746  
747  	tg = pipe->stream_res.tg;
748  
749  	/* Only call if supported */
750  	if (tg->funcs->configure_crc)
751  		return tg->funcs->configure_crc(tg, &param);
752  	DC_LOG_WARNING("CRC capture not supported.");
753  	return false;
754  }
755  
756  /**
757   * dc_stream_get_crc() - Get CRC values for the given stream.
758   *
759   * @dc: DC object.
760   * @stream: The DC stream state of the stream to get CRCs from.
761   * @idx: index of crc engine to get CRC from
762   * @r_cr: CRC value for the red component.
763   * @g_y:  CRC value for the green component.
764   * @b_cb: CRC value for the blue component.
765   *
766   * dc_stream_configure_crc needs to be called beforehand to enable CRCs.
767   *
768   * Return:
769   * %false if stream is not found, or if CRCs are not enabled.
770   */
dc_stream_get_crc(struct dc * dc,struct dc_stream_state * stream,uint8_t idx,uint32_t * r_cr,uint32_t * g_y,uint32_t * b_cb)771  bool dc_stream_get_crc(struct dc *dc, struct dc_stream_state *stream, uint8_t idx,
772  		       uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
773  {
774  	int i;
775  	struct pipe_ctx *pipe;
776  	struct timing_generator *tg;
777  
778  	dc_exit_ips_for_hw_access(dc);
779  
780  	for (i = 0; i < MAX_PIPES; i++) {
781  		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
782  		if (pipe->stream == stream)
783  			break;
784  	}
785  	/* Stream not found */
786  	if (i == MAX_PIPES)
787  		return false;
788  
789  	tg = pipe->stream_res.tg;
790  
791  	if (tg->funcs->get_crc)
792  		return tg->funcs->get_crc(tg, idx, r_cr, g_y, b_cb);
793  	DC_LOG_WARNING("CRC capture not supported.");
794  	return false;
795  }
796  
dc_stream_set_dyn_expansion(struct dc * dc,struct dc_stream_state * stream,enum dc_dynamic_expansion option)797  void dc_stream_set_dyn_expansion(struct dc *dc, struct dc_stream_state *stream,
798  		enum dc_dynamic_expansion option)
799  {
800  	/* OPP FMT dyn expansion updates*/
801  	int i;
802  	struct pipe_ctx *pipe_ctx;
803  
804  	dc_exit_ips_for_hw_access(dc);
805  
806  	for (i = 0; i < MAX_PIPES; i++) {
807  		if (dc->current_state->res_ctx.pipe_ctx[i].stream
808  				== stream) {
809  			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
810  			pipe_ctx->stream_res.opp->dyn_expansion = option;
811  			pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
812  					pipe_ctx->stream_res.opp,
813  					COLOR_SPACE_YCBCR601,
814  					stream->timing.display_color_depth,
815  					stream->signal);
816  		}
817  	}
818  }
819  
dc_stream_set_dither_option(struct dc_stream_state * stream,enum dc_dither_option option)820  void dc_stream_set_dither_option(struct dc_stream_state *stream,
821  		enum dc_dither_option option)
822  {
823  	struct bit_depth_reduction_params params;
824  	struct dc_link *link = stream->link;
825  	struct pipe_ctx *pipes = NULL;
826  	int i;
827  
828  	for (i = 0; i < MAX_PIPES; i++) {
829  		if (link->dc->current_state->res_ctx.pipe_ctx[i].stream ==
830  				stream) {
831  			pipes = &link->dc->current_state->res_ctx.pipe_ctx[i];
832  			break;
833  		}
834  	}
835  
836  	if (!pipes)
837  		return;
838  	if (option > DITHER_OPTION_MAX)
839  		return;
840  
841  	dc_exit_ips_for_hw_access(stream->ctx->dc);
842  
843  	stream->dither_option = option;
844  
845  	memset(&params, 0, sizeof(params));
846  	resource_build_bit_depth_reduction_params(stream, &params);
847  	stream->bit_depth_params = params;
848  
849  	if (pipes->plane_res.xfm &&
850  	    pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth) {
851  		pipes->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
852  			pipes->plane_res.xfm,
853  			pipes->plane_res.scl_data.lb_params.depth,
854  			&stream->bit_depth_params);
855  	}
856  
857  	pipes->stream_res.opp->funcs->
858  		opp_program_bit_depth_reduction(pipes->stream_res.opp, &params);
859  }
860  
dc_stream_set_gamut_remap(struct dc * dc,const struct dc_stream_state * stream)861  bool dc_stream_set_gamut_remap(struct dc *dc, const struct dc_stream_state *stream)
862  {
863  	int i;
864  	bool ret = false;
865  	struct pipe_ctx *pipes;
866  
867  	dc_exit_ips_for_hw_access(dc);
868  
869  	for (i = 0; i < MAX_PIPES; i++) {
870  		if (dc->current_state->res_ctx.pipe_ctx[i].stream == stream) {
871  			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
872  			dc->hwss.program_gamut_remap(pipes);
873  			ret = true;
874  		}
875  	}
876  
877  	return ret;
878  }
879  
dc_stream_program_csc_matrix(struct dc * dc,struct dc_stream_state * stream)880  bool dc_stream_program_csc_matrix(struct dc *dc, struct dc_stream_state *stream)
881  {
882  	int i;
883  	bool ret = false;
884  	struct pipe_ctx *pipes;
885  
886  	dc_exit_ips_for_hw_access(dc);
887  
888  	for (i = 0; i < MAX_PIPES; i++) {
889  		if (dc->current_state->res_ctx.pipe_ctx[i].stream
890  				== stream) {
891  
892  			pipes = &dc->current_state->res_ctx.pipe_ctx[i];
893  			dc->hwss.program_output_csc(dc,
894  					pipes,
895  					stream->output_color_space,
896  					stream->csc_color_matrix.matrix,
897  					pipes->stream_res.opp->inst);
898  			ret = true;
899  		}
900  	}
901  
902  	return ret;
903  }
904  
dc_stream_set_static_screen_params(struct dc * dc,struct dc_stream_state ** streams,int num_streams,const struct dc_static_screen_params * params)905  void dc_stream_set_static_screen_params(struct dc *dc,
906  		struct dc_stream_state **streams,
907  		int num_streams,
908  		const struct dc_static_screen_params *params)
909  {
910  	int i, j;
911  	struct pipe_ctx *pipes_affected[MAX_PIPES];
912  	int num_pipes_affected = 0;
913  
914  	dc_exit_ips_for_hw_access(dc);
915  
916  	for (i = 0; i < num_streams; i++) {
917  		struct dc_stream_state *stream = streams[i];
918  
919  		for (j = 0; j < MAX_PIPES; j++) {
920  			if (dc->current_state->res_ctx.pipe_ctx[j].stream
921  					== stream) {
922  				pipes_affected[num_pipes_affected++] =
923  						&dc->current_state->res_ctx.pipe_ctx[j];
924  			}
925  		}
926  	}
927  
928  	dc->hwss.set_static_screen_control(pipes_affected, num_pipes_affected, params);
929  }
930  
dc_destruct(struct dc * dc)931  static void dc_destruct(struct dc *dc)
932  {
933  	// reset link encoder assignment table on destruct
934  	if (dc->res_pool && dc->res_pool->funcs->link_encs_assign)
935  		link_enc_cfg_init(dc, dc->current_state);
936  
937  	if (dc->current_state) {
938  		dc_state_release(dc->current_state);
939  		dc->current_state = NULL;
940  	}
941  
942  	destroy_links(dc);
943  
944  	destroy_link_encoders(dc);
945  
946  	if (dc->clk_mgr) {
947  		dc_destroy_clk_mgr(dc->clk_mgr);
948  		dc->clk_mgr = NULL;
949  	}
950  
951  	dc_destroy_resource_pool(dc);
952  
953  	if (dc->link_srv)
954  		link_destroy_link_service(&dc->link_srv);
955  
956  	if (dc->ctx->gpio_service)
957  		dal_gpio_service_destroy(&dc->ctx->gpio_service);
958  
959  	if (dc->ctx->created_bios)
960  		dal_bios_parser_destroy(&dc->ctx->dc_bios);
961  
962  	kfree(dc->ctx->logger);
963  	dc_perf_trace_destroy(&dc->ctx->perf_trace);
964  
965  	kfree(dc->ctx);
966  	dc->ctx = NULL;
967  
968  	kfree(dc->bw_vbios);
969  	dc->bw_vbios = NULL;
970  
971  	kfree(dc->bw_dceip);
972  	dc->bw_dceip = NULL;
973  
974  	kfree(dc->dcn_soc);
975  	dc->dcn_soc = NULL;
976  
977  	kfree(dc->dcn_ip);
978  	dc->dcn_ip = NULL;
979  
980  	kfree(dc->vm_helper);
981  	dc->vm_helper = NULL;
982  
983  }
984  
dc_construct_ctx(struct dc * dc,const struct dc_init_data * init_params)985  static bool dc_construct_ctx(struct dc *dc,
986  		const struct dc_init_data *init_params)
987  {
988  	struct dc_context *dc_ctx;
989  
990  	dc_ctx = kzalloc(sizeof(*dc_ctx), GFP_KERNEL);
991  	if (!dc_ctx)
992  		return false;
993  
994  	dc_ctx->cgs_device = init_params->cgs_device;
995  	dc_ctx->driver_context = init_params->driver;
996  	dc_ctx->dc = dc;
997  	dc_ctx->asic_id = init_params->asic_id;
998  	dc_ctx->dc_sink_id_count = 0;
999  	dc_ctx->dc_stream_id_count = 0;
1000  	dc_ctx->dce_environment = init_params->dce_environment;
1001  	dc_ctx->dcn_reg_offsets = init_params->dcn_reg_offsets;
1002  	dc_ctx->nbio_reg_offsets = init_params->nbio_reg_offsets;
1003  	dc_ctx->clk_reg_offsets = init_params->clk_reg_offsets;
1004  
1005  	/* Create logger */
1006  	dc_ctx->logger = kmalloc(sizeof(*dc_ctx->logger), GFP_KERNEL);
1007  
1008  	if (!dc_ctx->logger) {
1009  		kfree(dc_ctx);
1010  		return false;
1011  	}
1012  
1013  	dc_ctx->logger->dev = adev_to_drm(init_params->driver);
1014  	dc->dml.logger = dc_ctx->logger;
1015  
1016  	dc_ctx->dce_version = resource_parse_asic_id(init_params->asic_id);
1017  
1018  	dc_ctx->perf_trace = dc_perf_trace_create();
1019  	if (!dc_ctx->perf_trace) {
1020  		kfree(dc_ctx);
1021  		ASSERT_CRITICAL(false);
1022  		return false;
1023  	}
1024  
1025  	dc->ctx = dc_ctx;
1026  
1027  	dc->link_srv = link_create_link_service();
1028  	if (!dc->link_srv)
1029  		return false;
1030  
1031  	return true;
1032  }
1033  
dc_construct(struct dc * dc,const struct dc_init_data * init_params)1034  static bool dc_construct(struct dc *dc,
1035  		const struct dc_init_data *init_params)
1036  {
1037  	struct dc_context *dc_ctx;
1038  	struct bw_calcs_dceip *dc_dceip;
1039  	struct bw_calcs_vbios *dc_vbios;
1040  	struct dcn_soc_bounding_box *dcn_soc;
1041  	struct dcn_ip_params *dcn_ip;
1042  
1043  	dc->config = init_params->flags;
1044  
1045  	// Allocate memory for the vm_helper
1046  	dc->vm_helper = kzalloc(sizeof(struct vm_helper), GFP_KERNEL);
1047  	if (!dc->vm_helper) {
1048  		dm_error("%s: failed to create dc->vm_helper\n", __func__);
1049  		goto fail;
1050  	}
1051  
1052  	memcpy(&dc->bb_overrides, &init_params->bb_overrides, sizeof(dc->bb_overrides));
1053  
1054  	dc_dceip = kzalloc(sizeof(*dc_dceip), GFP_KERNEL);
1055  	if (!dc_dceip) {
1056  		dm_error("%s: failed to create dceip\n", __func__);
1057  		goto fail;
1058  	}
1059  
1060  	dc->bw_dceip = dc_dceip;
1061  
1062  	dc_vbios = kzalloc(sizeof(*dc_vbios), GFP_KERNEL);
1063  	if (!dc_vbios) {
1064  		dm_error("%s: failed to create vbios\n", __func__);
1065  		goto fail;
1066  	}
1067  
1068  	dc->bw_vbios = dc_vbios;
1069  	dcn_soc = kzalloc(sizeof(*dcn_soc), GFP_KERNEL);
1070  	if (!dcn_soc) {
1071  		dm_error("%s: failed to create dcn_soc\n", __func__);
1072  		goto fail;
1073  	}
1074  
1075  	dc->dcn_soc = dcn_soc;
1076  
1077  	dcn_ip = kzalloc(sizeof(*dcn_ip), GFP_KERNEL);
1078  	if (!dcn_ip) {
1079  		dm_error("%s: failed to create dcn_ip\n", __func__);
1080  		goto fail;
1081  	}
1082  
1083  	dc->dcn_ip = dcn_ip;
1084  
1085  	if (init_params->bb_from_dmub)
1086  		dc->dml2_options.bb_from_dmub = init_params->bb_from_dmub;
1087  	else
1088  		dc->dml2_options.bb_from_dmub = NULL;
1089  
1090  	if (!dc_construct_ctx(dc, init_params)) {
1091  		dm_error("%s: failed to create ctx\n", __func__);
1092  		goto fail;
1093  	}
1094  
1095  	dc_ctx = dc->ctx;
1096  
1097  	/* Resource should construct all asic specific resources.
1098  	 * This should be the only place where we need to parse the asic id
1099  	 */
1100  	if (init_params->vbios_override)
1101  		dc_ctx->dc_bios = init_params->vbios_override;
1102  	else {
1103  		/* Create BIOS parser */
1104  		struct bp_init_data bp_init_data;
1105  
1106  		bp_init_data.ctx = dc_ctx;
1107  		bp_init_data.bios = init_params->asic_id.atombios_base_address;
1108  
1109  		dc_ctx->dc_bios = dal_bios_parser_create(
1110  				&bp_init_data, dc_ctx->dce_version);
1111  
1112  		if (!dc_ctx->dc_bios) {
1113  			ASSERT_CRITICAL(false);
1114  			goto fail;
1115  		}
1116  
1117  		dc_ctx->created_bios = true;
1118  	}
1119  
1120  	dc->vendor_signature = init_params->vendor_signature;
1121  
1122  	/* Create GPIO service */
1123  	dc_ctx->gpio_service = dal_gpio_service_create(
1124  			dc_ctx->dce_version,
1125  			dc_ctx->dce_environment,
1126  			dc_ctx);
1127  
1128  	if (!dc_ctx->gpio_service) {
1129  		ASSERT_CRITICAL(false);
1130  		goto fail;
1131  	}
1132  
1133  	dc->res_pool = dc_create_resource_pool(dc, init_params, dc_ctx->dce_version);
1134  	if (!dc->res_pool)
1135  		goto fail;
1136  
1137  	/* set i2c speed if not done by the respective dcnxxx__resource.c */
1138  	if (dc->caps.i2c_speed_in_khz_hdcp == 0)
1139  		dc->caps.i2c_speed_in_khz_hdcp = dc->caps.i2c_speed_in_khz;
1140  	if (dc->caps.max_optimizable_video_width == 0)
1141  		dc->caps.max_optimizable_video_width = 5120;
1142  	dc->clk_mgr = dc_clk_mgr_create(dc->ctx, dc->res_pool->pp_smu, dc->res_pool->dccg);
1143  	if (!dc->clk_mgr)
1144  		goto fail;
1145  #ifdef CONFIG_DRM_AMD_DC_FP
1146  	dc->clk_mgr->force_smu_not_present = init_params->force_smu_not_present;
1147  
1148  	if (dc->res_pool->funcs->update_bw_bounding_box) {
1149  		DC_FP_START();
1150  		dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
1151  		DC_FP_END();
1152  	}
1153  #endif
1154  
1155  	if (!create_links(dc, init_params->num_virtual_links))
1156  		goto fail;
1157  
1158  	/* Create additional DIG link encoder objects if fewer than the platform
1159  	 * supports were created during link construction.
1160  	 */
1161  	if (!create_link_encoders(dc))
1162  		goto fail;
1163  
1164  	/* Creation of current_state must occur after dc->dml
1165  	 * is initialized in dc_create_resource_pool because
1166  	 * on creation it copies the contents of dc->dml
1167  	 */
1168  	dc->current_state = dc_state_create(dc, NULL);
1169  
1170  	if (!dc->current_state) {
1171  		dm_error("%s: failed to create validate ctx\n", __func__);
1172  		goto fail;
1173  	}
1174  
1175  	return true;
1176  
1177  fail:
1178  	return false;
1179  }
1180  
disable_all_writeback_pipes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)1181  static void disable_all_writeback_pipes_for_stream(
1182  		const struct dc *dc,
1183  		struct dc_stream_state *stream,
1184  		struct dc_state *context)
1185  {
1186  	int i;
1187  
1188  	for (i = 0; i < stream->num_wb_info; i++)
1189  		stream->writeback_info[i].wb_enabled = false;
1190  }
1191  
apply_ctx_interdependent_lock(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,bool lock)1192  static void apply_ctx_interdependent_lock(struct dc *dc,
1193  					  struct dc_state *context,
1194  					  struct dc_stream_state *stream,
1195  					  bool lock)
1196  {
1197  	int i;
1198  
1199  	/* Checks if interdependent update function pointer is NULL or not, takes care of DCE110 case */
1200  	if (dc->hwss.interdependent_update_lock)
1201  		dc->hwss.interdependent_update_lock(dc, context, lock);
1202  	else {
1203  		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1204  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1205  			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1206  
1207  			// Copied conditions that were previously in dce110_apply_ctx_for_surface
1208  			if (stream == pipe_ctx->stream) {
1209  				if (resource_is_pipe_type(pipe_ctx, OPP_HEAD) &&
1210  					(pipe_ctx->plane_state || old_pipe_ctx->plane_state))
1211  					dc->hwss.pipe_control_lock(dc, pipe_ctx, lock);
1212  			}
1213  		}
1214  	}
1215  }
1216  
dc_update_visual_confirm_color(struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)1217  static void dc_update_visual_confirm_color(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1218  {
1219  	if (dc->ctx->dce_version >= DCN_VERSION_1_0) {
1220  		memset(&pipe_ctx->visual_confirm_color, 0, sizeof(struct tg_color));
1221  
1222  		if (dc->debug.visual_confirm == VISUAL_CONFIRM_HDR)
1223  			get_hdr_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1224  		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE)
1225  			get_surface_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1226  		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SWIZZLE)
1227  			get_surface_tile_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1228  		else if (dc->debug.visual_confirm == VISUAL_CONFIRM_HW_CURSOR)
1229  			get_cursor_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1230  		else {
1231  			if (dc->ctx->dce_version < DCN_VERSION_2_0)
1232  				color_space_to_black_color(
1233  					dc, pipe_ctx->stream->output_color_space, &(pipe_ctx->visual_confirm_color));
1234  		}
1235  		if (dc->ctx->dce_version >= DCN_VERSION_2_0) {
1236  			if (dc->debug.visual_confirm == VISUAL_CONFIRM_MPCTREE)
1237  				get_mpctree_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1238  			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP)
1239  				get_subvp_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1240  			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH)
1241  				get_mclk_switch_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1242  			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_FAMS2)
1243  				get_fams2_visual_confirm_color(dc, context, pipe_ctx, &(pipe_ctx->visual_confirm_color));
1244  			else if (dc->debug.visual_confirm == VISUAL_CONFIRM_VABC)
1245  				get_vabc_visual_confirm_color(pipe_ctx, &(pipe_ctx->visual_confirm_color));
1246  		}
1247  	}
1248  }
1249  
disable_dangling_plane(struct dc * dc,struct dc_state * context)1250  static void disable_dangling_plane(struct dc *dc, struct dc_state *context)
1251  {
1252  	int i, j;
1253  	struct dc_state *dangling_context = dc_state_create_current_copy(dc);
1254  	struct dc_state *current_ctx;
1255  	struct pipe_ctx *pipe;
1256  	struct timing_generator *tg;
1257  
1258  	if (dangling_context == NULL)
1259  		return;
1260  
1261  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1262  		struct dc_stream_state *old_stream =
1263  				dc->current_state->res_ctx.pipe_ctx[i].stream;
1264  		bool should_disable = true;
1265  		bool pipe_split_change = false;
1266  
1267  		if ((context->res_ctx.pipe_ctx[i].top_pipe) &&
1268  			(dc->current_state->res_ctx.pipe_ctx[i].top_pipe))
1269  			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe->pipe_idx !=
1270  				dc->current_state->res_ctx.pipe_ctx[i].top_pipe->pipe_idx;
1271  		else
1272  			pipe_split_change = context->res_ctx.pipe_ctx[i].top_pipe !=
1273  				dc->current_state->res_ctx.pipe_ctx[i].top_pipe;
1274  
1275  		for (j = 0; j < context->stream_count; j++) {
1276  			if (old_stream == context->streams[j]) {
1277  				should_disable = false;
1278  				break;
1279  			}
1280  		}
1281  		if (!should_disable && pipe_split_change &&
1282  				dc->current_state->stream_count != context->stream_count)
1283  			should_disable = true;
1284  
1285  		if (old_stream && !dc->current_state->res_ctx.pipe_ctx[i].top_pipe &&
1286  				!dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe) {
1287  			struct pipe_ctx *old_pipe, *new_pipe;
1288  
1289  			old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1290  			new_pipe = &context->res_ctx.pipe_ctx[i];
1291  
1292  			if (old_pipe->plane_state && !new_pipe->plane_state)
1293  				should_disable = true;
1294  		}
1295  
1296  		if (should_disable && old_stream) {
1297  			bool is_phantom = dc_state_get_stream_subvp_type(dc->current_state, old_stream) == SUBVP_PHANTOM;
1298  			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1299  			tg = pipe->stream_res.tg;
1300  			/* When disabling plane for a phantom pipe, we must turn on the
1301  			 * phantom OTG so the disable programming gets the double buffer
1302  			 * update. Otherwise the pipe will be left in a partially disabled
1303  			 * state that can result in underflow or hang when enabling it
1304  			 * again for different use.
1305  			 */
1306  			if (is_phantom) {
1307  				if (tg->funcs->enable_crtc) {
1308  					if (dc->hwseq->funcs.blank_pixel_data)
1309  						dc->hwseq->funcs.blank_pixel_data(dc, pipe, true);
1310  					tg->funcs->enable_crtc(tg);
1311  				}
1312  			}
1313  
1314  			if (is_phantom)
1315  				dc_state_rem_all_phantom_planes_for_stream(dc, old_stream, dangling_context, true);
1316  			else
1317  				dc_state_rem_all_planes_for_stream(dc, old_stream, dangling_context);
1318  			disable_all_writeback_pipes_for_stream(dc, old_stream, dangling_context);
1319  
1320  			if (pipe->stream && pipe->plane_state) {
1321  				if (!dc->debug.using_dml2)
1322  					set_p_state_switch_method(dc, context, pipe);
1323  				dc_update_visual_confirm_color(dc, context, pipe);
1324  			}
1325  
1326  			if (dc->hwss.apply_ctx_for_surface) {
1327  				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, true);
1328  				dc->hwss.apply_ctx_for_surface(dc, old_stream, 0, dangling_context);
1329  				apply_ctx_interdependent_lock(dc, dc->current_state, old_stream, false);
1330  				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1331  			}
1332  
1333  			if (dc->res_pool->funcs->prepare_mcache_programming)
1334  				dc->res_pool->funcs->prepare_mcache_programming(dc, dangling_context);
1335  			if (dc->hwss.program_front_end_for_ctx) {
1336  				dc->hwss.interdependent_update_lock(dc, dc->current_state, true);
1337  				dc->hwss.program_front_end_for_ctx(dc, dangling_context);
1338  				dc->hwss.interdependent_update_lock(dc, dc->current_state, false);
1339  				dc->hwss.post_unlock_program_front_end(dc, dangling_context);
1340  			}
1341  			/* We need to put the phantom OTG back into it's default (disabled) state or we
1342  			 * can get corruption when transition from one SubVP config to a different one.
1343  			 * The OTG is set to disable on falling edge of VUPDATE so the plane disable
1344  			 * will still get it's double buffer update.
1345  			 */
1346  			if (is_phantom) {
1347  				if (tg->funcs->disable_phantom_crtc)
1348  					tg->funcs->disable_phantom_crtc(tg);
1349  			}
1350  		}
1351  	}
1352  
1353  	current_ctx = dc->current_state;
1354  	dc->current_state = dangling_context;
1355  	dc_state_release(current_ctx);
1356  }
1357  
disable_vbios_mode_if_required(struct dc * dc,struct dc_state * context)1358  static void disable_vbios_mode_if_required(
1359  		struct dc *dc,
1360  		struct dc_state *context)
1361  {
1362  	unsigned int i, j;
1363  
1364  	/* check if timing_changed, disable stream*/
1365  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1366  		struct dc_stream_state *stream = NULL;
1367  		struct dc_link *link = NULL;
1368  		struct pipe_ctx *pipe = NULL;
1369  
1370  		pipe = &context->res_ctx.pipe_ctx[i];
1371  		stream = pipe->stream;
1372  		if (stream == NULL)
1373  			continue;
1374  
1375  		if (stream->apply_seamless_boot_optimization)
1376  			continue;
1377  
1378  		// only looking for first odm pipe
1379  		if (pipe->prev_odm_pipe)
1380  			continue;
1381  
1382  		if (stream->link->local_sink &&
1383  			stream->link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1384  			link = stream->link;
1385  		}
1386  
1387  		if (link != NULL && link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
1388  			unsigned int enc_inst, tg_inst = 0;
1389  			unsigned int pix_clk_100hz = 0;
1390  
1391  			enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1392  			if (enc_inst != ENGINE_ID_UNKNOWN) {
1393  				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
1394  					if (dc->res_pool->stream_enc[j]->id == enc_inst) {
1395  						tg_inst = dc->res_pool->stream_enc[j]->funcs->dig_source_otg(
1396  							dc->res_pool->stream_enc[j]);
1397  						break;
1398  					}
1399  				}
1400  
1401  				dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1402  					dc->res_pool->dp_clock_source,
1403  					tg_inst, &pix_clk_100hz);
1404  
1405  				if (link->link_status.link_active) {
1406  					uint32_t requested_pix_clk_100hz =
1407  						pipe->stream_res.pix_clk_params.requested_pix_clk_100hz;
1408  
1409  					if (pix_clk_100hz != requested_pix_clk_100hz) {
1410  						dc->link_srv->set_dpms_off(pipe);
1411  						pipe->stream->dpms_off = false;
1412  					}
1413  				}
1414  			}
1415  		}
1416  	}
1417  }
1418  
1419  /* Public functions */
1420  
dc_create(const struct dc_init_data * init_params)1421  struct dc *dc_create(const struct dc_init_data *init_params)
1422  {
1423  	struct dc *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1424  	unsigned int full_pipe_count;
1425  
1426  	if (!dc)
1427  		return NULL;
1428  
1429  	if (init_params->dce_environment == DCE_ENV_VIRTUAL_HW) {
1430  		dc->caps.linear_pitch_alignment = 64;
1431  		if (!dc_construct_ctx(dc, init_params))
1432  			goto destruct_dc;
1433  	} else {
1434  		if (!dc_construct(dc, init_params))
1435  			goto destruct_dc;
1436  
1437  		full_pipe_count = dc->res_pool->pipe_count;
1438  		if (dc->res_pool->underlay_pipe_index != NO_UNDERLAY_PIPE)
1439  			full_pipe_count--;
1440  		dc->caps.max_streams = min(
1441  				full_pipe_count,
1442  				dc->res_pool->stream_enc_count);
1443  
1444  		dc->caps.max_links = dc->link_count;
1445  		dc->caps.max_audios = dc->res_pool->audio_count;
1446  		dc->caps.linear_pitch_alignment = 64;
1447  
1448  		dc->caps.max_dp_protocol_version = DP_VERSION_1_4;
1449  
1450  		dc->caps.max_otg_num = dc->res_pool->res_cap->num_timing_generator;
1451  
1452  		if (dc->res_pool->dmcu != NULL)
1453  			dc->versions.dmcu_version = dc->res_pool->dmcu->dmcu_version;
1454  	}
1455  
1456  	dc->dcn_reg_offsets = init_params->dcn_reg_offsets;
1457  	dc->nbio_reg_offsets = init_params->nbio_reg_offsets;
1458  	dc->clk_reg_offsets = init_params->clk_reg_offsets;
1459  
1460  	/* Populate versioning information */
1461  	dc->versions.dc_ver = DC_VER;
1462  
1463  	dc->build_id = DC_BUILD_ID;
1464  
1465  	DC_LOG_DC("Display Core initialized\n");
1466  
1467  	return dc;
1468  
1469  destruct_dc:
1470  	dc_destruct(dc);
1471  	kfree(dc);
1472  	return NULL;
1473  }
1474  
detect_edp_presence(struct dc * dc)1475  static void detect_edp_presence(struct dc *dc)
1476  {
1477  	struct dc_link *edp_links[MAX_NUM_EDP];
1478  	struct dc_link *edp_link = NULL;
1479  	enum dc_connection_type type;
1480  	int i;
1481  	int edp_num;
1482  
1483  	dc_get_edp_links(dc, edp_links, &edp_num);
1484  	if (!edp_num)
1485  		return;
1486  
1487  	for (i = 0; i < edp_num; i++) {
1488  		edp_link = edp_links[i];
1489  		if (dc->config.edp_not_connected) {
1490  			edp_link->edp_sink_present = false;
1491  		} else {
1492  			dc_link_detect_connection_type(edp_link, &type);
1493  			edp_link->edp_sink_present = (type != dc_connection_none);
1494  		}
1495  	}
1496  }
1497  
dc_hardware_init(struct dc * dc)1498  void dc_hardware_init(struct dc *dc)
1499  {
1500  
1501  	detect_edp_presence(dc);
1502  	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW)
1503  		dc->hwss.init_hw(dc);
1504  	dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0);
1505  }
1506  
dc_init_callbacks(struct dc * dc,const struct dc_callback_init * init_params)1507  void dc_init_callbacks(struct dc *dc,
1508  		const struct dc_callback_init *init_params)
1509  {
1510  	dc->ctx->cp_psp = init_params->cp_psp;
1511  }
1512  
dc_deinit_callbacks(struct dc * dc)1513  void dc_deinit_callbacks(struct dc *dc)
1514  {
1515  	memset(&dc->ctx->cp_psp, 0, sizeof(dc->ctx->cp_psp));
1516  }
1517  
dc_destroy(struct dc ** dc)1518  void dc_destroy(struct dc **dc)
1519  {
1520  	dc_destruct(*dc);
1521  	kfree(*dc);
1522  	*dc = NULL;
1523  }
1524  
enable_timing_multisync(struct dc * dc,struct dc_state * ctx)1525  static void enable_timing_multisync(
1526  		struct dc *dc,
1527  		struct dc_state *ctx)
1528  {
1529  	int i, multisync_count = 0;
1530  	int pipe_count = dc->res_pool->pipe_count;
1531  	struct pipe_ctx *multisync_pipes[MAX_PIPES] = { NULL };
1532  
1533  	for (i = 0; i < pipe_count; i++) {
1534  		if (!ctx->res_ctx.pipe_ctx[i].stream ||
1535  				!ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.enabled)
1536  			continue;
1537  		if (ctx->res_ctx.pipe_ctx[i].stream == ctx->res_ctx.pipe_ctx[i].stream->triggered_crtc_reset.event_source)
1538  			continue;
1539  		multisync_pipes[multisync_count] = &ctx->res_ctx.pipe_ctx[i];
1540  		multisync_count++;
1541  	}
1542  
1543  	if (multisync_count > 0) {
1544  		dc->hwss.enable_per_frame_crtc_position_reset(
1545  			dc, multisync_count, multisync_pipes);
1546  	}
1547  }
1548  
program_timing_sync(struct dc * dc,struct dc_state * ctx)1549  static void program_timing_sync(
1550  		struct dc *dc,
1551  		struct dc_state *ctx)
1552  {
1553  	int i, j, k;
1554  	int group_index = 0;
1555  	int num_group = 0;
1556  	int pipe_count = dc->res_pool->pipe_count;
1557  	struct pipe_ctx *unsynced_pipes[MAX_PIPES] = { NULL };
1558  
1559  	for (i = 0; i < pipe_count; i++) {
1560  		if (!ctx->res_ctx.pipe_ctx[i].stream
1561  				|| ctx->res_ctx.pipe_ctx[i].top_pipe
1562  				|| ctx->res_ctx.pipe_ctx[i].prev_odm_pipe)
1563  			continue;
1564  
1565  		unsynced_pipes[i] = &ctx->res_ctx.pipe_ctx[i];
1566  	}
1567  
1568  	for (i = 0; i < pipe_count; i++) {
1569  		int group_size = 1;
1570  		enum timing_synchronization_type sync_type = NOT_SYNCHRONIZABLE;
1571  		struct pipe_ctx *pipe_set[MAX_PIPES];
1572  
1573  		if (!unsynced_pipes[i])
1574  			continue;
1575  
1576  		pipe_set[0] = unsynced_pipes[i];
1577  		unsynced_pipes[i] = NULL;
1578  
1579  		/* Add tg to the set, search rest of the tg's for ones with
1580  		 * same timing, add all tgs with same timing to the group
1581  		 */
1582  		for (j = i + 1; j < pipe_count; j++) {
1583  			if (!unsynced_pipes[j])
1584  				continue;
1585  			if (sync_type != TIMING_SYNCHRONIZABLE &&
1586  				dc->hwss.enable_vblanks_synchronization &&
1587  				unsynced_pipes[j]->stream_res.tg->funcs->align_vblanks &&
1588  				resource_are_vblanks_synchronizable(
1589  					unsynced_pipes[j]->stream,
1590  					pipe_set[0]->stream)) {
1591  				sync_type = VBLANK_SYNCHRONIZABLE;
1592  				pipe_set[group_size] = unsynced_pipes[j];
1593  				unsynced_pipes[j] = NULL;
1594  				group_size++;
1595  			} else
1596  			if (sync_type != VBLANK_SYNCHRONIZABLE &&
1597  				resource_are_streams_timing_synchronizable(
1598  					unsynced_pipes[j]->stream,
1599  					pipe_set[0]->stream)) {
1600  				sync_type = TIMING_SYNCHRONIZABLE;
1601  				pipe_set[group_size] = unsynced_pipes[j];
1602  				unsynced_pipes[j] = NULL;
1603  				group_size++;
1604  			}
1605  		}
1606  
1607  		/* set first unblanked pipe as master */
1608  		for (j = 0; j < group_size; j++) {
1609  			bool is_blanked;
1610  
1611  			if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1612  				is_blanked =
1613  					pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1614  			else
1615  				is_blanked =
1616  					pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1617  			if (!is_blanked) {
1618  				if (j == 0)
1619  					break;
1620  
1621  				swap(pipe_set[0], pipe_set[j]);
1622  				break;
1623  			}
1624  		}
1625  
1626  		for (k = 0; k < group_size; k++) {
1627  			struct dc_stream_status *status = dc_state_get_stream_status(ctx, pipe_set[k]->stream);
1628  
1629  			if (!status)
1630  				continue;
1631  
1632  			status->timing_sync_info.group_id = num_group;
1633  			status->timing_sync_info.group_size = group_size;
1634  			if (k == 0)
1635  				status->timing_sync_info.master = true;
1636  			else
1637  				status->timing_sync_info.master = false;
1638  
1639  		}
1640  
1641  		/* remove any other unblanked pipes as they have already been synced */
1642  		if (dc->config.use_pipe_ctx_sync_logic) {
1643  			/* check pipe's syncd to decide which pipe to be removed */
1644  			for (j = 1; j < group_size; j++) {
1645  				if (pipe_set[j]->pipe_idx_syncd == pipe_set[0]->pipe_idx_syncd) {
1646  					group_size--;
1647  					pipe_set[j] = pipe_set[group_size];
1648  					j--;
1649  				} else
1650  					/* link slave pipe's syncd with master pipe */
1651  					pipe_set[j]->pipe_idx_syncd = pipe_set[0]->pipe_idx_syncd;
1652  			}
1653  		} else {
1654  			/* remove any other pipes by checking valid plane */
1655  			for (j = j + 1; j < group_size; j++) {
1656  				bool is_blanked;
1657  
1658  				if (pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked)
1659  					is_blanked =
1660  						pipe_set[j]->stream_res.opp->funcs->dpg_is_blanked(pipe_set[j]->stream_res.opp);
1661  				else
1662  					is_blanked =
1663  						pipe_set[j]->stream_res.tg->funcs->is_blanked(pipe_set[j]->stream_res.tg);
1664  				if (!is_blanked) {
1665  					group_size--;
1666  					pipe_set[j] = pipe_set[group_size];
1667  					j--;
1668  				}
1669  			}
1670  		}
1671  
1672  		if (group_size > 1) {
1673  			if (sync_type == TIMING_SYNCHRONIZABLE) {
1674  				dc->hwss.enable_timing_synchronization(
1675  					dc, ctx, group_index, group_size, pipe_set);
1676  			} else
1677  				if (sync_type == VBLANK_SYNCHRONIZABLE) {
1678  				dc->hwss.enable_vblanks_synchronization(
1679  					dc, group_index, group_size, pipe_set);
1680  				}
1681  			group_index++;
1682  		}
1683  		num_group++;
1684  	}
1685  }
1686  
streams_changed(struct dc * dc,struct dc_stream_state * streams[],uint8_t stream_count)1687  static bool streams_changed(struct dc *dc,
1688  			    struct dc_stream_state *streams[],
1689  			    uint8_t stream_count)
1690  {
1691  	uint8_t i;
1692  
1693  	if (stream_count != dc->current_state->stream_count)
1694  		return true;
1695  
1696  	for (i = 0; i < dc->current_state->stream_count; i++) {
1697  		if (dc->current_state->streams[i] != streams[i])
1698  			return true;
1699  		if (!streams[i]->link->link_state_valid)
1700  			return true;
1701  	}
1702  
1703  	return false;
1704  }
1705  
dc_validate_boot_timing(const struct dc * dc,const struct dc_sink * sink,struct dc_crtc_timing * crtc_timing)1706  bool dc_validate_boot_timing(const struct dc *dc,
1707  				const struct dc_sink *sink,
1708  				struct dc_crtc_timing *crtc_timing)
1709  {
1710  	struct timing_generator *tg;
1711  	struct stream_encoder *se = NULL;
1712  
1713  	struct dc_crtc_timing hw_crtc_timing = {0};
1714  
1715  	struct dc_link *link = sink->link;
1716  	unsigned int i, enc_inst, tg_inst = 0;
1717  
1718  	/* Support seamless boot on EDP displays only */
1719  	if (sink->sink_signal != SIGNAL_TYPE_EDP) {
1720  		return false;
1721  	}
1722  
1723  	if (dc->debug.force_odm_combine)
1724  		return false;
1725  
1726  	/* Check for enabled DIG to identify enabled display */
1727  	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
1728  		return false;
1729  
1730  	enc_inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
1731  
1732  	if (enc_inst == ENGINE_ID_UNKNOWN)
1733  		return false;
1734  
1735  	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1736  		if (dc->res_pool->stream_enc[i]->id == enc_inst) {
1737  
1738  			se = dc->res_pool->stream_enc[i];
1739  
1740  			tg_inst = dc->res_pool->stream_enc[i]->funcs->dig_source_otg(
1741  				dc->res_pool->stream_enc[i]);
1742  			break;
1743  		}
1744  	}
1745  
1746  	// tg_inst not found
1747  	if (i == dc->res_pool->stream_enc_count)
1748  		return false;
1749  
1750  	if (tg_inst >= dc->res_pool->timing_generator_count)
1751  		return false;
1752  
1753  	if (tg_inst != link->link_enc->preferred_engine)
1754  		return false;
1755  
1756  	tg = dc->res_pool->timing_generators[tg_inst];
1757  
1758  	if (!tg->funcs->get_hw_timing)
1759  		return false;
1760  
1761  	if (!tg->funcs->get_hw_timing(tg, &hw_crtc_timing))
1762  		return false;
1763  
1764  	if (crtc_timing->h_total != hw_crtc_timing.h_total)
1765  		return false;
1766  
1767  	if (crtc_timing->h_border_left != hw_crtc_timing.h_border_left)
1768  		return false;
1769  
1770  	if (crtc_timing->h_addressable != hw_crtc_timing.h_addressable)
1771  		return false;
1772  
1773  	if (crtc_timing->h_border_right != hw_crtc_timing.h_border_right)
1774  		return false;
1775  
1776  	if (crtc_timing->h_front_porch != hw_crtc_timing.h_front_porch)
1777  		return false;
1778  
1779  	if (crtc_timing->h_sync_width != hw_crtc_timing.h_sync_width)
1780  		return false;
1781  
1782  	if (crtc_timing->v_total != hw_crtc_timing.v_total)
1783  		return false;
1784  
1785  	if (crtc_timing->v_border_top != hw_crtc_timing.v_border_top)
1786  		return false;
1787  
1788  	if (crtc_timing->v_addressable != hw_crtc_timing.v_addressable)
1789  		return false;
1790  
1791  	if (crtc_timing->v_border_bottom != hw_crtc_timing.v_border_bottom)
1792  		return false;
1793  
1794  	if (crtc_timing->v_front_porch != hw_crtc_timing.v_front_porch)
1795  		return false;
1796  
1797  	if (crtc_timing->v_sync_width != hw_crtc_timing.v_sync_width)
1798  		return false;
1799  
1800  	/* block DSC for now, as VBIOS does not currently support DSC timings */
1801  	if (crtc_timing->flags.DSC)
1802  		return false;
1803  
1804  	if (dc_is_dp_signal(link->connector_signal)) {
1805  		unsigned int pix_clk_100hz = 0;
1806  		uint32_t numOdmPipes = 1;
1807  		uint32_t id_src[4] = {0};
1808  
1809  		dc->res_pool->dp_clock_source->funcs->get_pixel_clk_frequency_100hz(
1810  			dc->res_pool->dp_clock_source,
1811  			tg_inst, &pix_clk_100hz);
1812  
1813  		if (tg->funcs->get_optc_source)
1814  			tg->funcs->get_optc_source(tg,
1815  						&numOdmPipes, &id_src[0], &id_src[1]);
1816  
1817  		if (numOdmPipes == 2) {
1818  			pix_clk_100hz *= 2;
1819  		} else if (numOdmPipes == 4) {
1820  			pix_clk_100hz *= 4;
1821  		} else if (se && se->funcs->get_pixels_per_cycle) {
1822  			uint32_t pixels_per_cycle = se->funcs->get_pixels_per_cycle(se);
1823  
1824  			if (pixels_per_cycle != 1 && !dc->debug.enable_dp_dig_pixel_rate_div_policy)
1825  				return false;
1826  
1827  			pix_clk_100hz *= pixels_per_cycle;
1828  		}
1829  
1830  		// Note: In rare cases, HW pixclk may differ from crtc's pixclk
1831  		// slightly due to rounding issues in 10 kHz units.
1832  		if (crtc_timing->pix_clk_100hz != pix_clk_100hz)
1833  			return false;
1834  
1835  		if (!se || !se->funcs->dp_get_pixel_format)
1836  			return false;
1837  
1838  		if (!se->funcs->dp_get_pixel_format(
1839  			se,
1840  			&hw_crtc_timing.pixel_encoding,
1841  			&hw_crtc_timing.display_color_depth))
1842  			return false;
1843  
1844  		if (hw_crtc_timing.display_color_depth != crtc_timing->display_color_depth)
1845  			return false;
1846  
1847  		if (hw_crtc_timing.pixel_encoding != crtc_timing->pixel_encoding)
1848  			return false;
1849  	}
1850  
1851  	if (link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED) {
1852  		return false;
1853  	}
1854  
1855  	if (link->dpcd_caps.channel_coding_cap.bits.DP_128b_132b_SUPPORTED)
1856  		return false;
1857  
1858  	if (dc->link_srv->edp_is_ilr_optimization_required(link, crtc_timing)) {
1859  		DC_LOG_EVENT_LINK_TRAINING("Seamless boot disabled to optimize eDP link rate\n");
1860  		return false;
1861  	}
1862  
1863  	return true;
1864  }
1865  
should_update_pipe_for_stream(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)1866  static inline bool should_update_pipe_for_stream(
1867  		struct dc_state *context,
1868  		struct pipe_ctx *pipe_ctx,
1869  		struct dc_stream_state *stream)
1870  {
1871  	return (pipe_ctx->stream && pipe_ctx->stream == stream);
1872  }
1873  
should_update_pipe_for_plane(struct dc_state * context,struct pipe_ctx * pipe_ctx,struct dc_plane_state * plane_state)1874  static inline bool should_update_pipe_for_plane(
1875  		struct dc_state *context,
1876  		struct pipe_ctx *pipe_ctx,
1877  		struct dc_plane_state *plane_state)
1878  {
1879  	return (pipe_ctx->plane_state == plane_state);
1880  }
1881  
dc_enable_stereo(struct dc * dc,struct dc_state * context,struct dc_stream_state * streams[],uint8_t stream_count)1882  void dc_enable_stereo(
1883  	struct dc *dc,
1884  	struct dc_state *context,
1885  	struct dc_stream_state *streams[],
1886  	uint8_t stream_count)
1887  {
1888  	int i, j;
1889  	struct pipe_ctx *pipe;
1890  
1891  	dc_exit_ips_for_hw_access(dc);
1892  
1893  	for (i = 0; i < MAX_PIPES; i++) {
1894  		if (context != NULL) {
1895  			pipe = &context->res_ctx.pipe_ctx[i];
1896  		} else {
1897  			context = dc->current_state;
1898  			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1899  		}
1900  
1901  		for (j = 0; pipe && j < stream_count; j++)  {
1902  			if (should_update_pipe_for_stream(context, pipe, streams[j]) &&
1903  				dc->hwss.setup_stereo)
1904  				dc->hwss.setup_stereo(pipe, dc);
1905  		}
1906  	}
1907  }
1908  
dc_trigger_sync(struct dc * dc,struct dc_state * context)1909  void dc_trigger_sync(struct dc *dc, struct dc_state *context)
1910  {
1911  	if (context->stream_count > 1 && !dc->debug.disable_timing_sync) {
1912  		dc_exit_ips_for_hw_access(dc);
1913  
1914  		enable_timing_multisync(dc, context);
1915  		program_timing_sync(dc, context);
1916  	}
1917  }
1918  
get_stream_mask(struct dc * dc,struct dc_state * context)1919  static uint8_t get_stream_mask(struct dc *dc, struct dc_state *context)
1920  {
1921  	int i;
1922  	unsigned int stream_mask = 0;
1923  
1924  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1925  		if (context->res_ctx.pipe_ctx[i].stream)
1926  			stream_mask |= 1 << i;
1927  	}
1928  
1929  	return stream_mask;
1930  }
1931  
dc_z10_restore(const struct dc * dc)1932  void dc_z10_restore(const struct dc *dc)
1933  {
1934  	if (dc->hwss.z10_restore)
1935  		dc->hwss.z10_restore(dc);
1936  }
1937  
dc_z10_save_init(struct dc * dc)1938  void dc_z10_save_init(struct dc *dc)
1939  {
1940  	if (dc->hwss.z10_save_init)
1941  		dc->hwss.z10_save_init(dc);
1942  }
1943  
1944  /* Set a pipe unlock order based on the change in DET allocation and stores it in dc scratch memory
1945   * Prevents over allocation of DET during unlock process
1946   * e.g. 2 pipe config with different streams with a max of 20 DET segments
1947   *	Before:								After:
1948   *		- Pipe0: 10 DET segments			- Pipe0: 12 DET segments
1949   *		- Pipe1: 10 DET segments			- Pipe1: 8 DET segments
1950   * If Pipe0 gets updated first, 22 DET segments will be allocated
1951   */
determine_pipe_unlock_order(struct dc * dc,struct dc_state * context)1952  static void determine_pipe_unlock_order(struct dc *dc, struct dc_state *context)
1953  {
1954  	unsigned int i = 0;
1955  	struct pipe_ctx *pipe = NULL;
1956  	struct timing_generator *tg = NULL;
1957  
1958  	if (!dc->config.set_pipe_unlock_order)
1959  		return;
1960  
1961  	memset(dc->scratch.pipes_to_unlock_first, 0, sizeof(dc->scratch.pipes_to_unlock_first));
1962  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1963  		pipe = &context->res_ctx.pipe_ctx[i];
1964  		tg = pipe->stream_res.tg;
1965  
1966  		if (!resource_is_pipe_type(pipe, OTG_MASTER) ||
1967  				!tg->funcs->is_tg_enabled(tg) ||
1968  				dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
1969  			continue;
1970  		}
1971  
1972  		if (resource_calculate_det_for_stream(context, pipe) <
1973  				resource_calculate_det_for_stream(dc->current_state, &dc->current_state->res_ctx.pipe_ctx[i])) {
1974  			dc->scratch.pipes_to_unlock_first[i] = true;
1975  		}
1976  	}
1977  }
1978  
1979  /**
1980   * dc_commit_state_no_check - Apply context to the hardware
1981   *
1982   * @dc: DC object with the current status to be updated
1983   * @context: New state that will become the current status at the end of this function
1984   *
1985   * Applies given context to the hardware and copy it into current context.
1986   * It's up to the user to release the src context afterwards.
1987   *
1988   * Return: an enum dc_status result code for the operation
1989   */
dc_commit_state_no_check(struct dc * dc,struct dc_state * context)1990  static enum dc_status dc_commit_state_no_check(struct dc *dc, struct dc_state *context)
1991  {
1992  	struct dc_bios *dcb = dc->ctx->dc_bios;
1993  	enum dc_status result = DC_ERROR_UNEXPECTED;
1994  	struct pipe_ctx *pipe;
1995  	int i, k, l;
1996  	struct dc_stream_state *dc_streams[MAX_STREAMS] = {0};
1997  	struct dc_state *old_state;
1998  	bool subvp_prev_use = false;
1999  
2000  	dc_z10_restore(dc);
2001  	dc_allow_idle_optimizations(dc, false);
2002  
2003  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2004  		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2005  
2006  		/* Check old context for SubVP */
2007  		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
2008  		if (subvp_prev_use)
2009  			break;
2010  	}
2011  
2012  	for (i = 0; i < context->stream_count; i++)
2013  		dc_streams[i] =  context->streams[i];
2014  
2015  	if (!dcb->funcs->is_accelerated_mode(dcb)) {
2016  		disable_vbios_mode_if_required(dc, context);
2017  		dc->hwss.enable_accelerated_mode(dc, context);
2018  	}
2019  
2020  	if (context->stream_count > get_seamless_boot_stream_count(context) ||
2021  		context->stream_count == 0)
2022  		dc->hwss.prepare_bandwidth(dc, context);
2023  
2024  	/* When SubVP is active, all HW programming must be done while
2025  	 * SubVP lock is acquired
2026  	 */
2027  	if (dc->hwss.subvp_pipe_control_lock)
2028  		dc->hwss.subvp_pipe_control_lock(dc, context, true, true, NULL, subvp_prev_use);
2029  	if (dc->hwss.fams2_global_control_lock)
2030  		dc->hwss.fams2_global_control_lock(dc, context, true);
2031  
2032  	if (dc->hwss.update_dsc_pg)
2033  		dc->hwss.update_dsc_pg(dc, context, false);
2034  
2035  	disable_dangling_plane(dc, context);
2036  	/* re-program planes for existing stream, in case we need to
2037  	 * free up plane resource for later use
2038  	 */
2039  	if (dc->hwss.apply_ctx_for_surface) {
2040  		for (i = 0; i < context->stream_count; i++) {
2041  			if (context->streams[i]->mode_changed)
2042  				continue;
2043  			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
2044  			dc->hwss.apply_ctx_for_surface(
2045  				dc, context->streams[i],
2046  				context->stream_status[i].plane_count,
2047  				context); /* use new pipe config in new context */
2048  			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
2049  			dc->hwss.post_unlock_program_front_end(dc, context);
2050  		}
2051  	}
2052  
2053  	/* Program hardware */
2054  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2055  		pipe = &context->res_ctx.pipe_ctx[i];
2056  		dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe);
2057  	}
2058  
2059  	result = dc->hwss.apply_ctx_to_hw(dc, context);
2060  
2061  	if (result != DC_OK) {
2062  		/* Application of dc_state to hardware stopped. */
2063  		dc->current_state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
2064  		return result;
2065  	}
2066  
2067  	dc_trigger_sync(dc, context);
2068  
2069  	/* Full update should unconditionally be triggered when dc_commit_state_no_check is called */
2070  	for (i = 0; i < context->stream_count; i++) {
2071  		uint32_t prev_dsc_changed = context->streams[i]->update_flags.bits.dsc_changed;
2072  
2073  		context->streams[i]->update_flags.raw = 0xFFFFFFFF;
2074  		context->streams[i]->update_flags.bits.dsc_changed = prev_dsc_changed;
2075  	}
2076  
2077  	determine_pipe_unlock_order(dc, context);
2078  	/* Program all planes within new context*/
2079  	if (dc->res_pool->funcs->prepare_mcache_programming)
2080  		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
2081  	if (dc->hwss.program_front_end_for_ctx) {
2082  		dc->hwss.interdependent_update_lock(dc, context, true);
2083  		dc->hwss.program_front_end_for_ctx(dc, context);
2084  		dc->hwss.interdependent_update_lock(dc, context, false);
2085  		dc->hwss.post_unlock_program_front_end(dc, context);
2086  	}
2087  
2088  	if (dc->hwss.commit_subvp_config)
2089  		dc->hwss.commit_subvp_config(dc, context);
2090  	if (dc->hwss.subvp_pipe_control_lock)
2091  		dc->hwss.subvp_pipe_control_lock(dc, context, false, true, NULL, subvp_prev_use);
2092  	if (dc->hwss.fams2_global_control_lock)
2093  		dc->hwss.fams2_global_control_lock(dc, context, false);
2094  
2095  	for (i = 0; i < context->stream_count; i++) {
2096  		const struct dc_link *link = context->streams[i]->link;
2097  
2098  		if (!context->streams[i]->mode_changed)
2099  			continue;
2100  
2101  		if (dc->hwss.apply_ctx_for_surface) {
2102  			apply_ctx_interdependent_lock(dc, context, context->streams[i], true);
2103  			dc->hwss.apply_ctx_for_surface(
2104  					dc, context->streams[i],
2105  					context->stream_status[i].plane_count,
2106  					context);
2107  			apply_ctx_interdependent_lock(dc, context, context->streams[i], false);
2108  			dc->hwss.post_unlock_program_front_end(dc, context);
2109  		}
2110  
2111  		/*
2112  		 * enable stereo
2113  		 * TODO rework dc_enable_stereo call to work with validation sets?
2114  		 */
2115  		for (k = 0; k < MAX_PIPES; k++) {
2116  			pipe = &context->res_ctx.pipe_ctx[k];
2117  
2118  			for (l = 0 ; pipe && l < context->stream_count; l++)  {
2119  				if (context->streams[l] &&
2120  					context->streams[l] == pipe->stream &&
2121  					dc->hwss.setup_stereo)
2122  					dc->hwss.setup_stereo(pipe, dc);
2123  			}
2124  		}
2125  
2126  		CONN_MSG_MODE(link, "{%dx%d, %dx%d@%dKhz}",
2127  				context->streams[i]->timing.h_addressable,
2128  				context->streams[i]->timing.v_addressable,
2129  				context->streams[i]->timing.h_total,
2130  				context->streams[i]->timing.v_total,
2131  				context->streams[i]->timing.pix_clk_100hz / 10);
2132  	}
2133  
2134  	dc_enable_stereo(dc, context, dc_streams, context->stream_count);
2135  
2136  	if (get_seamless_boot_stream_count(context) == 0 ||
2137  		context->stream_count == 0) {
2138  		/* Must wait for no flips to be pending before doing optimize bw */
2139  		hwss_wait_for_no_pipes_pending(dc, context);
2140  		/*
2141  		 * optimized dispclk depends on ODM setup. Need to wait for ODM
2142  		 * update pending complete before optimizing bandwidth.
2143  		 */
2144  		hwss_wait_for_odm_update_pending_complete(dc, context);
2145  		/* pplib is notified if disp_num changed */
2146  		dc->hwss.optimize_bandwidth(dc, context);
2147  		/* Need to do otg sync again as otg could be out of sync due to otg
2148  		 * workaround applied during clock update
2149  		 */
2150  		dc_trigger_sync(dc, context);
2151  	}
2152  
2153  	if (dc->hwss.update_dsc_pg)
2154  		dc->hwss.update_dsc_pg(dc, context, true);
2155  
2156  	if (dc->ctx->dce_version >= DCE_VERSION_MAX)
2157  		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2158  	else
2159  		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2160  
2161  	context->stream_mask = get_stream_mask(dc, context);
2162  
2163  	if (context->stream_mask != dc->current_state->stream_mask)
2164  		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, context->stream_mask);
2165  
2166  	for (i = 0; i < context->stream_count; i++)
2167  		context->streams[i]->mode_changed = false;
2168  
2169  	/* Clear update flags that were set earlier to avoid redundant programming */
2170  	for (i = 0; i < context->stream_count; i++) {
2171  		context->streams[i]->update_flags.raw = 0x0;
2172  	}
2173  
2174  	old_state = dc->current_state;
2175  	dc->current_state = context;
2176  
2177  	dc_state_release(old_state);
2178  
2179  	dc_state_retain(dc->current_state);
2180  
2181  	return result;
2182  }
2183  
2184  static bool commit_minimal_transition_state(struct dc *dc,
2185  		struct dc_state *transition_base_context);
2186  
2187  /**
2188   * dc_commit_streams - Commit current stream state
2189   *
2190   * @dc: DC object with the commit state to be configured in the hardware
2191   * @params: Parameters for the commit, including the streams to be committed
2192   *
2193   * Function responsible for commit streams change to the hardware.
2194   *
2195   * Return:
2196   * Return DC_OK if everything work as expected, otherwise, return a dc_status
2197   * code.
2198   */
dc_commit_streams(struct dc * dc,struct dc_commit_streams_params * params)2199  enum dc_status dc_commit_streams(struct dc *dc, struct dc_commit_streams_params *params)
2200  {
2201  	int i, j;
2202  	struct dc_state *context;
2203  	enum dc_status res = DC_OK;
2204  	struct dc_validation_set set[MAX_STREAMS] = {0};
2205  	struct pipe_ctx *pipe;
2206  	bool handle_exit_odm2to1 = false;
2207  
2208  	if (!params)
2209  		return DC_ERROR_UNEXPECTED;
2210  
2211  	if (dc->ctx->dce_environment == DCE_ENV_VIRTUAL_HW)
2212  		return res;
2213  
2214  	if (!streams_changed(dc, params->streams, params->stream_count) &&
2215  			dc->current_state->power_source == params->power_source)
2216  		return res;
2217  
2218  	dc_exit_ips_for_hw_access(dc);
2219  
2220  	DC_LOG_DC("%s: %d streams\n", __func__, params->stream_count);
2221  
2222  	for (i = 0; i < params->stream_count; i++) {
2223  		struct dc_stream_state *stream = params->streams[i];
2224  		struct dc_stream_status *status = dc_stream_get_status(stream);
2225  
2226  		/* revalidate streams */
2227  		res = dc_validate_stream(dc, stream);
2228  		if (res != DC_OK)
2229  			return res;
2230  
2231  		dc_stream_log(dc, stream);
2232  
2233  		set[i].stream = stream;
2234  
2235  		if (status) {
2236  			set[i].plane_count = status->plane_count;
2237  			for (j = 0; j < status->plane_count; j++)
2238  				set[i].plane_states[j] = status->plane_states[j];
2239  		}
2240  	}
2241  
2242  	/* ODM Combine 2:1 power optimization is only applied for single stream
2243  	 * scenario, it uses extra pipes than needed to reduce power consumption
2244  	 * We need to switch off this feature to make room for new streams.
2245  	 */
2246  	if (params->stream_count > dc->current_state->stream_count &&
2247  			dc->current_state->stream_count == 1) {
2248  		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2249  			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2250  			if (pipe->next_odm_pipe)
2251  				handle_exit_odm2to1 = true;
2252  		}
2253  	}
2254  
2255  	if (handle_exit_odm2to1)
2256  		res = commit_minimal_transition_state(dc, dc->current_state);
2257  
2258  	context = dc_state_create_current_copy(dc);
2259  	if (!context)
2260  		goto context_alloc_fail;
2261  
2262  	context->power_source = params->power_source;
2263  
2264  	res = dc_validate_with_context(dc, set, params->stream_count, context, false);
2265  
2266  	/*
2267  	 * Only update link encoder to stream assignment after bandwidth validation passed.
2268  	 */
2269  	if (res == DC_OK && dc->res_pool->funcs->link_encs_assign)
2270  		dc->res_pool->funcs->link_encs_assign(
2271  			dc, context, context->streams, context->stream_count);
2272  
2273  	if (res != DC_OK) {
2274  		BREAK_TO_DEBUGGER();
2275  		goto fail;
2276  	}
2277  
2278  	res = dc_commit_state_no_check(dc, context);
2279  
2280  	for (i = 0; i < params->stream_count; i++) {
2281  		for (j = 0; j < context->stream_count; j++) {
2282  			if (params->streams[i]->stream_id == context->streams[j]->stream_id)
2283  				params->streams[i]->out.otg_offset = context->stream_status[j].primary_otg_inst;
2284  
2285  			if (dc_is_embedded_signal(params->streams[i]->signal)) {
2286  				struct dc_stream_status *status = dc_state_get_stream_status(context, params->streams[i]);
2287  
2288  				if (!status)
2289  					continue;
2290  
2291  				if (dc->hwss.is_abm_supported)
2292  					status->is_abm_supported = dc->hwss.is_abm_supported(dc, context, params->streams[i]);
2293  				else
2294  					status->is_abm_supported = true;
2295  			}
2296  		}
2297  	}
2298  
2299  fail:
2300  	dc_state_release(context);
2301  
2302  context_alloc_fail:
2303  
2304  	DC_LOG_DC("%s Finished.\n", __func__);
2305  
2306  	return res;
2307  }
2308  
dc_acquire_release_mpc_3dlut(struct dc * dc,bool acquire,struct dc_stream_state * stream,struct dc_3dlut ** lut,struct dc_transfer_func ** shaper)2309  bool dc_acquire_release_mpc_3dlut(
2310  		struct dc *dc, bool acquire,
2311  		struct dc_stream_state *stream,
2312  		struct dc_3dlut **lut,
2313  		struct dc_transfer_func **shaper)
2314  {
2315  	int pipe_idx;
2316  	bool ret = false;
2317  	bool found_pipe_idx = false;
2318  	const struct resource_pool *pool = dc->res_pool;
2319  	struct resource_context *res_ctx = &dc->current_state->res_ctx;
2320  	int mpcc_id = 0;
2321  
2322  	if (pool && res_ctx) {
2323  		if (acquire) {
2324  			/*find pipe idx for the given stream*/
2325  			for (pipe_idx = 0; pipe_idx < pool->pipe_count; pipe_idx++) {
2326  				if (res_ctx->pipe_ctx[pipe_idx].stream == stream) {
2327  					found_pipe_idx = true;
2328  					mpcc_id = res_ctx->pipe_ctx[pipe_idx].plane_res.hubp->inst;
2329  					break;
2330  				}
2331  			}
2332  		} else
2333  			found_pipe_idx = true;/*for release pipe_idx is not required*/
2334  
2335  		if (found_pipe_idx) {
2336  			if (acquire && pool->funcs->acquire_post_bldn_3dlut)
2337  				ret = pool->funcs->acquire_post_bldn_3dlut(res_ctx, pool, mpcc_id, lut, shaper);
2338  			else if (!acquire && pool->funcs->release_post_bldn_3dlut)
2339  				ret = pool->funcs->release_post_bldn_3dlut(res_ctx, pool, lut, shaper);
2340  		}
2341  	}
2342  	return ret;
2343  }
2344  
is_flip_pending_in_pipes(struct dc * dc,struct dc_state * context)2345  static bool is_flip_pending_in_pipes(struct dc *dc, struct dc_state *context)
2346  {
2347  	int i;
2348  	struct pipe_ctx *pipe;
2349  
2350  	for (i = 0; i < MAX_PIPES; i++) {
2351  		pipe = &context->res_ctx.pipe_ctx[i];
2352  
2353  		// Don't check flip pending on phantom pipes
2354  		if (!pipe->plane_state || (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM))
2355  			continue;
2356  
2357  		/* Must set to false to start with, due to OR in update function */
2358  		pipe->plane_state->status.is_flip_pending = false;
2359  		dc->hwss.update_pending_status(pipe);
2360  		if (pipe->plane_state->status.is_flip_pending)
2361  			return true;
2362  	}
2363  	return false;
2364  }
2365  
2366  /* Perform updates here which need to be deferred until next vupdate
2367   *
2368   * i.e. blnd lut, 3dlut, and shaper lut bypass regs are double buffered
2369   * but forcing lut memory to shutdown state is immediate. This causes
2370   * single frame corruption as lut gets disabled mid-frame unless shutdown
2371   * is deferred until after entering bypass.
2372   */
process_deferred_updates(struct dc * dc)2373  static void process_deferred_updates(struct dc *dc)
2374  {
2375  	int i = 0;
2376  
2377  	if (dc->debug.enable_mem_low_power.bits.cm) {
2378  		ASSERT(dc->dcn_ip->max_num_dpp);
2379  		for (i = 0; i < dc->dcn_ip->max_num_dpp; i++)
2380  			if (dc->res_pool->dpps[i]->funcs->dpp_deferred_update)
2381  				dc->res_pool->dpps[i]->funcs->dpp_deferred_update(dc->res_pool->dpps[i]);
2382  	}
2383  }
2384  
dc_post_update_surfaces_to_stream(struct dc * dc)2385  void dc_post_update_surfaces_to_stream(struct dc *dc)
2386  {
2387  	int i;
2388  	struct dc_state *context = dc->current_state;
2389  
2390  	if ((!dc->optimized_required) || get_seamless_boot_stream_count(context) > 0)
2391  		return;
2392  
2393  	post_surface_trace(dc);
2394  
2395  	/*
2396  	 * Only relevant for DCN behavior where we can guarantee the optimization
2397  	 * is safe to apply - retain the legacy behavior for DCE.
2398  	 */
2399  
2400  	if (dc->ctx->dce_version < DCE_VERSION_MAX)
2401  		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
2402  	else {
2403  		TRACE_DCN_CLOCK_STATE(&context->bw_ctx.bw.dcn.clk);
2404  
2405  		if (is_flip_pending_in_pipes(dc, context))
2406  			return;
2407  
2408  		for (i = 0; i < dc->res_pool->pipe_count; i++)
2409  			if (context->res_ctx.pipe_ctx[i].stream == NULL ||
2410  					context->res_ctx.pipe_ctx[i].plane_state == NULL) {
2411  				context->res_ctx.pipe_ctx[i].pipe_idx = i;
2412  				dc->hwss.disable_plane(dc, context, &context->res_ctx.pipe_ctx[i]);
2413  			}
2414  
2415  		process_deferred_updates(dc);
2416  
2417  		dc->hwss.optimize_bandwidth(dc, context);
2418  
2419  		if (dc->hwss.update_dsc_pg)
2420  			dc->hwss.update_dsc_pg(dc, context, true);
2421  	}
2422  
2423  	dc->optimized_required = false;
2424  	dc->wm_optimized_required = false;
2425  }
2426  
dc_set_generic_gpio_for_stereo(bool enable,struct gpio_service * gpio_service)2427  bool dc_set_generic_gpio_for_stereo(bool enable,
2428  		struct gpio_service *gpio_service)
2429  {
2430  	enum gpio_result gpio_result = GPIO_RESULT_NON_SPECIFIC_ERROR;
2431  	struct gpio_pin_info pin_info;
2432  	struct gpio *generic;
2433  	struct gpio_generic_mux_config *config = kzalloc(sizeof(struct gpio_generic_mux_config),
2434  			   GFP_KERNEL);
2435  
2436  	if (!config)
2437  		return false;
2438  	pin_info = dal_gpio_get_generic_pin_info(gpio_service, GPIO_ID_GENERIC, 0);
2439  
2440  	if (pin_info.mask == 0xFFFFFFFF || pin_info.offset == 0xFFFFFFFF) {
2441  		kfree(config);
2442  		return false;
2443  	} else {
2444  		generic = dal_gpio_service_create_generic_mux(
2445  			gpio_service,
2446  			pin_info.offset,
2447  			pin_info.mask);
2448  	}
2449  
2450  	if (!generic) {
2451  		kfree(config);
2452  		return false;
2453  	}
2454  
2455  	gpio_result = dal_gpio_open(generic, GPIO_MODE_OUTPUT);
2456  
2457  	config->enable_output_from_mux = enable;
2458  	config->mux_select = GPIO_SIGNAL_SOURCE_PASS_THROUGH_STEREO_SYNC;
2459  
2460  	if (gpio_result == GPIO_RESULT_OK)
2461  		gpio_result = dal_mux_setup_config(generic, config);
2462  
2463  	if (gpio_result == GPIO_RESULT_OK) {
2464  		dal_gpio_close(generic);
2465  		dal_gpio_destroy_generic_mux(&generic);
2466  		kfree(config);
2467  		return true;
2468  	} else {
2469  		dal_gpio_close(generic);
2470  		dal_gpio_destroy_generic_mux(&generic);
2471  		kfree(config);
2472  		return false;
2473  	}
2474  }
2475  
is_surface_in_context(const struct dc_state * context,const struct dc_plane_state * plane_state)2476  static bool is_surface_in_context(
2477  		const struct dc_state *context,
2478  		const struct dc_plane_state *plane_state)
2479  {
2480  	int j;
2481  
2482  	for (j = 0; j < MAX_PIPES; j++) {
2483  		const struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
2484  
2485  		if (plane_state == pipe_ctx->plane_state) {
2486  			return true;
2487  		}
2488  	}
2489  
2490  	return false;
2491  }
2492  
get_plane_info_update_type(const struct dc * dc,const struct dc_surface_update * u)2493  static enum surface_update_type get_plane_info_update_type(const struct dc *dc, const struct dc_surface_update *u)
2494  {
2495  	union surface_update_flags *update_flags = &u->surface->update_flags;
2496  	enum surface_update_type update_type = UPDATE_TYPE_FAST;
2497  
2498  	if (!u->plane_info)
2499  		return UPDATE_TYPE_FAST;
2500  
2501  	if (u->plane_info->color_space != u->surface->color_space) {
2502  		update_flags->bits.color_space_change = 1;
2503  		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2504  	}
2505  
2506  	if (u->plane_info->horizontal_mirror != u->surface->horizontal_mirror) {
2507  		update_flags->bits.horizontal_mirror_change = 1;
2508  		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2509  	}
2510  
2511  	if (u->plane_info->rotation != u->surface->rotation) {
2512  		update_flags->bits.rotation_change = 1;
2513  		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2514  	}
2515  
2516  	if (u->plane_info->format != u->surface->format) {
2517  		update_flags->bits.pixel_format_change = 1;
2518  		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2519  	}
2520  
2521  	if (u->plane_info->stereo_format != u->surface->stereo_format) {
2522  		update_flags->bits.stereo_format_change = 1;
2523  		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2524  	}
2525  
2526  	if (u->plane_info->per_pixel_alpha != u->surface->per_pixel_alpha) {
2527  		update_flags->bits.per_pixel_alpha_change = 1;
2528  		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2529  	}
2530  
2531  	if (u->plane_info->global_alpha_value != u->surface->global_alpha_value) {
2532  		update_flags->bits.global_alpha_change = 1;
2533  		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2534  	}
2535  
2536  	if (u->plane_info->dcc.enable != u->surface->dcc.enable
2537  			|| u->plane_info->dcc.dcc_ind_blk != u->surface->dcc.dcc_ind_blk
2538  			|| u->plane_info->dcc.meta_pitch != u->surface->dcc.meta_pitch) {
2539  		/* During DCC on/off, stutter period is calculated before
2540  		 * DCC has fully transitioned. This results in incorrect
2541  		 * stutter period calculation. Triggering a full update will
2542  		 * recalculate stutter period.
2543  		 */
2544  		update_flags->bits.dcc_change = 1;
2545  		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2546  	}
2547  
2548  	if (resource_pixel_format_to_bpp(u->plane_info->format) !=
2549  			resource_pixel_format_to_bpp(u->surface->format)) {
2550  		/* different bytes per element will require full bandwidth
2551  		 * and DML calculation
2552  		 */
2553  		update_flags->bits.bpp_change = 1;
2554  		elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2555  	}
2556  
2557  	if (u->plane_info->plane_size.surface_pitch != u->surface->plane_size.surface_pitch
2558  			|| u->plane_info->plane_size.chroma_pitch != u->surface->plane_size.chroma_pitch) {
2559  		update_flags->bits.plane_size_change = 1;
2560  		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2561  	}
2562  
2563  
2564  	if (memcmp(&u->plane_info->tiling_info, &u->surface->tiling_info,
2565  			sizeof(struct dc_tiling_info)) != 0) {
2566  		update_flags->bits.swizzle_change = 1;
2567  		elevate_update_type(&update_type, UPDATE_TYPE_MED);
2568  
2569  		/* todo: below are HW dependent, we should add a hook to
2570  		 * DCE/N resource and validated there.
2571  		 */
2572  		if (!dc->debug.skip_full_updated_if_possible) {
2573  			/* swizzled mode requires RQ to be setup properly,
2574  			 * thus need to run DML to calculate RQ settings
2575  			 */
2576  			update_flags->bits.bandwidth_change = 1;
2577  			elevate_update_type(&update_type, UPDATE_TYPE_FULL);
2578  		}
2579  	}
2580  
2581  	/* This should be UPDATE_TYPE_FAST if nothing has changed. */
2582  	return update_type;
2583  }
2584  
get_scaling_info_update_type(const struct dc * dc,const struct dc_surface_update * u)2585  static enum surface_update_type get_scaling_info_update_type(
2586  		const struct dc *dc,
2587  		const struct dc_surface_update *u)
2588  {
2589  	union surface_update_flags *update_flags = &u->surface->update_flags;
2590  
2591  	if (!u->scaling_info)
2592  		return UPDATE_TYPE_FAST;
2593  
2594  	if (u->scaling_info->src_rect.width != u->surface->src_rect.width
2595  			|| u->scaling_info->src_rect.height != u->surface->src_rect.height
2596  			|| u->scaling_info->dst_rect.width != u->surface->dst_rect.width
2597  			|| u->scaling_info->dst_rect.height != u->surface->dst_rect.height
2598  			|| u->scaling_info->clip_rect.width != u->surface->clip_rect.width
2599  			|| u->scaling_info->clip_rect.height != u->surface->clip_rect.height
2600  			|| u->scaling_info->scaling_quality.integer_scaling !=
2601  					u->surface->scaling_quality.integer_scaling) {
2602  		update_flags->bits.scaling_change = 1;
2603  
2604  		if (u->scaling_info->src_rect.width > u->surface->src_rect.width
2605  				|| u->scaling_info->src_rect.height > u->surface->src_rect.height)
2606  			/* Making src rect bigger requires a bandwidth change */
2607  			update_flags->bits.clock_change = 1;
2608  
2609  		if ((u->scaling_info->dst_rect.width < u->surface->dst_rect.width
2610  			|| u->scaling_info->dst_rect.height < u->surface->dst_rect.height)
2611  				&& (u->scaling_info->dst_rect.width < u->surface->src_rect.width
2612  					|| u->scaling_info->dst_rect.height < u->surface->src_rect.height))
2613  			/* Making dst rect smaller requires a bandwidth change */
2614  			update_flags->bits.bandwidth_change = 1;
2615  
2616  		if (u->scaling_info->src_rect.width > dc->caps.max_optimizable_video_width &&
2617  			(u->scaling_info->clip_rect.width > u->surface->clip_rect.width ||
2618  			 u->scaling_info->clip_rect.height > u->surface->clip_rect.height))
2619  			 /* Changing clip size of a large surface may result in MPC slice count change */
2620  			update_flags->bits.bandwidth_change = 1;
2621  	}
2622  
2623  	if (u->scaling_info->src_rect.x != u->surface->src_rect.x
2624  			|| u->scaling_info->src_rect.y != u->surface->src_rect.y
2625  			|| u->scaling_info->clip_rect.x != u->surface->clip_rect.x
2626  			|| u->scaling_info->clip_rect.y != u->surface->clip_rect.y
2627  			|| u->scaling_info->dst_rect.x != u->surface->dst_rect.x
2628  			|| u->scaling_info->dst_rect.y != u->surface->dst_rect.y)
2629  		update_flags->bits.position_change = 1;
2630  
2631  	/* process every update flag before returning */
2632  	if (update_flags->bits.clock_change
2633  			|| update_flags->bits.bandwidth_change
2634  			|| update_flags->bits.scaling_change)
2635  		return UPDATE_TYPE_FULL;
2636  
2637  	if (update_flags->bits.position_change)
2638  		return UPDATE_TYPE_MED;
2639  
2640  	return UPDATE_TYPE_FAST;
2641  }
2642  
det_surface_update(const struct dc * dc,const struct dc_surface_update * u)2643  static enum surface_update_type det_surface_update(const struct dc *dc,
2644  		const struct dc_surface_update *u)
2645  {
2646  	const struct dc_state *context = dc->current_state;
2647  	enum surface_update_type type;
2648  	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
2649  	union surface_update_flags *update_flags = &u->surface->update_flags;
2650  
2651  	if (!is_surface_in_context(context, u->surface) || u->surface->force_full_update) {
2652  		update_flags->raw = 0xFFFFFFFF;
2653  		return UPDATE_TYPE_FULL;
2654  	}
2655  
2656  	update_flags->raw = 0; // Reset all flags
2657  
2658  	type = get_plane_info_update_type(dc, u);
2659  	elevate_update_type(&overall_type, type);
2660  
2661  	type = get_scaling_info_update_type(dc, u);
2662  	elevate_update_type(&overall_type, type);
2663  
2664  	if (u->flip_addr) {
2665  		update_flags->bits.addr_update = 1;
2666  		if (u->flip_addr->address.tmz_surface != u->surface->address.tmz_surface) {
2667  			update_flags->bits.tmz_changed = 1;
2668  			elevate_update_type(&overall_type, UPDATE_TYPE_FULL);
2669  		}
2670  	}
2671  	if (u->in_transfer_func)
2672  		update_flags->bits.in_transfer_func_change = 1;
2673  
2674  	if (u->input_csc_color_matrix)
2675  		update_flags->bits.input_csc_change = 1;
2676  
2677  	if (u->coeff_reduction_factor)
2678  		update_flags->bits.coeff_reduction_change = 1;
2679  
2680  	if (u->gamut_remap_matrix)
2681  		update_flags->bits.gamut_remap_change = 1;
2682  
2683  	if (u->blend_tf)
2684  		update_flags->bits.gamma_change = 1;
2685  
2686  	if (u->gamma) {
2687  		enum surface_pixel_format format = SURFACE_PIXEL_FORMAT_GRPH_BEGIN;
2688  
2689  		if (u->plane_info)
2690  			format = u->plane_info->format;
2691  		else
2692  			format = u->surface->format;
2693  
2694  		if (dce_use_lut(format))
2695  			update_flags->bits.gamma_change = 1;
2696  	}
2697  
2698  	if (u->lut3d_func || u->func_shaper)
2699  		update_flags->bits.lut_3d = 1;
2700  
2701  	if (u->hdr_mult.value)
2702  		if (u->hdr_mult.value != u->surface->hdr_mult.value) {
2703  			update_flags->bits.hdr_mult = 1;
2704  			elevate_update_type(&overall_type, UPDATE_TYPE_MED);
2705  		}
2706  
2707  	if (u->sdr_white_level_nits)
2708  		if (u->sdr_white_level_nits != u->surface->sdr_white_level_nits) {
2709  			update_flags->bits.sdr_white_level_nits = 1;
2710  			elevate_update_type(&overall_type, UPDATE_TYPE_FULL);
2711  		}
2712  
2713  	if (u->cm2_params) {
2714  		if ((u->cm2_params->component_settings.shaper_3dlut_setting
2715  					!= u->surface->mcm_shaper_3dlut_setting)
2716  				|| (u->cm2_params->component_settings.lut1d_enable
2717  					!= u->surface->mcm_lut1d_enable))
2718  			update_flags->bits.mcm_transfer_function_enable_change = 1;
2719  		if (u->cm2_params->cm2_luts.lut3d_data.lut3d_src
2720  				!= u->surface->mcm_luts.lut3d_data.lut3d_src)
2721  			update_flags->bits.mcm_transfer_function_enable_change = 1;
2722  	}
2723  	if (update_flags->bits.in_transfer_func_change) {
2724  		type = UPDATE_TYPE_MED;
2725  		elevate_update_type(&overall_type, type);
2726  	}
2727  
2728  	if (update_flags->bits.lut_3d &&
2729  			u->surface->mcm_luts.lut3d_data.lut3d_src != DC_CM2_TRANSFER_FUNC_SOURCE_VIDMEM) {
2730  		type = UPDATE_TYPE_FULL;
2731  		elevate_update_type(&overall_type, type);
2732  	}
2733  	if (update_flags->bits.mcm_transfer_function_enable_change) {
2734  		type = UPDATE_TYPE_FULL;
2735  		elevate_update_type(&overall_type, type);
2736  	}
2737  
2738  	if (dc->debug.enable_legacy_fast_update &&
2739  			(update_flags->bits.gamma_change ||
2740  			update_flags->bits.gamut_remap_change ||
2741  			update_flags->bits.input_csc_change ||
2742  			update_flags->bits.coeff_reduction_change)) {
2743  		type = UPDATE_TYPE_FULL;
2744  		elevate_update_type(&overall_type, type);
2745  	}
2746  	return overall_type;
2747  }
2748  
2749  /* May need to flip the desktop plane in cases where MPO plane receives a flip but desktop plane doesn't
2750   * while both planes are flip_immediate
2751   */
force_immediate_gsl_plane_flip(struct dc * dc,struct dc_surface_update * updates,int surface_count)2752  static void force_immediate_gsl_plane_flip(struct dc *dc, struct dc_surface_update *updates, int surface_count)
2753  {
2754  	bool has_flip_immediate_plane = false;
2755  	int i;
2756  
2757  	for (i = 0; i < surface_count; i++) {
2758  		if (updates[i].surface->flip_immediate) {
2759  			has_flip_immediate_plane = true;
2760  			break;
2761  		}
2762  	}
2763  
2764  	if (has_flip_immediate_plane && surface_count > 1) {
2765  		for (i = 0; i < surface_count; i++) {
2766  			if (updates[i].surface->flip_immediate)
2767  				updates[i].surface->update_flags.bits.addr_update = 1;
2768  		}
2769  	}
2770  }
2771  
check_update_surfaces_for_stream(struct dc * dc,struct dc_surface_update * updates,int surface_count,struct dc_stream_update * stream_update,const struct dc_stream_status * stream_status)2772  static enum surface_update_type check_update_surfaces_for_stream(
2773  		struct dc *dc,
2774  		struct dc_surface_update *updates,
2775  		int surface_count,
2776  		struct dc_stream_update *stream_update,
2777  		const struct dc_stream_status *stream_status)
2778  {
2779  	int i;
2780  	enum surface_update_type overall_type = UPDATE_TYPE_FAST;
2781  
2782  	if (dc->idle_optimizations_allowed)
2783  		overall_type = UPDATE_TYPE_FULL;
2784  
2785  	if (stream_status == NULL || stream_status->plane_count != surface_count)
2786  		overall_type = UPDATE_TYPE_FULL;
2787  
2788  	if (stream_update && stream_update->pending_test_pattern) {
2789  		overall_type = UPDATE_TYPE_FULL;
2790  	}
2791  
2792  	if (stream_update && stream_update->hw_cursor_req) {
2793  		overall_type = UPDATE_TYPE_FULL;
2794  	}
2795  
2796  	/* some stream updates require passive update */
2797  	if (stream_update) {
2798  		union stream_update_flags *su_flags = &stream_update->stream->update_flags;
2799  
2800  		if ((stream_update->src.height != 0 && stream_update->src.width != 0) ||
2801  			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
2802  			stream_update->integer_scaling_update)
2803  			su_flags->bits.scaling = 1;
2804  
2805  		if (dc->debug.enable_legacy_fast_update && stream_update->out_transfer_func)
2806  			su_flags->bits.out_tf = 1;
2807  
2808  		if (stream_update->abm_level)
2809  			su_flags->bits.abm_level = 1;
2810  
2811  		if (stream_update->dpms_off)
2812  			su_flags->bits.dpms_off = 1;
2813  
2814  		if (stream_update->gamut_remap)
2815  			su_flags->bits.gamut_remap = 1;
2816  
2817  		if (stream_update->wb_update)
2818  			su_flags->bits.wb_update = 1;
2819  
2820  		if (stream_update->dsc_config)
2821  			su_flags->bits.dsc_changed = 1;
2822  
2823  		if (stream_update->mst_bw_update)
2824  			su_flags->bits.mst_bw = 1;
2825  
2826  		if (stream_update->stream->freesync_on_desktop &&
2827  			(stream_update->vrr_infopacket || stream_update->allow_freesync ||
2828  				stream_update->vrr_active_variable || stream_update->vrr_active_fixed))
2829  			su_flags->bits.fams_changed = 1;
2830  
2831  		if (stream_update->scaler_sharpener_update)
2832  			su_flags->bits.scaler_sharpener = 1;
2833  
2834  		if (stream_update->sharpening_required)
2835  			su_flags->bits.sharpening_required = 1;
2836  
2837  		if (su_flags->raw != 0)
2838  			overall_type = UPDATE_TYPE_FULL;
2839  
2840  		if (stream_update->output_csc_transform || stream_update->output_color_space)
2841  			su_flags->bits.out_csc = 1;
2842  
2843  		/* Output transfer function changes do not require bandwidth recalculation,
2844  		 * so don't trigger a full update
2845  		 */
2846  		if (!dc->debug.enable_legacy_fast_update && stream_update->out_transfer_func)
2847  			su_flags->bits.out_tf = 1;
2848  	}
2849  
2850  	for (i = 0 ; i < surface_count; i++) {
2851  		enum surface_update_type type =
2852  				det_surface_update(dc, &updates[i]);
2853  
2854  		elevate_update_type(&overall_type, type);
2855  	}
2856  
2857  	return overall_type;
2858  }
2859  
2860  /*
2861   * dc_check_update_surfaces_for_stream() - Determine update type (fast, med, or full)
2862   *
2863   * See :c:type:`enum surface_update_type <surface_update_type>` for explanation of update types
2864   */
dc_check_update_surfaces_for_stream(struct dc * dc,struct dc_surface_update * updates,int surface_count,struct dc_stream_update * stream_update,const struct dc_stream_status * stream_status)2865  enum surface_update_type dc_check_update_surfaces_for_stream(
2866  		struct dc *dc,
2867  		struct dc_surface_update *updates,
2868  		int surface_count,
2869  		struct dc_stream_update *stream_update,
2870  		const struct dc_stream_status *stream_status)
2871  {
2872  	int i;
2873  	enum surface_update_type type;
2874  
2875  	if (stream_update)
2876  		stream_update->stream->update_flags.raw = 0;
2877  	for (i = 0; i < surface_count; i++)
2878  		updates[i].surface->update_flags.raw = 0;
2879  
2880  	type = check_update_surfaces_for_stream(dc, updates, surface_count, stream_update, stream_status);
2881  	if (type == UPDATE_TYPE_FULL) {
2882  		if (stream_update) {
2883  			uint32_t dsc_changed = stream_update->stream->update_flags.bits.dsc_changed;
2884  			stream_update->stream->update_flags.raw = 0xFFFFFFFF;
2885  			stream_update->stream->update_flags.bits.dsc_changed = dsc_changed;
2886  		}
2887  		for (i = 0; i < surface_count; i++)
2888  			updates[i].surface->update_flags.raw = 0xFFFFFFFF;
2889  	}
2890  
2891  	if (type == UPDATE_TYPE_FAST) {
2892  		// If there's an available clock comparator, we use that.
2893  		if (dc->clk_mgr->funcs->are_clock_states_equal) {
2894  			if (!dc->clk_mgr->funcs->are_clock_states_equal(&dc->clk_mgr->clks, &dc->current_state->bw_ctx.bw.dcn.clk))
2895  				dc->optimized_required = true;
2896  		// Else we fallback to mem compare.
2897  		} else if (memcmp(&dc->current_state->bw_ctx.bw.dcn.clk, &dc->clk_mgr->clks, offsetof(struct dc_clocks, prev_p_state_change_support)) != 0) {
2898  			dc->optimized_required = true;
2899  		}
2900  
2901  		dc->optimized_required |= dc->wm_optimized_required;
2902  	}
2903  
2904  	return type;
2905  }
2906  
stream_get_status(struct dc_state * ctx,struct dc_stream_state * stream)2907  static struct dc_stream_status *stream_get_status(
2908  	struct dc_state *ctx,
2909  	struct dc_stream_state *stream)
2910  {
2911  	uint8_t i;
2912  
2913  	for (i = 0; i < ctx->stream_count; i++) {
2914  		if (stream == ctx->streams[i]) {
2915  			return &ctx->stream_status[i];
2916  		}
2917  	}
2918  
2919  	return NULL;
2920  }
2921  
2922  static const enum surface_update_type update_surface_trace_level = UPDATE_TYPE_FULL;
2923  
copy_surface_update_to_plane(struct dc_plane_state * surface,struct dc_surface_update * srf_update)2924  static void copy_surface_update_to_plane(
2925  		struct dc_plane_state *surface,
2926  		struct dc_surface_update *srf_update)
2927  {
2928  	if (srf_update->flip_addr) {
2929  		surface->address = srf_update->flip_addr->address;
2930  		surface->flip_immediate =
2931  			srf_update->flip_addr->flip_immediate;
2932  		surface->time.time_elapsed_in_us[surface->time.index] =
2933  			srf_update->flip_addr->flip_timestamp_in_us -
2934  				surface->time.prev_update_time_in_us;
2935  		surface->time.prev_update_time_in_us =
2936  			srf_update->flip_addr->flip_timestamp_in_us;
2937  		surface->time.index++;
2938  		if (surface->time.index >= DC_PLANE_UPDATE_TIMES_MAX)
2939  			surface->time.index = 0;
2940  
2941  		surface->triplebuffer_flips = srf_update->flip_addr->triplebuffer_flips;
2942  	}
2943  
2944  	if (srf_update->scaling_info) {
2945  		surface->scaling_quality =
2946  				srf_update->scaling_info->scaling_quality;
2947  		surface->dst_rect =
2948  				srf_update->scaling_info->dst_rect;
2949  		surface->src_rect =
2950  				srf_update->scaling_info->src_rect;
2951  		surface->clip_rect =
2952  				srf_update->scaling_info->clip_rect;
2953  	}
2954  
2955  	if (srf_update->plane_info) {
2956  		surface->color_space =
2957  				srf_update->plane_info->color_space;
2958  		surface->format =
2959  				srf_update->plane_info->format;
2960  		surface->plane_size =
2961  				srf_update->plane_info->plane_size;
2962  		surface->rotation =
2963  				srf_update->plane_info->rotation;
2964  		surface->horizontal_mirror =
2965  				srf_update->plane_info->horizontal_mirror;
2966  		surface->stereo_format =
2967  				srf_update->plane_info->stereo_format;
2968  		surface->tiling_info =
2969  				srf_update->plane_info->tiling_info;
2970  		surface->visible =
2971  				srf_update->plane_info->visible;
2972  		surface->per_pixel_alpha =
2973  				srf_update->plane_info->per_pixel_alpha;
2974  		surface->global_alpha =
2975  				srf_update->plane_info->global_alpha;
2976  		surface->global_alpha_value =
2977  				srf_update->plane_info->global_alpha_value;
2978  		surface->dcc =
2979  				srf_update->plane_info->dcc;
2980  		surface->layer_index =
2981  				srf_update->plane_info->layer_index;
2982  	}
2983  
2984  	if (srf_update->gamma) {
2985  		memcpy(&surface->gamma_correction.entries,
2986  			&srf_update->gamma->entries,
2987  			sizeof(struct dc_gamma_entries));
2988  		surface->gamma_correction.is_identity =
2989  			srf_update->gamma->is_identity;
2990  		surface->gamma_correction.num_entries =
2991  			srf_update->gamma->num_entries;
2992  		surface->gamma_correction.type =
2993  			srf_update->gamma->type;
2994  	}
2995  
2996  	if (srf_update->in_transfer_func) {
2997  		surface->in_transfer_func.sdr_ref_white_level =
2998  			srf_update->in_transfer_func->sdr_ref_white_level;
2999  		surface->in_transfer_func.tf =
3000  			srf_update->in_transfer_func->tf;
3001  		surface->in_transfer_func.type =
3002  			srf_update->in_transfer_func->type;
3003  		memcpy(&surface->in_transfer_func.tf_pts,
3004  			&srf_update->in_transfer_func->tf_pts,
3005  			sizeof(struct dc_transfer_func_distributed_points));
3006  	}
3007  
3008  	if (srf_update->cm2_params) {
3009  		surface->mcm_shaper_3dlut_setting = srf_update->cm2_params->component_settings.shaper_3dlut_setting;
3010  		surface->mcm_lut1d_enable = srf_update->cm2_params->component_settings.lut1d_enable;
3011  		surface->mcm_luts = srf_update->cm2_params->cm2_luts;
3012  	}
3013  
3014  	if (srf_update->func_shaper) {
3015  		memcpy(&surface->in_shaper_func, srf_update->func_shaper,
3016  		sizeof(surface->in_shaper_func));
3017  
3018  		if (surface->mcm_shaper_3dlut_setting >= DC_CM2_SHAPER_3DLUT_SETTING_ENABLE_SHAPER)
3019  			surface->mcm_luts.shaper = &surface->in_shaper_func;
3020  	}
3021  
3022  	if (srf_update->lut3d_func)
3023  		memcpy(&surface->lut3d_func, srf_update->lut3d_func,
3024  		sizeof(surface->lut3d_func));
3025  
3026  	if (srf_update->hdr_mult.value)
3027  		surface->hdr_mult =
3028  				srf_update->hdr_mult;
3029  
3030  	if (srf_update->sdr_white_level_nits)
3031  		surface->sdr_white_level_nits =
3032  				srf_update->sdr_white_level_nits;
3033  
3034  	if (srf_update->blend_tf) {
3035  		memcpy(&surface->blend_tf, srf_update->blend_tf,
3036  		sizeof(surface->blend_tf));
3037  
3038  		if (surface->mcm_lut1d_enable)
3039  			surface->mcm_luts.lut1d_func = &surface->blend_tf;
3040  	}
3041  
3042  	if (srf_update->cm2_params || srf_update->blend_tf)
3043  		surface->lut_bank_a = !surface->lut_bank_a;
3044  
3045  	if (srf_update->input_csc_color_matrix)
3046  		surface->input_csc_color_matrix =
3047  			*srf_update->input_csc_color_matrix;
3048  
3049  	if (srf_update->coeff_reduction_factor)
3050  		surface->coeff_reduction_factor =
3051  			*srf_update->coeff_reduction_factor;
3052  
3053  	if (srf_update->gamut_remap_matrix)
3054  		surface->gamut_remap_matrix =
3055  			*srf_update->gamut_remap_matrix;
3056  
3057  	if (srf_update->cursor_csc_color_matrix)
3058  		surface->cursor_csc_color_matrix =
3059  			*srf_update->cursor_csc_color_matrix;
3060  
3061  	if (srf_update->bias_and_scale.bias_and_scale_valid)
3062  			surface->bias_and_scale =
3063  					srf_update->bias_and_scale;
3064  }
3065  
copy_stream_update_to_stream(struct dc * dc,struct dc_state * context,struct dc_stream_state * stream,struct dc_stream_update * update)3066  static void copy_stream_update_to_stream(struct dc *dc,
3067  					 struct dc_state *context,
3068  					 struct dc_stream_state *stream,
3069  					 struct dc_stream_update *update)
3070  {
3071  	struct dc_context *dc_ctx = dc->ctx;
3072  
3073  	if (update == NULL || stream == NULL)
3074  		return;
3075  
3076  	if (update->src.height && update->src.width)
3077  		stream->src = update->src;
3078  
3079  	if (update->dst.height && update->dst.width)
3080  		stream->dst = update->dst;
3081  
3082  	if (update->out_transfer_func) {
3083  		stream->out_transfer_func.sdr_ref_white_level =
3084  			update->out_transfer_func->sdr_ref_white_level;
3085  		stream->out_transfer_func.tf = update->out_transfer_func->tf;
3086  		stream->out_transfer_func.type =
3087  			update->out_transfer_func->type;
3088  		memcpy(&stream->out_transfer_func.tf_pts,
3089  		       &update->out_transfer_func->tf_pts,
3090  		       sizeof(struct dc_transfer_func_distributed_points));
3091  	}
3092  
3093  	if (update->hdr_static_metadata)
3094  		stream->hdr_static_metadata = *update->hdr_static_metadata;
3095  
3096  	if (update->abm_level)
3097  		stream->abm_level = *update->abm_level;
3098  
3099  	if (update->periodic_interrupt)
3100  		stream->periodic_interrupt = *update->periodic_interrupt;
3101  
3102  	if (update->gamut_remap)
3103  		stream->gamut_remap_matrix = *update->gamut_remap;
3104  
3105  	/* Note: this being updated after mode set is currently not a use case
3106  	 * however if it arises OCSC would need to be reprogrammed at the
3107  	 * minimum
3108  	 */
3109  	if (update->output_color_space)
3110  		stream->output_color_space = *update->output_color_space;
3111  
3112  	if (update->output_csc_transform)
3113  		stream->csc_color_matrix = *update->output_csc_transform;
3114  
3115  	if (update->vrr_infopacket)
3116  		stream->vrr_infopacket = *update->vrr_infopacket;
3117  
3118  	if (update->hw_cursor_req)
3119  		stream->hw_cursor_req = *update->hw_cursor_req;
3120  
3121  	if (update->allow_freesync)
3122  		stream->allow_freesync = *update->allow_freesync;
3123  
3124  	if (update->vrr_active_variable)
3125  		stream->vrr_active_variable = *update->vrr_active_variable;
3126  
3127  	if (update->vrr_active_fixed)
3128  		stream->vrr_active_fixed = *update->vrr_active_fixed;
3129  
3130  	if (update->crtc_timing_adjust)
3131  		stream->adjust = *update->crtc_timing_adjust;
3132  
3133  	if (update->dpms_off)
3134  		stream->dpms_off = *update->dpms_off;
3135  
3136  	if (update->hfvsif_infopacket)
3137  		stream->hfvsif_infopacket = *update->hfvsif_infopacket;
3138  
3139  	if (update->vtem_infopacket)
3140  		stream->vtem_infopacket = *update->vtem_infopacket;
3141  
3142  	if (update->vsc_infopacket)
3143  		stream->vsc_infopacket = *update->vsc_infopacket;
3144  
3145  	if (update->vsp_infopacket)
3146  		stream->vsp_infopacket = *update->vsp_infopacket;
3147  
3148  	if (update->adaptive_sync_infopacket)
3149  		stream->adaptive_sync_infopacket = *update->adaptive_sync_infopacket;
3150  
3151  	if (update->dither_option)
3152  		stream->dither_option = *update->dither_option;
3153  
3154  	if (update->pending_test_pattern)
3155  		stream->test_pattern = *update->pending_test_pattern;
3156  	/* update current stream with writeback info */
3157  	if (update->wb_update) {
3158  		int i;
3159  
3160  		stream->num_wb_info = update->wb_update->num_wb_info;
3161  		ASSERT(stream->num_wb_info <= MAX_DWB_PIPES);
3162  		for (i = 0; i < stream->num_wb_info; i++)
3163  			stream->writeback_info[i] =
3164  				update->wb_update->writeback_info[i];
3165  	}
3166  	if (update->dsc_config) {
3167  		struct dc_dsc_config old_dsc_cfg = stream->timing.dsc_cfg;
3168  		uint32_t old_dsc_enabled = stream->timing.flags.DSC;
3169  		uint32_t enable_dsc = (update->dsc_config->num_slices_h != 0 &&
3170  				       update->dsc_config->num_slices_v != 0);
3171  
3172  		/* Use temporarry context for validating new DSC config */
3173  		struct dc_state *dsc_validate_context = dc_state_create_copy(dc->current_state);
3174  
3175  		if (dsc_validate_context) {
3176  			stream->timing.dsc_cfg = *update->dsc_config;
3177  			stream->timing.flags.DSC = enable_dsc;
3178  			if (!dc->res_pool->funcs->validate_bandwidth(dc, dsc_validate_context, true)) {
3179  				stream->timing.dsc_cfg = old_dsc_cfg;
3180  				stream->timing.flags.DSC = old_dsc_enabled;
3181  				update->dsc_config = NULL;
3182  			}
3183  
3184  			dc_state_release(dsc_validate_context);
3185  		} else {
3186  			DC_ERROR("Failed to allocate new validate context for DSC change\n");
3187  			update->dsc_config = NULL;
3188  		}
3189  	}
3190  	if (update->scaler_sharpener_update)
3191  		stream->scaler_sharpener_update = *update->scaler_sharpener_update;
3192  	if (update->sharpening_required)
3193  		stream->sharpening_required = *update->sharpening_required;
3194  }
3195  
backup_planes_and_stream_state(struct dc_scratch_space * scratch,struct dc_stream_state * stream)3196  static void backup_planes_and_stream_state(
3197  		struct dc_scratch_space *scratch,
3198  		struct dc_stream_state *stream)
3199  {
3200  	int i;
3201  	struct dc_stream_status *status = dc_stream_get_status(stream);
3202  
3203  	if (!status)
3204  		return;
3205  
3206  	for (i = 0; i < status->plane_count; i++) {
3207  		scratch->plane_states[i] = *status->plane_states[i];
3208  	}
3209  	scratch->stream_state = *stream;
3210  }
3211  
restore_planes_and_stream_state(struct dc_scratch_space * scratch,struct dc_stream_state * stream)3212  static void restore_planes_and_stream_state(
3213  		struct dc_scratch_space *scratch,
3214  		struct dc_stream_state *stream)
3215  {
3216  	int i;
3217  	struct dc_stream_status *status = dc_stream_get_status(stream);
3218  
3219  	if (!status)
3220  		return;
3221  
3222  	for (i = 0; i < status->plane_count; i++) {
3223  		/* refcount will always be valid, restore everything else */
3224  		struct kref refcount = status->plane_states[i]->refcount;
3225  		*status->plane_states[i] = scratch->plane_states[i];
3226  		status->plane_states[i]->refcount = refcount;
3227  	}
3228  	*stream = scratch->stream_state;
3229  }
3230  
3231  /**
3232   * update_seamless_boot_flags() - Helper function for updating seamless boot flags
3233   *
3234   * @dc: Current DC state
3235   * @context: New DC state to be programmed
3236   * @surface_count: Number of surfaces that have an updated
3237   * @stream: Corresponding stream to be updated in the current flip
3238   *
3239   * Updating seamless boot flags do not need to be part of the commit sequence. This
3240   * helper function will update the seamless boot flags on each flip (if required)
3241   * outside of the HW commit sequence (fast or slow).
3242   *
3243   * Return: void
3244   */
update_seamless_boot_flags(struct dc * dc,struct dc_state * context,int surface_count,struct dc_stream_state * stream)3245  static void update_seamless_boot_flags(struct dc *dc,
3246  		struct dc_state *context,
3247  		int surface_count,
3248  		struct dc_stream_state *stream)
3249  {
3250  	if (get_seamless_boot_stream_count(context) > 0 && surface_count > 0) {
3251  		/* Optimize seamless boot flag keeps clocks and watermarks high until
3252  		 * first flip. After first flip, optimization is required to lower
3253  		 * bandwidth. Important to note that it is expected UEFI will
3254  		 * only light up a single display on POST, therefore we only expect
3255  		 * one stream with seamless boot flag set.
3256  		 */
3257  		if (stream->apply_seamless_boot_optimization) {
3258  			stream->apply_seamless_boot_optimization = false;
3259  
3260  			if (get_seamless_boot_stream_count(context) == 0)
3261  				dc->optimized_required = true;
3262  		}
3263  	}
3264  }
3265  
3266  /**
3267   * update_planes_and_stream_state() - The function takes planes and stream
3268   * updates as inputs and determines the appropriate update type. If update type
3269   * is FULL, the function allocates a new context, populates and validates it.
3270   * Otherwise, it updates current dc context. The function will return both
3271   * new_context and new_update_type back to the caller. The function also backs
3272   * up both current and new contexts into corresponding dc state scratch memory.
3273   * TODO: The function does too many things, and even conditionally allocates dc
3274   * context memory implicitly. We should consider to break it down.
3275   *
3276   * @dc: Current DC state
3277   * @srf_updates: an array of surface updates
3278   * @surface_count: surface update count
3279   * @stream: Corresponding stream to be updated
3280   * @stream_update: stream update
3281   * @new_update_type: [out] determined update type by the function
3282   * @new_context: [out] new context allocated and validated if update type is
3283   * FULL, reference to current context if update type is less than FULL.
3284   *
3285   * Return: true if a valid update is populated into new_context, false
3286   * otherwise.
3287   */
update_planes_and_stream_state(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type * new_update_type,struct dc_state ** new_context)3288  static bool update_planes_and_stream_state(struct dc *dc,
3289  		struct dc_surface_update *srf_updates, int surface_count,
3290  		struct dc_stream_state *stream,
3291  		struct dc_stream_update *stream_update,
3292  		enum surface_update_type *new_update_type,
3293  		struct dc_state **new_context)
3294  {
3295  	struct dc_state *context;
3296  	int i, j;
3297  	enum surface_update_type update_type;
3298  	const struct dc_stream_status *stream_status;
3299  	struct dc_context *dc_ctx = dc->ctx;
3300  
3301  	stream_status = dc_stream_get_status(stream);
3302  
3303  	if (!stream_status) {
3304  		if (surface_count) /* Only an error condition if surf_count non-zero*/
3305  			ASSERT(false);
3306  
3307  		return false; /* Cannot commit surface to stream that is not committed */
3308  	}
3309  
3310  	context = dc->current_state;
3311  	update_type = dc_check_update_surfaces_for_stream(
3312  			dc, srf_updates, surface_count, stream_update, stream_status);
3313  	/* It is possible to receive a flip for one plane while there are multiple flip_immediate planes in the same stream.
3314  	 * E.g. Desktop and MPO plane are flip_immediate but only the MPO plane received a flip
3315  	 * Force the other flip_immediate planes to flip so GSL doesn't wait for a flip that won't come.
3316  	 */
3317  	force_immediate_gsl_plane_flip(dc, srf_updates, surface_count);
3318  	if (update_type == UPDATE_TYPE_FULL)
3319  		backup_planes_and_stream_state(&dc->scratch.current_state, stream);
3320  
3321  	/* update current stream with the new updates */
3322  	copy_stream_update_to_stream(dc, context, stream, stream_update);
3323  
3324  	/* do not perform surface update if surface has invalid dimensions
3325  	 * (all zero) and no scaling_info is provided
3326  	 */
3327  	if (surface_count > 0) {
3328  		for (i = 0; i < surface_count; i++) {
3329  			if ((srf_updates[i].surface->src_rect.width == 0 ||
3330  				 srf_updates[i].surface->src_rect.height == 0 ||
3331  				 srf_updates[i].surface->dst_rect.width == 0 ||
3332  				 srf_updates[i].surface->dst_rect.height == 0) &&
3333  				(!srf_updates[i].scaling_info ||
3334  				  srf_updates[i].scaling_info->src_rect.width == 0 ||
3335  				  srf_updates[i].scaling_info->src_rect.height == 0 ||
3336  				  srf_updates[i].scaling_info->dst_rect.width == 0 ||
3337  				  srf_updates[i].scaling_info->dst_rect.height == 0)) {
3338  				DC_ERROR("Invalid src/dst rects in surface update!\n");
3339  				return false;
3340  			}
3341  		}
3342  	}
3343  
3344  	if (update_type >= update_surface_trace_level)
3345  		update_surface_trace(dc, srf_updates, surface_count);
3346  
3347  	for (i = 0; i < surface_count; i++)
3348  		copy_surface_update_to_plane(srf_updates[i].surface, &srf_updates[i]);
3349  
3350  	if (update_type >= UPDATE_TYPE_FULL) {
3351  		struct dc_plane_state *new_planes[MAX_SURFACES] = {0};
3352  
3353  		for (i = 0; i < surface_count; i++)
3354  			new_planes[i] = srf_updates[i].surface;
3355  
3356  		/* initialize scratch memory for building context */
3357  		context = dc_state_create_copy(dc->current_state);
3358  		if (context == NULL) {
3359  			DC_ERROR("Failed to allocate new validate context!\n");
3360  			return false;
3361  		}
3362  
3363  		/* For each full update, remove all existing phantom pipes first.
3364  		 * Ensures that we have enough pipes for newly added MPO planes
3365  		 */
3366  		dc_state_remove_phantom_streams_and_planes(dc, context);
3367  		dc_state_release_phantom_streams_and_planes(dc, context);
3368  
3369  		/*remove old surfaces from context */
3370  		if (!dc_state_rem_all_planes_for_stream(dc, stream, context)) {
3371  
3372  			BREAK_TO_DEBUGGER();
3373  			goto fail;
3374  		}
3375  
3376  		/* add surface to context */
3377  		if (!dc_state_add_all_planes_for_stream(dc, stream, new_planes, surface_count, context)) {
3378  
3379  			BREAK_TO_DEBUGGER();
3380  			goto fail;
3381  		}
3382  	}
3383  
3384  	/* save update parameters into surface */
3385  	for (i = 0; i < surface_count; i++) {
3386  		struct dc_plane_state *surface = srf_updates[i].surface;
3387  
3388  		if (update_type != UPDATE_TYPE_MED)
3389  			continue;
3390  		if (surface->update_flags.bits.position_change) {
3391  			for (j = 0; j < dc->res_pool->pipe_count; j++) {
3392  				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3393  
3394  				if (pipe_ctx->plane_state != surface)
3395  					continue;
3396  
3397  				resource_build_scaling_params(pipe_ctx);
3398  			}
3399  		}
3400  	}
3401  
3402  	if (update_type == UPDATE_TYPE_FULL) {
3403  		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
3404  			BREAK_TO_DEBUGGER();
3405  			goto fail;
3406  		}
3407  	}
3408  	update_seamless_boot_flags(dc, context, surface_count, stream);
3409  
3410  	*new_context = context;
3411  	*new_update_type = update_type;
3412  	if (update_type == UPDATE_TYPE_FULL)
3413  		backup_planes_and_stream_state(&dc->scratch.new_state, stream);
3414  
3415  	return true;
3416  
3417  fail:
3418  	dc_state_release(context);
3419  
3420  	return false;
3421  
3422  }
3423  
commit_planes_do_stream_update(struct dc * dc,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type update_type,struct dc_state * context)3424  static void commit_planes_do_stream_update(struct dc *dc,
3425  		struct dc_stream_state *stream,
3426  		struct dc_stream_update *stream_update,
3427  		enum surface_update_type update_type,
3428  		struct dc_state *context)
3429  {
3430  	int j;
3431  
3432  	// Stream updates
3433  	for (j = 0; j < dc->res_pool->pipe_count; j++) {
3434  		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3435  
3436  		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) && pipe_ctx->stream == stream) {
3437  
3438  			if (stream_update->periodic_interrupt && dc->hwss.setup_periodic_interrupt)
3439  				dc->hwss.setup_periodic_interrupt(dc, pipe_ctx);
3440  
3441  			if ((stream_update->hdr_static_metadata && !stream->use_dynamic_meta) ||
3442  					stream_update->vrr_infopacket ||
3443  					stream_update->vsc_infopacket ||
3444  					stream_update->vsp_infopacket ||
3445  					stream_update->hfvsif_infopacket ||
3446  					stream_update->adaptive_sync_infopacket ||
3447  					stream_update->vtem_infopacket) {
3448  				resource_build_info_frame(pipe_ctx);
3449  				dc->hwss.update_info_frame(pipe_ctx);
3450  
3451  				if (dc_is_dp_signal(pipe_ctx->stream->signal))
3452  					dc->link_srv->dp_trace_source_sequence(
3453  							pipe_ctx->stream->link,
3454  							DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
3455  			}
3456  
3457  			if (stream_update->hdr_static_metadata &&
3458  					stream->use_dynamic_meta &&
3459  					dc->hwss.set_dmdata_attributes &&
3460  					pipe_ctx->stream->dmdata_address.quad_part != 0)
3461  				dc->hwss.set_dmdata_attributes(pipe_ctx);
3462  
3463  			if (stream_update->gamut_remap)
3464  				dc_stream_set_gamut_remap(dc, stream);
3465  
3466  			if (stream_update->output_csc_transform)
3467  				dc_stream_program_csc_matrix(dc, stream);
3468  
3469  			if (stream_update->dither_option) {
3470  				struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
3471  				resource_build_bit_depth_reduction_params(pipe_ctx->stream,
3472  									&pipe_ctx->stream->bit_depth_params);
3473  				pipe_ctx->stream_res.opp->funcs->opp_program_fmt(pipe_ctx->stream_res.opp,
3474  						&stream->bit_depth_params,
3475  						&stream->clamping);
3476  				while (odm_pipe) {
3477  					odm_pipe->stream_res.opp->funcs->opp_program_fmt(odm_pipe->stream_res.opp,
3478  							&stream->bit_depth_params,
3479  							&stream->clamping);
3480  					odm_pipe = odm_pipe->next_odm_pipe;
3481  				}
3482  			}
3483  
3484  			if (stream_update->cursor_attributes)
3485  				program_cursor_attributes(dc, stream);
3486  
3487  			if (stream_update->cursor_position)
3488  				program_cursor_position(dc, stream);
3489  
3490  			/* Full fe update*/
3491  			if (update_type == UPDATE_TYPE_FAST)
3492  				continue;
3493  
3494  			if (stream_update->dsc_config)
3495  				dc->link_srv->update_dsc_config(pipe_ctx);
3496  
3497  			if (stream_update->mst_bw_update) {
3498  				if (stream_update->mst_bw_update->is_increase)
3499  					dc->link_srv->increase_mst_payload(pipe_ctx,
3500  							stream_update->mst_bw_update->mst_stream_bw);
3501  				else
3502  					dc->link_srv->reduce_mst_payload(pipe_ctx,
3503  							stream_update->mst_bw_update->mst_stream_bw);
3504  			}
3505  
3506  			if (stream_update->pending_test_pattern) {
3507  				/*
3508  				 * test pattern params depends on ODM topology
3509  				 * changes that we could be applying to front
3510  				 * end. Since at the current stage front end
3511  				 * changes are not yet applied. We can only
3512  				 * apply test pattern in hw based on current
3513  				 * state and populate the final test pattern
3514  				 * params in new state. If current and new test
3515  				 * pattern params are different as result of
3516  				 * different ODM topology being used, it will be
3517  				 * detected and handle during front end
3518  				 * programming update.
3519  				 */
3520  				dc->link_srv->dp_set_test_pattern(stream->link,
3521  					stream->test_pattern.type,
3522  					stream->test_pattern.color_space,
3523  					stream->test_pattern.p_link_settings,
3524  					stream->test_pattern.p_custom_pattern,
3525  					stream->test_pattern.cust_pattern_size);
3526  				resource_build_test_pattern_params(&context->res_ctx, pipe_ctx);
3527  			}
3528  
3529  			if (stream_update->dpms_off) {
3530  				if (*stream_update->dpms_off) {
3531  					dc->link_srv->set_dpms_off(pipe_ctx);
3532  					/* for dpms, keep acquired resources*/
3533  					if (pipe_ctx->stream_res.audio && !dc->debug.az_endpoint_mute_only)
3534  						pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
3535  
3536  					dc->optimized_required = true;
3537  
3538  				} else {
3539  					if (get_seamless_boot_stream_count(context) == 0)
3540  						dc->hwss.prepare_bandwidth(dc, dc->current_state);
3541  					dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3542  				}
3543  			} else if (pipe_ctx->stream->link->wa_flags.blank_stream_on_ocs_change && stream_update->output_color_space
3544  					&& !stream->dpms_off && dc_is_dp_signal(pipe_ctx->stream->signal)) {
3545  				/*
3546  				 * Workaround for firmware issue in some receivers where they don't pick up
3547  				 * correct output color space unless DP link is disabled/re-enabled
3548  				 */
3549  				dc->link_srv->set_dpms_on(dc->current_state, pipe_ctx);
3550  			}
3551  
3552  			if (stream_update->abm_level && pipe_ctx->stream_res.abm) {
3553  				bool should_program_abm = true;
3554  
3555  				// if otg funcs defined check if blanked before programming
3556  				if (pipe_ctx->stream_res.tg->funcs->is_blanked)
3557  					if (pipe_ctx->stream_res.tg->funcs->is_blanked(pipe_ctx->stream_res.tg))
3558  						should_program_abm = false;
3559  
3560  				if (should_program_abm) {
3561  					if (*stream_update->abm_level == ABM_LEVEL_IMMEDIATE_DISABLE) {
3562  						dc->hwss.set_abm_immediate_disable(pipe_ctx);
3563  					} else {
3564  						pipe_ctx->stream_res.abm->funcs->set_abm_level(
3565  							pipe_ctx->stream_res.abm, stream->abm_level);
3566  					}
3567  				}
3568  			}
3569  		}
3570  	}
3571  }
3572  
dc_dmub_should_send_dirty_rect_cmd(struct dc * dc,struct dc_stream_state * stream)3573  static bool dc_dmub_should_send_dirty_rect_cmd(struct dc *dc, struct dc_stream_state *stream)
3574  {
3575  	if ((stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1
3576  			|| stream->link->psr_settings.psr_version == DC_PSR_VERSION_1)
3577  			&& stream->ctx->dce_version >= DCN_VERSION_3_1)
3578  		return true;
3579  
3580  	if (stream->link->replay_settings.config.replay_supported)
3581  		return true;
3582  
3583  	if (stream->ctx->dce_version >= DCN_VERSION_3_5 && stream->abm_level)
3584  		return true;
3585  
3586  	return false;
3587  }
3588  
dc_dmub_update_dirty_rect(struct dc * dc,int surface_count,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,struct dc_state * context)3589  void dc_dmub_update_dirty_rect(struct dc *dc,
3590  			       int surface_count,
3591  			       struct dc_stream_state *stream,
3592  			       struct dc_surface_update *srf_updates,
3593  			       struct dc_state *context)
3594  {
3595  	union dmub_rb_cmd cmd;
3596  	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3597  	unsigned int i, j;
3598  	unsigned int panel_inst = 0;
3599  
3600  	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3601  		return;
3602  
3603  	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3604  		return;
3605  
3606  	memset(&cmd, 0x0, sizeof(cmd));
3607  	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3608  	cmd.update_dirty_rect.header.sub_type = 0;
3609  	cmd.update_dirty_rect.header.payload_bytes =
3610  		sizeof(cmd.update_dirty_rect) -
3611  		sizeof(cmd.update_dirty_rect.header);
3612  	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3613  	for (i = 0; i < surface_count; i++) {
3614  		struct dc_plane_state *plane_state = srf_updates[i].surface;
3615  		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3616  
3617  		if (!srf_updates[i].surface || !flip_addr)
3618  			continue;
3619  		/* Do not send in immediate flip mode */
3620  		if (srf_updates[i].surface->flip_immediate)
3621  			continue;
3622  
3623  		update_dirty_rect->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
3624  		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3625  		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3626  				sizeof(flip_addr->dirty_rects));
3627  		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3628  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3629  
3630  			if (pipe_ctx->stream != stream)
3631  				continue;
3632  			if (pipe_ctx->plane_state != plane_state)
3633  				continue;
3634  
3635  			update_dirty_rect->panel_inst = panel_inst;
3636  			update_dirty_rect->pipe_idx = j;
3637  			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
3638  		}
3639  	}
3640  }
3641  
build_dmub_update_dirty_rect(struct dc * dc,int surface_count,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,struct dc_state * context,struct dc_dmub_cmd dc_dmub_cmd[],unsigned int * dmub_cmd_count)3642  static void build_dmub_update_dirty_rect(
3643  		struct dc *dc,
3644  		int surface_count,
3645  		struct dc_stream_state *stream,
3646  		struct dc_surface_update *srf_updates,
3647  		struct dc_state *context,
3648  		struct dc_dmub_cmd dc_dmub_cmd[],
3649  		unsigned int *dmub_cmd_count)
3650  {
3651  	union dmub_rb_cmd cmd;
3652  	struct dmub_cmd_update_dirty_rect_data *update_dirty_rect;
3653  	unsigned int i, j;
3654  	unsigned int panel_inst = 0;
3655  
3656  	if (!dc_dmub_should_send_dirty_rect_cmd(dc, stream))
3657  		return;
3658  
3659  	if (!dc_get_edp_link_panel_inst(dc, stream->link, &panel_inst))
3660  		return;
3661  
3662  	memset(&cmd, 0x0, sizeof(cmd));
3663  	cmd.update_dirty_rect.header.type = DMUB_CMD__UPDATE_DIRTY_RECT;
3664  	cmd.update_dirty_rect.header.sub_type = 0;
3665  	cmd.update_dirty_rect.header.payload_bytes =
3666  		sizeof(cmd.update_dirty_rect) -
3667  		sizeof(cmd.update_dirty_rect.header);
3668  	update_dirty_rect = &cmd.update_dirty_rect.update_dirty_rect_data;
3669  	for (i = 0; i < surface_count; i++) {
3670  		struct dc_plane_state *plane_state = srf_updates[i].surface;
3671  		const struct dc_flip_addrs *flip_addr = srf_updates[i].flip_addr;
3672  
3673  		if (!srf_updates[i].surface || !flip_addr)
3674  			continue;
3675  		/* Do not send in immediate flip mode */
3676  		if (srf_updates[i].surface->flip_immediate)
3677  			continue;
3678  		update_dirty_rect->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
3679  		update_dirty_rect->dirty_rect_count = flip_addr->dirty_rect_count;
3680  		memcpy(update_dirty_rect->src_dirty_rects, flip_addr->dirty_rects,
3681  				sizeof(flip_addr->dirty_rects));
3682  		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3683  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3684  
3685  			if (pipe_ctx->stream != stream)
3686  				continue;
3687  			if (pipe_ctx->plane_state != plane_state)
3688  				continue;
3689  			update_dirty_rect->panel_inst = panel_inst;
3690  			update_dirty_rect->pipe_idx = j;
3691  			dc_dmub_cmd[*dmub_cmd_count].dmub_cmd = cmd;
3692  			dc_dmub_cmd[*dmub_cmd_count].wait_type = DM_DMUB_WAIT_TYPE_NO_WAIT;
3693  			(*dmub_cmd_count)++;
3694  		}
3695  	}
3696  }
3697  
check_address_only_update(union surface_update_flags update_flags)3698  static bool check_address_only_update(union surface_update_flags update_flags)
3699  {
3700  	union surface_update_flags addr_only_update_flags;
3701  	addr_only_update_flags.raw = 0;
3702  	addr_only_update_flags.bits.addr_update = 1;
3703  
3704  	return update_flags.bits.addr_update &&
3705  			!(update_flags.raw & ~addr_only_update_flags.raw);
3706  }
3707  
3708  /**
3709   * build_dmub_cmd_list() - Build an array of DMCUB commands to be sent to DMCUB
3710   *
3711   * @dc: Current DC state
3712   * @srf_updates: Array of surface updates
3713   * @surface_count: Number of surfaces that have an updated
3714   * @stream: Corresponding stream to be updated in the current flip
3715   * @context: New DC state to be programmed
3716   *
3717   * @dc_dmub_cmd: Array of DMCUB commands to be sent to DMCUB
3718   * @dmub_cmd_count: Count indicating the number of DMCUB commands in dc_dmub_cmd array
3719   *
3720   * This function builds an array of DMCUB commands to be sent to DMCUB. This function is required
3721   * to build an array of commands and have them sent while the OTG lock is acquired.
3722   *
3723   * Return: void
3724   */
build_dmub_cmd_list(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_state * context,struct dc_dmub_cmd dc_dmub_cmd[],unsigned int * dmub_cmd_count)3725  static void build_dmub_cmd_list(struct dc *dc,
3726  		struct dc_surface_update *srf_updates,
3727  		int surface_count,
3728  		struct dc_stream_state *stream,
3729  		struct dc_state *context,
3730  		struct dc_dmub_cmd dc_dmub_cmd[],
3731  		unsigned int *dmub_cmd_count)
3732  {
3733  	// Initialize cmd count to 0
3734  	*dmub_cmd_count = 0;
3735  	build_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context, dc_dmub_cmd, dmub_cmd_count);
3736  }
3737  
commit_plane_for_stream_offload_fams2_flip(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_state * context)3738  static void commit_plane_for_stream_offload_fams2_flip(struct dc *dc,
3739  		struct dc_surface_update *srf_updates,
3740  		int surface_count,
3741  		struct dc_stream_state *stream,
3742  		struct dc_state *context)
3743  {
3744  	int i, j;
3745  
3746  	/* update dirty rect for PSR */
3747  	dc_dmub_update_dirty_rect(dc, surface_count, stream,
3748  			srf_updates, context);
3749  
3750  	/* Perform requested Updates */
3751  	for (i = 0; i < surface_count; i++) {
3752  		struct dc_plane_state *plane_state = srf_updates[i].surface;
3753  
3754  		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3755  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3756  
3757  			if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
3758  				continue;
3759  
3760  			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3761  				continue;
3762  
3763  			/* update pipe context for plane */
3764  			if (pipe_ctx->plane_state->update_flags.bits.addr_update)
3765  				dc->hwss.update_plane_addr(dc, pipe_ctx);
3766  		}
3767  	}
3768  
3769  	/* Send commands to DMCUB */
3770  	dc_dmub_srv_fams2_passthrough_flip(dc,
3771  				context,
3772  				stream,
3773  				srf_updates,
3774  				surface_count);
3775  }
3776  
commit_planes_for_stream_fast(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type update_type,struct dc_state * context)3777  static void commit_planes_for_stream_fast(struct dc *dc,
3778  		struct dc_surface_update *srf_updates,
3779  		int surface_count,
3780  		struct dc_stream_state *stream,
3781  		struct dc_stream_update *stream_update,
3782  		enum surface_update_type update_type,
3783  		struct dc_state *context)
3784  {
3785  	int i, j;
3786  	struct pipe_ctx *top_pipe_to_program = NULL;
3787  	struct dc_stream_status *stream_status = NULL;
3788  	bool should_offload_fams2_flip = false;
3789  	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
3790  
3791  	if (should_lock_all_pipes)
3792  		determine_pipe_unlock_order(dc, context);
3793  
3794  	if (dc->debug.fams2_config.bits.enable &&
3795  			dc->debug.fams2_config.bits.enable_offload_flip &&
3796  			dc_state_is_fams2_in_use(dc, context)) {
3797  		/* if not offloading to HWFQ, offload to FAMS2 if needed */
3798  		should_offload_fams2_flip = true;
3799  		for (i = 0; i < surface_count; i++) {
3800  			if (srf_updates[i].surface &&
3801  					srf_updates[i].surface->update_flags.raw &&
3802  					!check_address_only_update(srf_updates[i].surface->update_flags)) {
3803  				/* more than address update, need to acquire FAMS2 lock */
3804  				should_offload_fams2_flip = false;
3805  				break;
3806  			}
3807  		}
3808  		if (stream_update) {
3809  			/* more than address update, need to acquire FAMS2 lock */
3810  			should_offload_fams2_flip = false;
3811  		}
3812  	}
3813  
3814  	dc_exit_ips_for_hw_access(dc);
3815  
3816  	dc_z10_restore(dc);
3817  
3818  	top_pipe_to_program = resource_get_otg_master_for_stream(
3819  			&context->res_ctx,
3820  			stream);
3821  
3822  	if (!top_pipe_to_program)
3823  		return;
3824  
3825  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3826  		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3827  
3828  		if (pipe->stream && pipe->plane_state) {
3829  			if (!dc->debug.using_dml2)
3830  				set_p_state_switch_method(dc, context, pipe);
3831  
3832  			if (dc->debug.visual_confirm)
3833  				dc_update_visual_confirm_color(dc, context, pipe);
3834  		}
3835  	}
3836  
3837  	for (i = 0; i < surface_count; i++) {
3838  		struct dc_plane_state *plane_state = srf_updates[i].surface;
3839  		/*set logical flag for lock/unlock use*/
3840  		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3841  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
3842  
3843  			if (!pipe_ctx->plane_state)
3844  				continue;
3845  			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
3846  				continue;
3847  
3848  			pipe_ctx->plane_state->triplebuffer_flips = false;
3849  			if (update_type == UPDATE_TYPE_FAST &&
3850  					dc->hwss.program_triplebuffer != NULL &&
3851  					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
3852  				/*triple buffer for VUpdate only*/
3853  				pipe_ctx->plane_state->triplebuffer_flips = true;
3854  			}
3855  		}
3856  	}
3857  
3858  	stream_status = dc_state_get_stream_status(context, stream);
3859  
3860  	if (should_offload_fams2_flip) {
3861  		commit_plane_for_stream_offload_fams2_flip(dc,
3862  				srf_updates,
3863  				surface_count,
3864  				stream,
3865  				context);
3866  	} else if (stream_status) {
3867  		build_dmub_cmd_list(dc,
3868  				srf_updates,
3869  				surface_count,
3870  				stream,
3871  				context,
3872  				context->dc_dmub_cmd,
3873  				&(context->dmub_cmd_count));
3874  		hwss_build_fast_sequence(dc,
3875  				context->dc_dmub_cmd,
3876  				context->dmub_cmd_count,
3877  				context->block_sequence,
3878  				&(context->block_sequence_steps),
3879  				top_pipe_to_program,
3880  				stream_status,
3881  				context);
3882  		hwss_execute_sequence(dc,
3883  				context->block_sequence,
3884  				context->block_sequence_steps);
3885  	}
3886  
3887  	/* Clear update flags so next flip doesn't have redundant programming
3888  	 * (if there's no stream update, the update flags are not cleared).
3889  	 * Surface updates are cleared unconditionally at the beginning of each flip,
3890  	 * so no need to clear here.
3891  	 */
3892  	if (top_pipe_to_program->stream)
3893  		top_pipe_to_program->stream->update_flags.raw = 0;
3894  }
3895  
commit_planes_for_stream(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type update_type,struct dc_state * context)3896  static void commit_planes_for_stream(struct dc *dc,
3897  		struct dc_surface_update *srf_updates,
3898  		int surface_count,
3899  		struct dc_stream_state *stream,
3900  		struct dc_stream_update *stream_update,
3901  		enum surface_update_type update_type,
3902  		struct dc_state *context)
3903  {
3904  	int i, j;
3905  	struct pipe_ctx *top_pipe_to_program = NULL;
3906  	bool should_lock_all_pipes = (update_type != UPDATE_TYPE_FAST);
3907  	bool subvp_prev_use = false;
3908  	bool subvp_curr_use = false;
3909  	uint8_t current_stream_mask = 0;
3910  
3911  	if (should_lock_all_pipes)
3912  		determine_pipe_unlock_order(dc, context);
3913  	// Once we apply the new subvp context to hardware it won't be in the
3914  	// dc->current_state anymore, so we have to cache it before we apply
3915  	// the new SubVP context
3916  	subvp_prev_use = false;
3917  	dc_exit_ips_for_hw_access(dc);
3918  
3919  	dc_z10_restore(dc);
3920  	if (update_type == UPDATE_TYPE_FULL && dc->optimized_required)
3921  		hwss_process_outstanding_hw_updates(dc, dc->current_state);
3922  
3923  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3924  		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3925  
3926  		if (pipe->stream && pipe->plane_state) {
3927  			if (!dc->debug.using_dml2)
3928  				set_p_state_switch_method(dc, context, pipe);
3929  
3930  			if (dc->debug.visual_confirm)
3931  				dc_update_visual_confirm_color(dc, context, pipe);
3932  		}
3933  	}
3934  
3935  	if (update_type == UPDATE_TYPE_FULL) {
3936  		dc_allow_idle_optimizations(dc, false);
3937  
3938  		if (get_seamless_boot_stream_count(context) == 0)
3939  			dc->hwss.prepare_bandwidth(dc, context);
3940  
3941  		if (dc->hwss.update_dsc_pg)
3942  			dc->hwss.update_dsc_pg(dc, context, false);
3943  
3944  		context_clock_trace(dc, context);
3945  	}
3946  
3947  	if (update_type == UPDATE_TYPE_FULL)
3948  		hwss_wait_for_outstanding_hw_updates(dc, dc->current_state);
3949  
3950  	top_pipe_to_program = resource_get_otg_master_for_stream(
3951  				&context->res_ctx,
3952  				stream);
3953  	ASSERT(top_pipe_to_program != NULL);
3954  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3955  		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3956  
3957  		// Check old context for SubVP
3958  		subvp_prev_use |= (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) == SUBVP_PHANTOM);
3959  		if (subvp_prev_use)
3960  			break;
3961  	}
3962  
3963  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3964  		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3965  
3966  		if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
3967  			subvp_curr_use = true;
3968  			break;
3969  		}
3970  	}
3971  
3972  	if (stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE) {
3973  		struct pipe_ctx *mpcc_pipe;
3974  		struct pipe_ctx *odm_pipe;
3975  
3976  		for (mpcc_pipe = top_pipe_to_program; mpcc_pipe; mpcc_pipe = mpcc_pipe->bottom_pipe)
3977  			for (odm_pipe = mpcc_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
3978  				odm_pipe->ttu_regs.min_ttu_vblank = MAX_TTU;
3979  	}
3980  
3981  	if (update_type != UPDATE_TYPE_FAST && dc->res_pool->funcs->prepare_mcache_programming)
3982  		dc->res_pool->funcs->prepare_mcache_programming(dc, context);
3983  
3984  	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
3985  		if (top_pipe_to_program &&
3986  			top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
3987  			if (should_use_dmub_lock(stream->link)) {
3988  				union dmub_hw_lock_flags hw_locks = { 0 };
3989  				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
3990  
3991  				hw_locks.bits.lock_dig = 1;
3992  				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
3993  
3994  				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
3995  							true,
3996  							&hw_locks,
3997  							&inst_flags);
3998  			} else
3999  				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable(
4000  						top_pipe_to_program->stream_res.tg);
4001  		}
4002  
4003  	if (dc->hwss.wait_for_dcc_meta_propagation) {
4004  		dc->hwss.wait_for_dcc_meta_propagation(dc, top_pipe_to_program);
4005  	}
4006  
4007  	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4008  		if (dc->hwss.subvp_pipe_control_lock)
4009  			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, NULL, subvp_prev_use);
4010  
4011  		if (dc->hwss.fams2_global_control_lock)
4012  			dc->hwss.fams2_global_control_lock(dc, context, true);
4013  
4014  		dc->hwss.interdependent_update_lock(dc, context, true);
4015  	} else {
4016  		if (dc->hwss.subvp_pipe_control_lock)
4017  			dc->hwss.subvp_pipe_control_lock(dc, context, true, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4018  
4019  		if (dc->hwss.fams2_global_control_lock)
4020  			dc->hwss.fams2_global_control_lock(dc, context, true);
4021  
4022  		/* Lock the top pipe while updating plane addrs, since freesync requires
4023  		 *  plane addr update event triggers to be synchronized.
4024  		 *  top_pipe_to_program is expected to never be NULL
4025  		 */
4026  		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, true);
4027  	}
4028  
4029  	dc_dmub_update_dirty_rect(dc, surface_count, stream, srf_updates, context);
4030  
4031  	// Stream updates
4032  	if (stream_update)
4033  		commit_planes_do_stream_update(dc, stream, stream_update, update_type, context);
4034  
4035  	if (surface_count == 0) {
4036  		/*
4037  		 * In case of turning off screen, no need to program front end a second time.
4038  		 * just return after program blank.
4039  		 */
4040  		if (dc->hwss.apply_ctx_for_surface)
4041  			dc->hwss.apply_ctx_for_surface(dc, stream, 0, context);
4042  		if (dc->hwss.program_front_end_for_ctx)
4043  			dc->hwss.program_front_end_for_ctx(dc, context);
4044  
4045  		if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4046  			dc->hwss.interdependent_update_lock(dc, context, false);
4047  		} else {
4048  			dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
4049  		}
4050  		dc->hwss.post_unlock_program_front_end(dc, context);
4051  
4052  		if (update_type != UPDATE_TYPE_FAST)
4053  			if (dc->hwss.commit_subvp_config)
4054  				dc->hwss.commit_subvp_config(dc, context);
4055  
4056  		/* Since phantom pipe programming is moved to post_unlock_program_front_end,
4057  		 * move the SubVP lock to after the phantom pipes have been setup
4058  		 */
4059  		if (dc->hwss.subvp_pipe_control_lock)
4060  			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes,
4061  							 NULL, subvp_prev_use);
4062  
4063  		if (dc->hwss.fams2_global_control_lock)
4064  			dc->hwss.fams2_global_control_lock(dc, context, false);
4065  
4066  		return;
4067  	}
4068  
4069  	if (update_type != UPDATE_TYPE_FAST) {
4070  		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4071  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4072  
4073  			if ((dc->debug.visual_confirm == VISUAL_CONFIRM_SUBVP ||
4074  				dc->debug.visual_confirm == VISUAL_CONFIRM_MCLK_SWITCH) &&
4075  				pipe_ctx->stream && pipe_ctx->plane_state) {
4076  				/* Only update visual confirm for SUBVP and Mclk switching here.
4077  				 * The bar appears on all pipes, so we need to update the bar on all displays,
4078  				 * so the information doesn't get stale.
4079  				 */
4080  				dc->hwss.update_visual_confirm_color(dc, pipe_ctx,
4081  						pipe_ctx->plane_res.hubp->inst);
4082  			}
4083  		}
4084  	}
4085  
4086  	for (i = 0; i < surface_count; i++) {
4087  		struct dc_plane_state *plane_state = srf_updates[i].surface;
4088  
4089  		/*set logical flag for lock/unlock use*/
4090  		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4091  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4092  			if (!pipe_ctx->plane_state)
4093  				continue;
4094  			if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4095  				continue;
4096  			pipe_ctx->plane_state->triplebuffer_flips = false;
4097  			if (update_type == UPDATE_TYPE_FAST &&
4098  					dc->hwss.program_triplebuffer != NULL &&
4099  					!pipe_ctx->plane_state->flip_immediate && dc->debug.enable_tri_buf) {
4100  				/*triple buffer for VUpdate only*/
4101  				pipe_ctx->plane_state->triplebuffer_flips = true;
4102  			}
4103  		}
4104  		if (update_type == UPDATE_TYPE_FULL) {
4105  			/* force vsync flip when reconfiguring pipes to prevent underflow */
4106  			plane_state->flip_immediate = false;
4107  			plane_state->triplebuffer_flips = false;
4108  		}
4109  	}
4110  
4111  	// Update Type FULL, Surface updates
4112  	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4113  		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4114  
4115  		if (!pipe_ctx->top_pipe &&
4116  			!pipe_ctx->prev_odm_pipe &&
4117  			should_update_pipe_for_stream(context, pipe_ctx, stream)) {
4118  			struct dc_stream_status *stream_status = NULL;
4119  
4120  			if (!pipe_ctx->plane_state)
4121  				continue;
4122  
4123  			/* Full fe update*/
4124  			if (update_type == UPDATE_TYPE_FAST)
4125  				continue;
4126  
4127  			ASSERT(!pipe_ctx->plane_state->triplebuffer_flips);
4128  			if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
4129  				/*turn off triple buffer for full update*/
4130  				dc->hwss.program_triplebuffer(
4131  					dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
4132  			}
4133  			stream_status =
4134  				stream_get_status(context, pipe_ctx->stream);
4135  
4136  			if (dc->hwss.apply_ctx_for_surface && stream_status)
4137  				dc->hwss.apply_ctx_for_surface(
4138  					dc, pipe_ctx->stream, stream_status->plane_count, context);
4139  		}
4140  	}
4141  	if (dc->hwss.program_front_end_for_ctx && update_type != UPDATE_TYPE_FAST) {
4142  		dc->hwss.program_front_end_for_ctx(dc, context);
4143  		if (dc->debug.validate_dml_output) {
4144  			for (i = 0; i < dc->res_pool->pipe_count; i++) {
4145  				struct pipe_ctx *cur_pipe = &context->res_ctx.pipe_ctx[i];
4146  				if (cur_pipe->stream == NULL)
4147  					continue;
4148  
4149  				cur_pipe->plane_res.hubp->funcs->validate_dml_output(
4150  						cur_pipe->plane_res.hubp, dc->ctx,
4151  						&context->res_ctx.pipe_ctx[i].rq_regs,
4152  						&context->res_ctx.pipe_ctx[i].dlg_regs,
4153  						&context->res_ctx.pipe_ctx[i].ttu_regs);
4154  			}
4155  		}
4156  	}
4157  
4158  	// Update Type FAST, Surface updates
4159  	if (update_type == UPDATE_TYPE_FAST) {
4160  		if (dc->hwss.set_flip_control_gsl)
4161  			for (i = 0; i < surface_count; i++) {
4162  				struct dc_plane_state *plane_state = srf_updates[i].surface;
4163  
4164  				for (j = 0; j < dc->res_pool->pipe_count; j++) {
4165  					struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4166  
4167  					if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4168  						continue;
4169  
4170  					if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4171  						continue;
4172  
4173  					// GSL has to be used for flip immediate
4174  					dc->hwss.set_flip_control_gsl(pipe_ctx,
4175  							pipe_ctx->plane_state->flip_immediate);
4176  				}
4177  			}
4178  
4179  		/* Perform requested Updates */
4180  		for (i = 0; i < surface_count; i++) {
4181  			struct dc_plane_state *plane_state = srf_updates[i].surface;
4182  
4183  			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4184  				struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4185  
4186  				if (!should_update_pipe_for_stream(context, pipe_ctx, stream))
4187  					continue;
4188  
4189  				if (!should_update_pipe_for_plane(context, pipe_ctx, plane_state))
4190  					continue;
4191  
4192  				if (srf_updates[i].cm2_params &&
4193  						srf_updates[i].cm2_params->cm2_luts.lut3d_data.lut3d_src ==
4194  								DC_CM2_TRANSFER_FUNC_SOURCE_VIDMEM &&
4195  						srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting ==
4196  								DC_CM2_SHAPER_3DLUT_SETTING_ENABLE_SHAPER_3DLUT &&
4197  						dc->hwss.trigger_3dlut_dma_load)
4198  					dc->hwss.trigger_3dlut_dma_load(dc, pipe_ctx);
4199  
4200  				/*program triple buffer after lock based on flip type*/
4201  				if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
4202  					/*only enable triplebuffer for fast_update*/
4203  					dc->hwss.program_triplebuffer(
4204  						dc, pipe_ctx, pipe_ctx->plane_state->triplebuffer_flips);
4205  				}
4206  				if (pipe_ctx->plane_state->update_flags.bits.addr_update)
4207  					dc->hwss.update_plane_addr(dc, pipe_ctx);
4208  			}
4209  		}
4210  	}
4211  
4212  	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4213  		dc->hwss.interdependent_update_lock(dc, context, false);
4214  	} else {
4215  		dc->hwss.pipe_control_lock(dc, top_pipe_to_program, false);
4216  	}
4217  
4218  	if ((update_type != UPDATE_TYPE_FAST) && stream->update_flags.bits.dsc_changed)
4219  		if (top_pipe_to_program &&
4220  		    top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_enable) {
4221  			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4222  				top_pipe_to_program->stream_res.tg,
4223  				CRTC_STATE_VACTIVE);
4224  			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4225  				top_pipe_to_program->stream_res.tg,
4226  				CRTC_STATE_VBLANK);
4227  			top_pipe_to_program->stream_res.tg->funcs->wait_for_state(
4228  				top_pipe_to_program->stream_res.tg,
4229  				CRTC_STATE_VACTIVE);
4230  
4231  			if (should_use_dmub_lock(stream->link)) {
4232  				union dmub_hw_lock_flags hw_locks = { 0 };
4233  				struct dmub_hw_lock_inst_flags inst_flags = { 0 };
4234  
4235  				hw_locks.bits.lock_dig = 1;
4236  				inst_flags.dig_inst = top_pipe_to_program->stream_res.tg->inst;
4237  
4238  				dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
4239  							false,
4240  							&hw_locks,
4241  							&inst_flags);
4242  			} else
4243  				top_pipe_to_program->stream_res.tg->funcs->lock_doublebuffer_disable(
4244  					top_pipe_to_program->stream_res.tg);
4245  		}
4246  
4247  	if (subvp_curr_use) {
4248  		/* If enabling subvp or transitioning from subvp->subvp, enable the
4249  		 * phantom streams before we program front end for the phantom pipes.
4250  		 */
4251  		if (update_type != UPDATE_TYPE_FAST) {
4252  			if (dc->hwss.enable_phantom_streams)
4253  				dc->hwss.enable_phantom_streams(dc, context);
4254  		}
4255  	}
4256  
4257  	if (update_type != UPDATE_TYPE_FAST)
4258  		dc->hwss.post_unlock_program_front_end(dc, context);
4259  
4260  	if (subvp_prev_use && !subvp_curr_use) {
4261  		/* If disabling subvp, disable phantom streams after front end
4262  		 * programming has completed (we turn on phantom OTG in order
4263  		 * to complete the plane disable for phantom pipes).
4264  		 */
4265  
4266  		if (dc->hwss.disable_phantom_streams)
4267  			dc->hwss.disable_phantom_streams(dc, context);
4268  	}
4269  
4270  	if (update_type != UPDATE_TYPE_FAST)
4271  		if (dc->hwss.commit_subvp_config)
4272  			dc->hwss.commit_subvp_config(dc, context);
4273  	/* Since phantom pipe programming is moved to post_unlock_program_front_end,
4274  	 * move the SubVP lock to after the phantom pipes have been setup
4275  	 */
4276  	if (should_lock_all_pipes && dc->hwss.interdependent_update_lock) {
4277  		if (dc->hwss.subvp_pipe_control_lock)
4278  			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, NULL, subvp_prev_use);
4279  		if (dc->hwss.fams2_global_control_lock)
4280  			dc->hwss.fams2_global_control_lock(dc, context, false);
4281  	} else {
4282  		if (dc->hwss.subvp_pipe_control_lock)
4283  			dc->hwss.subvp_pipe_control_lock(dc, context, false, should_lock_all_pipes, top_pipe_to_program, subvp_prev_use);
4284  		if (dc->hwss.fams2_global_control_lock)
4285  			dc->hwss.fams2_global_control_lock(dc, context, false);
4286  	}
4287  
4288  	// Fire manual trigger only when bottom plane is flipped
4289  	for (j = 0; j < dc->res_pool->pipe_count; j++) {
4290  		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[j];
4291  
4292  		if (!pipe_ctx->plane_state)
4293  			continue;
4294  
4295  		if (pipe_ctx->bottom_pipe || pipe_ctx->next_odm_pipe ||
4296  				!pipe_ctx->stream || !should_update_pipe_for_stream(context, pipe_ctx, stream) ||
4297  				!pipe_ctx->plane_state->update_flags.bits.addr_update ||
4298  				pipe_ctx->plane_state->skip_manual_trigger)
4299  			continue;
4300  
4301  		if (pipe_ctx->stream_res.tg->funcs->program_manual_trigger)
4302  			pipe_ctx->stream_res.tg->funcs->program_manual_trigger(pipe_ctx->stream_res.tg);
4303  	}
4304  
4305  	current_stream_mask = get_stream_mask(dc, context);
4306  	if (current_stream_mask != context->stream_mask) {
4307  		context->stream_mask = current_stream_mask;
4308  		dc_dmub_srv_notify_stream_mask(dc->ctx->dmub_srv, current_stream_mask);
4309  	}
4310  }
4311  
4312  /**
4313   * could_mpcc_tree_change_for_active_pipes - Check if an OPP associated with MPCC might change
4314   *
4315   * @dc: Used to get the current state status
4316   * @stream: Target stream, which we want to remove the attached planes
4317   * @srf_updates: Array of surface updates
4318   * @surface_count: Number of surface update
4319   * @is_plane_addition: [in] Fill out with true if it is a plane addition case
4320   *
4321   * DCN32x and newer support a feature named Dynamic ODM which can conflict with
4322   * the MPO if used simultaneously in some specific configurations (e.g.,
4323   * 4k@144). This function checks if the incoming context requires applying a
4324   * transition state with unnecessary pipe splitting and ODM disabled to
4325   * circumvent our hardware limitations to prevent this edge case. If the OPP
4326   * associated with an MPCC might change due to plane additions, this function
4327   * returns true.
4328   *
4329   * Return:
4330   * Return true if OPP and MPCC might change, otherwise, return false.
4331   */
could_mpcc_tree_change_for_active_pipes(struct dc * dc,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,int surface_count,bool * is_plane_addition)4332  static bool could_mpcc_tree_change_for_active_pipes(struct dc *dc,
4333  		struct dc_stream_state *stream,
4334  		struct dc_surface_update *srf_updates,
4335  		int surface_count,
4336  		bool *is_plane_addition)
4337  {
4338  
4339  	struct dc_stream_status *cur_stream_status = stream_get_status(dc->current_state, stream);
4340  	bool force_minimal_pipe_splitting = false;
4341  	bool subvp_active = false;
4342  	uint32_t i;
4343  
4344  	*is_plane_addition = false;
4345  
4346  	if (cur_stream_status &&
4347  			dc->current_state->stream_count > 0 &&
4348  			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID) {
4349  		/* determine if minimal transition is required due to MPC*/
4350  		if (surface_count > 0) {
4351  			if (cur_stream_status->plane_count > surface_count) {
4352  				force_minimal_pipe_splitting = true;
4353  			} else if (cur_stream_status->plane_count < surface_count) {
4354  				force_minimal_pipe_splitting = true;
4355  				*is_plane_addition = true;
4356  			}
4357  		}
4358  	}
4359  
4360  	if (cur_stream_status &&
4361  			dc->current_state->stream_count == 1 &&
4362  			dc->debug.enable_single_display_2to1_odm_policy) {
4363  		/* determine if minimal transition is required due to dynamic ODM*/
4364  		if (surface_count > 0) {
4365  			if (cur_stream_status->plane_count > 2 && cur_stream_status->plane_count > surface_count) {
4366  				force_minimal_pipe_splitting = true;
4367  			} else if (surface_count > 2 && cur_stream_status->plane_count < surface_count) {
4368  				force_minimal_pipe_splitting = true;
4369  				*is_plane_addition = true;
4370  			}
4371  		}
4372  	}
4373  
4374  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4375  		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4376  
4377  		if (dc_state_get_pipe_subvp_type(dc->current_state, pipe) != SUBVP_NONE) {
4378  			subvp_active = true;
4379  			break;
4380  		}
4381  	}
4382  
4383  	/* For SubVP when adding or removing planes we need to add a minimal transition
4384  	 * (even when disabling all planes). Whenever disabling a phantom pipe, we
4385  	 * must use the minimal transition path to disable the pipe correctly.
4386  	 *
4387  	 * We want to use the minimal transition whenever subvp is active, not only if
4388  	 * a plane is being added / removed from a subvp stream (MPO plane can be added
4389  	 * to a DRR pipe of SubVP + DRR config, in which case we still want to run through
4390  	 * a min transition to disable subvp.
4391  	 */
4392  	if (cur_stream_status && subvp_active) {
4393  		/* determine if minimal transition is required due to SubVP*/
4394  		if (cur_stream_status->plane_count > surface_count) {
4395  			force_minimal_pipe_splitting = true;
4396  		} else if (cur_stream_status->plane_count < surface_count) {
4397  			force_minimal_pipe_splitting = true;
4398  			*is_plane_addition = true;
4399  		}
4400  	}
4401  
4402  	return force_minimal_pipe_splitting;
4403  }
4404  
4405  struct pipe_split_policy_backup {
4406  	bool dynamic_odm_policy;
4407  	bool subvp_policy;
4408  	enum pipe_split_policy mpc_policy;
4409  	char force_odm[MAX_PIPES];
4410  };
4411  
backup_and_set_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)4412  static void backup_and_set_minimal_pipe_split_policy(struct dc *dc,
4413  		struct dc_state *context,
4414  		struct pipe_split_policy_backup *policy)
4415  {
4416  	int i;
4417  
4418  	if (!dc->config.is_vmin_only_asic) {
4419  		policy->mpc_policy = dc->debug.pipe_split_policy;
4420  		dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
4421  	}
4422  	policy->dynamic_odm_policy = dc->debug.enable_single_display_2to1_odm_policy;
4423  	dc->debug.enable_single_display_2to1_odm_policy = false;
4424  	policy->subvp_policy = dc->debug.force_disable_subvp;
4425  	dc->debug.force_disable_subvp = true;
4426  	for (i = 0; i < context->stream_count; i++) {
4427  		policy->force_odm[i] = context->streams[i]->debug.force_odm_combine_segments;
4428  		if (context->streams[i]->debug.allow_transition_for_forced_odm)
4429  			context->streams[i]->debug.force_odm_combine_segments = 0;
4430  	}
4431  }
4432  
restore_minimal_pipe_split_policy(struct dc * dc,struct dc_state * context,struct pipe_split_policy_backup * policy)4433  static void restore_minimal_pipe_split_policy(struct dc *dc,
4434  		struct dc_state *context,
4435  		struct pipe_split_policy_backup *policy)
4436  {
4437  	uint8_t i;
4438  
4439  	if (!dc->config.is_vmin_only_asic)
4440  		dc->debug.pipe_split_policy = policy->mpc_policy;
4441  	dc->debug.enable_single_display_2to1_odm_policy =
4442  			policy->dynamic_odm_policy;
4443  	dc->debug.force_disable_subvp = policy->subvp_policy;
4444  	for (i = 0; i < context->stream_count; i++)
4445  		context->streams[i]->debug.force_odm_combine_segments = policy->force_odm[i];
4446  }
4447  
release_minimal_transition_state(struct dc * dc,struct dc_state * minimal_transition_context,struct dc_state * base_context,struct pipe_split_policy_backup * policy)4448  static void release_minimal_transition_state(struct dc *dc,
4449  		struct dc_state *minimal_transition_context,
4450  		struct dc_state *base_context,
4451  		struct pipe_split_policy_backup *policy)
4452  {
4453  	restore_minimal_pipe_split_policy(dc, base_context, policy);
4454  	dc_state_release(minimal_transition_context);
4455  }
4456  
force_vsync_flip_in_minimal_transition_context(struct dc_state * context)4457  static void force_vsync_flip_in_minimal_transition_context(struct dc_state *context)
4458  {
4459  	uint8_t i;
4460  	int j;
4461  	struct dc_stream_status *stream_status;
4462  
4463  	for (i = 0; i < context->stream_count; i++) {
4464  		stream_status = &context->stream_status[i];
4465  
4466  		for (j = 0; j < stream_status->plane_count; j++)
4467  			stream_status->plane_states[j]->flip_immediate = false;
4468  	}
4469  }
4470  
create_minimal_transition_state(struct dc * dc,struct dc_state * base_context,struct pipe_split_policy_backup * policy)4471  static struct dc_state *create_minimal_transition_state(struct dc *dc,
4472  		struct dc_state *base_context, struct pipe_split_policy_backup *policy)
4473  {
4474  	struct dc_state *minimal_transition_context = NULL;
4475  
4476  	minimal_transition_context = dc_state_create_copy(base_context);
4477  	if (!minimal_transition_context)
4478  		return NULL;
4479  
4480  	backup_and_set_minimal_pipe_split_policy(dc, base_context, policy);
4481  	/* commit minimal state */
4482  	if (dc->res_pool->funcs->validate_bandwidth(dc, minimal_transition_context, false)) {
4483  		/* prevent underflow and corruption when reconfiguring pipes */
4484  		force_vsync_flip_in_minimal_transition_context(minimal_transition_context);
4485  	} else {
4486  		/*
4487  		 * This should never happen, minimal transition state should
4488  		 * always be validated first before adding pipe split features.
4489  		 */
4490  		release_minimal_transition_state(dc, minimal_transition_context, base_context, policy);
4491  		BREAK_TO_DEBUGGER();
4492  		minimal_transition_context = NULL;
4493  	}
4494  	return minimal_transition_context;
4495  }
4496  
is_pipe_topology_transition_seamless_with_intermediate_step(struct dc * dc,struct dc_state * initial_state,struct dc_state * intermediate_state,struct dc_state * final_state)4497  static bool is_pipe_topology_transition_seamless_with_intermediate_step(
4498  		struct dc *dc,
4499  		struct dc_state *initial_state,
4500  		struct dc_state *intermediate_state,
4501  		struct dc_state *final_state)
4502  {
4503  	return dc->hwss.is_pipe_topology_transition_seamless(dc, initial_state,
4504  			intermediate_state) &&
4505  			dc->hwss.is_pipe_topology_transition_seamless(dc,
4506  					intermediate_state, final_state);
4507  }
4508  
swap_and_release_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)4509  static void swap_and_release_current_context(struct dc *dc,
4510  		struct dc_state *new_context, struct dc_stream_state *stream)
4511  {
4512  
4513  	int i;
4514  	struct dc_state *old = dc->current_state;
4515  	struct pipe_ctx *pipe_ctx;
4516  
4517  	/* Since memory free requires elevated IRQ, an interrupt
4518  	 * request is generated by mem free. If this happens
4519  	 * between freeing and reassigning the context, our vsync
4520  	 * interrupt will call into dc and cause a memory
4521  	 * corruption. Hence, we first reassign the context,
4522  	 * then free the old context.
4523  	 */
4524  	dc->current_state = new_context;
4525  	dc_state_release(old);
4526  
4527  	// clear any forced full updates
4528  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4529  		pipe_ctx = &new_context->res_ctx.pipe_ctx[i];
4530  
4531  		if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
4532  			pipe_ctx->plane_state->force_full_update = false;
4533  	}
4534  }
4535  
initialize_empty_surface_updates(struct dc_stream_state * stream,struct dc_surface_update * srf_updates)4536  static int initialize_empty_surface_updates(
4537  		struct dc_stream_state *stream,
4538  		struct dc_surface_update *srf_updates)
4539  {
4540  	struct dc_stream_status *status = dc_stream_get_status(stream);
4541  	int i;
4542  
4543  	if (!status)
4544  		return 0;
4545  
4546  	for (i = 0; i < status->plane_count; i++)
4547  		srf_updates[i].surface = status->plane_states[i];
4548  
4549  	return status->plane_count;
4550  }
4551  
commit_minimal_transition_based_on_new_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,int surface_count)4552  static bool commit_minimal_transition_based_on_new_context(struct dc *dc,
4553  		struct dc_state *new_context,
4554  		struct dc_stream_state *stream,
4555  		struct dc_surface_update *srf_updates,
4556  		int surface_count)
4557  {
4558  	bool success = false;
4559  	struct pipe_split_policy_backup policy;
4560  	struct dc_state *intermediate_context =
4561  			create_minimal_transition_state(dc, new_context,
4562  					&policy);
4563  
4564  	if (intermediate_context) {
4565  		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4566  				dc,
4567  				dc->current_state,
4568  				intermediate_context,
4569  				new_context)) {
4570  			DC_LOG_DC("commit minimal transition state: base = new state\n");
4571  			commit_planes_for_stream(dc, srf_updates,
4572  					surface_count, stream, NULL,
4573  					UPDATE_TYPE_FULL, intermediate_context);
4574  			swap_and_release_current_context(
4575  					dc, intermediate_context, stream);
4576  			dc_state_retain(dc->current_state);
4577  			success = true;
4578  		}
4579  		release_minimal_transition_state(
4580  				dc, intermediate_context, new_context, &policy);
4581  	}
4582  	return success;
4583  }
4584  
commit_minimal_transition_based_on_current_context(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream)4585  static bool commit_minimal_transition_based_on_current_context(struct dc *dc,
4586  		struct dc_state *new_context, struct dc_stream_state *stream)
4587  {
4588  	bool success = false;
4589  	struct pipe_split_policy_backup policy;
4590  	struct dc_state *intermediate_context;
4591  	struct dc_state *old_current_state = dc->current_state;
4592  	struct dc_surface_update srf_updates[MAX_SURFACES] = {0};
4593  	int surface_count;
4594  
4595  	/*
4596  	 * Both current and new contexts share the same stream and plane state
4597  	 * pointers. When new context is validated, stream and planes get
4598  	 * populated with new updates such as new plane addresses. This makes
4599  	 * the current context no longer valid because stream and planes are
4600  	 * modified from the original. We backup current stream and plane states
4601  	 * into scratch space whenever we are populating new context. So we can
4602  	 * restore the original values back by calling the restore function now.
4603  	 * This restores back the original stream and plane states associated
4604  	 * with the current state.
4605  	 */
4606  	restore_planes_and_stream_state(&dc->scratch.current_state, stream);
4607  	dc_state_retain(old_current_state);
4608  	intermediate_context = create_minimal_transition_state(dc,
4609  			old_current_state, &policy);
4610  
4611  	if (intermediate_context) {
4612  		if (is_pipe_topology_transition_seamless_with_intermediate_step(
4613  				dc,
4614  				dc->current_state,
4615  				intermediate_context,
4616  				new_context)) {
4617  			DC_LOG_DC("commit minimal transition state: base = current state\n");
4618  			surface_count = initialize_empty_surface_updates(
4619  					stream, srf_updates);
4620  			commit_planes_for_stream(dc, srf_updates,
4621  					surface_count, stream, NULL,
4622  					UPDATE_TYPE_FULL, intermediate_context);
4623  			swap_and_release_current_context(
4624  					dc, intermediate_context, stream);
4625  			dc_state_retain(dc->current_state);
4626  			success = true;
4627  		}
4628  		release_minimal_transition_state(dc, intermediate_context,
4629  				old_current_state, &policy);
4630  	}
4631  	dc_state_release(old_current_state);
4632  	/*
4633  	 * Restore stream and plane states back to the values associated with
4634  	 * new context.
4635  	 */
4636  	restore_planes_and_stream_state(&dc->scratch.new_state, stream);
4637  	return success;
4638  }
4639  
4640  /**
4641   * commit_minimal_transition_state_in_dc_update - Commit a minimal state based
4642   * on current or new context
4643   *
4644   * @dc: DC structure, used to get the current state
4645   * @new_context: New context
4646   * @stream: Stream getting the update for the flip
4647   * @srf_updates: Surface updates
4648   * @surface_count: Number of surfaces
4649   *
4650   * The function takes in current state and new state and determine a minimal
4651   * transition state as the intermediate step which could make the transition
4652   * between current and new states seamless. If found, it will commit the minimal
4653   * transition state and update current state to this minimal transition state
4654   * and return true, if not, it will return false.
4655   *
4656   * Return:
4657   * Return True if the minimal transition succeeded, false otherwise
4658   */
commit_minimal_transition_state_in_dc_update(struct dc * dc,struct dc_state * new_context,struct dc_stream_state * stream,struct dc_surface_update * srf_updates,int surface_count)4659  static bool commit_minimal_transition_state_in_dc_update(struct dc *dc,
4660  		struct dc_state *new_context,
4661  		struct dc_stream_state *stream,
4662  		struct dc_surface_update *srf_updates,
4663  		int surface_count)
4664  {
4665  	bool success = commit_minimal_transition_based_on_new_context(
4666  				dc, new_context, stream, srf_updates,
4667  				surface_count);
4668  	if (!success)
4669  		success = commit_minimal_transition_based_on_current_context(dc,
4670  				new_context, stream);
4671  	if (!success)
4672  		DC_LOG_ERROR("Fail to commit a seamless minimal transition state between current and new states.\nThis pipe topology update is non-seamless!\n");
4673  	return success;
4674  }
4675  
4676  /**
4677   * commit_minimal_transition_state - Create a transition pipe split state
4678   *
4679   * @dc: Used to get the current state status
4680   * @transition_base_context: New transition state
4681   *
4682   * In some specific configurations, such as pipe split on multi-display with
4683   * MPO and/or Dynamic ODM, removing a plane may cause unsupported pipe
4684   * programming when moving to new planes. To mitigate those types of problems,
4685   * this function adds a transition state that minimizes pipe usage before
4686   * programming the new configuration. When adding a new plane, the current
4687   * state requires the least pipes, so it is applied without splitting. When
4688   * removing a plane, the new state requires the least pipes, so it is applied
4689   * without splitting.
4690   *
4691   * Return:
4692   * Return false if something is wrong in the transition state.
4693   */
commit_minimal_transition_state(struct dc * dc,struct dc_state * transition_base_context)4694  static bool commit_minimal_transition_state(struct dc *dc,
4695  		struct dc_state *transition_base_context)
4696  {
4697  	struct dc_state *transition_context;
4698  	struct pipe_split_policy_backup policy;
4699  	enum dc_status ret = DC_ERROR_UNEXPECTED;
4700  	unsigned int i, j;
4701  	unsigned int pipe_in_use = 0;
4702  	bool subvp_in_use = false;
4703  	bool odm_in_use = false;
4704  
4705  	/* check current pipes in use*/
4706  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4707  		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4708  
4709  		if (pipe->plane_state)
4710  			pipe_in_use++;
4711  	}
4712  
4713  	/* If SubVP is enabled and we are adding or removing planes from any main subvp
4714  	 * pipe, we must use the minimal transition.
4715  	 */
4716  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4717  		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4718  
4719  		if (pipe->stream && dc_state_get_pipe_subvp_type(dc->current_state, pipe) == SUBVP_PHANTOM) {
4720  			subvp_in_use = true;
4721  			break;
4722  		}
4723  	}
4724  
4725  	/* If ODM is enabled and we are adding or removing planes from any ODM
4726  	 * pipe, we must use the minimal transition.
4727  	 */
4728  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4729  		struct pipe_ctx *pipe = &transition_base_context->res_ctx.pipe_ctx[i];
4730  
4731  		if (resource_is_pipe_type(pipe, OTG_MASTER)) {
4732  			odm_in_use = resource_get_odm_slice_count(pipe) > 1;
4733  			break;
4734  		}
4735  	}
4736  
4737  	/* When the OS add a new surface if we have been used all of pipes with odm combine
4738  	 * and mpc split feature, it need use commit_minimal_transition_state to transition safely.
4739  	 * After OS exit MPO, it will back to use odm and mpc split with all of pipes, we need
4740  	 * call it again. Otherwise return true to skip.
4741  	 *
4742  	 * Reduce the scenarios to use dc_commit_state_no_check in the stage of flip. Especially
4743  	 * enter/exit MPO when DCN still have enough resources.
4744  	 */
4745  	if (pipe_in_use != dc->res_pool->pipe_count && !subvp_in_use && !odm_in_use)
4746  		return true;
4747  
4748  	DC_LOG_DC("%s base = %s state, reason = %s\n", __func__,
4749  			dc->current_state == transition_base_context ? "current" : "new",
4750  			subvp_in_use ? "Subvp In Use" :
4751  			odm_in_use ? "ODM in Use" :
4752  			dc->debug.pipe_split_policy != MPC_SPLIT_AVOID ? "MPC in Use" :
4753  			"Unknown");
4754  
4755  	dc_state_retain(transition_base_context);
4756  	transition_context = create_minimal_transition_state(dc,
4757  			transition_base_context, &policy);
4758  	if (transition_context) {
4759  		ret = dc_commit_state_no_check(dc, transition_context);
4760  		release_minimal_transition_state(dc, transition_context, transition_base_context, &policy);
4761  	}
4762  	dc_state_release(transition_base_context);
4763  
4764  	if (ret != DC_OK) {
4765  		/* this should never happen */
4766  		BREAK_TO_DEBUGGER();
4767  		return false;
4768  	}
4769  
4770  	/* force full surface update */
4771  	for (i = 0; i < dc->current_state->stream_count; i++) {
4772  		for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
4773  			dc->current_state->stream_status[i].plane_states[j]->update_flags.raw = 0xFFFFFFFF;
4774  		}
4775  	}
4776  
4777  	return true;
4778  }
4779  
populate_fast_updates(struct dc_fast_update * fast_update,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update)4780  void populate_fast_updates(struct dc_fast_update *fast_update,
4781  		struct dc_surface_update *srf_updates,
4782  		int surface_count,
4783  		struct dc_stream_update *stream_update)
4784  {
4785  	int i = 0;
4786  
4787  	if (stream_update) {
4788  		fast_update[0].out_transfer_func = stream_update->out_transfer_func;
4789  		fast_update[0].output_csc_transform = stream_update->output_csc_transform;
4790  	} else {
4791  		fast_update[0].out_transfer_func = NULL;
4792  		fast_update[0].output_csc_transform = NULL;
4793  	}
4794  
4795  	for (i = 0; i < surface_count; i++) {
4796  		fast_update[i].flip_addr = srf_updates[i].flip_addr;
4797  		fast_update[i].gamma = srf_updates[i].gamma;
4798  		fast_update[i].gamut_remap_matrix = srf_updates[i].gamut_remap_matrix;
4799  		fast_update[i].input_csc_color_matrix = srf_updates[i].input_csc_color_matrix;
4800  		fast_update[i].coeff_reduction_factor = srf_updates[i].coeff_reduction_factor;
4801  		fast_update[i].cursor_csc_color_matrix = srf_updates[i].cursor_csc_color_matrix;
4802  	}
4803  }
4804  
fast_updates_exist(struct dc_fast_update * fast_update,int surface_count)4805  static bool fast_updates_exist(struct dc_fast_update *fast_update, int surface_count)
4806  {
4807  	int i;
4808  
4809  	if (fast_update[0].out_transfer_func ||
4810  		fast_update[0].output_csc_transform)
4811  		return true;
4812  
4813  	for (i = 0; i < surface_count; i++) {
4814  		if (fast_update[i].flip_addr ||
4815  				fast_update[i].gamma ||
4816  				fast_update[i].gamut_remap_matrix ||
4817  				fast_update[i].input_csc_color_matrix ||
4818  				fast_update[i].cursor_csc_color_matrix ||
4819  				fast_update[i].coeff_reduction_factor)
4820  			return true;
4821  	}
4822  
4823  	return false;
4824  }
4825  
fast_nonaddr_updates_exist(struct dc_fast_update * fast_update,int surface_count)4826  bool fast_nonaddr_updates_exist(struct dc_fast_update *fast_update, int surface_count)
4827  {
4828  	int i;
4829  
4830  	if (fast_update[0].out_transfer_func ||
4831  		fast_update[0].output_csc_transform)
4832  		return true;
4833  
4834  	for (i = 0; i < surface_count; i++) {
4835  		if (fast_update[i].input_csc_color_matrix ||
4836  				fast_update[i].gamma ||
4837  				fast_update[i].gamut_remap_matrix ||
4838  				fast_update[i].coeff_reduction_factor ||
4839  				fast_update[i].cursor_csc_color_matrix)
4840  			return true;
4841  	}
4842  
4843  	return false;
4844  }
4845  
full_update_required(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update,struct dc_stream_state * stream)4846  static bool full_update_required(struct dc *dc,
4847  		struct dc_surface_update *srf_updates,
4848  		int surface_count,
4849  		struct dc_stream_update *stream_update,
4850  		struct dc_stream_state *stream)
4851  {
4852  
4853  	int i;
4854  	struct dc_stream_status *stream_status;
4855  	const struct dc_state *context = dc->current_state;
4856  
4857  	for (i = 0; i < surface_count; i++) {
4858  		if (srf_updates &&
4859  				(srf_updates[i].plane_info ||
4860  				srf_updates[i].scaling_info ||
4861  				(srf_updates[i].hdr_mult.value &&
4862  				srf_updates[i].hdr_mult.value != srf_updates->surface->hdr_mult.value) ||
4863  				(srf_updates[i].sdr_white_level_nits &&
4864  				srf_updates[i].sdr_white_level_nits != srf_updates->surface->sdr_white_level_nits) ||
4865  				srf_updates[i].in_transfer_func ||
4866  				srf_updates[i].func_shaper ||
4867  				srf_updates[i].lut3d_func ||
4868  				srf_updates[i].surface->force_full_update ||
4869  				(srf_updates[i].flip_addr &&
4870  				srf_updates[i].flip_addr->address.tmz_surface != srf_updates[i].surface->address.tmz_surface) ||
4871  				(srf_updates[i].cm2_params &&
4872  				 (srf_updates[i].cm2_params->component_settings.shaper_3dlut_setting != srf_updates[i].surface->mcm_shaper_3dlut_setting ||
4873  				  srf_updates[i].cm2_params->component_settings.lut1d_enable != srf_updates[i].surface->mcm_lut1d_enable)) ||
4874  				!is_surface_in_context(context, srf_updates[i].surface)))
4875  			return true;
4876  	}
4877  
4878  	if (stream_update &&
4879  			(((stream_update->src.height != 0 && stream_update->src.width != 0) ||
4880  			(stream_update->dst.height != 0 && stream_update->dst.width != 0) ||
4881  			stream_update->integer_scaling_update) ||
4882  			stream_update->hdr_static_metadata ||
4883  			stream_update->abm_level ||
4884  			stream_update->periodic_interrupt ||
4885  			stream_update->vrr_infopacket ||
4886  			stream_update->vsc_infopacket ||
4887  			stream_update->vsp_infopacket ||
4888  			stream_update->hfvsif_infopacket ||
4889  			stream_update->vtem_infopacket ||
4890  			stream_update->adaptive_sync_infopacket ||
4891  			stream_update->dpms_off ||
4892  			stream_update->allow_freesync ||
4893  			stream_update->vrr_active_variable ||
4894  			stream_update->vrr_active_fixed ||
4895  			stream_update->gamut_remap ||
4896  			stream_update->output_color_space ||
4897  			stream_update->dither_option ||
4898  			stream_update->wb_update ||
4899  			stream_update->dsc_config ||
4900  			stream_update->mst_bw_update ||
4901  			stream_update->func_shaper ||
4902  			stream_update->lut3d_func ||
4903  			stream_update->pending_test_pattern ||
4904  			stream_update->crtc_timing_adjust ||
4905  			stream_update->scaler_sharpener_update))
4906  		return true;
4907  
4908  	if (stream) {
4909  		stream_status = dc_stream_get_status(stream);
4910  		if (stream_status == NULL || stream_status->plane_count != surface_count)
4911  			return true;
4912  	}
4913  	if (dc->idle_optimizations_allowed)
4914  		return true;
4915  
4916  	return false;
4917  }
4918  
fast_update_only(struct dc * dc,struct dc_fast_update * fast_update,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_update * stream_update,struct dc_stream_state * stream)4919  static bool fast_update_only(struct dc *dc,
4920  		struct dc_fast_update *fast_update,
4921  		struct dc_surface_update *srf_updates,
4922  		int surface_count,
4923  		struct dc_stream_update *stream_update,
4924  		struct dc_stream_state *stream)
4925  {
4926  	return fast_updates_exist(fast_update, surface_count)
4927  			&& !full_update_required(dc, srf_updates, surface_count, stream_update, stream);
4928  }
4929  
update_planes_and_stream_v1(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,struct dc_state * state)4930  static bool update_planes_and_stream_v1(struct dc *dc,
4931  		struct dc_surface_update *srf_updates, int surface_count,
4932  		struct dc_stream_state *stream,
4933  		struct dc_stream_update *stream_update,
4934  		struct dc_state *state)
4935  {
4936  	const struct dc_stream_status *stream_status;
4937  	enum surface_update_type update_type;
4938  	struct dc_state *context;
4939  	struct dc_context *dc_ctx = dc->ctx;
4940  	int i, j;
4941  	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
4942  
4943  	dc_exit_ips_for_hw_access(dc);
4944  
4945  	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
4946  	stream_status = dc_stream_get_status(stream);
4947  	context = dc->current_state;
4948  
4949  	update_type = dc_check_update_surfaces_for_stream(
4950  				dc, srf_updates, surface_count, stream_update, stream_status);
4951  	/* It is possible to receive a flip for one plane while there are multiple flip_immediate planes in the same stream.
4952  	 * E.g. Desktop and MPO plane are flip_immediate but only the MPO plane received a flip
4953  	 * Force the other flip_immediate planes to flip so GSL doesn't wait for a flip that won't come.
4954  	 */
4955  	force_immediate_gsl_plane_flip(dc, srf_updates, surface_count);
4956  
4957  	if (update_type >= UPDATE_TYPE_FULL) {
4958  
4959  		/* initialize scratch memory for building context */
4960  		context = dc_state_create_copy(state);
4961  		if (context == NULL) {
4962  			DC_ERROR("Failed to allocate new validate context!\n");
4963  			return false;
4964  		}
4965  
4966  		for (i = 0; i < dc->res_pool->pipe_count; i++) {
4967  			struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
4968  			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
4969  
4970  			if (new_pipe->plane_state && new_pipe->plane_state != old_pipe->plane_state)
4971  				new_pipe->plane_state->force_full_update = true;
4972  		}
4973  	} else if (update_type == UPDATE_TYPE_FAST) {
4974  		/*
4975  		 * Previous frame finished and HW is ready for optimization.
4976  		 */
4977  		dc_post_update_surfaces_to_stream(dc);
4978  	}
4979  
4980  	for (i = 0; i < surface_count; i++) {
4981  		struct dc_plane_state *surface = srf_updates[i].surface;
4982  
4983  		copy_surface_update_to_plane(surface, &srf_updates[i]);
4984  
4985  		if (update_type >= UPDATE_TYPE_MED) {
4986  			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4987  				struct pipe_ctx *pipe_ctx =
4988  					&context->res_ctx.pipe_ctx[j];
4989  
4990  				if (pipe_ctx->plane_state != surface)
4991  					continue;
4992  
4993  				resource_build_scaling_params(pipe_ctx);
4994  			}
4995  		}
4996  	}
4997  
4998  	copy_stream_update_to_stream(dc, context, stream, stream_update);
4999  
5000  	if (update_type >= UPDATE_TYPE_FULL) {
5001  		if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false)) {
5002  			DC_ERROR("Mode validation failed for stream update!\n");
5003  			dc_state_release(context);
5004  			return false;
5005  		}
5006  	}
5007  
5008  	TRACE_DC_PIPE_STATE(pipe_ctx, i, MAX_PIPES);
5009  
5010  	if (fast_update_only(dc, fast_update, srf_updates, surface_count, stream_update, stream) &&
5011  			!dc->debug.enable_legacy_fast_update) {
5012  		commit_planes_for_stream_fast(dc,
5013  				srf_updates,
5014  				surface_count,
5015  				stream,
5016  				stream_update,
5017  				update_type,
5018  				context);
5019  	} else {
5020  		commit_planes_for_stream(
5021  				dc,
5022  				srf_updates,
5023  				surface_count,
5024  				stream,
5025  				stream_update,
5026  				update_type,
5027  				context);
5028  	}
5029  	/*update current_State*/
5030  	if (dc->current_state != context) {
5031  
5032  		struct dc_state *old = dc->current_state;
5033  
5034  		dc->current_state = context;
5035  		dc_state_release(old);
5036  
5037  		for (i = 0; i < dc->res_pool->pipe_count; i++) {
5038  			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
5039  
5040  			if (pipe_ctx->plane_state && pipe_ctx->stream == stream)
5041  				pipe_ctx->plane_state->force_full_update = false;
5042  		}
5043  	}
5044  
5045  	/* Legacy optimization path for DCE. */
5046  	if (update_type >= UPDATE_TYPE_FULL && dc_ctx->dce_version < DCE_VERSION_MAX) {
5047  		dc_post_update_surfaces_to_stream(dc);
5048  		TRACE_DCE_CLOCK_STATE(&context->bw_ctx.bw.dce);
5049  	}
5050  	return true;
5051  }
5052  
update_planes_and_stream_v2(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update)5053  static bool update_planes_and_stream_v2(struct dc *dc,
5054  		struct dc_surface_update *srf_updates, int surface_count,
5055  		struct dc_stream_state *stream,
5056  		struct dc_stream_update *stream_update)
5057  {
5058  	struct dc_state *context;
5059  	enum surface_update_type update_type;
5060  	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
5061  
5062  	/* In cases where MPO and split or ODM are used transitions can
5063  	 * cause underflow. Apply stream configuration with minimal pipe
5064  	 * split first to avoid unsupported transitions for active pipes.
5065  	 */
5066  	bool force_minimal_pipe_splitting = 0;
5067  	bool is_plane_addition = 0;
5068  	bool is_fast_update_only;
5069  
5070  	populate_fast_updates(fast_update, srf_updates, surface_count, stream_update);
5071  	is_fast_update_only = fast_update_only(dc, fast_update, srf_updates,
5072  			surface_count, stream_update, stream);
5073  	force_minimal_pipe_splitting = could_mpcc_tree_change_for_active_pipes(
5074  			dc,
5075  			stream,
5076  			srf_updates,
5077  			surface_count,
5078  			&is_plane_addition);
5079  
5080  	/* on plane addition, minimal state is the current one */
5081  	if (force_minimal_pipe_splitting && is_plane_addition &&
5082  		!commit_minimal_transition_state(dc, dc->current_state))
5083  		return false;
5084  
5085  	if (!update_planes_and_stream_state(
5086  			dc,
5087  			srf_updates,
5088  			surface_count,
5089  			stream,
5090  			stream_update,
5091  			&update_type,
5092  			&context))
5093  		return false;
5094  
5095  	/* on plane removal, minimal state is the new one */
5096  	if (force_minimal_pipe_splitting && !is_plane_addition) {
5097  		if (!commit_minimal_transition_state(dc, context)) {
5098  			dc_state_release(context);
5099  			return false;
5100  		}
5101  		update_type = UPDATE_TYPE_FULL;
5102  	}
5103  
5104  	if (dc->hwss.is_pipe_topology_transition_seamless &&
5105  			!dc->hwss.is_pipe_topology_transition_seamless(
5106  					dc, dc->current_state, context))
5107  		commit_minimal_transition_state_in_dc_update(dc, context, stream,
5108  				srf_updates, surface_count);
5109  
5110  	if (is_fast_update_only && !dc->debug.enable_legacy_fast_update) {
5111  		commit_planes_for_stream_fast(dc,
5112  				srf_updates,
5113  				surface_count,
5114  				stream,
5115  				stream_update,
5116  				update_type,
5117  				context);
5118  	} else {
5119  		if (!stream_update &&
5120  				dc->hwss.is_pipe_topology_transition_seamless &&
5121  				!dc->hwss.is_pipe_topology_transition_seamless(
5122  						dc, dc->current_state, context)) {
5123  			DC_LOG_ERROR("performing non-seamless pipe topology transition with surface only update!\n");
5124  			BREAK_TO_DEBUGGER();
5125  		}
5126  		commit_planes_for_stream(
5127  				dc,
5128  				srf_updates,
5129  				surface_count,
5130  				stream,
5131  				stream_update,
5132  				update_type,
5133  				context);
5134  	}
5135  	if (dc->current_state != context)
5136  		swap_and_release_current_context(dc, context, stream);
5137  	return true;
5138  }
5139  
commit_planes_and_stream_update_on_current_context(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type update_type)5140  static void commit_planes_and_stream_update_on_current_context(struct dc *dc,
5141  		struct dc_surface_update *srf_updates, int surface_count,
5142  		struct dc_stream_state *stream,
5143  		struct dc_stream_update *stream_update,
5144  		enum surface_update_type update_type)
5145  {
5146  	struct dc_fast_update fast_update[MAX_SURFACES] = {0};
5147  
5148  	ASSERT(update_type < UPDATE_TYPE_FULL);
5149  	populate_fast_updates(fast_update, srf_updates, surface_count,
5150  			stream_update);
5151  	if (fast_update_only(dc, fast_update, srf_updates, surface_count,
5152  			stream_update, stream) &&
5153  			!dc->debug.enable_legacy_fast_update)
5154  		commit_planes_for_stream_fast(dc,
5155  				srf_updates,
5156  				surface_count,
5157  				stream,
5158  				stream_update,
5159  				update_type,
5160  				dc->current_state);
5161  	else
5162  		commit_planes_for_stream(
5163  				dc,
5164  				srf_updates,
5165  				surface_count,
5166  				stream,
5167  				stream_update,
5168  				update_type,
5169  				dc->current_state);
5170  }
5171  
commit_planes_and_stream_update_with_new_context(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,enum surface_update_type update_type,struct dc_state * new_context)5172  static void commit_planes_and_stream_update_with_new_context(struct dc *dc,
5173  		struct dc_surface_update *srf_updates, int surface_count,
5174  		struct dc_stream_state *stream,
5175  		struct dc_stream_update *stream_update,
5176  		enum surface_update_type update_type,
5177  		struct dc_state *new_context)
5178  {
5179  	ASSERT(update_type >= UPDATE_TYPE_FULL);
5180  	if (!dc->hwss.is_pipe_topology_transition_seamless(dc,
5181  			dc->current_state, new_context))
5182  		/*
5183  		 * It is required by the feature design that all pipe topologies
5184  		 * using extra free pipes for power saving purposes such as
5185  		 * dynamic ODM or SubVp shall only be enabled when it can be
5186  		 * transitioned seamlessly to AND from its minimal transition
5187  		 * state. A minimal transition state is defined as the same dc
5188  		 * state but with all power saving features disabled. So it uses
5189  		 * the minimum pipe topology. When we can't seamlessly
5190  		 * transition from state A to state B, we will insert the
5191  		 * minimal transition state A' or B' in between so seamless
5192  		 * transition between A and B can be made possible.
5193  		 */
5194  		commit_minimal_transition_state_in_dc_update(dc, new_context,
5195  				stream, srf_updates, surface_count);
5196  
5197  	commit_planes_for_stream(
5198  			dc,
5199  			srf_updates,
5200  			surface_count,
5201  			stream,
5202  			stream_update,
5203  			update_type,
5204  			new_context);
5205  }
5206  
update_planes_and_stream_v3(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update)5207  static bool update_planes_and_stream_v3(struct dc *dc,
5208  		struct dc_surface_update *srf_updates, int surface_count,
5209  		struct dc_stream_state *stream,
5210  		struct dc_stream_update *stream_update)
5211  {
5212  	struct dc_state *new_context;
5213  	enum surface_update_type update_type;
5214  
5215  	/*
5216  	 * When this function returns true and new_context is not equal to
5217  	 * current state, the function allocates and validates a new dc state
5218  	 * and assigns it to new_context. The function expects that the caller
5219  	 * is responsible to free this memory when new_context is no longer
5220  	 * used. We swap current with new context and free current instead. So
5221  	 * new_context's memory will live until the next full update after it is
5222  	 * replaced by a newer context. Refer to the use of
5223  	 * swap_and_free_current_context below.
5224  	 */
5225  	if (!update_planes_and_stream_state(dc, srf_updates, surface_count,
5226  				stream, stream_update, &update_type,
5227  				&new_context))
5228  		return false;
5229  
5230  	if (new_context == dc->current_state) {
5231  		commit_planes_and_stream_update_on_current_context(dc,
5232  				srf_updates, surface_count, stream,
5233  				stream_update, update_type);
5234  	} else {
5235  		commit_planes_and_stream_update_with_new_context(dc,
5236  				srf_updates, surface_count, stream,
5237  				stream_update, update_type, new_context);
5238  		swap_and_release_current_context(dc, new_context, stream);
5239  	}
5240  
5241  	return true;
5242  }
5243  
clear_update_flags(struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream)5244  static void clear_update_flags(struct dc_surface_update *srf_updates,
5245  	int surface_count, struct dc_stream_state *stream)
5246  {
5247  	int i;
5248  
5249  	if (stream)
5250  		stream->update_flags.raw = 0;
5251  
5252  	for (i = 0; i < surface_count; i++)
5253  		if (srf_updates[i].surface)
5254  			srf_updates[i].surface->update_flags.raw = 0;
5255  }
5256  
dc_update_planes_and_stream(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update)5257  bool dc_update_planes_and_stream(struct dc *dc,
5258  		struct dc_surface_update *srf_updates, int surface_count,
5259  		struct dc_stream_state *stream,
5260  		struct dc_stream_update *stream_update)
5261  {
5262  	bool ret = false;
5263  
5264  	dc_exit_ips_for_hw_access(dc);
5265  	/*
5266  	 * update planes and stream version 3 separates FULL and FAST updates
5267  	 * to their own sequences. It aims to clean up frequent checks for
5268  	 * update type resulting unnecessary branching in logic flow. It also
5269  	 * adds a new commit minimal transition sequence, which detects the need
5270  	 * for minimal transition based on the actual comparison of current and
5271  	 * new states instead of "predicting" it based on per feature software
5272  	 * policy.i.e could_mpcc_tree_change_for_active_pipes. The new commit
5273  	 * minimal transition sequence is made universal to any power saving
5274  	 * features that would use extra free pipes such as Dynamic ODM/MPC
5275  	 * Combine, MPO or SubVp. Therefore there is no longer a need to
5276  	 * specially handle compatibility problems with transitions among those
5277  	 * features as they are now transparent to the new sequence.
5278  	 */
5279  	if (dc->ctx->dce_version >= DCN_VERSION_4_01)
5280  		ret = update_planes_and_stream_v3(dc, srf_updates,
5281  				surface_count, stream, stream_update);
5282  	else
5283  		ret = update_planes_and_stream_v2(dc, srf_updates,
5284  			surface_count, stream, stream_update);
5285  
5286  	if (ret)
5287  		clear_update_flags(srf_updates, surface_count, stream);
5288  
5289  	return ret;
5290  }
5291  
dc_commit_updates_for_stream(struct dc * dc,struct dc_surface_update * srf_updates,int surface_count,struct dc_stream_state * stream,struct dc_stream_update * stream_update,struct dc_state * state)5292  void dc_commit_updates_for_stream(struct dc *dc,
5293  		struct dc_surface_update *srf_updates,
5294  		int surface_count,
5295  		struct dc_stream_state *stream,
5296  		struct dc_stream_update *stream_update,
5297  		struct dc_state *state)
5298  {
5299  	bool ret = false;
5300  
5301  	dc_exit_ips_for_hw_access(dc);
5302  	/* TODO: Since change commit sequence can have a huge impact,
5303  	 * we decided to only enable it for DCN3x. However, as soon as
5304  	 * we get more confident about this change we'll need to enable
5305  	 * the new sequence for all ASICs.
5306  	 */
5307  	if (dc->ctx->dce_version >= DCN_VERSION_4_01) {
5308  		ret = update_planes_and_stream_v3(dc, srf_updates, surface_count,
5309  				stream, stream_update);
5310  	} else if (dc->ctx->dce_version >= DCN_VERSION_3_2) {
5311  		ret = update_planes_and_stream_v2(dc, srf_updates, surface_count,
5312  				stream, stream_update);
5313  	} else
5314  		ret = update_planes_and_stream_v1(dc, srf_updates, surface_count, stream,
5315  				stream_update, state);
5316  
5317  	if (ret)
5318  		clear_update_flags(srf_updates, surface_count, stream);
5319  }
5320  
dc_get_current_stream_count(struct dc * dc)5321  uint8_t dc_get_current_stream_count(struct dc *dc)
5322  {
5323  	return dc->current_state->stream_count;
5324  }
5325  
dc_get_stream_at_index(struct dc * dc,uint8_t i)5326  struct dc_stream_state *dc_get_stream_at_index(struct dc *dc, uint8_t i)
5327  {
5328  	if (i < dc->current_state->stream_count)
5329  		return dc->current_state->streams[i];
5330  	return NULL;
5331  }
5332  
dc_interrupt_to_irq_source(struct dc * dc,uint32_t src_id,uint32_t ext_id)5333  enum dc_irq_source dc_interrupt_to_irq_source(
5334  		struct dc *dc,
5335  		uint32_t src_id,
5336  		uint32_t ext_id)
5337  {
5338  	return dal_irq_service_to_irq_source(dc->res_pool->irqs, src_id, ext_id);
5339  }
5340  
5341  /*
5342   * dc_interrupt_set() - Enable/disable an AMD hw interrupt source
5343   */
dc_interrupt_set(struct dc * dc,enum dc_irq_source src,bool enable)5344  bool dc_interrupt_set(struct dc *dc, enum dc_irq_source src, bool enable)
5345  {
5346  
5347  	if (dc == NULL)
5348  		return false;
5349  
5350  	return dal_irq_service_set(dc->res_pool->irqs, src, enable);
5351  }
5352  
dc_interrupt_ack(struct dc * dc,enum dc_irq_source src)5353  void dc_interrupt_ack(struct dc *dc, enum dc_irq_source src)
5354  {
5355  	dal_irq_service_ack(dc->res_pool->irqs, src);
5356  }
5357  
dc_power_down_on_boot(struct dc * dc)5358  void dc_power_down_on_boot(struct dc *dc)
5359  {
5360  	if (dc->ctx->dce_environment != DCE_ENV_VIRTUAL_HW &&
5361  	    dc->hwss.power_down_on_boot) {
5362  		if (dc->caps.ips_support)
5363  			dc_exit_ips_for_hw_access(dc);
5364  		dc->hwss.power_down_on_boot(dc);
5365  	}
5366  }
5367  
dc_set_power_state(struct dc * dc,enum dc_acpi_cm_power_state power_state)5368  void dc_set_power_state(struct dc *dc, enum dc_acpi_cm_power_state power_state)
5369  {
5370  	if (!dc->current_state)
5371  		return;
5372  
5373  	switch (power_state) {
5374  	case DC_ACPI_CM_POWER_STATE_D0:
5375  		dc_state_construct(dc, dc->current_state);
5376  
5377  		dc_exit_ips_for_hw_access(dc);
5378  
5379  		dc_z10_restore(dc);
5380  
5381  		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5382  
5383  		dc->hwss.init_hw(dc);
5384  
5385  		if (dc->hwss.init_sys_ctx != NULL &&
5386  			dc->vm_pa_config.valid) {
5387  			dc->hwss.init_sys_ctx(dc->hwseq, dc, &dc->vm_pa_config);
5388  		}
5389  		break;
5390  	default:
5391  		ASSERT(dc->current_state->stream_count == 0);
5392  		dc_dmub_srv_notify_fw_dc_power_state(dc->ctx->dmub_srv, power_state);
5393  
5394  		dc_state_destruct(dc->current_state);
5395  
5396  		break;
5397  	}
5398  }
5399  
dc_resume(struct dc * dc)5400  void dc_resume(struct dc *dc)
5401  {
5402  	uint32_t i;
5403  
5404  	for (i = 0; i < dc->link_count; i++)
5405  		dc->link_srv->resume(dc->links[i]);
5406  }
5407  
dc_is_dmcu_initialized(struct dc * dc)5408  bool dc_is_dmcu_initialized(struct dc *dc)
5409  {
5410  	struct dmcu *dmcu = dc->res_pool->dmcu;
5411  
5412  	if (dmcu)
5413  		return dmcu->funcs->is_dmcu_initialized(dmcu);
5414  	return false;
5415  }
5416  
get_clock_requirements_for_state(struct dc_state * state,struct AsicStateEx * info)5417  void get_clock_requirements_for_state(struct dc_state *state, struct AsicStateEx *info)
5418  {
5419  	info->displayClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dispclk_khz;
5420  	info->engineClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_khz;
5421  	info->memoryClock				= (unsigned int)state->bw_ctx.bw.dcn.clk.dramclk_khz;
5422  	info->maxSupportedDppClock		= (unsigned int)state->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
5423  	info->dppClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.dppclk_khz;
5424  	info->socClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.socclk_khz;
5425  	info->dcfClockDeepSleep			= (unsigned int)state->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz;
5426  	info->fClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.fclk_khz;
5427  	info->phyClock					= (unsigned int)state->bw_ctx.bw.dcn.clk.phyclk_khz;
5428  }
dc_set_clock(struct dc * dc,enum dc_clock_type clock_type,uint32_t clk_khz,uint32_t stepping)5429  enum dc_status dc_set_clock(struct dc *dc, enum dc_clock_type clock_type, uint32_t clk_khz, uint32_t stepping)
5430  {
5431  	if (dc->hwss.set_clock)
5432  		return dc->hwss.set_clock(dc, clock_type, clk_khz, stepping);
5433  	return DC_ERROR_UNEXPECTED;
5434  }
dc_get_clock(struct dc * dc,enum dc_clock_type clock_type,struct dc_clock_config * clock_cfg)5435  void dc_get_clock(struct dc *dc, enum dc_clock_type clock_type, struct dc_clock_config *clock_cfg)
5436  {
5437  	if (dc->hwss.get_clock)
5438  		dc->hwss.get_clock(dc, clock_type, clock_cfg);
5439  }
5440  
5441  /* enable/disable eDP PSR without specify stream for eDP */
dc_set_psr_allow_active(struct dc * dc,bool enable)5442  bool dc_set_psr_allow_active(struct dc *dc, bool enable)
5443  {
5444  	int i;
5445  	bool allow_active;
5446  
5447  	for (i = 0; i < dc->current_state->stream_count ; i++) {
5448  		struct dc_link *link;
5449  		struct dc_stream_state *stream = dc->current_state->streams[i];
5450  
5451  		link = stream->link;
5452  		if (!link)
5453  			continue;
5454  
5455  		if (link->psr_settings.psr_feature_enabled) {
5456  			if (enable && !link->psr_settings.psr_allow_active) {
5457  				allow_active = true;
5458  				if (!dc_link_set_psr_allow_active(link, &allow_active, false, false, NULL))
5459  					return false;
5460  			} else if (!enable && link->psr_settings.psr_allow_active) {
5461  				allow_active = false;
5462  				if (!dc_link_set_psr_allow_active(link, &allow_active, true, false, NULL))
5463  					return false;
5464  			}
5465  		}
5466  	}
5467  
5468  	return true;
5469  }
5470  
5471  /* enable/disable eDP Replay without specify stream for eDP */
dc_set_replay_allow_active(struct dc * dc,bool active)5472  bool dc_set_replay_allow_active(struct dc *dc, bool active)
5473  {
5474  	int i;
5475  	bool allow_active;
5476  
5477  	for (i = 0; i < dc->current_state->stream_count; i++) {
5478  		struct dc_link *link;
5479  		struct dc_stream_state *stream = dc->current_state->streams[i];
5480  
5481  		link = stream->link;
5482  		if (!link)
5483  			continue;
5484  
5485  		if (link->replay_settings.replay_feature_enabled) {
5486  			if (active && !link->replay_settings.replay_allow_active) {
5487  				allow_active = true;
5488  				if (!dc_link_set_replay_allow_active(link, &allow_active,
5489  					false, false, NULL))
5490  					return false;
5491  			} else if (!active && link->replay_settings.replay_allow_active) {
5492  				allow_active = false;
5493  				if (!dc_link_set_replay_allow_active(link, &allow_active,
5494  					true, false, NULL))
5495  					return false;
5496  			}
5497  		}
5498  	}
5499  
5500  	return true;
5501  }
5502  
5503  /* set IPS disable state */
dc_set_ips_disable(struct dc * dc,unsigned int disable_ips)5504  bool dc_set_ips_disable(struct dc *dc, unsigned int disable_ips)
5505  {
5506  	dc_exit_ips_for_hw_access(dc);
5507  
5508  	dc->config.disable_ips = disable_ips;
5509  
5510  	return true;
5511  }
5512  
dc_allow_idle_optimizations_internal(struct dc * dc,bool allow,char const * caller_name)5513  void dc_allow_idle_optimizations_internal(struct dc *dc, bool allow, char const *caller_name)
5514  {
5515  	int idle_fclk_khz = 0, idle_dramclk_khz = 0, i = 0;
5516  	enum mall_stream_type subvp_pipe_type[MAX_PIPES] = {0};
5517  	struct pipe_ctx *pipe = NULL;
5518  	struct dc_state *context = dc->current_state;
5519  
5520  	if (dc->debug.disable_idle_power_optimizations) {
5521  		DC_LOG_DEBUG("%s: disabled\n", __func__);
5522  		return;
5523  	}
5524  
5525  	if (allow != dc->idle_optimizations_allowed)
5526  		DC_LOG_IPS("%s: allow_idle old=%d new=%d (caller=%s)\n", __func__,
5527  			   dc->idle_optimizations_allowed, allow, caller_name);
5528  
5529  	if (dc->caps.ips_support && (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5530  		return;
5531  
5532  	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->is_smu_present)
5533  		if (!dc->clk_mgr->funcs->is_smu_present(dc->clk_mgr))
5534  			return;
5535  
5536  	if (allow == dc->idle_optimizations_allowed)
5537  		return;
5538  
5539  	if (dc->hwss.apply_idle_power_optimizations && dc->clk_mgr != NULL &&
5540  	    dc->hwss.apply_idle_power_optimizations(dc, allow)) {
5541  		dc->idle_optimizations_allowed = allow;
5542  		DC_LOG_DEBUG("%s: %s\n", __func__, allow ? "enabled" : "disabled");
5543  	}
5544  
5545  	// log idle clocks and sub vp pipe types at idle optimization time
5546  	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->get_hard_min_fclk)
5547  		idle_fclk_khz = dc->clk_mgr->funcs->get_hard_min_fclk(dc->clk_mgr);
5548  
5549  	if (dc->clk_mgr != NULL && dc->clk_mgr->funcs->get_hard_min_memclk)
5550  		idle_dramclk_khz = dc->clk_mgr->funcs->get_hard_min_memclk(dc->clk_mgr);
5551  
5552  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5553  		pipe = &context->res_ctx.pipe_ctx[i];
5554  		subvp_pipe_type[i] = dc_state_get_pipe_subvp_type(context, pipe);
5555  	}
5556  
5557  	DC_LOG_DC("%s: allow_idle=%d\n HardMinUClk_Khz=%d HardMinDramclk_Khz=%d\n Pipe_0=%d Pipe_1=%d Pipe_2=%d Pipe_3=%d Pipe_4=%d Pipe_5=%d (caller=%s)\n",
5558  			__func__, allow, idle_fclk_khz, idle_dramclk_khz, subvp_pipe_type[0], subvp_pipe_type[1], subvp_pipe_type[2],
5559  			subvp_pipe_type[3], subvp_pipe_type[4], subvp_pipe_type[5], caller_name);
5560  
5561  }
5562  
dc_exit_ips_for_hw_access_internal(struct dc * dc,const char * caller_name)5563  void dc_exit_ips_for_hw_access_internal(struct dc *dc, const char *caller_name)
5564  {
5565  	if (dc->caps.ips_support)
5566  		dc_allow_idle_optimizations_internal(dc, false, caller_name);
5567  }
5568  
dc_dmub_is_ips_idle_state(struct dc * dc)5569  bool dc_dmub_is_ips_idle_state(struct dc *dc)
5570  {
5571  	if (dc->debug.disable_idle_power_optimizations)
5572  		return false;
5573  
5574  	if (!dc->caps.ips_support || (dc->config.disable_ips == DMUB_IPS_DISABLE_ALL))
5575  		return false;
5576  
5577  	if (!dc->ctx->dmub_srv)
5578  		return false;
5579  
5580  	return dc->ctx->dmub_srv->idle_allowed;
5581  }
5582  
5583  /* set min and max memory clock to lowest and highest DPM level, respectively */
dc_unlock_memory_clock_frequency(struct dc * dc)5584  void dc_unlock_memory_clock_frequency(struct dc *dc)
5585  {
5586  	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5587  		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, false);
5588  
5589  	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5590  		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5591  }
5592  
5593  /* set min memory clock to the min required for current mode, max to maxDPM */
dc_lock_memory_clock_frequency(struct dc * dc)5594  void dc_lock_memory_clock_frequency(struct dc *dc)
5595  {
5596  	if (dc->clk_mgr->funcs->get_memclk_states_from_smu)
5597  		dc->clk_mgr->funcs->get_memclk_states_from_smu(dc->clk_mgr);
5598  
5599  	if (dc->clk_mgr->funcs->set_hard_min_memclk)
5600  		dc->clk_mgr->funcs->set_hard_min_memclk(dc->clk_mgr, true);
5601  
5602  	if (dc->clk_mgr->funcs->set_hard_max_memclk)
5603  		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
5604  }
5605  
blank_and_force_memclk(struct dc * dc,bool apply,unsigned int memclk_mhz)5606  static void blank_and_force_memclk(struct dc *dc, bool apply, unsigned int memclk_mhz)
5607  {
5608  	struct dc_state *context = dc->current_state;
5609  	struct hubp *hubp;
5610  	struct pipe_ctx *pipe;
5611  	int i;
5612  
5613  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5614  		pipe = &context->res_ctx.pipe_ctx[i];
5615  
5616  		if (pipe->stream != NULL) {
5617  			dc->hwss.disable_pixel_data(dc, pipe, true);
5618  
5619  			// wait for double buffer
5620  			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5621  			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
5622  			pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VACTIVE);
5623  
5624  			hubp = pipe->plane_res.hubp;
5625  			hubp->funcs->set_blank_regs(hubp, true);
5626  		}
5627  	}
5628  	if (dc->clk_mgr->funcs->set_max_memclk)
5629  		dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, memclk_mhz);
5630  	if (dc->clk_mgr->funcs->set_min_memclk)
5631  		dc->clk_mgr->funcs->set_min_memclk(dc->clk_mgr, memclk_mhz);
5632  
5633  	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5634  		pipe = &context->res_ctx.pipe_ctx[i];
5635  
5636  		if (pipe->stream != NULL) {
5637  			dc->hwss.disable_pixel_data(dc, pipe, false);
5638  
5639  			hubp = pipe->plane_res.hubp;
5640  			hubp->funcs->set_blank_regs(hubp, false);
5641  		}
5642  	}
5643  }
5644  
5645  
5646  /**
5647   * dc_enable_dcmode_clk_limit() - lower clocks in dc (battery) mode
5648   * @dc: pointer to dc of the dm calling this
5649   * @enable: True = transition to DC mode, false = transition back to AC mode
5650   *
5651   * Some SoCs define additional clock limits when in DC mode, DM should
5652   * invoke this function when the platform undergoes a power source transition
5653   * so DC can apply/unapply the limit. This interface may be disruptive to
5654   * the onscreen content.
5655   *
5656   * Context: Triggered by OS through DM interface, or manually by escape calls.
5657   * Need to hold a dclock when doing so.
5658   *
5659   * Return: none (void function)
5660   *
5661   */
dc_enable_dcmode_clk_limit(struct dc * dc,bool enable)5662  void dc_enable_dcmode_clk_limit(struct dc *dc, bool enable)
5663  {
5664  	unsigned int softMax = 0, maxDPM = 0, funcMin = 0, i;
5665  	bool p_state_change_support;
5666  
5667  	if (!dc->config.dc_mode_clk_limit_support)
5668  		return;
5669  
5670  	softMax = dc->clk_mgr->bw_params->dc_mode_softmax_memclk;
5671  	for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries; i++) {
5672  		if (dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz > maxDPM)
5673  			maxDPM = dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz;
5674  	}
5675  	funcMin = (dc->clk_mgr->clks.dramclk_khz + 999) / 1000;
5676  	p_state_change_support = dc->clk_mgr->clks.p_state_change_support;
5677  
5678  	if (enable && !dc->clk_mgr->dc_mode_softmax_enabled) {
5679  		if (p_state_change_support) {
5680  			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5681  				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, softMax);
5682  			// else: No-Op
5683  		} else {
5684  			if (funcMin <= softMax)
5685  				blank_and_force_memclk(dc, true, softMax);
5686  			// else: No-Op
5687  		}
5688  	} else if (!enable && dc->clk_mgr->dc_mode_softmax_enabled) {
5689  		if (p_state_change_support) {
5690  			if (funcMin <= softMax && dc->clk_mgr->funcs->set_max_memclk)
5691  				dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, maxDPM);
5692  			// else: No-Op
5693  		} else {
5694  			if (funcMin <= softMax)
5695  				blank_and_force_memclk(dc, true, maxDPM);
5696  			// else: No-Op
5697  		}
5698  	}
5699  	dc->clk_mgr->dc_mode_softmax_enabled = enable;
5700  }
dc_is_plane_eligible_for_idle_optimizations(struct dc * dc,unsigned int pitch,unsigned int height,enum surface_pixel_format format,struct dc_cursor_attributes * cursor_attr)5701  bool dc_is_plane_eligible_for_idle_optimizations(struct dc *dc,
5702  		unsigned int pitch,
5703  		unsigned int height,
5704  		enum surface_pixel_format format,
5705  		struct dc_cursor_attributes *cursor_attr)
5706  {
5707  	if (dc->hwss.does_plane_fit_in_mall && dc->hwss.does_plane_fit_in_mall(dc, pitch, height, format, cursor_attr))
5708  		return true;
5709  	return false;
5710  }
5711  
5712  /* cleanup on driver unload */
dc_hardware_release(struct dc * dc)5713  void dc_hardware_release(struct dc *dc)
5714  {
5715  	dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(dc);
5716  
5717  	if (dc->hwss.hardware_release)
5718  		dc->hwss.hardware_release(dc);
5719  }
5720  
dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc * dc)5721  void dc_mclk_switch_using_fw_based_vblank_stretch_shut_down(struct dc *dc)
5722  {
5723  	if (dc->current_state)
5724  		dc->current_state->bw_ctx.bw.dcn.clk.fw_based_mclk_switching_shut_down = true;
5725  }
5726  
5727  /**
5728   * dc_is_dmub_outbox_supported - Check if DMUB firmware support outbox notification
5729   *
5730   * @dc: [in] dc structure
5731   *
5732   * Checks whether DMUB FW supports outbox notifications, if supported DM
5733   * should register outbox interrupt prior to actually enabling interrupts
5734   * via dc_enable_dmub_outbox
5735   *
5736   * Return:
5737   * True if DMUB FW supports outbox notifications, False otherwise
5738   */
dc_is_dmub_outbox_supported(struct dc * dc)5739  bool dc_is_dmub_outbox_supported(struct dc *dc)
5740  {
5741  	if (!dc->caps.dmcub_support)
5742  		return false;
5743  
5744  	switch (dc->ctx->asic_id.chip_family) {
5745  
5746  	case FAMILY_YELLOW_CARP:
5747  		/* DCN31 B0 USB4 DPIA needs dmub notifications for interrupts */
5748  		if (dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0 &&
5749  		    !dc->debug.dpia_debug.bits.disable_dpia)
5750  			return true;
5751  	break;
5752  
5753  	case AMDGPU_FAMILY_GC_11_0_1:
5754  	case AMDGPU_FAMILY_GC_11_5_0:
5755  		if (!dc->debug.dpia_debug.bits.disable_dpia)
5756  			return true;
5757  	break;
5758  
5759  	default:
5760  		break;
5761  	}
5762  
5763  	/* dmub aux needs dmub notifications to be enabled */
5764  	return dc->debug.enable_dmub_aux_for_legacy_ddc;
5765  
5766  }
5767  
5768  /**
5769   * dc_enable_dmub_notifications - Check if dmub fw supports outbox
5770   *
5771   * @dc: [in] dc structure
5772   *
5773   * Calls dc_is_dmub_outbox_supported to check if dmub fw supports outbox
5774   * notifications. All DMs shall switch to dc_is_dmub_outbox_supported.  This
5775   * API shall be removed after switching.
5776   *
5777   * Return:
5778   * True if DMUB FW supports outbox notifications, False otherwise
5779   */
dc_enable_dmub_notifications(struct dc * dc)5780  bool dc_enable_dmub_notifications(struct dc *dc)
5781  {
5782  	return dc_is_dmub_outbox_supported(dc);
5783  }
5784  
5785  /**
5786   * dc_enable_dmub_outbox - Enables DMUB unsolicited notification
5787   *
5788   * @dc: [in] dc structure
5789   *
5790   * Enables DMUB unsolicited notifications to x86 via outbox.
5791   */
dc_enable_dmub_outbox(struct dc * dc)5792  void dc_enable_dmub_outbox(struct dc *dc)
5793  {
5794  	struct dc_context *dc_ctx = dc->ctx;
5795  
5796  	dmub_enable_outbox_notification(dc_ctx->dmub_srv);
5797  	DC_LOG_DC("%s: dmub outbox notifications enabled\n", __func__);
5798  }
5799  
5800  /**
5801   * dc_process_dmub_aux_transfer_async - Submits aux command to dmub via inbox message
5802   *                                      Sets port index appropriately for legacy DDC
5803   * @dc: dc structure
5804   * @link_index: link index
5805   * @payload: aux payload
5806   *
5807   * Returns: True if successful, False if failure
5808   */
dc_process_dmub_aux_transfer_async(struct dc * dc,uint32_t link_index,struct aux_payload * payload)5809  bool dc_process_dmub_aux_transfer_async(struct dc *dc,
5810  				uint32_t link_index,
5811  				struct aux_payload *payload)
5812  {
5813  	uint8_t action;
5814  	union dmub_rb_cmd cmd = {0};
5815  
5816  	ASSERT(payload->length <= 16);
5817  
5818  	cmd.dp_aux_access.header.type = DMUB_CMD__DP_AUX_ACCESS;
5819  	cmd.dp_aux_access.header.payload_bytes = 0;
5820  	/* For dpia, ddc_pin is set to NULL */
5821  	if (!dc->links[link_index]->ddc->ddc_pin)
5822  		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_DPIA;
5823  	else
5824  		cmd.dp_aux_access.aux_control.type = AUX_CHANNEL_LEGACY_DDC;
5825  
5826  	cmd.dp_aux_access.aux_control.instance = dc->links[link_index]->ddc_hw_inst;
5827  	cmd.dp_aux_access.aux_control.sw_crc_enabled = 0;
5828  	cmd.dp_aux_access.aux_control.timeout = 0;
5829  	cmd.dp_aux_access.aux_control.dpaux.address = payload->address;
5830  	cmd.dp_aux_access.aux_control.dpaux.is_i2c_over_aux = payload->i2c_over_aux;
5831  	cmd.dp_aux_access.aux_control.dpaux.length = payload->length;
5832  
5833  	/* set aux action */
5834  	if (payload->i2c_over_aux) {
5835  		if (payload->write) {
5836  			if (payload->mot)
5837  				action = DP_AUX_REQ_ACTION_I2C_WRITE_MOT;
5838  			else
5839  				action = DP_AUX_REQ_ACTION_I2C_WRITE;
5840  		} else {
5841  			if (payload->mot)
5842  				action = DP_AUX_REQ_ACTION_I2C_READ_MOT;
5843  			else
5844  				action = DP_AUX_REQ_ACTION_I2C_READ;
5845  			}
5846  	} else {
5847  		if (payload->write)
5848  			action = DP_AUX_REQ_ACTION_DPCD_WRITE;
5849  		else
5850  			action = DP_AUX_REQ_ACTION_DPCD_READ;
5851  	}
5852  
5853  	cmd.dp_aux_access.aux_control.dpaux.action = action;
5854  
5855  	if (payload->length && payload->write) {
5856  		memcpy(cmd.dp_aux_access.aux_control.dpaux.data,
5857  			payload->data,
5858  			payload->length
5859  			);
5860  	}
5861  
5862  	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
5863  
5864  	return true;
5865  }
5866  
get_link_index_from_dpia_port_index(const struct dc * dc,uint8_t dpia_port_index)5867  uint8_t get_link_index_from_dpia_port_index(const struct dc *dc,
5868  					    uint8_t dpia_port_index)
5869  {
5870  	uint8_t index, link_index = 0xFF;
5871  
5872  	for (index = 0; index < dc->link_count; index++) {
5873  		/* ddc_hw_inst has dpia port index for dpia links
5874  		 * and ddc instance for legacy links
5875  		 */
5876  		if (!dc->links[index]->ddc->ddc_pin) {
5877  			if (dc->links[index]->ddc_hw_inst == dpia_port_index) {
5878  				link_index = index;
5879  				break;
5880  			}
5881  		}
5882  	}
5883  	ASSERT(link_index != 0xFF);
5884  	return link_index;
5885  }
5886  
5887  /**
5888   * dc_process_dmub_set_config_async - Submits set_config command
5889   *
5890   * @dc: [in] dc structure
5891   * @link_index: [in] link_index: link index
5892   * @payload: [in] aux payload
5893   * @notify: [out] set_config immediate reply
5894   *
5895   * Submits set_config command to dmub via inbox message.
5896   *
5897   * Return:
5898   * True if successful, False if failure
5899   */
dc_process_dmub_set_config_async(struct dc * dc,uint32_t link_index,struct set_config_cmd_payload * payload,struct dmub_notification * notify)5900  bool dc_process_dmub_set_config_async(struct dc *dc,
5901  				uint32_t link_index,
5902  				struct set_config_cmd_payload *payload,
5903  				struct dmub_notification *notify)
5904  {
5905  	union dmub_rb_cmd cmd = {0};
5906  	bool is_cmd_complete = true;
5907  
5908  	/* prepare SET_CONFIG command */
5909  	cmd.set_config_access.header.type = DMUB_CMD__DPIA;
5910  	cmd.set_config_access.header.sub_type = DMUB_CMD__DPIA_SET_CONFIG_ACCESS;
5911  
5912  	cmd.set_config_access.set_config_control.instance = dc->links[link_index]->ddc_hw_inst;
5913  	cmd.set_config_access.set_config_control.cmd_pkt.msg_type = payload->msg_type;
5914  	cmd.set_config_access.set_config_control.cmd_pkt.msg_data = payload->msg_data;
5915  
5916  	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)) {
5917  		/* command is not processed by dmub */
5918  		notify->sc_status = SET_CONFIG_UNKNOWN_ERROR;
5919  		return is_cmd_complete;
5920  	}
5921  
5922  	/* command processed by dmub, if ret_status is 1, it is completed instantly */
5923  	if (cmd.set_config_access.header.ret_status == 1)
5924  		notify->sc_status = cmd.set_config_access.set_config_control.immed_status;
5925  	else
5926  		/* cmd pending, will receive notification via outbox */
5927  		is_cmd_complete = false;
5928  
5929  	return is_cmd_complete;
5930  }
5931  
5932  /**
5933   * dc_process_dmub_set_mst_slots - Submits MST solt allocation
5934   *
5935   * @dc: [in] dc structure
5936   * @link_index: [in] link index
5937   * @mst_alloc_slots: [in] mst slots to be allotted
5938   * @mst_slots_in_use: [out] mst slots in use returned in failure case
5939   *
5940   * Submits mst slot allocation command to dmub via inbox message
5941   *
5942   * Return:
5943   * DC_OK if successful, DC_ERROR if failure
5944   */
dc_process_dmub_set_mst_slots(const struct dc * dc,uint32_t link_index,uint8_t mst_alloc_slots,uint8_t * mst_slots_in_use)5945  enum dc_status dc_process_dmub_set_mst_slots(const struct dc *dc,
5946  				uint32_t link_index,
5947  				uint8_t mst_alloc_slots,
5948  				uint8_t *mst_slots_in_use)
5949  {
5950  	union dmub_rb_cmd cmd = {0};
5951  
5952  	/* prepare MST_ALLOC_SLOTS command */
5953  	cmd.set_mst_alloc_slots.header.type = DMUB_CMD__DPIA;
5954  	cmd.set_mst_alloc_slots.header.sub_type = DMUB_CMD__DPIA_MST_ALLOC_SLOTS;
5955  
5956  	cmd.set_mst_alloc_slots.mst_slots_control.instance = dc->links[link_index]->ddc_hw_inst;
5957  	cmd.set_mst_alloc_slots.mst_slots_control.mst_alloc_slots = mst_alloc_slots;
5958  
5959  	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
5960  		/* command is not processed by dmub */
5961  		return DC_ERROR_UNEXPECTED;
5962  
5963  	/* command processed by dmub, if ret_status is 1 */
5964  	if (cmd.set_config_access.header.ret_status != 1)
5965  		/* command processing error */
5966  		return DC_ERROR_UNEXPECTED;
5967  
5968  	/* command processed and we have a status of 2, mst not enabled in dpia */
5969  	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 2)
5970  		return DC_FAIL_UNSUPPORTED_1;
5971  
5972  	/* previously configured mst alloc and used slots did not match */
5973  	if (cmd.set_mst_alloc_slots.mst_slots_control.immed_status == 3) {
5974  		*mst_slots_in_use = cmd.set_mst_alloc_slots.mst_slots_control.mst_slots_in_use;
5975  		return DC_NOT_SUPPORTED;
5976  	}
5977  
5978  	return DC_OK;
5979  }
5980  
5981  /**
5982   * dc_process_dmub_dpia_set_tps_notification - Submits tps notification
5983   *
5984   * @dc: [in] dc structure
5985   * @link_index: [in] link index
5986   * @tps: [in] request tps
5987   *
5988   * Submits set_tps_notification command to dmub via inbox message
5989   */
dc_process_dmub_dpia_set_tps_notification(const struct dc * dc,uint32_t link_index,uint8_t tps)5990  void dc_process_dmub_dpia_set_tps_notification(const struct dc *dc, uint32_t link_index, uint8_t tps)
5991  {
5992  	union dmub_rb_cmd cmd = {0};
5993  
5994  	cmd.set_tps_notification.header.type = DMUB_CMD__DPIA;
5995  	cmd.set_tps_notification.header.sub_type = DMUB_CMD__DPIA_SET_TPS_NOTIFICATION;
5996  	cmd.set_tps_notification.tps_notification.instance = dc->links[link_index]->ddc_hw_inst;
5997  	cmd.set_tps_notification.tps_notification.tps = tps;
5998  
5999  	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
6000  }
6001  
6002  /**
6003   * dc_process_dmub_dpia_hpd_int_enable - Submits DPIA DPD interruption
6004   *
6005   * @dc: [in] dc structure
6006   * @hpd_int_enable: [in] 1 for hpd int enable, 0 to disable
6007   *
6008   * Submits dpia hpd int enable command to dmub via inbox message
6009   */
dc_process_dmub_dpia_hpd_int_enable(const struct dc * dc,uint32_t hpd_int_enable)6010  void dc_process_dmub_dpia_hpd_int_enable(const struct dc *dc,
6011  				uint32_t hpd_int_enable)
6012  {
6013  	union dmub_rb_cmd cmd = {0};
6014  
6015  	cmd.dpia_hpd_int_enable.header.type = DMUB_CMD__DPIA_HPD_INT_ENABLE;
6016  	cmd.dpia_hpd_int_enable.enable = hpd_int_enable;
6017  
6018  	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
6019  
6020  	DC_LOG_DEBUG("%s: hpd_int_enable(%d)\n", __func__, hpd_int_enable);
6021  }
6022  
6023  /**
6024   * dc_print_dmub_diagnostic_data - Print DMUB diagnostic data for debugging
6025   *
6026   * @dc: [in] dc structure
6027   *
6028   *
6029   */
dc_print_dmub_diagnostic_data(const struct dc * dc)6030  void dc_print_dmub_diagnostic_data(const struct dc *dc)
6031  {
6032  	dc_dmub_srv_log_diagnostic_data(dc->ctx->dmub_srv);
6033  }
6034  
6035  /**
6036   * dc_disable_accelerated_mode - disable accelerated mode
6037   * @dc: dc structure
6038   */
dc_disable_accelerated_mode(struct dc * dc)6039  void dc_disable_accelerated_mode(struct dc *dc)
6040  {
6041  	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 0);
6042  }
6043  
6044  
6045  /**
6046   *  dc_notify_vsync_int_state - notifies vsync enable/disable state
6047   *  @dc: dc structure
6048   *  @stream: stream where vsync int state changed
6049   *  @enable: whether vsync is enabled or disabled
6050   *
6051   *  Called when vsync is enabled/disabled Will notify DMUB to start/stop ABM
6052   *  interrupts after steady state is reached.
6053   */
dc_notify_vsync_int_state(struct dc * dc,struct dc_stream_state * stream,bool enable)6054  void dc_notify_vsync_int_state(struct dc *dc, struct dc_stream_state *stream, bool enable)
6055  {
6056  	int i;
6057  	int edp_num;
6058  	struct pipe_ctx *pipe = NULL;
6059  	struct dc_link *link = stream->sink->link;
6060  	struct dc_link *edp_links[MAX_NUM_EDP];
6061  
6062  
6063  	if (link->psr_settings.psr_feature_enabled)
6064  		return;
6065  
6066  	if (link->replay_settings.replay_feature_enabled)
6067  		return;
6068  
6069  	/*find primary pipe associated with stream*/
6070  	for (i = 0; i < MAX_PIPES; i++) {
6071  		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
6072  
6073  		if (pipe->stream == stream && pipe->stream_res.tg)
6074  			break;
6075  	}
6076  
6077  	if (i == MAX_PIPES) {
6078  		ASSERT(0);
6079  		return;
6080  	}
6081  
6082  	dc_get_edp_links(dc, edp_links, &edp_num);
6083  
6084  	/* Determine panel inst */
6085  	for (i = 0; i < edp_num; i++) {
6086  		if (edp_links[i] == link)
6087  			break;
6088  	}
6089  
6090  	if (i == edp_num) {
6091  		return;
6092  	}
6093  
6094  	if (pipe->stream_res.abm && pipe->stream_res.abm->funcs->set_abm_pause)
6095  		pipe->stream_res.abm->funcs->set_abm_pause(pipe->stream_res.abm, !enable, i, pipe->stream_res.tg->inst);
6096  }
6097  
6098  /*****************************************************************************
6099   *  dc_abm_save_restore() - Interface to DC for save+pause and restore+un-pause
6100   *                          ABM
6101   *  @dc: dc structure
6102   *	@stream: stream where vsync int state changed
6103   *  @pData: abm hw states
6104   *
6105   ****************************************************************************/
dc_abm_save_restore(struct dc * dc,struct dc_stream_state * stream,struct abm_save_restore * pData)6106  bool dc_abm_save_restore(
6107  		struct dc *dc,
6108  		struct dc_stream_state *stream,
6109  		struct abm_save_restore *pData)
6110  {
6111  	int i;
6112  	int edp_num;
6113  	struct pipe_ctx *pipe = NULL;
6114  	struct dc_link *link = stream->sink->link;
6115  	struct dc_link *edp_links[MAX_NUM_EDP];
6116  
6117  	if (link->replay_settings.replay_feature_enabled)
6118  		return false;
6119  
6120  	/*find primary pipe associated with stream*/
6121  	for (i = 0; i < MAX_PIPES; i++) {
6122  		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
6123  
6124  		if (pipe->stream == stream && pipe->stream_res.tg)
6125  			break;
6126  	}
6127  
6128  	if (i == MAX_PIPES) {
6129  		ASSERT(0);
6130  		return false;
6131  	}
6132  
6133  	dc_get_edp_links(dc, edp_links, &edp_num);
6134  
6135  	/* Determine panel inst */
6136  	for (i = 0; i < edp_num; i++)
6137  		if (edp_links[i] == link)
6138  			break;
6139  
6140  	if (i == edp_num)
6141  		return false;
6142  
6143  	if (pipe->stream_res.abm &&
6144  		pipe->stream_res.abm->funcs->save_restore)
6145  		return pipe->stream_res.abm->funcs->save_restore(
6146  				pipe->stream_res.abm,
6147  				i,
6148  				pData);
6149  	return false;
6150  }
6151  
dc_query_current_properties(struct dc * dc,struct dc_current_properties * properties)6152  void dc_query_current_properties(struct dc *dc, struct dc_current_properties *properties)
6153  {
6154  	unsigned int i;
6155  	bool subvp_sw_cursor_req = false;
6156  
6157  	for (i = 0; i < dc->current_state->stream_count; i++) {
6158  		if (check_subvp_sw_cursor_fallback_req(dc, dc->current_state->streams[i]) && !dc->current_state->streams[i]->hw_cursor_req) {
6159  			subvp_sw_cursor_req = true;
6160  			break;
6161  		}
6162  	}
6163  	properties->cursor_size_limit = subvp_sw_cursor_req ? 64 : dc->caps.max_cursor_size;
6164  }
6165  
6166  /**
6167   * dc_set_edp_power() - DM controls eDP power to be ON/OFF
6168   *
6169   * Called when DM wants to power on/off eDP.
6170   *     Only work on links with flag skip_implict_edp_power_control is set.
6171   *
6172   * @dc: Current DC state
6173   * @edp_link: a link with eDP connector signal type
6174   * @powerOn: power on/off eDP
6175   *
6176   * Return: void
6177   */
dc_set_edp_power(const struct dc * dc,struct dc_link * edp_link,bool powerOn)6178  void dc_set_edp_power(const struct dc *dc, struct dc_link *edp_link,
6179  				 bool powerOn)
6180  {
6181  	if (edp_link->connector_signal != SIGNAL_TYPE_EDP)
6182  		return;
6183  
6184  	if (edp_link->skip_implict_edp_power_control == false)
6185  		return;
6186  
6187  	edp_link->dc->link_srv->edp_set_panel_power(edp_link, powerOn);
6188  }
6189  
6190  /*
6191   *****************************************************************************
6192   * dc_get_power_profile_for_dc_state() - extracts power profile from dc state
6193   *
6194   * Called when DM wants to make power policy decisions based on dc_state
6195   *
6196   *****************************************************************************
6197   */
dc_get_power_profile_for_dc_state(const struct dc_state * context)6198  struct dc_power_profile dc_get_power_profile_for_dc_state(const struct dc_state *context)
6199  {
6200  	struct dc_power_profile profile = { 0 };
6201  
6202  	profile.power_level = !context->bw_ctx.bw.dcn.clk.p_state_change_support;
6203  	if (!context->clk_mgr || !context->clk_mgr->ctx || !context->clk_mgr->ctx->dc)
6204  		return profile;
6205  	struct dc *dc = context->clk_mgr->ctx->dc;
6206  
6207  	if (dc->res_pool->funcs->get_power_profile)
6208  		profile.power_level = dc->res_pool->funcs->get_power_profile(context);
6209  	return profile;
6210  }
6211  
6212  /*
6213   **********************************************************************************
6214   * dc_get_det_buffer_size_from_state() - extracts detile buffer size from dc state
6215   *
6216   * Called when DM wants to log detile buffer size from dc_state
6217   *
6218   **********************************************************************************
6219   */
dc_get_det_buffer_size_from_state(const struct dc_state * context)6220  unsigned int dc_get_det_buffer_size_from_state(const struct dc_state *context)
6221  {
6222  	struct dc *dc = context->clk_mgr->ctx->dc;
6223  
6224  	if (dc->res_pool->funcs->get_det_buffer_size)
6225  		return dc->res_pool->funcs->get_det_buffer_size(context);
6226  	else
6227  		return 0;
6228  }
6229