xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc_link_enc_cfg.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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. */
is_dig_link_enc_stream(struct dc_stream_state * stream)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 					stream->ctx->dc->link_srv->dp_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 
get_assignment(struct dc * dc,int i)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. */
get_stream_using_link_enc(struct dc_state * state,enum engine_id eng_id)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 
remove_link_enc_assignment(struct dc_state * state,struct dc_stream_state * stream,enum engine_id eng_id)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 
add_link_enc_assignment(struct dc_state * state,struct dc_stream_state * stream,enum engine_id eng_id)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. */
find_first_avail_link_enc(const struct dc_context * ctx,const struct dc_state * state,enum engine_id eng_id_requested)170 static enum engine_id find_first_avail_link_enc(
171 		const struct dc_context *ctx,
172 		const struct dc_state *state,
173 		enum engine_id eng_id_requested)
174 {
175 	enum engine_id eng_id = ENGINE_ID_UNKNOWN;
176 	int i;
177 
178 	if (eng_id_requested != ENGINE_ID_UNKNOWN) {
179 
180 		for (i = 0; i < ctx->dc->res_pool->res_cap->num_dig_link_enc; i++) {
181 			eng_id = state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i];
182 			if (eng_id == eng_id_requested)
183 				return eng_id;
184 		}
185 	}
186 
187 	eng_id = ENGINE_ID_UNKNOWN;
188 
189 	for (i = 0; i < ctx->dc->res_pool->res_cap->num_dig_link_enc; i++) {
190 		eng_id = state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i];
191 		if (eng_id != ENGINE_ID_UNKNOWN)
192 			break;
193 	}
194 
195 	return eng_id;
196 }
197 
198 /* Check for availability of link encoder eng_id. */
is_avail_link_enc(struct dc_state * state,enum engine_id eng_id,struct dc_stream_state * stream)199 static bool is_avail_link_enc(struct dc_state *state, enum engine_id eng_id, struct dc_stream_state *stream)
200 {
201 	bool is_avail = false;
202 	int eng_idx = eng_id - ENGINE_ID_DIGA;
203 
204 	/* An encoder is available if it is still in the availability pool. */
205 	if (eng_id != ENGINE_ID_UNKNOWN && state->res_ctx.link_enc_cfg_ctx.link_enc_avail[eng_idx] != ENGINE_ID_UNKNOWN) {
206 		is_avail = true;
207 	} else {
208 		struct dc_stream_state *stream_assigned = NULL;
209 
210 		/* MST streams share the same link and should share the same encoder.
211 		 * If a stream that has already been assigned a link encoder uses as the
212 		 * same link as the stream checking for availability, it is an MST stream
213 		 * and should use the same link encoder.
214 		 */
215 		stream_assigned = get_stream_using_link_enc(state, eng_id);
216 		if (stream_assigned && stream != stream_assigned && stream->link == stream_assigned->link)
217 			is_avail = true;
218 	}
219 
220 	return is_avail;
221 }
222 
223 /* Test for display_endpoint_id equality. */
are_ep_ids_equal(struct display_endpoint_id * lhs,struct display_endpoint_id * rhs)224 static bool are_ep_ids_equal(struct display_endpoint_id *lhs, struct display_endpoint_id *rhs)
225 {
226 	bool are_equal = false;
227 
228 	if (lhs->link_id.id == rhs->link_id.id &&
229 			lhs->link_id.enum_id == rhs->link_id.enum_id &&
230 			lhs->link_id.type == rhs->link_id.type &&
231 			lhs->ep_type == rhs->ep_type)
232 		are_equal = true;
233 
234 	return are_equal;
235 }
236 
get_link_enc_used_by_link(struct dc_state * state,const struct dc_link * link)237 static struct link_encoder *get_link_enc_used_by_link(
238 		struct dc_state *state,
239 		const struct dc_link *link)
240 {
241 	struct link_encoder *link_enc = NULL;
242 	struct display_endpoint_id ep_id;
243 	int i;
244 
245 	ep_id = (struct display_endpoint_id) {
246 		.link_id = link->link_id,
247 		.ep_type = link->ep_type};
248 
249 	for (i = 0; i < MAX_PIPES; i++) {
250 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
251 		if (assignment.eng_id == ENGINE_ID_UNKNOWN)
252 			continue;
253 
254 		if (assignment.valid == true && are_ep_ids_equal(&assignment.ep_id, &ep_id))
255 			link_enc = link->dc->res_pool->link_encoders[assignment.eng_id - ENGINE_ID_DIGA];
256 	}
257 
258 	return link_enc;
259 }
260 /* Clear all link encoder assignments. */
clear_enc_assignments(const struct dc * dc,struct dc_state * state)261 static void clear_enc_assignments(const struct dc *dc, struct dc_state *state)
262 {
263 	int i;
264 
265 	for (i = 0; i < MAX_PIPES; i++) {
266 		state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].valid = false;
267 		state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].eng_id = ENGINE_ID_UNKNOWN;
268 		if (state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream != NULL) {
269 			dc_stream_release(state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream);
270 			state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].stream = NULL;
271 		}
272 	}
273 
274 	for (i = 0; i < dc->res_pool->res_cap->num_dig_link_enc; i++) {
275 		if (dc->res_pool->link_encoders[i])
276 			state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i] = (enum engine_id) i;
277 		else
278 			state->res_ctx.link_enc_cfg_ctx.link_enc_avail[i] = ENGINE_ID_UNKNOWN;
279 	}
280 }
281 
link_enc_cfg_init(const struct dc * dc,struct dc_state * state)282 void link_enc_cfg_init(
283 		const struct dc *dc,
284 		struct dc_state *state)
285 {
286 	clear_enc_assignments(dc, state);
287 
288 	state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
289 }
290 
link_enc_cfg_copy(const struct dc_state * src_ctx,struct dc_state * dst_ctx)291 void link_enc_cfg_copy(const struct dc_state *src_ctx, struct dc_state *dst_ctx)
292 {
293 	memcpy(&dst_ctx->res_ctx.link_enc_cfg_ctx,
294 	       &src_ctx->res_ctx.link_enc_cfg_ctx,
295 	       sizeof(dst_ctx->res_ctx.link_enc_cfg_ctx));
296 }
297 
link_enc_cfg_link_encs_assign(struct dc * dc,struct dc_state * state,struct dc_stream_state * streams[],uint8_t stream_count)298 void link_enc_cfg_link_encs_assign(
299 		struct dc *dc,
300 		struct dc_state *state,
301 		struct dc_stream_state *streams[],
302 		uint8_t stream_count)
303 {
304 	enum engine_id eng_id = ENGINE_ID_UNKNOWN, eng_id_req = ENGINE_ID_UNKNOWN;
305 	int i;
306 	int j;
307 
308 	ASSERT(state->stream_count == stream_count);
309 	ASSERT(dc->current_state->res_ctx.link_enc_cfg_ctx.mode == LINK_ENC_CFG_STEADY);
310 
311 	/* Release DIG link encoder resources before running assignment algorithm. */
312 	for (i = 0; i < dc->current_state->stream_count; i++)
313 		dc->res_pool->funcs->link_enc_unassign(state, dc->current_state->streams[i]);
314 
315 	for (i = 0; i < MAX_PIPES; i++)
316 		ASSERT(state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i].valid == false);
317 
318 	/* (a) Assign DIG link encoders to physical (unmappable) endpoints first. */
319 	for (i = 0; i < stream_count; i++) {
320 		struct dc_stream_state *stream = streams[i];
321 
322 		/* skip it if the link is mappable endpoint. */
323 		if (stream->link->is_dig_mapping_flexible)
324 			continue;
325 
326 		/* Skip stream if not supported by DIG link encoder. */
327 		if (!is_dig_link_enc_stream(stream))
328 			continue;
329 
330 		/* Physical endpoints have a fixed mapping to DIG link encoders. */
331 		eng_id = stream->link->eng_id;
332 		add_link_enc_assignment(state, stream, eng_id);
333 	}
334 
335 	/* (b) Retain previous assignments for mappable endpoints if encoders still available. */
336 	eng_id = ENGINE_ID_UNKNOWN;
337 
338 	if (state != dc->current_state) {
339 		struct dc_state *prev_state = dc->current_state;
340 
341 		for (i = 0; i < stream_count; i++) {
342 			struct dc_stream_state *stream = state->streams[i];
343 
344 			/* Skip it if the link is NOT mappable endpoint. */
345 			if (!stream->link->is_dig_mapping_flexible)
346 				continue;
347 
348 			/* Skip stream if not supported by DIG link encoder. */
349 			if (!is_dig_link_enc_stream(stream))
350 				continue;
351 
352 			for (j = 0; j < prev_state->stream_count; j++) {
353 				struct dc_stream_state *prev_stream = prev_state->streams[j];
354 
355 				if (stream == prev_stream && stream->link == prev_stream->link &&
356 						prev_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[j].valid) {
357 					eng_id = prev_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[j].eng_id;
358 
359 					if (is_avail_link_enc(state, eng_id, stream))
360 						add_link_enc_assignment(state, stream, eng_id);
361 				}
362 			}
363 		}
364 	}
365 
366 	/* (c) Then assign encoders to remaining mappable endpoints. */
367 	eng_id = ENGINE_ID_UNKNOWN;
368 
369 	for (i = 0; i < stream_count; i++) {
370 		struct dc_stream_state *stream = streams[i];
371 		struct link_encoder *link_enc = NULL;
372 
373 		/* Skip it if the link is NOT mappable endpoint. */
374 		if (!stream->link->is_dig_mapping_flexible)
375 			continue;
376 
377 		/* Skip if encoder assignment retained in step (b) above. */
378 		if (stream->link_enc)
379 			continue;
380 
381 		/* Skip stream if not supported by DIG link encoder. */
382 		if (!is_dig_link_enc_stream(stream)) {
383 			ASSERT(stream->link->is_dig_mapping_flexible != true);
384 			continue;
385 		}
386 
387 		/* Mappable endpoints have a flexible mapping to DIG link encoders. */
388 
389 		/* For MST, multiple streams will share the same link / display
390 		 * endpoint. These streams should use the same link encoder
391 		 * assigned to that endpoint.
392 		 */
393 		link_enc = get_link_enc_used_by_link(state, stream->link);
394 		if (link_enc == NULL) {
395 
396 			if (stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
397 					stream->link->dpia_preferred_eng_id != ENGINE_ID_UNKNOWN)
398 				eng_id_req = stream->link->dpia_preferred_eng_id;
399 
400 			eng_id = find_first_avail_link_enc(stream->ctx, state, eng_id_req);
401 		}
402 		else
403 			eng_id =  link_enc->preferred_engine;
404 
405 		add_link_enc_assignment(state, stream, eng_id);
406 	}
407 
408 	link_enc_cfg_validate(dc, state);
409 
410 	/* Update transient assignments. */
411 	for (i = 0; i < MAX_PIPES; i++) {
412 		dc->current_state->res_ctx.link_enc_cfg_ctx.transient_assignments[i] =
413 			state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
414 	}
415 
416 	/* Log encoder assignments. */
417 	for (i = 0; i < MAX_PIPES; i++) {
418 		struct link_enc_assignment assignment =
419 				dc->current_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
420 
421 		if (assignment.valid)
422 			DC_LOG_DEBUG("%s: CUR %s(%d) - enc_id(%d)\n",
423 					__func__,
424 					assignment.ep_id.ep_type == DISPLAY_ENDPOINT_PHY ? "PHY" : "DPIA",
425 					assignment.ep_id.ep_type == DISPLAY_ENDPOINT_PHY ?
426 							assignment.ep_id.link_id.enum_id :
427 							assignment.ep_id.link_id.enum_id - 1,
428 					assignment.eng_id);
429 	}
430 	for (i = 0; i < MAX_PIPES; i++) {
431 		struct link_enc_assignment assignment =
432 				state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
433 
434 		if (assignment.valid)
435 			DC_LOG_DEBUG("%s: NEW %s(%d) - enc_id(%d)\n",
436 					__func__,
437 					assignment.ep_id.ep_type == DISPLAY_ENDPOINT_PHY ? "PHY" : "DPIA",
438 					assignment.ep_id.ep_type == DISPLAY_ENDPOINT_PHY ?
439 							assignment.ep_id.link_id.enum_id :
440 							assignment.ep_id.link_id.enum_id - 1,
441 					assignment.eng_id);
442 	}
443 
444 	/* Current state mode will be set to steady once this state committed. */
445 	state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_STEADY;
446 }
447 
link_enc_cfg_link_enc_unassign(struct dc_state * state,struct dc_stream_state * stream)448 void link_enc_cfg_link_enc_unassign(
449 		struct dc_state *state,
450 		struct dc_stream_state *stream)
451 {
452 	enum engine_id eng_id = ENGINE_ID_UNKNOWN;
453 
454 	if (stream->link_enc)
455 		eng_id = stream->link_enc->preferred_engine;
456 
457 	remove_link_enc_assignment(state, stream, eng_id);
458 }
459 
link_enc_cfg_is_transmitter_mappable(struct dc * dc,struct link_encoder * link_enc)460 bool link_enc_cfg_is_transmitter_mappable(
461 		struct dc *dc,
462 		struct link_encoder *link_enc)
463 {
464 	bool is_mappable = false;
465 	enum engine_id eng_id = link_enc->preferred_engine;
466 	struct dc_stream_state *stream = link_enc_cfg_get_stream_using_link_enc(dc, eng_id);
467 
468 	if (stream)
469 		is_mappable = stream->link->is_dig_mapping_flexible;
470 
471 	return is_mappable;
472 }
473 
link_enc_cfg_get_stream_using_link_enc(struct dc * dc,enum engine_id eng_id)474 struct dc_stream_state *link_enc_cfg_get_stream_using_link_enc(
475 		struct dc *dc,
476 		enum engine_id eng_id)
477 {
478 	struct dc_stream_state *stream = NULL;
479 	int i;
480 
481 	for (i = 0; i < MAX_PIPES; i++) {
482 		struct link_enc_assignment assignment = get_assignment(dc, i);
483 
484 		if ((assignment.valid == true) && (assignment.eng_id == eng_id)) {
485 			stream = assignment.stream;
486 			break;
487 		}
488 	}
489 
490 	return stream;
491 }
492 
link_enc_cfg_get_link_using_link_enc(struct dc * dc,enum engine_id eng_id)493 struct dc_link *link_enc_cfg_get_link_using_link_enc(
494 		struct dc *dc,
495 		enum engine_id eng_id)
496 {
497 	struct dc_link *link = NULL;
498 	struct dc_stream_state *stream = NULL;
499 
500 	stream = link_enc_cfg_get_stream_using_link_enc(dc, eng_id);
501 
502 	if (stream)
503 		link = stream->link;
504 
505 	return link;
506 }
507 
link_enc_cfg_get_link_enc_used_by_link(struct dc * dc,const struct dc_link * link)508 struct link_encoder *link_enc_cfg_get_link_enc_used_by_link(
509 		struct dc *dc,
510 		const struct dc_link *link)
511 {
512 	struct link_encoder *link_enc = NULL;
513 	struct display_endpoint_id ep_id;
514 	int i;
515 
516 	ep_id = (struct display_endpoint_id) {
517 		.link_id = link->link_id,
518 		.ep_type = link->ep_type};
519 
520 	for (i = 0; i < MAX_PIPES; i++) {
521 		struct link_enc_assignment assignment = get_assignment(dc, i);
522 		if (assignment.eng_id == ENGINE_ID_UNKNOWN)
523 			continue;
524 
525 		if (assignment.valid == true && are_ep_ids_equal(&assignment.ep_id, &ep_id)) {
526 			link_enc = link->dc->res_pool->link_encoders[assignment.eng_id - ENGINE_ID_DIGA];
527 			break;
528 		}
529 	}
530 
531 	return link_enc;
532 }
533 
link_enc_cfg_get_next_avail_link_enc(struct dc * dc)534 struct link_encoder *link_enc_cfg_get_next_avail_link_enc(struct dc *dc)
535 {
536 	struct link_encoder *link_enc = NULL;
537 	enum engine_id encs_assigned[MAX_DIG_LINK_ENCODERS];
538 	int i;
539 
540 	for (i = 0; i < MAX_DIG_LINK_ENCODERS; i++)
541 		encs_assigned[i] = ENGINE_ID_UNKNOWN;
542 
543 	/* Add assigned encoders to list. */
544 	for (i = 0; i < MAX_PIPES; i++) {
545 		struct link_enc_assignment assignment = get_assignment(dc, i);
546 
547 		if (assignment.valid && assignment.eng_id != ENGINE_ID_UNKNOWN)
548 			encs_assigned[assignment.eng_id - ENGINE_ID_DIGA] = assignment.eng_id;
549 	}
550 
551 	for (i = 0; i < dc->res_pool->res_cap->num_dig_link_enc; i++) {
552 		if (encs_assigned[i] == ENGINE_ID_UNKNOWN &&
553 				dc->res_pool->link_encoders[i] != NULL) {
554 			link_enc = dc->res_pool->link_encoders[i];
555 			break;
556 		}
557 	}
558 
559 	return link_enc;
560 }
561 
link_enc_cfg_get_link_enc_used_by_stream(struct dc * dc,const struct dc_stream_state * stream)562 struct link_encoder *link_enc_cfg_get_link_enc_used_by_stream(
563 		struct dc *dc,
564 		const struct dc_stream_state *stream)
565 {
566 	struct link_encoder *link_enc;
567 
568 	link_enc = link_enc_cfg_get_link_enc_used_by_link(dc, stream->link);
569 
570 	return link_enc;
571 }
572 
link_enc_cfg_get_link_enc(const struct dc_link * link)573 struct link_encoder *link_enc_cfg_get_link_enc(
574 		const struct dc_link *link)
575 {
576 	struct link_encoder *link_enc = NULL;
577 
578 	/* Links supporting dynamically assigned link encoder will be assigned next
579 	 * available encoder if one not already assigned.
580 	 */
581 	if (link->is_dig_mapping_flexible &&
582 	    link->dc->res_pool->funcs->link_encs_assign) {
583 		link_enc = link_enc_cfg_get_link_enc_used_by_link(link->ctx->dc, link);
584 		if (link_enc == NULL)
585 			link_enc = link_enc_cfg_get_next_avail_link_enc(
586 				link->ctx->dc);
587 	} else
588 		link_enc = link->link_enc;
589 
590 	return link_enc;
591 }
592 
link_enc_cfg_get_link_enc_used_by_stream_current(struct dc * dc,const struct dc_stream_state * stream)593 struct link_encoder *link_enc_cfg_get_link_enc_used_by_stream_current(
594 		struct dc *dc,
595 		const struct dc_stream_state *stream)
596 {
597 	struct link_encoder *link_enc = NULL;
598 	struct display_endpoint_id ep_id;
599 	int i;
600 
601 	ep_id = (struct display_endpoint_id) {
602 		.link_id = stream->link->link_id,
603 		.ep_type = stream->link->ep_type};
604 
605 	for (i = 0; i < MAX_PIPES; i++) {
606 		struct link_enc_assignment assignment =
607 			dc->current_state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
608 
609 		if (assignment.eng_id == ENGINE_ID_UNKNOWN)
610 			continue;
611 
612 		if (assignment.valid == true && are_ep_ids_equal(&assignment.ep_id, &ep_id)) {
613 			link_enc = stream->link->dc->res_pool->link_encoders[assignment.eng_id - ENGINE_ID_DIGA];
614 			break;
615 		}
616 	}
617 
618 	return link_enc;
619 }
620 
link_enc_cfg_is_link_enc_avail(struct dc * dc,enum engine_id eng_id,struct dc_link * link)621 bool link_enc_cfg_is_link_enc_avail(struct dc *dc, enum engine_id eng_id, struct dc_link *link)
622 {
623 	bool is_avail = true;
624 	int i;
625 
626 	/* An encoder is not available if it has already been assigned to a different endpoint. */
627 	for (i = 0; i < MAX_PIPES; i++) {
628 		struct link_enc_assignment assignment = get_assignment(dc, i);
629 		struct display_endpoint_id ep_id = (struct display_endpoint_id) {
630 				.link_id = link->link_id,
631 				.ep_type = link->ep_type};
632 
633 		if (assignment.valid && assignment.eng_id == eng_id && !are_ep_ids_equal(&ep_id, &assignment.ep_id)) {
634 			is_avail = false;
635 			break;
636 		}
637 	}
638 
639 	return is_avail;
640 }
641 
link_enc_cfg_validate(struct dc * dc,struct dc_state * state)642 bool link_enc_cfg_validate(struct dc *dc, struct dc_state *state)
643 {
644 	bool is_valid = false;
645 	bool valid_entries = true;
646 	bool valid_stream_ptrs = true;
647 	bool valid_uniqueness = true;
648 	bool valid_avail = true;
649 	bool valid_streams = true;
650 	int i, j;
651 	uint8_t valid_count = 0;
652 	uint8_t dig_stream_count = 0;
653 	int eng_ids_per_ep_id[MAX_PIPES] = {0};
654 	int ep_ids_per_eng_id[MAX_PIPES] = {0};
655 	int valid_bitmap = 0;
656 
657 	/* (1) No. valid entries same as stream count. */
658 	for (i = 0; i < MAX_PIPES; i++) {
659 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
660 
661 		if (assignment.valid)
662 			valid_count++;
663 
664 		if (is_dig_link_enc_stream(state->streams[i]))
665 			dig_stream_count++;
666 	}
667 	if (valid_count != dig_stream_count)
668 		valid_entries = false;
669 
670 	/* (2) Matching stream ptrs. */
671 	for (i = 0; i < MAX_PIPES; i++) {
672 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
673 
674 		if (assignment.valid) {
675 			if (assignment.stream != state->streams[i])
676 				valid_stream_ptrs = false;
677 		}
678 	}
679 
680 	/* (3) Each endpoint assigned unique encoder. */
681 	for (i = 0; i < MAX_PIPES; i++) {
682 		struct link_enc_assignment assignment_i = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
683 
684 		if (assignment_i.valid) {
685 			struct display_endpoint_id ep_id_i = assignment_i.ep_id;
686 
687 			eng_ids_per_ep_id[i]++;
688 			ep_ids_per_eng_id[i]++;
689 			for (j = 0; j < MAX_PIPES; j++) {
690 				struct link_enc_assignment assignment_j =
691 					state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[j];
692 
693 				if (j == i)
694 					continue;
695 
696 				if (assignment_j.valid) {
697 					struct display_endpoint_id ep_id_j = assignment_j.ep_id;
698 
699 					if (are_ep_ids_equal(&ep_id_i, &ep_id_j) &&
700 							assignment_i.eng_id != assignment_j.eng_id) {
701 						valid_uniqueness = false;
702 						eng_ids_per_ep_id[i]++;
703 					} else if (!are_ep_ids_equal(&ep_id_i, &ep_id_j) &&
704 							assignment_i.eng_id == assignment_j.eng_id) {
705 						valid_uniqueness = false;
706 						ep_ids_per_eng_id[i]++;
707 					}
708 				}
709 			}
710 		}
711 	}
712 
713 	/* (4) Assigned encoders not in available pool. */
714 	for (i = 0; i < MAX_PIPES; i++) {
715 		struct link_enc_assignment assignment = state->res_ctx.link_enc_cfg_ctx.link_enc_assignments[i];
716 
717 		if (assignment.valid) {
718 			for (j = 0; j < dc->res_pool->res_cap->num_dig_link_enc; j++) {
719 				if (state->res_ctx.link_enc_cfg_ctx.link_enc_avail[j] == assignment.eng_id) {
720 					valid_avail = false;
721 					break;
722 				}
723 			}
724 		}
725 	}
726 
727 	/* (5) All streams have valid link encoders. */
728 	for (i = 0; i < state->stream_count; i++) {
729 		struct dc_stream_state *stream = state->streams[i];
730 
731 		if (is_dig_link_enc_stream(stream) && stream->link_enc == NULL) {
732 			valid_streams = false;
733 			break;
734 		}
735 	}
736 
737 	is_valid = valid_entries && valid_stream_ptrs && valid_uniqueness && valid_avail && valid_streams;
738 	ASSERT(is_valid);
739 
740 	if (is_valid == false) {
741 		valid_bitmap =
742 			(valid_entries & 0x1) |
743 			((valid_stream_ptrs & 0x1) << 1) |
744 			((valid_uniqueness & 0x1) << 2) |
745 			((valid_avail & 0x1) << 3) |
746 			((valid_streams & 0x1) << 4);
747 		DC_LOG_ERROR("%s: Invalid link encoder assignments - 0x%x\n", __func__, valid_bitmap);
748 	}
749 
750 	return is_valid;
751 }
752 
link_enc_cfg_set_transient_mode(struct dc * dc,struct dc_state * current_state,struct dc_state * new_state)753 void link_enc_cfg_set_transient_mode(struct dc *dc, struct dc_state *current_state, struct dc_state *new_state)
754 {
755 	int i = 0;
756 	int num_transient_assignments = 0;
757 
758 	for (i = 0; i < MAX_PIPES; i++) {
759 		if (current_state->res_ctx.link_enc_cfg_ctx.transient_assignments[i].valid)
760 			num_transient_assignments++;
761 	}
762 
763 	/* Only enter transient mode if the new encoder assignments are valid. */
764 	if (new_state->stream_count == num_transient_assignments) {
765 		current_state->res_ctx.link_enc_cfg_ctx.mode = LINK_ENC_CFG_TRANSIENT;
766 		DC_LOG_DEBUG("%s: current_state(%p) mode(%d)\n", __func__, current_state, LINK_ENC_CFG_TRANSIENT);
767 	}
768 }
769