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. */
construct_link_service_factory(struct link_service * link_srv)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  */
construct_link_service_detection(struct link_service * link_srv)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. */
construct_link_service_resource(struct link_service * link_srv)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  */
construct_link_service_validation(struct link_service * link_srv)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  */
construct_link_service_dpms(struct link_service * link_srv)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  */
construct_link_service_ddc(struct link_service * link_srv)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  */
construct_link_service_dp_capability(struct link_service * link_srv)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  */
construct_link_service_dp_phy_or_dpia(struct link_service * link_srv)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  */
construct_link_service_dp_irq_handler(struct link_service * link_srv)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  */
construct_link_service_edp_panel_control(struct link_service * link_srv)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  */
construct_link_service_dp_cts(struct link_service * link_srv)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  */
construct_link_service_dp_trace(struct link_service * link_srv)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 
construct_link_service(struct link_service * link_srv)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 
link_create_link_service(void)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 
link_destroy_link_service(struct link_service ** link_srv)299 void link_destroy_link_service(struct link_service **link_srv)
300 {
301 	kfree(*link_srv);
302 	*link_srv = NULL;
303 }
304 
translate_encoder_to_transmitter(struct graphics_object_id encoder)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 
translate_dig_inst_to_pwrseq_inst(struct dc_link * link)370 static uint8_t translate_dig_inst_to_pwrseq_inst(struct dc_link *link)
371 {
372 	uint8_t pwrseq_inst = 0xF;
373 
374 	switch (link->eng_id) {
375 	case ENGINE_ID_DIGA:
376 		pwrseq_inst = 0;
377 		break;
378 	case ENGINE_ID_DIGB:
379 		pwrseq_inst = 1;
380 		break;
381 	default:
382 		DC_LOG_WARNING("Unsupported pwrseq engine id: %d!\n", link->eng_id);
383 		ASSERT(false);
384 		break;
385 	}
386 
387 	return pwrseq_inst;
388 }
389 
390 
link_destruct(struct dc_link * link)391 static void link_destruct(struct dc_link *link)
392 {
393 	int i;
394 
395 	if (link->hpd_gpio) {
396 		dal_gpio_destroy_irq(&link->hpd_gpio);
397 		link->hpd_gpio = NULL;
398 	}
399 
400 	if (link->ddc)
401 		link_destroy_ddc_service(&link->ddc);
402 
403 	if (link->panel_cntl)
404 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
405 
406 	if (link->link_enc && !link->is_dig_mapping_flexible) {
407 		/* Update link encoder resource tracking variables. These are used for
408 		 * the dynamic assignment of link encoders to streams. Virtual links
409 		 * are not assigned encoder resources on creation.
410 		 */
411 		if (link->link_id.id != CONNECTOR_ID_VIRTUAL) {
412 			link->dc->res_pool->link_encoders[link->eng_id - ENGINE_ID_DIGA] = NULL;
413 			link->dc->res_pool->dig_link_enc_count--;
414 		}
415 		link->link_enc->funcs->destroy(&link->link_enc);
416 	}
417 
418 	if (link->local_sink)
419 		dc_sink_release(link->local_sink);
420 
421 	for (i = 0; i < link->sink_count; ++i)
422 		dc_sink_release(link->remote_sinks[i]);
423 }
424 
get_ddc_line(struct dc_link * link)425 static enum channel_id get_ddc_line(struct dc_link *link)
426 {
427 	struct ddc *ddc;
428 	enum channel_id channel;
429 
430 	channel = CHANNEL_ID_UNKNOWN;
431 
432 	ddc = get_ddc_pin(link->ddc);
433 
434 	if (ddc) {
435 		switch (dal_ddc_get_line(ddc)) {
436 		case GPIO_DDC_LINE_DDC1:
437 			channel = CHANNEL_ID_DDC1;
438 			break;
439 		case GPIO_DDC_LINE_DDC2:
440 			channel = CHANNEL_ID_DDC2;
441 			break;
442 		case GPIO_DDC_LINE_DDC3:
443 			channel = CHANNEL_ID_DDC3;
444 			break;
445 		case GPIO_DDC_LINE_DDC4:
446 			channel = CHANNEL_ID_DDC4;
447 			break;
448 		case GPIO_DDC_LINE_DDC5:
449 			channel = CHANNEL_ID_DDC5;
450 			break;
451 		case GPIO_DDC_LINE_DDC6:
452 			channel = CHANNEL_ID_DDC6;
453 			break;
454 		case GPIO_DDC_LINE_DDC_VGA:
455 			channel = CHANNEL_ID_DDC_VGA;
456 			break;
457 		case GPIO_DDC_LINE_I2C_PAD:
458 			channel = CHANNEL_ID_I2C_PAD;
459 			break;
460 		default:
461 			BREAK_TO_DEBUGGER();
462 			break;
463 		}
464 	}
465 
466 	return channel;
467 }
468 
construct_phy(struct dc_link * link,const struct link_init_data * init_params)469 static bool construct_phy(struct dc_link *link,
470 			      const struct link_init_data *init_params)
471 {
472 	uint8_t i;
473 	struct ddc_service_init_data ddc_service_init_data = { 0 };
474 	struct dc_context *dc_ctx = init_params->ctx;
475 	struct encoder_init_data enc_init_data = { 0 };
476 	struct panel_cntl_init_data panel_cntl_init_data = { 0 };
477 	struct integrated_info info = { 0 };
478 	struct dc_bios *bios = init_params->dc->ctx->dc_bios;
479 	const struct dc_vbios_funcs *bp_funcs = bios->funcs;
480 	struct bp_disp_connector_caps_info disp_connect_caps_info = { 0 };
481 
482 	DC_LOGGER_INIT(dc_ctx->logger);
483 
484 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
485 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
486 	link->link_status.dpcd_caps = &link->dpcd_caps;
487 
488 	link->dc = init_params->dc;
489 	link->ctx = dc_ctx;
490 	link->link_index = init_params->link_index;
491 
492 	memset(&link->preferred_training_settings, 0,
493 	       sizeof(struct dc_link_training_overrides));
494 	memset(&link->preferred_link_setting, 0,
495 	       sizeof(struct dc_link_settings));
496 
497 	link->link_id =
498 		bios->funcs->get_connector_id(bios, init_params->connector_index);
499 
500 	link->ep_type = DISPLAY_ENDPOINT_PHY;
501 
502 	DC_LOG_DC("BIOS object table - link_id: %d", link->link_id.id);
503 
504 	if (bios->funcs->get_disp_connector_caps_info) {
505 		bios->funcs->get_disp_connector_caps_info(bios, link->link_id, &disp_connect_caps_info);
506 		link->is_internal_display = disp_connect_caps_info.INTERNAL_DISPLAY;
507 		DC_LOG_DC("BIOS object table - is_internal_display: %d", link->is_internal_display);
508 	}
509 
510 	if (link->link_id.type != OBJECT_TYPE_CONNECTOR) {
511 		dm_output_to_console("%s: Invalid Connector ObjectID from Adapter Service for connector index:%d! type %d expected %d\n",
512 				     __func__, init_params->connector_index,
513 				     link->link_id.type, OBJECT_TYPE_CONNECTOR);
514 		goto create_fail;
515 	}
516 
517 	if (link->dc->res_pool->funcs->link_init)
518 		link->dc->res_pool->funcs->link_init(link);
519 
520 	link->hpd_gpio = link_get_hpd_gpio(link->ctx->dc_bios, link->link_id,
521 				      link->ctx->gpio_service);
522 
523 	if (link->hpd_gpio) {
524 		dal_gpio_open(link->hpd_gpio, GPIO_MODE_INTERRUPT);
525 		dal_gpio_unlock_pin(link->hpd_gpio);
526 		link->irq_source_hpd = dal_irq_get_source(link->hpd_gpio);
527 
528 		DC_LOG_DC("BIOS object table - hpd_gpio id: %d", link->hpd_gpio->id);
529 		DC_LOG_DC("BIOS object table - hpd_gpio en: %d", link->hpd_gpio->en);
530 	}
531 
532 	switch (link->link_id.id) {
533 	case CONNECTOR_ID_HDMI_TYPE_A:
534 		link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
535 
536 		break;
537 	case CONNECTOR_ID_SINGLE_LINK_DVID:
538 	case CONNECTOR_ID_SINGLE_LINK_DVII:
539 		link->connector_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
540 		break;
541 	case CONNECTOR_ID_DUAL_LINK_DVID:
542 	case CONNECTOR_ID_DUAL_LINK_DVII:
543 		link->connector_signal = SIGNAL_TYPE_DVI_DUAL_LINK;
544 		break;
545 	case CONNECTOR_ID_DISPLAY_PORT:
546 	case CONNECTOR_ID_USBC:
547 		link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
548 
549 		if (link->hpd_gpio)
550 			link->irq_source_hpd_rx =
551 					dal_irq_get_rx_source(link->hpd_gpio);
552 
553 		break;
554 	case CONNECTOR_ID_EDP:
555 		link->connector_signal = SIGNAL_TYPE_EDP;
556 
557 		if (link->hpd_gpio) {
558 			if (!link->dc->config.allow_edp_hotplug_detection)
559 				link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
560 
561 			switch (link->dc->config.allow_edp_hotplug_detection) {
562 			case HPD_EN_FOR_ALL_EDP:
563 				link->irq_source_hpd_rx =
564 						dal_irq_get_rx_source(link->hpd_gpio);
565 				break;
566 			case HPD_EN_FOR_PRIMARY_EDP_ONLY:
567 				if (link->link_index == 0)
568 					link->irq_source_hpd_rx =
569 						dal_irq_get_rx_source(link->hpd_gpio);
570 				else
571 					link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
572 				break;
573 			case HPD_EN_FOR_SECONDARY_EDP_ONLY:
574 				if (link->link_index == 1)
575 					link->irq_source_hpd_rx =
576 						dal_irq_get_rx_source(link->hpd_gpio);
577 				else
578 					link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
579 				break;
580 			default:
581 				link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
582 				break;
583 			}
584 		}
585 
586 		break;
587 	case CONNECTOR_ID_LVDS:
588 		link->connector_signal = SIGNAL_TYPE_LVDS;
589 		break;
590 	default:
591 		DC_LOG_WARNING("Unsupported Connector type:%d!\n",
592 			       link->link_id.id);
593 		goto create_fail;
594 	}
595 
596 	LINK_INFO("Connector[%d] description: signal: %s\n",
597 		  init_params->connector_index,
598 		  signal_type_to_string(link->connector_signal));
599 
600 	ddc_service_init_data.ctx = link->ctx;
601 	ddc_service_init_data.id = link->link_id;
602 	ddc_service_init_data.link = link;
603 	link->ddc = link_create_ddc_service(&ddc_service_init_data);
604 
605 	if (!link->ddc) {
606 		DC_ERROR("Failed to create ddc_service!\n");
607 		goto ddc_create_fail;
608 	}
609 
610 	if (!link->ddc->ddc_pin) {
611 		DC_ERROR("Failed to get I2C info for connector!\n");
612 		goto ddc_create_fail;
613 	}
614 
615 	link->ddc_hw_inst =
616 		dal_ddc_get_line(get_ddc_pin(link->ddc));
617 
618 	enc_init_data.ctx = dc_ctx;
619 	bp_funcs->get_src_obj(dc_ctx->dc_bios, link->link_id, 0,
620 			      &enc_init_data.encoder);
621 	enc_init_data.connector = link->link_id;
622 	enc_init_data.channel = get_ddc_line(link);
623 	enc_init_data.hpd_source = get_hpd_line(link);
624 
625 	link->hpd_src = enc_init_data.hpd_source;
626 
627 	enc_init_data.transmitter =
628 		translate_encoder_to_transmitter(enc_init_data.encoder);
629 	link->link_enc =
630 		link->dc->res_pool->funcs->link_enc_create(dc_ctx, &enc_init_data);
631 
632 	if (!link->link_enc) {
633 		DC_ERROR("Failed to create link encoder!\n");
634 		goto link_enc_create_fail;
635 	}
636 
637 	DC_LOG_DC("BIOS object table - DP_IS_USB_C: %d", link->link_enc->features.flags.bits.DP_IS_USB_C);
638 	DC_LOG_DC("BIOS object table - IS_DP2_CAPABLE: %d", link->link_enc->features.flags.bits.IS_DP2_CAPABLE);
639 
640 	/* Update link encoder tracking variables. These are used for the dynamic
641 	 * assignment of link encoders to streams.
642 	 */
643 	link->eng_id = link->link_enc->preferred_engine;
644 	link->dc->res_pool->link_encoders[link->eng_id - ENGINE_ID_DIGA] = link->link_enc;
645 	link->dc->res_pool->dig_link_enc_count++;
646 
647 	link->link_enc_hw_inst = link->link_enc->transmitter;
648 
649 	if (link->dc->res_pool->funcs->panel_cntl_create &&
650 		(link->link_id.id == CONNECTOR_ID_EDP ||
651 			link->link_id.id == CONNECTOR_ID_LVDS)) {
652 		panel_cntl_init_data.ctx = dc_ctx;
653 		panel_cntl_init_data.inst = panel_cntl_init_data.ctx->dc_edp_id_count;
654 		panel_cntl_init_data.pwrseq_inst = translate_dig_inst_to_pwrseq_inst(link);
655 		link->panel_cntl =
656 			link->dc->res_pool->funcs->panel_cntl_create(
657 								&panel_cntl_init_data);
658 		panel_cntl_init_data.ctx->dc_edp_id_count++;
659 
660 		if (link->panel_cntl == NULL) {
661 			DC_ERROR("Failed to create link panel_cntl!\n");
662 			goto panel_cntl_create_fail;
663 		}
664 	}
665 	for (i = 0; i < 4; i++) {
666 		if (bp_funcs->get_device_tag(dc_ctx->dc_bios,
667 					     link->link_id, i,
668 					     &link->device_tag) != BP_RESULT_OK) {
669 			DC_ERROR("Failed to find device tag!\n");
670 			goto device_tag_fail;
671 		}
672 
673 		/* Look for device tag that matches connector signal,
674 		 * CRT for rgb, LCD for other supported signal tyes
675 		 */
676 		if (!bp_funcs->is_device_id_supported(dc_ctx->dc_bios,
677 						      link->device_tag.dev_id))
678 			continue;
679 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_CRT &&
680 		    link->connector_signal != SIGNAL_TYPE_RGB)
681 			continue;
682 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_LCD &&
683 		    link->connector_signal == SIGNAL_TYPE_RGB)
684 			continue;
685 
686 		DC_LOG_DC("BIOS object table - device_tag.acpi_device: %d", link->device_tag.acpi_device);
687 		DC_LOG_DC("BIOS object table - device_tag.dev_id.device_type: %d", link->device_tag.dev_id.device_type);
688 		DC_LOG_DC("BIOS object table - device_tag.dev_id.enum_id: %d", link->device_tag.dev_id.enum_id);
689 		break;
690 	}
691 
692 	if (bios->integrated_info)
693 		info = *bios->integrated_info;
694 
695 	/* Look for channel mapping corresponding to connector and device tag */
696 	for (i = 0; i < MAX_NUMBER_OF_EXT_DISPLAY_PATH; i++) {
697 		struct external_display_path *path =
698 			&info.ext_disp_conn_info.path[i];
699 
700 		if (path->device_connector_id.enum_id == link->link_id.enum_id &&
701 		    path->device_connector_id.id == link->link_id.id &&
702 		    path->device_connector_id.type == link->link_id.type) {
703 			if (link->device_tag.acpi_device != 0 &&
704 			    path->device_acpi_enum == link->device_tag.acpi_device) {
705 				link->ddi_channel_mapping = path->channel_mapping;
706 				link->chip_caps = path->caps;
707 				DC_LOG_DC("BIOS object table - ddi_channel_mapping: 0x%04X", link->ddi_channel_mapping.raw);
708 				DC_LOG_DC("BIOS object table - chip_caps: %d", link->chip_caps);
709 			} else if (path->device_tag ==
710 				   link->device_tag.dev_id.raw_device_tag) {
711 				link->ddi_channel_mapping = path->channel_mapping;
712 				link->chip_caps = path->caps;
713 				DC_LOG_DC("BIOS object table - ddi_channel_mapping: 0x%04X", link->ddi_channel_mapping.raw);
714 				DC_LOG_DC("BIOS object table - chip_caps: %d", link->chip_caps);
715 			}
716 
717 			if (link->chip_caps & EXT_DISPLAY_PATH_CAPS__DP_FIXED_VS_EN) {
718 				link->bios_forced_drive_settings.VOLTAGE_SWING =
719 						(info.ext_disp_conn_info.fixdpvoltageswing & 0x3);
720 				link->bios_forced_drive_settings.PRE_EMPHASIS =
721 						((info.ext_disp_conn_info.fixdpvoltageswing >> 2) & 0x3);
722 			}
723 
724 			break;
725 		}
726 	}
727 
728 	if (bios->funcs->get_atom_dc_golden_table)
729 		bios->funcs->get_atom_dc_golden_table(bios);
730 
731 	/*
732 	 * TODO check if GPIO programmed correctly
733 	 *
734 	 * If GPIO isn't programmed correctly HPD might not rise or drain
735 	 * fast enough, leading to bounces.
736 	 */
737 	program_hpd_filter(link);
738 
739 	link->psr_settings.psr_vtotal_control_support = false;
740 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
741 
742 	DC_LOG_DC("BIOS object table - %s finished successfully.\n", __func__);
743 	return true;
744 device_tag_fail:
745 	link->link_enc->funcs->destroy(&link->link_enc);
746 link_enc_create_fail:
747 	if (link->panel_cntl != NULL)
748 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
749 panel_cntl_create_fail:
750 	link_destroy_ddc_service(&link->ddc);
751 ddc_create_fail:
752 create_fail:
753 
754 	if (link->hpd_gpio) {
755 		dal_gpio_destroy_irq(&link->hpd_gpio);
756 		link->hpd_gpio = NULL;
757 	}
758 
759 	DC_LOG_DC("BIOS object table - %s failed.\n", __func__);
760 	return false;
761 }
762 
construct_dpia(struct dc_link * link,const struct link_init_data * init_params)763 static bool construct_dpia(struct dc_link *link,
764 			      const struct link_init_data *init_params)
765 {
766 	struct ddc_service_init_data ddc_service_init_data = { 0 };
767 	struct dc_context *dc_ctx = init_params->ctx;
768 
769 	DC_LOGGER_INIT(dc_ctx->logger);
770 
771 	/* Initialized irq source for hpd and hpd rx */
772 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
773 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
774 	link->link_status.dpcd_caps = &link->dpcd_caps;
775 
776 	link->dc = init_params->dc;
777 	link->ctx = dc_ctx;
778 	link->link_index = init_params->link_index;
779 
780 	memset(&link->preferred_training_settings, 0,
781 	       sizeof(struct dc_link_training_overrides));
782 	memset(&link->preferred_link_setting, 0,
783 	       sizeof(struct dc_link_settings));
784 
785 	/* Dummy Init for linkid */
786 	link->link_id.type = OBJECT_TYPE_CONNECTOR;
787 	link->link_id.id = CONNECTOR_ID_DISPLAY_PORT;
788 	link->link_id.enum_id = ENUM_ID_1 + init_params->connector_index;
789 	link->is_internal_display = false;
790 	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
791 	LINK_INFO("Connector[%d] description:signal %d\n",
792 		  init_params->connector_index,
793 		  link->connector_signal);
794 
795 	link->ep_type = DISPLAY_ENDPOINT_USB4_DPIA;
796 	link->is_dig_mapping_flexible = true;
797 
798 	/* TODO: Initialize link : funcs->link_init */
799 
800 	ddc_service_init_data.ctx = link->ctx;
801 	ddc_service_init_data.id = link->link_id;
802 	ddc_service_init_data.link = link;
803 	/* Set indicator for dpia link so that ddc wont be created */
804 	ddc_service_init_data.is_dpia_link = true;
805 
806 	link->ddc = link_create_ddc_service(&ddc_service_init_data);
807 	if (!link->ddc) {
808 		DC_ERROR("Failed to create ddc_service!\n");
809 		goto ddc_create_fail;
810 	}
811 
812 	/* Set dpia port index : 0 to number of dpia ports */
813 	link->ddc_hw_inst = init_params->connector_index;
814 
815 	// Assign Dpia preferred eng_id
816 	if (link->dc->res_pool->funcs->get_preferred_eng_id_dpia)
817 		link->dpia_preferred_eng_id = link->dc->res_pool->funcs->get_preferred_eng_id_dpia(link->ddc_hw_inst);
818 
819 	/* TODO: Create link encoder */
820 
821 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
822 
823 	/* Some docks seem to NAK I2C writes to segment pointer with mot=0. */
824 	link->wa_flags.dp_mot_reset_segment = true;
825 
826 	return true;
827 
828 ddc_create_fail:
829 	return false;
830 }
831 
link_construct(struct dc_link * link,const struct link_init_data * init_params)832 static bool link_construct(struct dc_link *link,
833 			      const struct link_init_data *init_params)
834 {
835 	/* Handle dpia case */
836 	if (init_params->is_dpia_link == true)
837 		return construct_dpia(link, init_params);
838 	else
839 		return construct_phy(link, init_params);
840 }
841 
link_create(const struct link_init_data * init_params)842 struct dc_link *link_create(const struct link_init_data *init_params)
843 {
844 	struct dc_link *link =
845 			kzalloc(sizeof(*link), GFP_KERNEL);
846 
847 	if (NULL == link)
848 		goto alloc_fail;
849 
850 	if (false == link_construct(link, init_params))
851 		goto construct_fail;
852 
853 	return link;
854 
855 construct_fail:
856 	kfree(link);
857 
858 alloc_fail:
859 	return NULL;
860 }
861 
link_destroy(struct dc_link ** link)862 void link_destroy(struct dc_link **link)
863 {
864 	link_destruct(*link);
865 	kfree(*link);
866 	*link = NULL;
867 }
868