1 /*
2  * Copyright 2023 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 /* FILE POLICY AND INTENDED USAGE:
27  * This file owns the creation/destruction of link structure.
28  */
29 #include "link_factory.h"
30 #include "link_detection.h"
31 #include "link_resource.h"
32 #include "link_validation.h"
33 #include "link_dpms.h"
34 #include "accessories/link_dp_cts.h"
35 #include "accessories/link_dp_trace.h"
36 #include "accessories/link_fpga.h"
37 #include "protocols/link_ddc.h"
38 #include "protocols/link_dp_capability.h"
39 #include "protocols/link_dp_dpia_bw.h"
40 #include "protocols/link_dp_dpia.h"
41 #include "protocols/link_dp_irq_handler.h"
42 #include "protocols/link_dp_phy.h"
43 #include "protocols/link_dp_training.h"
44 #include "protocols/link_edp_panel_control.h"
45 #include "protocols/link_hpd.h"
46 #include "gpio_service_interface.h"
47 #include "atomfirmware.h"
48 
49 #define DC_LOGGER_INIT(logger)
50 
51 #define LINK_INFO(...) \
52 	DC_LOG_HW_HOTPLUG(  \
53 		__VA_ARGS__)
54 
55 /* link factory owns the creation/destruction of link structures. */
56 static void construct_link_service_factory(struct link_service *link_srv)
57 {
58 
59 	link_srv->create_link = link_create;
60 	link_srv->destroy_link = link_destroy;
61 }
62 
63 /* link_detection manages link detection states and receiver states by using
64  * various link protocols. It also provides helper functions to interpret
65  * certain capabilities or status based on the states it manages or retrieve
66  * them directly from connected receivers.
67  */
68 static void construct_link_service_detection(struct link_service *link_srv)
69 {
70 	link_srv->detect_link = link_detect;
71 	link_srv->detect_connection_type = link_detect_connection_type;
72 	link_srv->add_remote_sink = link_add_remote_sink;
73 	link_srv->remove_remote_sink = link_remove_remote_sink;
74 	link_srv->get_hpd_state = link_get_hpd_state;
75 	link_srv->get_hpd_gpio = link_get_hpd_gpio;
76 	link_srv->enable_hpd = link_enable_hpd;
77 	link_srv->disable_hpd = link_disable_hpd;
78 	link_srv->enable_hpd_filter = link_enable_hpd_filter;
79 	link_srv->reset_cur_dp_mst_topology = link_reset_cur_dp_mst_topology;
80 	link_srv->get_status = link_get_status;
81 	link_srv->is_hdcp1x_supported = link_is_hdcp14;
82 	link_srv->is_hdcp2x_supported = link_is_hdcp22;
83 	link_srv->clear_dprx_states = link_clear_dprx_states;
84 }
85 
86 /* link resource implements accessors to link resource. */
87 static void construct_link_service_resource(struct link_service *link_srv)
88 {
89 	link_srv->get_cur_res_map = link_get_cur_res_map;
90 	link_srv->restore_res_map = link_restore_res_map;
91 	link_srv->get_cur_link_res = link_get_cur_link_res;
92 }
93 
94 /* link validation owns timing validation against various link limitations. (ex.
95  * link bandwidth, receiver capability or our hardware capability) It also
96  * provides helper functions exposing bandwidth formulas used in validation.
97  */
98 static void construct_link_service_validation(struct link_service *link_srv)
99 {
100 	link_srv->validate_mode_timing = link_validate_mode_timing;
101 	link_srv->dp_link_bandwidth_kbps = dp_link_bandwidth_kbps;
102 	link_srv->validate_dpia_bandwidth = link_validate_dpia_bandwidth;
103 }
104 
105 /* link dpms owns the programming sequence of stream's dpms state associated
106  * with the link and link's enable/disable sequences as result of the stream's
107  * dpms state change.
108  */
109 static void construct_link_service_dpms(struct link_service *link_srv)
110 {
111 	link_srv->set_dpms_on = link_set_dpms_on;
112 	link_srv->set_dpms_off = link_set_dpms_off;
113 	link_srv->resume = link_resume;
114 	link_srv->blank_all_dp_displays = link_blank_all_dp_displays;
115 	link_srv->blank_all_edp_displays = link_blank_all_edp_displays;
116 	link_srv->blank_dp_stream = link_blank_dp_stream;
117 	link_srv->increase_mst_payload = link_increase_mst_payload;
118 	link_srv->reduce_mst_payload = link_reduce_mst_payload;
119 	link_srv->set_dsc_on_stream = link_set_dsc_on_stream;
120 	link_srv->set_dsc_enable = link_set_dsc_enable;
121 	link_srv->update_dsc_config = link_update_dsc_config;
122 }
123 
124 /* link ddc implements generic display communication protocols such as i2c, aux
125  * and scdc. It should not contain any specific applications of these
126  * protocols such as display capability query, detection, or handshaking such as
127  * link training.
128  */
129 static void construct_link_service_ddc(struct link_service *link_srv)
130 {
131 	link_srv->create_ddc_service = link_create_ddc_service;
132 	link_srv->destroy_ddc_service = link_destroy_ddc_service;
133 	link_srv->query_ddc_data = link_query_ddc_data;
134 	link_srv->aux_transfer_raw = link_aux_transfer_raw;
135 	link_srv->configure_fixed_vs_pe_retimer = link_configure_fixed_vs_pe_retimer;
136 	link_srv->aux_transfer_with_retries_no_mutex =
137 			link_aux_transfer_with_retries_no_mutex;
138 	link_srv->is_in_aux_transaction_mode = link_is_in_aux_transaction_mode;
139 	link_srv->get_aux_defer_delay = link_get_aux_defer_delay;
140 }
141 
142 /* link dp capability implements dp specific link capability retrieval sequence.
143  * It is responsible for retrieving, parsing, overriding, deciding capability
144  * obtained from dp link. Link capability consists of encoders, DPRXs, cables,
145  * retimers, usb and all other possible backend capabilities.
146  */
147 static void construct_link_service_dp_capability(struct link_service *link_srv)
148 {
149 	link_srv->dp_is_sink_present = dp_is_sink_present;
150 	link_srv->dp_is_fec_supported = dp_is_fec_supported;
151 	link_srv->dp_is_128b_132b_signal = dp_is_128b_132b_signal;
152 	link_srv->dp_get_max_link_enc_cap = dp_get_max_link_enc_cap;
153 	link_srv->dp_get_verified_link_cap = dp_get_verified_link_cap;
154 	link_srv->dp_get_encoding_format = link_dp_get_encoding_format;
155 	link_srv->dp_should_enable_fec = dp_should_enable_fec;
156 	link_srv->dp_decide_link_settings = link_decide_link_settings;
157 	link_srv->mst_decide_link_encoding_format =
158 			mst_decide_link_encoding_format;
159 	link_srv->edp_decide_link_settings = edp_decide_link_settings;
160 	link_srv->bw_kbps_from_raw_frl_link_rate_data =
161 			link_bw_kbps_from_raw_frl_link_rate_data;
162 	link_srv->dp_overwrite_extended_receiver_cap =
163 			dp_overwrite_extended_receiver_cap;
164 	link_srv->dp_decide_lttpr_mode = dp_decide_lttpr_mode;
165 }
166 
167 /* link dp phy/dpia implements basic dp phy/dpia functionality such as
168  * enable/disable output and set lane/drive settings. It is responsible for
169  * maintaining and update software state representing current phy/dpia status
170  * such as current link settings.
171  */
172 static void construct_link_service_dp_phy_or_dpia(struct link_service *link_srv)
173 {
174 	link_srv->dpia_handle_usb4_bandwidth_allocation_for_link =
175 			dpia_handle_usb4_bandwidth_allocation_for_link;
176 	link_srv->dpia_handle_bw_alloc_response = dpia_handle_bw_alloc_response;
177 	link_srv->dp_set_drive_settings = dp_set_drive_settings;
178 	link_srv->dpcd_write_rx_power_ctrl = dpcd_write_rx_power_ctrl;
179 }
180 
181 /* link dp irq handler implements DP HPD short pulse handling sequence according
182  * to DP specifications
183  */
184 static void construct_link_service_dp_irq_handler(struct link_service *link_srv)
185 {
186 	link_srv->dp_parse_link_loss_status = dp_parse_link_loss_status;
187 	link_srv->dp_should_allow_hpd_rx_irq = dp_should_allow_hpd_rx_irq;
188 	link_srv->dp_handle_link_loss = dp_handle_link_loss;
189 	link_srv->dp_read_hpd_rx_irq_data = dp_read_hpd_rx_irq_data;
190 	link_srv->dp_handle_hpd_rx_irq = dp_handle_hpd_rx_irq;
191 }
192 
193 /* link edp panel control implements retrieval and configuration of eDP panel
194  * features such as PSR and ABM and it also manages specs defined eDP panel
195  * power sequences.
196  */
197 static void construct_link_service_edp_panel_control(struct link_service *link_srv)
198 {
199 	link_srv->edp_panel_backlight_power_on = edp_panel_backlight_power_on;
200 	link_srv->edp_get_backlight_level = edp_get_backlight_level;
201 	link_srv->edp_get_backlight_level_nits = edp_get_backlight_level_nits;
202 	link_srv->edp_set_backlight_level = edp_set_backlight_level;
203 	link_srv->edp_set_backlight_level_nits = edp_set_backlight_level_nits;
204 	link_srv->edp_get_target_backlight_pwm = edp_get_target_backlight_pwm;
205 	link_srv->edp_get_psr_state = edp_get_psr_state;
206 	link_srv->edp_set_psr_allow_active = edp_set_psr_allow_active;
207 	link_srv->edp_setup_psr = edp_setup_psr;
208 	link_srv->edp_set_sink_vtotal_in_psr_active =
209 			edp_set_sink_vtotal_in_psr_active;
210 	link_srv->edp_get_psr_residency = edp_get_psr_residency;
211 
212 	link_srv->edp_get_replay_state = edp_get_replay_state;
213 	link_srv->edp_set_replay_allow_active = edp_set_replay_allow_active;
214 	link_srv->edp_setup_replay = edp_setup_replay;
215 	link_srv->edp_set_coasting_vtotal = edp_set_coasting_vtotal;
216 	link_srv->edp_replay_residency = edp_replay_residency;
217 
218 	link_srv->edp_wait_for_t12 = edp_wait_for_t12;
219 	link_srv->edp_is_ilr_optimization_required =
220 			edp_is_ilr_optimization_required;
221 	link_srv->edp_backlight_enable_aux = edp_backlight_enable_aux;
222 	link_srv->edp_add_delay_for_T9 = edp_add_delay_for_T9;
223 	link_srv->edp_receiver_ready_T9 = edp_receiver_ready_T9;
224 	link_srv->edp_receiver_ready_T7 = edp_receiver_ready_T7;
225 	link_srv->edp_power_alpm_dpcd_enable = edp_power_alpm_dpcd_enable;
226 	link_srv->edp_set_panel_power = edp_set_panel_power;
227 }
228 
229 /* link dp cts implements dp compliance test automation protocols and manual
230  * testing interfaces for debugging and certification purpose.
231  */
232 static void construct_link_service_dp_cts(struct link_service *link_srv)
233 {
234 	link_srv->dp_handle_automated_test = dp_handle_automated_test;
235 	link_srv->dp_set_test_pattern = dp_set_test_pattern;
236 	link_srv->dp_set_preferred_link_settings =
237 			dp_set_preferred_link_settings;
238 	link_srv->dp_set_preferred_training_settings =
239 			dp_set_preferred_training_settings;
240 }
241 
242 /* link dp trace implements tracing interfaces for tracking major dp sequences
243  * including execution status and timestamps
244  */
245 static void construct_link_service_dp_trace(struct link_service *link_srv)
246 {
247 	link_srv->dp_trace_is_initialized = dp_trace_is_initialized;
248 	link_srv->dp_trace_set_is_logged_flag = dp_trace_set_is_logged_flag;
249 	link_srv->dp_trace_is_logged = dp_trace_is_logged;
250 	link_srv->dp_trace_get_lt_end_timestamp = dp_trace_get_lt_end_timestamp;
251 	link_srv->dp_trace_get_lt_counts = dp_trace_get_lt_counts;
252 	link_srv->dp_trace_get_link_loss_count = dp_trace_get_link_loss_count;
253 	link_srv->dp_trace_set_edp_power_timestamp =
254 			dp_trace_set_edp_power_timestamp;
255 	link_srv->dp_trace_get_edp_poweron_timestamp =
256 			dp_trace_get_edp_poweron_timestamp;
257 	link_srv->dp_trace_get_edp_poweroff_timestamp =
258 			dp_trace_get_edp_poweroff_timestamp;
259 	link_srv->dp_trace_source_sequence = dp_trace_source_sequence;
260 }
261 
262 static void construct_link_service(struct link_service *link_srv)
263 {
264 	/* All link service functions should fall under some sub categories.
265 	 * If a new function doesn't perfectly fall under an existing sub
266 	 * category, it must be that you are either adding a whole new aspect of
267 	 * responsibility to link service or something doesn't belong to link
268 	 * service. In that case please contact the arch owner to arrange a
269 	 * design review meeting.
270 	 */
271 	construct_link_service_factory(link_srv);
272 	construct_link_service_detection(link_srv);
273 	construct_link_service_resource(link_srv);
274 	construct_link_service_validation(link_srv);
275 	construct_link_service_dpms(link_srv);
276 	construct_link_service_ddc(link_srv);
277 	construct_link_service_dp_capability(link_srv);
278 	construct_link_service_dp_phy_or_dpia(link_srv);
279 	construct_link_service_dp_irq_handler(link_srv);
280 	construct_link_service_edp_panel_control(link_srv);
281 	construct_link_service_dp_cts(link_srv);
282 	construct_link_service_dp_trace(link_srv);
283 }
284 
285 struct link_service *link_create_link_service(void)
286 {
287 	struct link_service *link_srv = kzalloc(sizeof(*link_srv), GFP_KERNEL);
288 
289 	if (link_srv == NULL)
290 		goto fail;
291 
292 	construct_link_service(link_srv);
293 
294 	return link_srv;
295 fail:
296 	return NULL;
297 }
298 
299 void link_destroy_link_service(struct link_service **link_srv)
300 {
301 	kfree(*link_srv);
302 	*link_srv = NULL;
303 }
304 
305 static enum transmitter translate_encoder_to_transmitter(
306 		struct graphics_object_id encoder)
307 {
308 	switch (encoder.id) {
309 	case ENCODER_ID_INTERNAL_UNIPHY:
310 		switch (encoder.enum_id) {
311 		case ENUM_ID_1:
312 			return TRANSMITTER_UNIPHY_A;
313 		case ENUM_ID_2:
314 			return TRANSMITTER_UNIPHY_B;
315 		default:
316 			return TRANSMITTER_UNKNOWN;
317 		}
318 	break;
319 	case ENCODER_ID_INTERNAL_UNIPHY1:
320 		switch (encoder.enum_id) {
321 		case ENUM_ID_1:
322 			return TRANSMITTER_UNIPHY_C;
323 		case ENUM_ID_2:
324 			return TRANSMITTER_UNIPHY_D;
325 		default:
326 			return TRANSMITTER_UNKNOWN;
327 		}
328 	break;
329 	case ENCODER_ID_INTERNAL_UNIPHY2:
330 		switch (encoder.enum_id) {
331 		case ENUM_ID_1:
332 			return TRANSMITTER_UNIPHY_E;
333 		case ENUM_ID_2:
334 			return TRANSMITTER_UNIPHY_F;
335 		default:
336 			return TRANSMITTER_UNKNOWN;
337 		}
338 	break;
339 	case ENCODER_ID_INTERNAL_UNIPHY3:
340 		switch (encoder.enum_id) {
341 		case ENUM_ID_1:
342 			return TRANSMITTER_UNIPHY_G;
343 		default:
344 			return TRANSMITTER_UNKNOWN;
345 		}
346 	break;
347 	case ENCODER_ID_EXTERNAL_NUTMEG:
348 		switch (encoder.enum_id) {
349 		case ENUM_ID_1:
350 			return TRANSMITTER_NUTMEG_CRT;
351 		default:
352 			return TRANSMITTER_UNKNOWN;
353 		}
354 	break;
355 	case ENCODER_ID_EXTERNAL_TRAVIS:
356 		switch (encoder.enum_id) {
357 		case ENUM_ID_1:
358 			return TRANSMITTER_TRAVIS_CRT;
359 		case ENUM_ID_2:
360 			return TRANSMITTER_TRAVIS_LCD;
361 		default:
362 			return TRANSMITTER_UNKNOWN;
363 		}
364 	break;
365 	default:
366 		return TRANSMITTER_UNKNOWN;
367 	}
368 }
369 
370 static void link_destruct(struct dc_link *link)
371 {
372 	int i;
373 
374 	if (link->hpd_gpio) {
375 		dal_gpio_destroy_irq(&link->hpd_gpio);
376 		link->hpd_gpio = NULL;
377 	}
378 
379 	if (link->ddc)
380 		link_destroy_ddc_service(&link->ddc);
381 
382 	if (link->panel_cntl)
383 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
384 
385 	if (link->link_enc) {
386 		/* Update link encoder resource tracking variables. These are used for
387 		 * the dynamic assignment of link encoders to streams. Virtual links
388 		 * are not assigned encoder resources on creation.
389 		 */
390 		if (link->link_id.id != CONNECTOR_ID_VIRTUAL) {
391 			link->dc->res_pool->link_encoders[link->eng_id - ENGINE_ID_DIGA] = NULL;
392 			link->dc->res_pool->dig_link_enc_count--;
393 		}
394 		link->link_enc->funcs->destroy(&link->link_enc);
395 	}
396 
397 	if (link->local_sink)
398 		dc_sink_release(link->local_sink);
399 
400 	for (i = 0; i < link->sink_count; ++i)
401 		dc_sink_release(link->remote_sinks[i]);
402 }
403 
404 static enum channel_id get_ddc_line(struct dc_link *link)
405 {
406 	struct ddc *ddc;
407 	enum channel_id channel;
408 
409 	channel = CHANNEL_ID_UNKNOWN;
410 
411 	ddc = get_ddc_pin(link->ddc);
412 
413 	if (ddc) {
414 		switch (dal_ddc_get_line(ddc)) {
415 		case GPIO_DDC_LINE_DDC1:
416 			channel = CHANNEL_ID_DDC1;
417 			break;
418 		case GPIO_DDC_LINE_DDC2:
419 			channel = CHANNEL_ID_DDC2;
420 			break;
421 		case GPIO_DDC_LINE_DDC3:
422 			channel = CHANNEL_ID_DDC3;
423 			break;
424 		case GPIO_DDC_LINE_DDC4:
425 			channel = CHANNEL_ID_DDC4;
426 			break;
427 		case GPIO_DDC_LINE_DDC5:
428 			channel = CHANNEL_ID_DDC5;
429 			break;
430 		case GPIO_DDC_LINE_DDC6:
431 			channel = CHANNEL_ID_DDC6;
432 			break;
433 		case GPIO_DDC_LINE_DDC_VGA:
434 			channel = CHANNEL_ID_DDC_VGA;
435 			break;
436 		case GPIO_DDC_LINE_I2C_PAD:
437 			channel = CHANNEL_ID_I2C_PAD;
438 			break;
439 		default:
440 			BREAK_TO_DEBUGGER();
441 			break;
442 		}
443 	}
444 
445 	return channel;
446 }
447 
448 static bool construct_phy(struct dc_link *link,
449 			      const struct link_init_data *init_params)
450 {
451 	uint8_t i;
452 	struct ddc_service_init_data ddc_service_init_data = { 0 };
453 	struct dc_context *dc_ctx = init_params->ctx;
454 	struct encoder_init_data enc_init_data = { 0 };
455 	struct panel_cntl_init_data panel_cntl_init_data = { 0 };
456 	struct integrated_info info = { 0 };
457 	struct dc_bios *bios = init_params->dc->ctx->dc_bios;
458 	const struct dc_vbios_funcs *bp_funcs = bios->funcs;
459 	struct bp_disp_connector_caps_info disp_connect_caps_info = { 0 };
460 
461 	DC_LOGGER_INIT(dc_ctx->logger);
462 
463 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
464 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
465 	link->link_status.dpcd_caps = &link->dpcd_caps;
466 
467 	link->dc = init_params->dc;
468 	link->ctx = dc_ctx;
469 	link->link_index = init_params->link_index;
470 
471 	memset(&link->preferred_training_settings, 0,
472 	       sizeof(struct dc_link_training_overrides));
473 	memset(&link->preferred_link_setting, 0,
474 	       sizeof(struct dc_link_settings));
475 
476 	link->link_id =
477 		bios->funcs->get_connector_id(bios, init_params->connector_index);
478 
479 	link->ep_type = DISPLAY_ENDPOINT_PHY;
480 
481 	DC_LOG_DC("BIOS object table - link_id: %d", link->link_id.id);
482 
483 	if (bios->funcs->get_disp_connector_caps_info) {
484 		bios->funcs->get_disp_connector_caps_info(bios, link->link_id, &disp_connect_caps_info);
485 		link->is_internal_display = disp_connect_caps_info.INTERNAL_DISPLAY;
486 		DC_LOG_DC("BIOS object table - is_internal_display: %d", link->is_internal_display);
487 	}
488 
489 	if (link->link_id.type != OBJECT_TYPE_CONNECTOR) {
490 		dm_output_to_console("%s: Invalid Connector ObjectID from Adapter Service for connector index:%d! type %d expected %d\n",
491 				     __func__, init_params->connector_index,
492 				     link->link_id.type, OBJECT_TYPE_CONNECTOR);
493 		goto create_fail;
494 	}
495 
496 	if (link->dc->res_pool->funcs->link_init)
497 		link->dc->res_pool->funcs->link_init(link);
498 
499 	link->hpd_gpio = link_get_hpd_gpio(link->ctx->dc_bios, link->link_id,
500 				      link->ctx->gpio_service);
501 
502 	if (link->hpd_gpio) {
503 		dal_gpio_open(link->hpd_gpio, GPIO_MODE_INTERRUPT);
504 		dal_gpio_unlock_pin(link->hpd_gpio);
505 		link->irq_source_hpd = dal_irq_get_source(link->hpd_gpio);
506 
507 		DC_LOG_DC("BIOS object table - hpd_gpio id: %d", link->hpd_gpio->id);
508 		DC_LOG_DC("BIOS object table - hpd_gpio en: %d", link->hpd_gpio->en);
509 	}
510 
511 	switch (link->link_id.id) {
512 	case CONNECTOR_ID_HDMI_TYPE_A:
513 		link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
514 
515 		break;
516 	case CONNECTOR_ID_SINGLE_LINK_DVID:
517 	case CONNECTOR_ID_SINGLE_LINK_DVII:
518 		link->connector_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
519 		break;
520 	case CONNECTOR_ID_DUAL_LINK_DVID:
521 	case CONNECTOR_ID_DUAL_LINK_DVII:
522 		link->connector_signal = SIGNAL_TYPE_DVI_DUAL_LINK;
523 		break;
524 	case CONNECTOR_ID_DISPLAY_PORT:
525 	case CONNECTOR_ID_USBC:
526 		link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
527 
528 		if (link->hpd_gpio)
529 			link->irq_source_hpd_rx =
530 					dal_irq_get_rx_source(link->hpd_gpio);
531 
532 		break;
533 	case CONNECTOR_ID_EDP:
534 		link->connector_signal = SIGNAL_TYPE_EDP;
535 
536 		if (link->hpd_gpio) {
537 			if (!link->dc->config.allow_edp_hotplug_detection)
538 				link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
539 
540 			switch (link->dc->config.allow_edp_hotplug_detection) {
541 			case HPD_EN_FOR_ALL_EDP:
542 				link->irq_source_hpd_rx =
543 						dal_irq_get_rx_source(link->hpd_gpio);
544 				break;
545 			case HPD_EN_FOR_PRIMARY_EDP_ONLY:
546 				if (link->link_index == 0)
547 					link->irq_source_hpd_rx =
548 						dal_irq_get_rx_source(link->hpd_gpio);
549 				else
550 					link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
551 				break;
552 			case HPD_EN_FOR_SECONDARY_EDP_ONLY:
553 				if (link->link_index == 1)
554 					link->irq_source_hpd_rx =
555 						dal_irq_get_rx_source(link->hpd_gpio);
556 				else
557 					link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
558 				break;
559 			default:
560 				link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
561 				break;
562 			}
563 		}
564 
565 		break;
566 	case CONNECTOR_ID_LVDS:
567 		link->connector_signal = SIGNAL_TYPE_LVDS;
568 		break;
569 	default:
570 		DC_LOG_WARNING("Unsupported Connector type:%d!\n",
571 			       link->link_id.id);
572 		goto create_fail;
573 	}
574 
575 	LINK_INFO("Connector[%d] description: signal: %s\n",
576 		  init_params->connector_index,
577 		  signal_type_to_string(link->connector_signal));
578 
579 	ddc_service_init_data.ctx = link->ctx;
580 	ddc_service_init_data.id = link->link_id;
581 	ddc_service_init_data.link = link;
582 	link->ddc = link_create_ddc_service(&ddc_service_init_data);
583 
584 	if (!link->ddc) {
585 		DC_ERROR("Failed to create ddc_service!\n");
586 		goto ddc_create_fail;
587 	}
588 
589 	if (!link->ddc->ddc_pin) {
590 		DC_ERROR("Failed to get I2C info for connector!\n");
591 		goto ddc_create_fail;
592 	}
593 
594 	link->ddc_hw_inst =
595 		dal_ddc_get_line(get_ddc_pin(link->ddc));
596 
597 
598 	if (link->dc->res_pool->funcs->panel_cntl_create &&
599 		(link->link_id.id == CONNECTOR_ID_EDP ||
600 			link->link_id.id == CONNECTOR_ID_LVDS)) {
601 		panel_cntl_init_data.ctx = dc_ctx;
602 		panel_cntl_init_data.inst =
603 			panel_cntl_init_data.ctx->dc_edp_id_count;
604 		link->panel_cntl =
605 			link->dc->res_pool->funcs->panel_cntl_create(
606 								&panel_cntl_init_data);
607 		panel_cntl_init_data.ctx->dc_edp_id_count++;
608 
609 		if (link->panel_cntl == NULL) {
610 			DC_ERROR("Failed to create link panel_cntl!\n");
611 			goto panel_cntl_create_fail;
612 		}
613 	}
614 
615 	enc_init_data.ctx = dc_ctx;
616 	bp_funcs->get_src_obj(dc_ctx->dc_bios, link->link_id, 0,
617 			      &enc_init_data.encoder);
618 	enc_init_data.connector = link->link_id;
619 	enc_init_data.channel = get_ddc_line(link);
620 	enc_init_data.hpd_source = get_hpd_line(link);
621 
622 	link->hpd_src = enc_init_data.hpd_source;
623 
624 	enc_init_data.transmitter =
625 		translate_encoder_to_transmitter(enc_init_data.encoder);
626 	link->link_enc =
627 		link->dc->res_pool->funcs->link_enc_create(dc_ctx, &enc_init_data);
628 
629 	DC_LOG_DC("BIOS object table - DP_IS_USB_C: %d", link->link_enc->features.flags.bits.DP_IS_USB_C);
630 	DC_LOG_DC("BIOS object table - IS_DP2_CAPABLE: %d", link->link_enc->features.flags.bits.IS_DP2_CAPABLE);
631 
632 	if (!link->link_enc) {
633 		DC_ERROR("Failed to create link encoder!\n");
634 		goto link_enc_create_fail;
635 	}
636 
637 	/* Update link encoder tracking variables. These are used for the dynamic
638 	 * assignment of link encoders to streams.
639 	 */
640 	link->eng_id = link->link_enc->preferred_engine;
641 	link->dc->res_pool->link_encoders[link->eng_id - ENGINE_ID_DIGA] = link->link_enc;
642 	link->dc->res_pool->dig_link_enc_count++;
643 
644 	link->link_enc_hw_inst = link->link_enc->transmitter;
645 	for (i = 0; i < 4; i++) {
646 		if (bp_funcs->get_device_tag(dc_ctx->dc_bios,
647 					     link->link_id, i,
648 					     &link->device_tag) != BP_RESULT_OK) {
649 			DC_ERROR("Failed to find device tag!\n");
650 			goto device_tag_fail;
651 		}
652 
653 		/* Look for device tag that matches connector signal,
654 		 * CRT for rgb, LCD for other supported signal tyes
655 		 */
656 		if (!bp_funcs->is_device_id_supported(dc_ctx->dc_bios,
657 						      link->device_tag.dev_id))
658 			continue;
659 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_CRT &&
660 		    link->connector_signal != SIGNAL_TYPE_RGB)
661 			continue;
662 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_LCD &&
663 		    link->connector_signal == SIGNAL_TYPE_RGB)
664 			continue;
665 
666 		DC_LOG_DC("BIOS object table - device_tag.acpi_device: %d", link->device_tag.acpi_device);
667 		DC_LOG_DC("BIOS object table - device_tag.dev_id.device_type: %d", link->device_tag.dev_id.device_type);
668 		DC_LOG_DC("BIOS object table - device_tag.dev_id.enum_id: %d", link->device_tag.dev_id.enum_id);
669 		break;
670 	}
671 
672 	if (bios->integrated_info)
673 		info = *bios->integrated_info;
674 
675 	/* Look for channel mapping corresponding to connector and device tag */
676 	for (i = 0; i < MAX_NUMBER_OF_EXT_DISPLAY_PATH; i++) {
677 		struct external_display_path *path =
678 			&info.ext_disp_conn_info.path[i];
679 
680 		if (path->device_connector_id.enum_id == link->link_id.enum_id &&
681 		    path->device_connector_id.id == link->link_id.id &&
682 		    path->device_connector_id.type == link->link_id.type) {
683 			if (link->device_tag.acpi_device != 0 &&
684 			    path->device_acpi_enum == link->device_tag.acpi_device) {
685 				link->ddi_channel_mapping = path->channel_mapping;
686 				link->chip_caps = path->caps;
687 				DC_LOG_DC("BIOS object table - ddi_channel_mapping: 0x%04X", link->ddi_channel_mapping.raw);
688 				DC_LOG_DC("BIOS object table - chip_caps: %d", link->chip_caps);
689 			} else if (path->device_tag ==
690 				   link->device_tag.dev_id.raw_device_tag) {
691 				link->ddi_channel_mapping = path->channel_mapping;
692 				link->chip_caps = path->caps;
693 				DC_LOG_DC("BIOS object table - ddi_channel_mapping: 0x%04X", link->ddi_channel_mapping.raw);
694 				DC_LOG_DC("BIOS object table - chip_caps: %d", link->chip_caps);
695 			}
696 
697 			if (link->chip_caps & EXT_DISPLAY_PATH_CAPS__DP_FIXED_VS_EN) {
698 				link->bios_forced_drive_settings.VOLTAGE_SWING =
699 						(info.ext_disp_conn_info.fixdpvoltageswing & 0x3);
700 				link->bios_forced_drive_settings.PRE_EMPHASIS =
701 						((info.ext_disp_conn_info.fixdpvoltageswing >> 2) & 0x3);
702 			}
703 
704 			break;
705 		}
706 	}
707 
708 	if (bios->funcs->get_atom_dc_golden_table)
709 		bios->funcs->get_atom_dc_golden_table(bios);
710 
711 	/*
712 	 * TODO check if GPIO programmed correctly
713 	 *
714 	 * If GPIO isn't programmed correctly HPD might not rise or drain
715 	 * fast enough, leading to bounces.
716 	 */
717 	program_hpd_filter(link);
718 
719 	link->psr_settings.psr_vtotal_control_support = false;
720 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
721 
722 	DC_LOG_DC("BIOS object table - %s finished successfully.\n", __func__);
723 	return true;
724 device_tag_fail:
725 	link->link_enc->funcs->destroy(&link->link_enc);
726 link_enc_create_fail:
727 	if (link->panel_cntl != NULL)
728 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
729 panel_cntl_create_fail:
730 	link_destroy_ddc_service(&link->ddc);
731 ddc_create_fail:
732 create_fail:
733 
734 	if (link->hpd_gpio) {
735 		dal_gpio_destroy_irq(&link->hpd_gpio);
736 		link->hpd_gpio = NULL;
737 	}
738 
739 	DC_LOG_DC("BIOS object table - %s failed.\n", __func__);
740 	return false;
741 }
742 
743 static bool construct_dpia(struct dc_link *link,
744 			      const struct link_init_data *init_params)
745 {
746 	struct ddc_service_init_data ddc_service_init_data = { 0 };
747 	struct dc_context *dc_ctx = init_params->ctx;
748 
749 	DC_LOGGER_INIT(dc_ctx->logger);
750 
751 	/* Initialized irq source for hpd and hpd rx */
752 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
753 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
754 	link->link_status.dpcd_caps = &link->dpcd_caps;
755 
756 	link->dc = init_params->dc;
757 	link->ctx = dc_ctx;
758 	link->link_index = init_params->link_index;
759 
760 	memset(&link->preferred_training_settings, 0,
761 	       sizeof(struct dc_link_training_overrides));
762 	memset(&link->preferred_link_setting, 0,
763 	       sizeof(struct dc_link_settings));
764 
765 	/* Dummy Init for linkid */
766 	link->link_id.type = OBJECT_TYPE_CONNECTOR;
767 	link->link_id.id = CONNECTOR_ID_DISPLAY_PORT;
768 	link->link_id.enum_id = ENUM_ID_1 + init_params->connector_index;
769 	link->is_internal_display = false;
770 	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
771 	LINK_INFO("Connector[%d] description:signal %d\n",
772 		  init_params->connector_index,
773 		  link->connector_signal);
774 
775 	link->ep_type = DISPLAY_ENDPOINT_USB4_DPIA;
776 	link->is_dig_mapping_flexible = true;
777 
778 	/* TODO: Initialize link : funcs->link_init */
779 
780 	ddc_service_init_data.ctx = link->ctx;
781 	ddc_service_init_data.id = link->link_id;
782 	ddc_service_init_data.link = link;
783 	/* Set indicator for dpia link so that ddc wont be created */
784 	ddc_service_init_data.is_dpia_link = true;
785 
786 	link->ddc = link_create_ddc_service(&ddc_service_init_data);
787 	if (!link->ddc) {
788 		DC_ERROR("Failed to create ddc_service!\n");
789 		goto ddc_create_fail;
790 	}
791 
792 	/* Set dpia port index : 0 to number of dpia ports */
793 	link->ddc_hw_inst = init_params->connector_index;
794 
795 	// Assign Dpia preferred eng_id
796 	if (link->dc->res_pool->funcs->get_preferred_eng_id_dpia)
797 		link->dpia_preferred_eng_id = link->dc->res_pool->funcs->get_preferred_eng_id_dpia(link->ddc_hw_inst);
798 
799 	/* TODO: Create link encoder */
800 
801 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
802 
803 	/* Some docks seem to NAK I2C writes to segment pointer with mot=0. */
804 	link->wa_flags.dp_mot_reset_segment = true;
805 
806 	return true;
807 
808 ddc_create_fail:
809 	return false;
810 }
811 
812 static bool link_construct(struct dc_link *link,
813 			      const struct link_init_data *init_params)
814 {
815 	/* Handle dpia case */
816 	if (init_params->is_dpia_link == true)
817 		return construct_dpia(link, init_params);
818 	else
819 		return construct_phy(link, init_params);
820 }
821 
822 struct dc_link *link_create(const struct link_init_data *init_params)
823 {
824 	struct dc_link *link =
825 			kzalloc(sizeof(*link), GFP_KERNEL);
826 
827 	if (NULL == link)
828 		goto alloc_fail;
829 
830 	if (false == link_construct(link, init_params))
831 		goto construct_fail;
832 
833 	return link;
834 
835 construct_fail:
836 	kfree(link);
837 
838 alloc_fail:
839 	return NULL;
840 }
841 
842 void link_destroy(struct dc_link **link)
843 {
844 	link_destruct(*link);
845 	kfree(*link);
846 	*link = NULL;
847 }
848