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 /* FILE POLICY AND INTENDED USAGE:
26  * This file implements accessors to link resource.
27  */
28 
29 #include "link_resource.h"
30 #include "protocols/link_dp_capability.h"
31 
32 void link_get_cur_link_res(const struct dc_link *link,
33 		struct link_resource *link_res)
34 {
35 	int i;
36 	struct pipe_ctx *pipe = NULL;
37 
38 	memset(link_res, 0, sizeof(*link_res));
39 
40 	for (i = 0; i < MAX_PIPES; i++) {
41 		pipe = &link->dc->current_state->res_ctx.pipe_ctx[i];
42 		if (pipe->stream && pipe->stream->link && pipe->top_pipe == NULL) {
43 			if (pipe->stream->link == link) {
44 				*link_res = pipe->link_res;
45 				break;
46 			}
47 		}
48 	}
49 
50 }
51 
52 void link_get_cur_res_map(const struct dc *dc, uint32_t *map)
53 {
54 	struct dc_link *link;
55 	uint32_t i;
56 	uint32_t hpo_dp_recycle_map = 0;
57 
58 	*map = 0;
59 
60 	if (dc->caps.dp_hpo) {
61 		for (i = 0; i < dc->caps.max_links; i++) {
62 			link = dc->links[i];
63 			if (link->link_status.link_active &&
64 					link_dp_get_encoding_format(&link->reported_link_cap) == DP_128b_132b_ENCODING &&
65 					link_dp_get_encoding_format(&link->cur_link_settings) != DP_128b_132b_ENCODING)
66 				/* hpo dp link encoder is considered as recycled, when RX reports 128b/132b encoding capability
67 				 * but current link doesn't use it.
68 				 */
69 				hpo_dp_recycle_map |= (1 << i);
70 		}
71 		*map |= (hpo_dp_recycle_map << LINK_RES_HPO_DP_REC_MAP__SHIFT);
72 	}
73 }
74 
75 void link_restore_res_map(const struct dc *dc, uint32_t *map)
76 {
77 	struct dc_link *link;
78 	uint32_t i;
79 	unsigned int available_hpo_dp_count;
80 	uint32_t hpo_dp_recycle_map = (*map & LINK_RES_HPO_DP_REC_MAP__MASK)
81 			>> LINK_RES_HPO_DP_REC_MAP__SHIFT;
82 
83 	if (dc->caps.dp_hpo) {
84 		available_hpo_dp_count = dc->res_pool->hpo_dp_link_enc_count;
85 		/* remove excess 128b/132b encoding support for not recycled links */
86 		for (i = 0; i < dc->caps.max_links; i++) {
87 			if ((hpo_dp_recycle_map & (1 << i)) == 0) {
88 				link = dc->links[i];
89 				if (link->type != dc_connection_none &&
90 						link_dp_get_encoding_format(&link->verified_link_cap) == DP_128b_132b_ENCODING) {
91 					if (available_hpo_dp_count > 0)
92 						available_hpo_dp_count--;
93 					else
94 						/* remove 128b/132b encoding capability by limiting verified link rate to HBR3 */
95 						link->verified_link_cap.link_rate = LINK_RATE_HIGH3;
96 				}
97 			}
98 		}
99 		/* remove excess 128b/132b encoding support for recycled links */
100 		for (i = 0; i < dc->caps.max_links; i++) {
101 			if ((hpo_dp_recycle_map & (1 << i)) != 0) {
102 				link = dc->links[i];
103 				if (link->type != dc_connection_none &&
104 						link_dp_get_encoding_format(&link->verified_link_cap) == DP_128b_132b_ENCODING) {
105 					if (available_hpo_dp_count > 0)
106 						available_hpo_dp_count--;
107 					else
108 						/* remove 128b/132b encoding capability by limiting verified link rate to HBR3 */
109 						link->verified_link_cap.link_rate = LINK_RATE_HIGH3;
110 				}
111 			}
112 		}
113 	}
114 }
115