1 /*
2  * Copyright 2012-14 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #ifndef DC_LINK_H_
27 #define DC_LINK_H_
28 
29 #include "dc.h"
30 #include "dc_types.h"
31 #include "grph_object_defs.h"
32 
33 struct link_resource;
34 enum aux_return_code_type;
35 
36 enum dc_link_fec_state {
37 	dc_link_fec_not_ready,
38 	dc_link_fec_ready,
39 	dc_link_fec_enabled
40 };
41 
42 /* DP MST stream allocation (payload bandwidth number) */
43 struct link_mst_stream_allocation {
44 	/* DIG front */
45 	const struct stream_encoder *stream_enc;
46 	/* HPO DP Stream Encoder */
47 	const struct hpo_dp_stream_encoder *hpo_dp_stream_enc;
48 	/* associate DRM payload table with DC stream encoder */
49 	uint8_t vcp_id;
50 	/* number of slots required for the DP stream in transport packet */
51 	uint8_t slot_count;
52 };
53 
54 /* DP MST stream allocation table */
55 struct link_mst_stream_allocation_table {
56 	/* number of DP video streams */
57 	int stream_count;
58 	/* array of stream allocations */
59 	struct link_mst_stream_allocation stream_allocations[MAX_CONTROLLER_NUM];
60 };
61 
62 struct edp_trace_power_timestamps {
63 	uint64_t poweroff;
64 	uint64_t poweron;
65 };
66 
67 struct dp_trace_lt_counts {
68 	unsigned int total;
69 	unsigned int fail;
70 };
71 
72 struct dp_trace_lt {
73 	struct dp_trace_lt_counts counts;
74 	struct dp_trace_timestamps {
75 		unsigned long long start;
76 		unsigned long long end;
77 	} timestamps;
78 	enum link_training_result result;
79 	bool is_logged;
80 };
81 
82 struct dp_trace {
83 	struct dp_trace_lt detect_lt_trace;
84 	struct dp_trace_lt commit_lt_trace;
85 	unsigned int link_loss_count;
86 	bool is_initialized;
87 	struct edp_trace_power_timestamps edp_trace_power_timestamps;
88 };
89 
90 /* PSR feature flags */
91 struct psr_settings {
92 	bool psr_feature_enabled;		// PSR is supported by sink
93 	bool psr_allow_active;			// PSR is currently active
94 	enum dc_psr_version psr_version;		// Internal PSR version, determined based on DPCD
95 	bool psr_vtotal_control_support;	// Vtotal control is supported by sink
96 	unsigned long long psr_dirty_rects_change_timestamp_ns;	// for delay of enabling PSR-SU
97 
98 	/* These parameters are calculated in Driver,
99 	 * based on display timing and Sink capabilities.
100 	 * If VBLANK region is too small and Sink takes a long time
101 	 * to set up RFB, it may take an extra frame to enter PSR state.
102 	 */
103 	bool psr_frame_capture_indication_req;
104 	unsigned int psr_sdp_transmit_line_num_deadline;
105 	uint8_t force_ffu_mode;
106 	unsigned int psr_power_opt;
107 };
108 
109 /* To split out "global" and "per-panel" config settings.
110  * Add a struct dc_panel_config under dc_link
111  */
112 struct dc_panel_config {
113 	/* extra panel power sequence parameters */
114 	struct pps {
115 		unsigned int extra_t3_ms;
116 		unsigned int extra_t7_ms;
117 		unsigned int extra_delay_backlight_off;
118 		unsigned int extra_post_t7_ms;
119 		unsigned int extra_pre_t11_ms;
120 		unsigned int extra_t12_ms;
121 		unsigned int extra_post_OUI_ms;
122 	} pps;
123 	/* PSR */
124 	struct psr {
125 		bool disable_psr;
126 		bool disallow_psrsu;
127 		bool rc_disable;
128 		bool rc_allow_static_screen;
129 		bool rc_allow_fullscreen_VPB;
130 	} psr;
131 	/* ABM */
132 	struct varib {
133 		unsigned int varibright_feature_enable;
134 		unsigned int def_varibright_level;
135 		unsigned int abm_config_setting;
136 	} varib;
137 	/* edp DSC */
138 	struct dsc {
139 		bool disable_dsc_edp;
140 		unsigned int force_dsc_edp_policy;
141 	} dsc;
142 	/* eDP ILR */
143 	struct ilr {
144 		bool optimize_edp_link_rate; /* eDP ILR */
145 	} ilr;
146 };
147 
148 /*
149  *  USB4 DPIA BW ALLOCATION STRUCTS
150  */
151 struct dc_dpia_bw_alloc {
152 	int sink_verified_bw;  // The Verified BW that sink can allocated and use that has been verified already
153 	int sink_allocated_bw; // The Actual Allocated BW that sink currently allocated
154 	int sink_max_bw;       // The Max BW that sink can require/support
155 	int estimated_bw;      // The estimated available BW for this DPIA
156 	int bw_granularity;    // BW Granularity
157 	bool bw_alloc_enabled; // The BW Alloc Mode Support is turned ON for all 3:  DP-Tx & Dpia & CM
158 	bool response_ready;   // Response ready from the CM side
159 };
160 
161 #define MAX_SINKS_PER_LINK 4
162 
163 /*
164  * A link contains one or more sinks and their connected status.
165  * The currently active signal type (HDMI, DP-SST, DP-MST) is also reported.
166  */
167 struct dc_link {
168 	struct dc_sink *remote_sinks[MAX_SINKS_PER_LINK];
169 	unsigned int sink_count;
170 	struct dc_sink *local_sink;
171 	unsigned int link_index;
172 	enum dc_connection_type type;
173 	enum signal_type connector_signal;
174 	enum dc_irq_source irq_source_hpd;
175 	enum dc_irq_source irq_source_hpd_rx;/* aka DP Short Pulse  */
176 	bool is_hpd_filter_disabled;
177 	bool dp_ss_off;
178 
179 	/**
180 	 * @link_state_valid:
181 	 *
182 	 * If there is no link and local sink, this variable should be set to
183 	 * false. Otherwise, it should be set to true; usually, the function
184 	 * core_link_enable_stream sets this field to true.
185 	 */
186 	bool link_state_valid;
187 	bool aux_access_disabled;
188 	bool sync_lt_in_progress;
189 	bool is_internal_display;
190 
191 	/* TODO: Rename. Flag an endpoint as having a programmable mapping to a
192 	 * DIG encoder. */
193 	bool is_dig_mapping_flexible;
194 	bool hpd_status; /* HPD status of link without physical HPD pin. */
195 	bool is_hpd_pending; /* Indicates a new received hpd */
196 	bool is_automated; /* Indicates automated testing */
197 
198 	bool edp_sink_present;
199 
200 	struct dp_trace dp_trace;
201 
202 	/* caps is the same as reported_link_cap. link_traing use
203 	 * reported_link_cap. Will clean up.  TODO
204 	 */
205 	struct dc_link_settings reported_link_cap;
206 	struct dc_link_settings verified_link_cap;
207 	struct dc_link_settings cur_link_settings;
208 	struct dc_lane_settings cur_lane_setting[LANE_COUNT_DP_MAX];
209 	struct dc_link_settings preferred_link_setting;
210 	/* preferred_training_settings are override values that
211 	 * come from DM. DM is responsible for the memory
212 	 * management of the override pointers.
213 	 */
214 	struct dc_link_training_overrides preferred_training_settings;
215 	struct dp_audio_test_data audio_test_data;
216 
217 	uint8_t ddc_hw_inst;
218 
219 	uint8_t hpd_src;
220 
221 	uint8_t link_enc_hw_inst;
222 	/* DIG link encoder ID. Used as index in link encoder resource pool.
223 	 * For links with fixed mapping to DIG, this is not changed after dc_link
224 	 * object creation.
225 	 */
226 	enum engine_id eng_id;
227 
228 	bool test_pattern_enabled;
229 	union compliance_test_state compliance_test_state;
230 
231 	void *priv;
232 
233 	struct ddc_service *ddc;
234 
235 	bool aux_mode;
236 
237 	/* Private to DC core */
238 
239 	const struct dc *dc;
240 
241 	struct dc_context *ctx;
242 
243 	struct panel_cntl *panel_cntl;
244 	struct link_encoder *link_enc;
245 	struct graphics_object_id link_id;
246 	/* Endpoint type distinguishes display endpoints which do not have entries
247 	 * in the BIOS connector table from those that do. Helps when tracking link
248 	 * encoder to display endpoint assignments.
249 	 */
250 	enum display_endpoint_type ep_type;
251 	union ddi_channel_mapping ddi_channel_mapping;
252 	struct connector_device_tag_info device_tag;
253 	struct dpcd_caps dpcd_caps;
254 	uint32_t dongle_max_pix_clk;
255 	unsigned short chip_caps;
256 	unsigned int dpcd_sink_count;
257 #if defined(CONFIG_DRM_AMD_DC_HDCP)
258 	struct hdcp_caps hdcp_caps;
259 #endif
260 	enum edp_revision edp_revision;
261 	union dpcd_sink_ext_caps dpcd_sink_ext_caps;
262 
263 	struct psr_settings psr_settings;
264 
265 	/* Drive settings read from integrated info table */
266 	struct dc_lane_settings bios_forced_drive_settings;
267 
268 	/* Vendor specific LTTPR workaround variables */
269 	uint8_t vendor_specific_lttpr_link_rate_wa;
270 	bool apply_vendor_specific_lttpr_link_rate_wa;
271 
272 	/* MST record stream using this link */
273 	struct link_flags {
274 		bool dp_keep_receiver_powered;
275 		bool dp_skip_DID2;
276 		bool dp_skip_reset_segment;
277 		bool dp_skip_fs_144hz;
278 		bool dp_mot_reset_segment;
279 		/* Some USB4 docks do not handle turning off MST DSC once it has been enabled. */
280 		bool dpia_mst_dsc_always_on;
281 		/* Forced DPIA into TBT3 compatibility mode. */
282 		bool dpia_forced_tbt3_mode;
283 		bool dongle_mode_timing_override;
284 	} wa_flags;
285 	struct link_mst_stream_allocation_table mst_stream_alloc_table;
286 
287 	struct dc_link_status link_status;
288 	struct dprx_states dprx_states;
289 
290 	struct gpio *hpd_gpio;
291 	enum dc_link_fec_state fec_state;
292 	bool link_powered_externally;	// Used to bypass hardware sequencing delays when panel is powered down forcibly
293 
294 	struct dc_panel_config panel_config;
295 	struct phy_state phy_state;
296 };
297 
298 
299 /**
300  * dc_get_link_at_index() - Return an enumerated dc_link.
301  *
302  * dc_link order is constant and determined at
303  * boot time.  They cannot be created or destroyed.
304  * Use dc_get_caps() to get number of links.
305  */
dc_get_link_at_index(struct dc * dc,uint32_t link_index)306 static inline struct dc_link *dc_get_link_at_index(struct dc *dc, uint32_t link_index)
307 {
308 	return dc->links[link_index];
309 }
310 
get_edp_links(const struct dc * dc,struct dc_link ** edp_links,int * edp_num)311 static inline void get_edp_links(const struct dc *dc,
312 		struct dc_link **edp_links,
313 		int *edp_num)
314 {
315 	int i;
316 
317 	*edp_num = 0;
318 	for (i = 0; i < dc->link_count; i++) {
319 		// report any eDP links, even unconnected DDI's
320 		if (!dc->links[i])
321 			continue;
322 		if (dc->links[i]->connector_signal == SIGNAL_TYPE_EDP) {
323 			edp_links[*edp_num] = dc->links[i];
324 			if (++(*edp_num) == MAX_NUM_EDP)
325 				return;
326 		}
327 	}
328 }
329 
dc_get_edp_link_panel_inst(const struct dc * dc,const struct dc_link * link,unsigned int * inst_out)330 static inline bool dc_get_edp_link_panel_inst(const struct dc *dc,
331 		const struct dc_link *link,
332 		unsigned int *inst_out)
333 {
334 	struct dc_link *edp_links[MAX_NUM_EDP];
335 	int edp_num, i;
336 
337 	*inst_out = 0;
338 	if (link->connector_signal != SIGNAL_TYPE_EDP)
339 		return false;
340 	get_edp_links(dc, edp_links, &edp_num);
341 	for (i = 0; i < edp_num; i++) {
342 		if (link == edp_links[i])
343 			break;
344 		(*inst_out)++;
345 	}
346 	return true;
347 }
348 
349 /* Set backlight level of an embedded panel (eDP, LVDS).
350  * backlight_pwm_u16_16 is unsigned 32 bit with 16 bit integer
351  * and 16 bit fractional, where 1.0 is max backlight value.
352  */
353 bool dc_link_set_backlight_level(const struct dc_link *dc_link,
354 		uint32_t backlight_pwm_u16_16,
355 		uint32_t frame_ramp);
356 
357 /* Set/get nits-based backlight level of an embedded panel (eDP, LVDS). */
358 bool dc_link_set_backlight_level_nits(struct dc_link *link,
359 		bool isHDR,
360 		uint32_t backlight_millinits,
361 		uint32_t transition_time_in_ms);
362 
363 bool dc_link_get_backlight_level_nits(struct dc_link *link,
364 		uint32_t *backlight_millinits,
365 		uint32_t *backlight_millinits_peak);
366 
367 int dc_link_get_backlight_level(const struct dc_link *dc_link);
368 
369 int dc_link_get_target_backlight_pwm(const struct dc_link *link);
370 
371 bool dc_link_set_psr_allow_active(struct dc_link *dc_link, const bool *enable,
372 		bool wait, bool force_static, const unsigned int *power_opts);
373 
374 bool dc_link_get_psr_state(const struct dc_link *dc_link, enum dc_psr_state *state);
375 
376 bool dc_link_setup_psr(struct dc_link *dc_link,
377 		const struct dc_stream_state *stream, struct psr_config *psr_config,
378 		struct psr_context *psr_context);
379 
380 bool dc_link_get_hpd_state(struct dc_link *dc_link);
381 
382 /* Notify DC about DP RX Interrupt (aka Short Pulse Interrupt).
383  * Return:
384  * true - Downstream port status changed. DM should call DC to do the
385  * detection.
386  * false - no change in Downstream port status. No further action required
387  * from DM. */
388 bool dc_link_handle_hpd_rx_irq(struct dc_link *dc_link,
389 		union hpd_irq_data *hpd_irq_dpcd_data, bool *out_link_loss,
390 		bool defer_handling, bool *has_left_work);
391 
392 /*
393  * On eDP links this function call will stall until T12 has elapsed.
394  * If the panel is not in power off state, this function will return
395  * immediately.
396  */
397 bool dc_link_wait_for_t12(struct dc_link *link);
398 
399 void dc_link_dp_handle_automated_test(struct dc_link *link);
400 void dc_link_dp_handle_link_loss(struct dc_link *link);
401 bool dc_link_dp_allow_hpd_rx_irq(const struct dc_link *link);
402 bool dc_link_check_link_loss_status(struct dc_link *link,
403 		union hpd_irq_data *hpd_irq_dpcd_data);
404 enum dc_status dc_link_dp_read_hpd_rx_irq_data(
405 	struct dc_link *link,
406 	union hpd_irq_data *irq_data);
407 struct dc_sink_init_data;
408 
409 struct dc_sink *dc_link_add_remote_sink(
410 		struct dc_link *dc_link,
411 		const uint8_t *edid,
412 		int len,
413 		struct dc_sink_init_data *init_data);
414 
415 void dc_link_remove_remote_sink(
416 	struct dc_link *link,
417 	struct dc_sink *sink);
418 
419 /* Used by diagnostics for virtual link at the moment */
420 
421 bool dc_link_dp_set_test_pattern(
422 	struct dc_link *link,
423 	enum dp_test_pattern test_pattern,
424 	enum dp_test_pattern_color_space test_pattern_color_space,
425 	const struct link_training_settings *p_link_settings,
426 	const unsigned char *p_custom_pattern,
427 	unsigned int cust_pattern_size);
428 
429 bool dc_link_dp_get_max_link_enc_cap(const struct dc_link *link, struct dc_link_settings *max_link_enc_cap);
430 
431 /**
432  *****************************************************************************
433  *  Function: dc_link_enable_hpd_filter
434  *
435  *  @brief
436  *     If enable is true, programs HPD filter on associated HPD line to default
437  *     values dependent on link->connector_signal
438  *
439  *     If enable is false, programs HPD filter on associated HPD line with no
440  *     delays on connect or disconnect
441  *
442  *  @param [in] link: pointer to the dc link
443  *  @param [in] enable: boolean specifying whether to enable hbd
444  *****************************************************************************
445  */
446 void dc_link_enable_hpd_filter(struct dc_link *link, bool enable);
447 
448 bool dc_link_is_dp_sink_present(struct dc_link *link);
449 /*
450  * DPCD access interfaces
451  */
452 
453 void dc_link_set_drive_settings(struct dc *dc,
454 				struct link_training_settings *lt_settings,
455 				const struct dc_link *link);
456 void dc_link_set_preferred_link_settings(struct dc *dc,
457 					 struct dc_link_settings *link_setting,
458 					 struct dc_link *link);
459 void dc_link_set_preferred_training_settings(struct dc *dc,
460 					struct dc_link_settings *link_setting,
461 					struct dc_link_training_overrides *lt_overrides,
462 					struct dc_link *link,
463 					bool skip_immediate_retrain);
464 void dc_link_enable_hpd(const struct dc_link *link);
465 void dc_link_disable_hpd(const struct dc_link *link);
466 void dc_link_set_test_pattern(struct dc_link *link,
467 			enum dp_test_pattern test_pattern,
468 			enum dp_test_pattern_color_space test_pattern_color_space,
469 			const struct link_training_settings *p_link_settings,
470 			const unsigned char *p_custom_pattern,
471 			unsigned int cust_pattern_size);
472 
473 const struct dc_link_settings *dc_link_get_link_cap(
474 		const struct dc_link *link);
475 
476 void dc_link_overwrite_extended_receiver_cap(
477 		struct dc_link *link);
478 
479 bool dc_is_oem_i2c_device_present(
480 	struct dc *dc,
481 	size_t slave_address
482 );
483 
484 bool dc_submit_i2c(
485 		struct dc *dc,
486 		uint32_t link_index,
487 		struct i2c_command *cmd);
488 
489 bool dc_submit_i2c_oem(
490 		struct dc *dc,
491 		struct i2c_command *cmd);
492 
493 bool dc_link_is_fec_supported(const struct dc_link *link);
494 bool dc_link_should_enable_fec(const struct dc_link *link);
495 
496 uint32_t dc_link_bw_kbps_from_raw_frl_link_rate_data(uint8_t bw);
497 enum dp_link_encoding dc_link_dp_mst_decide_link_encoding_format(const struct dc_link *link);
498 
499 /* take a snapshot of current link resource allocation state */
500 void dc_get_cur_link_res_map(const struct dc *dc, uint32_t *map);
501 /* restore link resource allocation state from a snapshot */
502 void dc_restore_link_res_map(const struct dc *dc, uint32_t *map);
503 void dp_trace_reset(struct dc_link *link);
504 bool dc_dp_trace_is_initialized(struct dc_link *link);
505 unsigned long long dc_dp_trace_get_lt_end_timestamp(struct dc_link *link,
506 		bool in_detection);
507 void dc_dp_trace_set_is_logged_flag(struct dc_link *link,
508 		bool in_detection,
509 		bool is_logged);
510 bool dc_dp_trace_is_logged(struct dc_link *link,
511 		bool in_detection);
512 struct dp_trace_lt_counts *dc_dp_trace_get_lt_counts(struct dc_link *link,
513 		bool in_detection);
514 unsigned int dc_dp_trace_get_link_loss_count(struct dc_link *link);
515 
516 /* Attempt to transfer the given aux payload. This function does not perform
517  * retries or handle error states. The reply is returned in the payload->reply
518  * and the result through operation_result. Returns the number of bytes
519  * transferred,or -1 on a failure.
520  */
521 int dc_link_aux_transfer_raw(struct ddc_service *ddc,
522 		struct aux_payload *payload,
523 		enum aux_return_code_type *operation_result);
524 
525 enum lttpr_mode dc_link_decide_lttpr_mode(struct dc_link *link,
526 		struct dc_link_settings *link_setting);
527 void dc_link_dp_receiver_power_ctrl(struct dc_link *link, bool on);
528 bool dc_link_decide_edp_link_settings(struct dc_link *link,
529 		struct dc_link_settings *link_setting,
530 		uint32_t req_bw);
531 void dc_link_edp_panel_backlight_power_on(struct dc_link *link,
532 		bool wait_for_hpd);
533 
534 /*
535  *  USB4 DPIA BW ALLOCATION PUBLIC FUNCTIONS
536  */
537 /*
538  * Send a request from DP-Tx requesting to allocate BW remotely after
539  * allocating it locally. This will get processed by CM and a CB function
540  * will be called.
541  *
542  * @link: pointer to the dc_link struct instance
543  * @req_bw: The requested bw in Kbyte to allocated
544  *
545  * return: none
546  */
547 void dc_link_set_usb4_req_bw_req(struct dc_link *link, int req_bw);
548 
549 /*
550  * CB function for when the status of the Req above is complete. We will
551  * find out the result of allocating on CM and update structs accordingly
552  *
553  * @link: pointer to the dc_link struct instance
554  * @bw: Allocated or Estimated BW depending on the result
555  * @result: Response type
556  *
557  * return: none
558  */
559 void dc_link_get_usb4_req_bw_resp(struct dc_link *link, uint8_t bw, uint8_t result);
560 
561 /*
562  * Handle the USB4 BW Allocation related functionality here:
563  * Plug => Try to allocate max bw from timing parameters supported by the sink
564  * Unplug => de-allocate bw
565  *
566  * @link: pointer to the dc_link struct instance
567  * @peak_bw: Peak bw used by the link/sink
568  *
569  * return: allocated bw else return 0
570  */
571 int dc_link_dp_dpia_handle_usb4_bandwidth_allocation_for_link(struct dc_link *link, int peak_bw);
572 
573 /* TODO: this is not meant to be exposed to DM. Should switch to stream update
574  * interface i.e stream_update->dsc_config
575  */
576 bool dc_link_update_dsc_config(struct pipe_ctx *pipe_ctx);
577 #endif /* DC_LINK_H_ */
578