1 /* Copyright 2021 Advanced Micro Devices, Inc. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
17  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19  * OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Authors: AMD
22  *
23  */
24 
25 #include "link_enc_cfg.h"
26 #include "resource.h"
27 #include "link.h"
28 
29 #define DC_LOGGER dc->ctx->logger
30 
31 /* Check whether stream is supported by DIG link encoders. */
32 static bool is_dig_link_enc_stream(struct dc_stream_state *stream)
33 {
34 	bool is_dig_stream = false;
35 	struct link_encoder *link_enc = NULL;
36 	int i;
37 
38 	/* Loop over created link encoder objects. */
39 	if (stream) {
40 		for (i = 0; i < stream->ctx->dc->res_pool->res_cap->num_dig_link_enc; i++) {
41 			link_enc = stream->ctx->dc->res_pool->link_encoders[i];
42 
43 			/* Need to check link signal type rather than stream signal type which may not
44 			 * yet match.
45 			 */
46 			if (link_enc && ((uint32_t)stream->link->connector_signal & link_enc->output_signals)) {
47 				if (dc_is_dp_signal(stream->signal)) {
48 					/* DIGs do not support DP2.0 streams with 128b/132b encoding. */
49 					struct dc_link_settings link_settings = {0};
50 
51 					link_decide_link_settings(stream, &link_settings);
52 					if ((link_settings.link_rate >= LINK_RATE_LOW) &&
53 							link_settings.link_rate <= LINK_RATE_HIGH3) {
54 						is_dig_stream = true;
55 						break;
56 					}
57 				} else {
58 					is_dig_stream = true;
59 					break;
60 				}
61 			}
62 		}
63 	}
64 	return is_dig_stream;
65 }
66 
67 static struct link_enc_assignment get_assignment(struct dc *dc, int i)
68 {
69 	struct link_enc_assignment assignment;
70 
71 	if (dc->current_state->res_ctx.link_enc_cfg_ctx.mode == LINK_ENC_CFG_TRANSIENT)
72 		assignment = dc->current_state->res_ctx.link_enc_cfg_ctx.transient_assignments[i];
73 	else /* LINK_ENC_CFG_STEADY */
74 		assignment = dc->current_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
75 
76 	return assignment;
77 }
78 
79 /* Return stream using DIG link encoder resource. NULL if unused. */
80 static struct dc_stream_state *get_stream_using_link_enc(
81 		struct dc_state *state,
82 		enum engine_id eng_id)
83 {
84 	struct dc_stream_state *stream = NULL;
85 	int i;
86 
87 	for (i = 0; i < state->stream_count; i++) {
88 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
89 
90 		if ((assignment.valid == true) && (assignment.eng_id == eng_id)) {
91 			stream = state->streams[i];
92 			break;
93 		}
94 	}
95 
96 	return stream;
97 }
98 
99 static void remove_link_enc_assignment(
100 		struct dc_state *state,
101 		struct dc_stream_state *stream,
102 		enum engine_id eng_id)
103 {
104 	int eng_idx;
105 	int i;
106 
107 	if (eng_id != ENGINE_ID_UNKNOWN) {
108 		eng_idx = eng_id - ENGINE_ID_DIGA;
109 
110 		/* stream ptr of stream in dc_state used to update correct entry in
111 		 * link_enc_assignments table.
112 		 */
113 		for (i = 0; i < MAX_PIPES; i++) {
114 			struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
115 
116 			if (assignment.valid && assignment.stream == stream) {
117 				state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].valid = false;
118 				/* Only add link encoder back to availability pool if not being
119 				 * used by any other stream (i.e. removing SST stream or last MST stream).
120 				 */
121 				if (get_stream_using_link_enc(state, eng_id) == NULL)
122 					state->res_ctx.link_enc_cfg_ctx.link_enc_avail[eng_idx] = eng_id;
123 
124 				stream->link_enc = NULL;
125 				state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].eng_id = ENGINE_ID_UNKNOWN;
126 				state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream = NULL;
127 				dc_stream_release(stream);
128 				break;
129 			}
130 		}
131 	}
132 }
133 
134 static void add_link_enc_assignment(
135 		struct dc_state *state,
136 		struct dc_stream_state *stream,
137 		enum engine_id eng_id)
138 {
139 	int eng_idx;
140 	int i;
141 
142 	if (eng_id != ENGINE_ID_UNKNOWN) {
143 		eng_idx = eng_id - ENGINE_ID_DIGA;
144 
145 		/* stream ptr of stream in dc_state used to update correct entry in
146 		 * link_enc_assignments table.
147 		 */
148 		for (i = 0; i < state->stream_count; i++) {
149 			if (stream == state->streams[i]) {
150 				state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i] = (struct link_enc_assignment){
151 					.valid = true,
152 					.ep_id = (struct display_endpoint_id) {
153 						.link_id = stream->link->link_id,
154 						.ep_type = stream->link->ep_type},
155 					.eng_id = eng_id,
156 					.stream = stream};
157 				dc_stream_retain(stream);
158 				state->res_ctx.link_enc_cfg_ctx.link_enc_avail[eng_idx] = ENGINE_ID_UNKNOWN;
159 				stream->link_enc = stream->ctx->dc->res_pool->link_encoders[eng_idx];
160 				break;
161 			}
162 		}
163 
164 		/* Attempted to add an encoder assignment for a stream not in dc_state. */
165 		ASSERT(i != state->stream_count);
166 	}
167 }
168 
169 /* Return first available DIG link encoder. */
170 static enum engine_id find_first_avail_link_enc(
171 		const struct dc_context *ctx,
172 		const struct dc_state *state)
173 {
174 	enum engine_id eng_id = ENGINE_ID_UNKNOWN;
175 	int i;
176 
177 	for (i = 0; i < ctx->dc->res_pool->res_cap->num_dig_link_enc; i++) {
178 		eng_id = state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i];
179 		if (eng_id != ENGINE_ID_UNKNOWN)
180 			break;
181 	}
182 
183 	return eng_id;
184 }
185 
186 /* Check for availability of link encoder eng_id. */
187 static bool is_avail_link_enc(struct dc_state *state, enum engine_id eng_id, struct dc_stream_state *stream)
188 {
189 	bool is_avail = false;
190 	int eng_idx = eng_id - ENGINE_ID_DIGA;
191 
192 	/* An encoder is available if it is still in the availability pool. */
193 	if (eng_id != ENGINE_ID_UNKNOWN && state->res_ctx.link_enc_cfg_ctx.link_enc_avail[eng_idx] != ENGINE_ID_UNKNOWN) {
194 		is_avail = true;
195 	} else {
196 		struct dc_stream_state *stream_assigned = NULL;
197 
198 		/* MST streams share the same link and should share the same encoder.
199 		 * If a stream that has already been assigned a link encoder uses as the
200 		 * same link as the stream checking for availability, it is an MST stream
201 		 * and should use the same link encoder.
202 		 */
203 		stream_assigned = get_stream_using_link_enc(state, eng_id);
204 		if (stream_assigned && stream != stream_assigned && stream->link == stream_assigned->link)
205 			is_avail = true;
206 	}
207 
208 	return is_avail;
209 }
210 
211 /* Test for display_endpoint_id equality. */
212 static bool are_ep_ids_equal(struct display_endpoint_id *lhs, struct display_endpoint_id *rhs)
213 {
214 	bool are_equal = false;
215 
216 	if (lhs->link_id.id == rhs->link_id.id &&
217 			lhs->link_id.enum_id == rhs->link_id.enum_id &&
218 			lhs->link_id.type == rhs->link_id.type &&
219 			lhs->ep_type == rhs->ep_type)
220 		are_equal = true;
221 
222 	return are_equal;
223 }
224 
225 static struct link_encoder *get_link_enc_used_by_link(
226 		struct dc_state *state,
227 		const struct dc_link *link)
228 {
229 	struct link_encoder *link_enc = NULL;
230 	struct display_endpoint_id ep_id;
231 	int i;
232 
233 	ep_id = (struct display_endpoint_id) {
234 		.link_id = link->link_id,
235 		.ep_type = link->ep_type};
236 
237 	for (i = 0; i < MAX_PIPES; i++) {
238 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
239 
240 		if (assignment.valid == true && are_ep_ids_equal(&assignment.ep_id, &ep_id))
241 			link_enc = link->dc->res_pool->link_encoders[assignment.eng_id - ENGINE_ID_DIGA];
242 	}
243 
244 	return link_enc;
245 }
246 /* Clear all link encoder assignments. */
247 static void clear_enc_assignments(const struct dc *dc, struct dc_state *state)
248 {
249 	int i;
250 
251 	for (i = 0; i < MAX_PIPES; i++) {
252 		state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].valid = false;
253 		state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].eng_id = ENGINE_ID_UNKNOWN;
254 		if (state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream != NULL) {
255 			dc_stream_release(state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream);
256 			state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream = NULL;
257 		}
258 	}
259 
260 	for (i = 0; i < dc->res_pool->res_cap->num_dig_link_enc; i++) {
261 		if (dc->res_pool->link_encoders[i])
262 			state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i] = (enum engine_id) i;
263 		else
264 			state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i] = ENGINE_ID_UNKNOWN;
265 	}
266 }
267 
268 void link_enc_cfg_init(
269 		const struct dc *dc,
270 		struct dc_state *state)
271 {
272 	clear_enc_assignments(dc, state);
273 
274 	state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
275 }
276 
277 void link_enc_cfg_copy(const struct dc_state *src_ctx, struct dc_state *dst_ctx)
278 {
279 	memcpy(&dst_ctx->res_ctx.link_enc_cfg_ctx,
280 	       &src_ctx->res_ctx.link_enc_cfg_ctx,
281 	       sizeof(dst_ctx->res_ctx.link_enc_cfg_ctx));
282 }
283 
284 void link_enc_cfg_link_encs_assign(
285 		struct dc *dc,
286 		struct dc_state *state,
287 		struct dc_stream_state *streams[],
288 		uint8_t stream_count)
289 {
290 	enum engine_id eng_id = ENGINE_ID_UNKNOWN;
291 	int i;
292 	int j;
293 
294 	ASSERT(state->stream_count == stream_count);
295 	ASSERT(dc->current_state->res_ctx.link_enc_cfg_ctx.mode == LINK_ENC_CFG_STEADY);
296 
297 	/* Release DIG link encoder resources before running assignment algorithm. */
298 	for (i = 0; i < dc->current_state->stream_count; i++)
299 		dc->res_pool->funcs->link_enc_unassign(state, dc->current_state->streams[i]);
300 
301 	for (i = 0; i < MAX_PIPES; i++)
302 		ASSERT(state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].valid == false);
303 
304 	/* (a) Assign DIG link encoders to physical (unmappable) endpoints first. */
305 	for (i = 0; i < stream_count; i++) {
306 		struct dc_stream_state *stream = streams[i];
307 
308 		/* skip it if the link is mappable endpoint. */
309 		if (stream->link->is_dig_mapping_flexible)
310 			continue;
311 
312 		/* Skip stream if not supported by DIG link encoder. */
313 		if (!is_dig_link_enc_stream(stream))
314 			continue;
315 
316 		/* Physical endpoints have a fixed mapping to DIG link encoders. */
317 		eng_id = stream->link->eng_id;
318 		add_link_enc_assignment(state, stream, eng_id);
319 	}
320 
321 	/* (b) Retain previous assignments for mappable endpoints if encoders still available. */
322 	eng_id = ENGINE_ID_UNKNOWN;
323 
324 	if (state != dc->current_state) {
325 		struct dc_state *prev_state = dc->current_state;
326 
327 		for (i = 0; i < stream_count; i++) {
328 			struct dc_stream_state *stream = state->streams[i];
329 
330 			/* Skip it if the link is NOT mappable endpoint. */
331 			if (!stream->link->is_dig_mapping_flexible)
332 				continue;
333 
334 			/* Skip stream if not supported by DIG link encoder. */
335 			if (!is_dig_link_enc_stream(stream))
336 				continue;
337 
338 			for (j = 0; j < prev_state->stream_count; j++) {
339 				struct dc_stream_state *prev_stream = prev_state->streams[j];
340 
341 				if (stream == prev_stream && stream->link == prev_stream->link &&
342 						prev_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[j].valid) {
343 					eng_id = prev_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[j].eng_id;
344 
345 					if (is_avail_link_enc(state, eng_id, stream))
346 						add_link_enc_assignment(state, stream, eng_id);
347 				}
348 			}
349 		}
350 	}
351 
352 	/* (c) Then assign encoders to remaining mappable endpoints. */
353 	eng_id = ENGINE_ID_UNKNOWN;
354 
355 	for (i = 0; i < stream_count; i++) {
356 		struct dc_stream_state *stream = streams[i];
357 		struct link_encoder *link_enc = NULL;
358 
359 		/* Skip it if the link is NOT mappable endpoint. */
360 		if (!stream->link->is_dig_mapping_flexible)
361 			continue;
362 
363 		/* Skip if encoder assignment retained in step (b) above. */
364 		if (stream->link_enc)
365 			continue;
366 
367 		/* Skip stream if not supported by DIG link encoder. */
368 		if (!is_dig_link_enc_stream(stream)) {
369 			ASSERT(stream->link->is_dig_mapping_flexible != true);
370 			continue;
371 		}
372 
373 		/* Mappable endpoints have a flexible mapping to DIG link encoders. */
374 
375 		/* For MST, multiple streams will share the same link / display
376 		 * endpoint. These streams should use the same link encoder
377 		 * assigned to that endpoint.
378 		 */
379 		link_enc = get_link_enc_used_by_link(state, stream->link);
380 		if (link_enc == NULL)
381 			eng_id = find_first_avail_link_enc(stream->ctx, state);
382 		else
383 			eng_id =  link_enc->preferred_engine;
384 
385 		add_link_enc_assignment(state, stream, eng_id);
386 	}
387 
388 	link_enc_cfg_validate(dc, state);
389 
390 	/* Update transient assignments. */
391 	for (i = 0; i < MAX_PIPES; i++) {
392 		dc->current_state->res_ctx.link_enc_cfg_ctx.transient_assignments[i] =
393 			state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
394 	}
395 
396 	/* Log encoder assignments. */
397 	for (i = 0; i < MAX_PIPES; i++) {
398 		struct link_enc_assignment assignment =
399 				dc->current_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
400 
401 		if (assignment.valid)
402 			DC_LOG_DEBUG("%s: CUR %s(%d) - enc_id(%d)\n",
403 					__func__,
404 					assignment.ep_id.ep_type == DISPLAY_ENDPOINT_PHY ? "PHY" : "DPIA",
405 					assignment.ep_id.link_id.enum_id - 1,
406 					assignment.eng_id);
407 	}
408 	for (i = 0; i < MAX_PIPES; i++) {
409 		struct link_enc_assignment assignment =
410 				state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
411 
412 		if (assignment.valid)
413 			DC_LOG_DEBUG("%s: NEW %s(%d) - enc_id(%d)\n",
414 					__func__,
415 					assignment.ep_id.ep_type == DISPLAY_ENDPOINT_PHY ? "PHY" : "DPIA",
416 					assignment.ep_id.link_id.enum_id - 1,
417 					assignment.eng_id);
418 	}
419 
420 	/* Current state mode will be set to steady once this state committed. */
421 	state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
422 }
423 
424 void link_enc_cfg_link_enc_unassign(
425 		struct dc_state *state,
426 		struct dc_stream_state *stream)
427 {
428 	enum engine_id eng_id = ENGINE_ID_UNKNOWN;
429 
430 	if (stream->link_enc)
431 		eng_id = stream->link_enc->preferred_engine;
432 
433 	remove_link_enc_assignment(state, stream, eng_id);
434 }
435 
436 bool link_enc_cfg_is_transmitter_mappable(
437 		struct dc *dc,
438 		struct link_encoder *link_enc)
439 {
440 	bool is_mappable = false;
441 	enum engine_id eng_id = link_enc->preferred_engine;
442 	struct dc_stream_state *stream = link_enc_cfg_get_stream_using_link_enc(dc, eng_id);
443 
444 	if (stream)
445 		is_mappable = stream->link->is_dig_mapping_flexible;
446 
447 	return is_mappable;
448 }
449 
450 struct dc_stream_state *link_enc_cfg_get_stream_using_link_enc(
451 		struct dc *dc,
452 		enum engine_id eng_id)
453 {
454 	struct dc_stream_state *stream = NULL;
455 	int i;
456 
457 	for (i = 0; i < MAX_PIPES; i++) {
458 		struct link_enc_assignment assignment = get_assignment(dc, i);
459 
460 		if ((assignment.valid == true) && (assignment.eng_id == eng_id)) {
461 			stream = assignment.stream;
462 			break;
463 		}
464 	}
465 
466 	return stream;
467 }
468 
469 struct dc_link *link_enc_cfg_get_link_using_link_enc(
470 		struct dc *dc,
471 		enum engine_id eng_id)
472 {
473 	struct dc_link *link = NULL;
474 	struct dc_stream_state *stream = NULL;
475 
476 	stream = link_enc_cfg_get_stream_using_link_enc(dc, eng_id);
477 
478 	if (stream)
479 		link = stream->link;
480 
481 	// dm_output_to_console("%s: No link using DIG(%d).\n", __func__, eng_id);
482 	return link;
483 }
484 
485 struct link_encoder *link_enc_cfg_get_link_enc_used_by_link(
486 		struct dc *dc,
487 		const struct dc_link *link)
488 {
489 	struct link_encoder *link_enc = NULL;
490 	struct display_endpoint_id ep_id;
491 	int i;
492 
493 	ep_id = (struct display_endpoint_id) {
494 		.link_id = link->link_id,
495 		.ep_type = link->ep_type};
496 
497 	for (i = 0; i < MAX_PIPES; i++) {
498 		struct link_enc_assignment assignment = get_assignment(dc, i);
499 
500 		if (assignment.valid == true && are_ep_ids_equal(&assignment.ep_id, &ep_id)) {
501 			link_enc = link->dc->res_pool->link_encoders[assignment.eng_id - ENGINE_ID_DIGA];
502 			break;
503 		}
504 	}
505 
506 	return link_enc;
507 }
508 
509 struct link_encoder *link_enc_cfg_get_next_avail_link_enc(struct dc *dc)
510 {
511 	struct link_encoder *link_enc = NULL;
512 	enum engine_id encs_assigned[MAX_DIG_LINK_ENCODERS];
513 	int i;
514 
515 	for (i = 0; i < MAX_DIG_LINK_ENCODERS; i++)
516 		encs_assigned[i] = ENGINE_ID_UNKNOWN;
517 
518 	/* Add assigned encoders to list. */
519 	for (i = 0; i < MAX_PIPES; i++) {
520 		struct link_enc_assignment assignment = get_assignment(dc, i);
521 
522 		if (assignment.valid)
523 			encs_assigned[assignment.eng_id - ENGINE_ID_DIGA] = assignment.eng_id;
524 	}
525 
526 	for (i = 0; i < dc->res_pool->res_cap->num_dig_link_enc; i++) {
527 		if (encs_assigned[i] == ENGINE_ID_UNKNOWN &&
528 				dc->res_pool->link_encoders[i] != NULL) {
529 			link_enc = dc->res_pool->link_encoders[i];
530 			break;
531 		}
532 	}
533 
534 	return link_enc;
535 }
536 
537 struct link_encoder *link_enc_cfg_get_link_enc_used_by_stream(
538 		struct dc *dc,
539 		const struct dc_stream_state *stream)
540 {
541 	struct link_encoder *link_enc;
542 
543 	link_enc = link_enc_cfg_get_link_enc_used_by_link(dc, stream->link);
544 
545 	return link_enc;
546 }
547 
548 struct link_encoder *link_enc_cfg_get_link_enc(
549 		const struct dc_link *link)
550 {
551 	struct link_encoder *link_enc = NULL;
552 
553 	/* Links supporting dynamically assigned link encoder will be assigned next
554 	 * available encoder if one not already assigned.
555 	 */
556 	if (link->is_dig_mapping_flexible &&
557 	    link->dc->res_pool->funcs->link_encs_assign) {
558 		link_enc = link_enc_cfg_get_link_enc_used_by_link(link->ctx->dc, link);
559 		if (link_enc == NULL)
560 			link_enc = link_enc_cfg_get_next_avail_link_enc(
561 				link->ctx->dc);
562 	} else
563 		link_enc = link->link_enc;
564 
565 	return link_enc;
566 }
567 
568 struct link_encoder *link_enc_cfg_get_link_enc_used_by_stream_current(
569 		struct dc *dc,
570 		const struct dc_stream_state *stream)
571 {
572 	struct link_encoder *link_enc = NULL;
573 	struct display_endpoint_id ep_id;
574 	int i;
575 
576 	ep_id = (struct display_endpoint_id) {
577 		.link_id = stream->link->link_id,
578 		.ep_type = stream->link->ep_type};
579 
580 	for (i = 0; i < MAX_PIPES; i++) {
581 		struct link_enc_assignment assignment =
582 			dc->current_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
583 
584 		if (assignment.valid == true && are_ep_ids_equal(&assignment.ep_id, &ep_id)) {
585 			link_enc = stream->link->dc->res_pool->link_encoders[assignment.eng_id - ENGINE_ID_DIGA];
586 			break;
587 		}
588 	}
589 
590 	return link_enc;
591 }
592 
593 bool link_enc_cfg_is_link_enc_avail(struct dc *dc, enum engine_id eng_id, struct dc_link *link)
594 {
595 	bool is_avail = true;
596 	int i;
597 
598 	/* An encoder is not available if it has already been assigned to a different endpoint. */
599 	for (i = 0; i < MAX_PIPES; i++) {
600 		struct link_enc_assignment assignment = get_assignment(dc, i);
601 		struct display_endpoint_id ep_id = (struct display_endpoint_id) {
602 				.link_id = link->link_id,
603 				.ep_type = link->ep_type};
604 
605 		if (assignment.valid && assignment.eng_id == eng_id && !are_ep_ids_equal(&ep_id, &assignment.ep_id)) {
606 			is_avail = false;
607 			break;
608 		}
609 	}
610 
611 	return is_avail;
612 }
613 
614 bool link_enc_cfg_validate(struct dc *dc, struct dc_state *state)
615 {
616 	bool is_valid = false;
617 	bool valid_entries = true;
618 	bool valid_stream_ptrs = true;
619 	bool valid_uniqueness = true;
620 	bool valid_avail = true;
621 	bool valid_streams = true;
622 	int i, j;
623 	uint8_t valid_count = 0;
624 	uint8_t dig_stream_count = 0;
625 	int matching_stream_ptrs = 0;
626 	int eng_ids_per_ep_id[MAX_PIPES] = {0};
627 	int ep_ids_per_eng_id[MAX_PIPES] = {0};
628 	int valid_bitmap = 0;
629 
630 	/* (1) No. valid entries same as stream count. */
631 	for (i = 0; i < MAX_PIPES; i++) {
632 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
633 
634 		if (assignment.valid)
635 			valid_count++;
636 
637 		if (is_dig_link_enc_stream(state->streams[i]))
638 			dig_stream_count++;
639 	}
640 	if (valid_count != dig_stream_count)
641 		valid_entries = false;
642 
643 	/* (2) Matching stream ptrs. */
644 	for (i = 0; i < MAX_PIPES; i++) {
645 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
646 
647 		if (assignment.valid) {
648 			if (assignment.stream == state->streams[i])
649 				matching_stream_ptrs++;
650 			else
651 				valid_stream_ptrs = false;
652 		}
653 	}
654 
655 	/* (3) Each endpoint assigned unique encoder. */
656 	for (i = 0; i < MAX_PIPES; i++) {
657 		struct link_enc_assignment assignment_i = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
658 
659 		if (assignment_i.valid) {
660 			struct display_endpoint_id ep_id_i = assignment_i.ep_id;
661 
662 			eng_ids_per_ep_id[i]++;
663 			ep_ids_per_eng_id[i]++;
664 			for (j = 0; j < MAX_PIPES; j++) {
665 				struct link_enc_assignment assignment_j =
666 					state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[j];
667 
668 				if (j == i)
669 					continue;
670 
671 				if (assignment_j.valid) {
672 					struct display_endpoint_id ep_id_j = assignment_j.ep_id;
673 
674 					if (are_ep_ids_equal(&ep_id_i, &ep_id_j) &&
675 							assignment_i.eng_id != assignment_j.eng_id) {
676 						valid_uniqueness = false;
677 						eng_ids_per_ep_id[i]++;
678 					} else if (!are_ep_ids_equal(&ep_id_i, &ep_id_j) &&
679 							assignment_i.eng_id == assignment_j.eng_id) {
680 						valid_uniqueness = false;
681 						ep_ids_per_eng_id[i]++;
682 					}
683 				}
684 			}
685 		}
686 	}
687 
688 	/* (4) Assigned encoders not in available pool. */
689 	for (i = 0; i < MAX_PIPES; i++) {
690 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
691 
692 		if (assignment.valid) {
693 			for (j = 0; j < dc->res_pool->res_cap->num_dig_link_enc; j++) {
694 				if (state->res_ctx.link_enc_cfg_ctx.link_enc_avail[j] == assignment.eng_id) {
695 					valid_avail = false;
696 					break;
697 				}
698 			}
699 		}
700 	}
701 
702 	/* (5) All streams have valid link encoders. */
703 	for (i = 0; i < state->stream_count; i++) {
704 		struct dc_stream_state *stream = state->streams[i];
705 
706 		if (is_dig_link_enc_stream(stream) && stream->link_enc == NULL) {
707 			valid_streams = false;
708 			break;
709 		}
710 	}
711 
712 	is_valid = valid_entries && valid_stream_ptrs && valid_uniqueness && valid_avail && valid_streams;
713 	ASSERT(is_valid);
714 
715 	if (is_valid == false) {
716 		valid_bitmap =
717 			(valid_entries & 0x1) |
718 			((valid_stream_ptrs & 0x1) << 1) |
719 			((valid_uniqueness & 0x1) << 2) |
720 			((valid_avail & 0x1) << 3) |
721 			((valid_streams & 0x1) << 4);
722 		DC_LOG_ERROR("%s: Invalid link encoder assignments - 0x%x\n", __func__, valid_bitmap);
723 	}
724 
725 	return is_valid;
726 }
727 
728 void link_enc_cfg_set_transient_mode(struct dc *dc, struct dc_state *current_state, struct dc_state *new_state)
729 {
730 	int i = 0;
731 	int num_transient_assignments = 0;
732 
733 	for (i = 0; i < MAX_PIPES; i++) {
734 		if (current_state->res_ctx.link_enc_cfg_ctx.transient_assignments[i].valid)
735 			num_transient_assignments++;
736 	}
737 
738 	/* Only enter transient mode if the new encoder assignments are valid. */
739 	if (new_state->stream_count == num_transient_assignments) {
740 		current_state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_TRANSIENT;
741 		DC_LOG_DEBUG("%s: current_state(%p) mode(%d)\n", __func__, current_state, LINK_ENC_CFG_TRANSIENT);
742 	}
743 }
744