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 dc_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 /**
53  * dc_get_cur_link_res_map() - take a snapshot of current link resource allocation state
54  * @dc: pointer to dc of the dm calling this
55  * @map: a dc link resource snapshot defined internally to dc.
56  *
57  * DM needs to capture a snapshot of current link resource allocation mapping
58  * and store it in its persistent storage.
59  *
60  * Some of the link resource is using first come first serve policy.
61  * The allocation mapping depends on original hotplug order. This information
62  * is lost after driver is loaded next time. The snapshot is used in order to
63  * restore link resource to its previous state so user will get consistent
64  * link capability allocation across reboot.
65  *
66  * Return: none (void function)
67  *
68  */
69 void dc_get_cur_link_res_map(const struct dc *dc, uint32_t *map)
70 {
71 	struct dc_link *link;
72 	uint32_t i;
73 	uint32_t hpo_dp_recycle_map = 0;
74 
75 	*map = 0;
76 
77 	if (dc->caps.dp_hpo) {
78 		for (i = 0; i < dc->caps.max_links; i++) {
79 			link = dc->links[i];
80 			if (link->link_status.link_active &&
81 					link_dp_get_encoding_format(&link->reported_link_cap) == DP_128b_132b_ENCODING &&
82 					link_dp_get_encoding_format(&link->cur_link_settings) != DP_128b_132b_ENCODING)
83 				/* hpo dp link encoder is considered as recycled, when RX reports 128b/132b encoding capability
84 				 * but current link doesn't use it.
85 				 */
86 				hpo_dp_recycle_map |= (1 << i);
87 		}
88 		*map |= (hpo_dp_recycle_map << LINK_RES_HPO_DP_REC_MAP__SHIFT);
89 	}
90 }
91 
92 /**
93  * dc_restore_link_res_map() - restore link resource allocation state from a snapshot
94  * @dc: pointer to dc of the dm calling this
95  * @map: a dc link resource snapshot defined internally to dc.
96  *
97  * DM needs to call this function after initial link detection on boot and
98  * before first commit streams to restore link resource allocation state
99  * from previous boot session.
100  *
101  * Some of the link resource is using first come first serve policy.
102  * The allocation mapping depends on original hotplug order. This information
103  * is lost after driver is loaded next time. The snapshot is used in order to
104  * restore link resource to its previous state so user will get consistent
105  * link capability allocation across reboot.
106  *
107  * Return: none (void function)
108  *
109  */
110 void dc_restore_link_res_map(const struct dc *dc, uint32_t *map)
111 {
112 	struct dc_link *link;
113 	uint32_t i;
114 	unsigned int available_hpo_dp_count;
115 	uint32_t hpo_dp_recycle_map = (*map & LINK_RES_HPO_DP_REC_MAP__MASK)
116 			>> LINK_RES_HPO_DP_REC_MAP__SHIFT;
117 
118 	if (dc->caps.dp_hpo) {
119 		available_hpo_dp_count = dc->res_pool->hpo_dp_link_enc_count;
120 		/* remove excess 128b/132b encoding support for not recycled links */
121 		for (i = 0; i < dc->caps.max_links; i++) {
122 			if ((hpo_dp_recycle_map & (1 << i)) == 0) {
123 				link = dc->links[i];
124 				if (link->type != dc_connection_none &&
125 						link_dp_get_encoding_format(&link->verified_link_cap) == DP_128b_132b_ENCODING) {
126 					if (available_hpo_dp_count > 0)
127 						available_hpo_dp_count--;
128 					else
129 						/* remove 128b/132b encoding capability by limiting verified link rate to HBR3 */
130 						link->verified_link_cap.link_rate = LINK_RATE_HIGH3;
131 				}
132 			}
133 		}
134 		/* remove excess 128b/132b encoding support for recycled links */
135 		for (i = 0; i < dc->caps.max_links; i++) {
136 			if ((hpo_dp_recycle_map & (1 << i)) != 0) {
137 				link = dc->links[i];
138 				if (link->type != dc_connection_none &&
139 						link_dp_get_encoding_format(&link->verified_link_cap) == DP_128b_132b_ENCODING) {
140 					if (available_hpo_dp_count > 0)
141 						available_hpo_dp_count--;
142 					else
143 						/* remove 128b/132b encoding capability by limiting verified link rate to HBR3 */
144 						link->verified_link_cap.link_rate = LINK_RATE_HIGH3;
145 				}
146 			}
147 		}
148 	}
149 }
150