1 /*
2  * Copyright 2019 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 #ifndef MOD_HDCP_H_
27 #define MOD_HDCP_H_
28 
29 #include "os_types.h"
30 #include "signal_types.h"
31 
32 /* Forward Declarations */
33 struct mod_hdcp;
34 
35 #define MAX_NUM_OF_DISPLAYS 6
36 #define MAX_NUM_OF_ATTEMPTS 4
37 #define MAX_NUM_OF_ERROR_TRACE 10
38 
39 /* detailed return status */
40 enum mod_hdcp_status {
41 	MOD_HDCP_STATUS_SUCCESS = 0,
42 	MOD_HDCP_STATUS_FAILURE,
43 	MOD_HDCP_STATUS_RESET_NEEDED,
44 	MOD_HDCP_STATUS_DISPLAY_OUT_OF_BOUND,
45 	MOD_HDCP_STATUS_DISPLAY_NOT_FOUND,
46 	MOD_HDCP_STATUS_INVALID_STATE,
47 	MOD_HDCP_STATUS_NOT_IMPLEMENTED,
48 	MOD_HDCP_STATUS_INTERNAL_POLICY_FAILURE,
49 	MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE,
50 	MOD_HDCP_STATUS_CREATE_PSP_SERVICE_FAILURE,
51 	MOD_HDCP_STATUS_DESTROY_PSP_SERVICE_FAILURE,
52 	MOD_HDCP_STATUS_HDCP1_CREATE_SESSION_FAILURE,
53 	MOD_HDCP_STATUS_HDCP1_DESTROY_SESSION_FAILURE,
54 	MOD_HDCP_STATUS_HDCP1_VALIDATE_ENCRYPTION_FAILURE,
55 	MOD_HDCP_STATUS_HDCP1_NOT_HDCP_REPEATER,
56 	MOD_HDCP_STATUS_HDCP1_NOT_CAPABLE,
57 	MOD_HDCP_STATUS_HDCP1_R0_PRIME_PENDING,
58 	MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE,
59 	MOD_HDCP_STATUS_HDCP1_KSV_LIST_NOT_READY,
60 	MOD_HDCP_STATUS_HDCP1_VALIDATE_KSV_LIST_FAILURE,
61 	MOD_HDCP_STATUS_HDCP1_ENABLE_ENCRYPTION,
62 	MOD_HDCP_STATUS_HDCP1_ENABLE_STREAM_ENCRYPTION_FAILURE,
63 	MOD_HDCP_STATUS_HDCP1_MAX_CASCADE_EXCEEDED_FAILURE,
64 	MOD_HDCP_STATUS_HDCP1_MAX_DEVS_EXCEEDED_FAILURE,
65 	MOD_HDCP_STATUS_HDCP1_DEVICE_COUNT_MISMATCH_FAILURE,
66 	MOD_HDCP_STATUS_HDCP1_LINK_INTEGRITY_FAILURE,
67 	MOD_HDCP_STATUS_HDCP1_REAUTH_REQUEST_ISSUED,
68 	MOD_HDCP_STATUS_HDCP1_LINK_MAINTENANCE_FAILURE,
69 	MOD_HDCP_STATUS_HDCP1_INVALID_BKSV,
70 	MOD_HDCP_STATUS_DDC_FAILURE, /* TODO: specific errors */
71 	MOD_HDCP_STATUS_INVALID_OPERATION,
72 	MOD_HDCP_STATUS_HDCP2_NOT_CAPABLE,
73 	MOD_HDCP_STATUS_HDCP2_CREATE_SESSION_FAILURE,
74 	MOD_HDCP_STATUS_HDCP2_DESTROY_SESSION_FAILURE,
75 	MOD_HDCP_STATUS_HDCP2_PREP_AKE_INIT_FAILURE,
76 	MOD_HDCP_STATUS_HDCP2_AKE_CERT_PENDING,
77 	MOD_HDCP_STATUS_HDCP2_H_PRIME_PENDING,
78 	MOD_HDCP_STATUS_HDCP2_PAIRING_INFO_PENDING,
79 	MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE,
80 	MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE,
81 	MOD_HDCP_STATUS_HDCP2_VALIDATE_PAIRING_INFO_FAILURE,
82 	MOD_HDCP_STATUS_HDCP2_PREP_LC_INIT_FAILURE,
83 	MOD_HDCP_STATUS_HDCP2_L_PRIME_PENDING,
84 	MOD_HDCP_STATUS_HDCP2_VALIDATE_L_PRIME_FAILURE,
85 	MOD_HDCP_STATUS_HDCP2_PREP_EKS_FAILURE,
86 	MOD_HDCP_STATUS_HDCP2_ENABLE_ENCRYPTION_FAILURE,
87 	MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_NOT_READY,
88 	MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE,
89 	MOD_HDCP_STATUS_HDCP2_ENABLE_STREAM_ENCRYPTION,
90 	MOD_HDCP_STATUS_HDCP2_STREAM_READY_PENDING,
91 	MOD_HDCP_STATUS_HDCP2_VALIDATE_STREAM_READY_FAILURE,
92 	MOD_HDCP_STATUS_HDCP2_PREPARE_STREAM_MANAGEMENT_FAILURE,
93 	MOD_HDCP_STATUS_HDCP2_REAUTH_REQUEST,
94 	MOD_HDCP_STATUS_HDCP2_REAUTH_LINK_INTEGRITY_FAILURE,
95 	MOD_HDCP_STATUS_HDCP2_DEVICE_COUNT_MISMATCH_FAILURE,
96 };
97 
98 struct mod_hdcp_displayport {
99 	uint8_t rev;
100 	uint8_t assr_supported;
101 };
102 
103 struct mod_hdcp_hdmi {
104 	uint8_t reserved;
105 };
106 enum mod_hdcp_operation_mode {
107 	MOD_HDCP_MODE_OFF,
108 	MOD_HDCP_MODE_DEFAULT,
109 	MOD_HDCP_MODE_DP,
110 	MOD_HDCP_MODE_DP_MST
111 };
112 
113 enum mod_hdcp_display_state {
114 	MOD_HDCP_DISPLAY_INACTIVE = 0,
115 	MOD_HDCP_DISPLAY_ACTIVE,
116 	MOD_HDCP_DISPLAY_ACTIVE_AND_ADDED,
117 	MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED
118 };
119 
120 struct mod_hdcp_ddc {
121 	void *handle;
122 	struct {
123 		bool (*read_i2c)(void *handle,
124 				uint32_t address,
125 				uint8_t offset,
126 				uint8_t *data,
127 				uint32_t size);
128 		bool (*write_i2c)(void *handle,
129 				uint32_t address,
130 				const uint8_t *data,
131 				uint32_t size);
132 		bool (*read_dpcd)(void *handle,
133 				uint32_t address,
134 				uint8_t *data,
135 				uint32_t size);
136 		bool (*write_dpcd)(void *handle,
137 				uint32_t address,
138 				const uint8_t *data,
139 				uint32_t size);
140 	} funcs;
141 };
142 
143 struct mod_hdcp_psp {
144 	void *handle;
145 	void *funcs;
146 };
147 
148 struct mod_hdcp_display_adjustment {
149 	uint8_t disable			: 1;
150 	uint8_t reserved		: 7;
151 };
152 
153 struct mod_hdcp_link_adjustment_hdcp1 {
154 	uint8_t disable			: 1;
155 	uint8_t postpone_encryption	: 1;
156 	uint8_t reserved		: 6;
157 };
158 
159 struct mod_hdcp_link_adjustment_hdcp2 {
160 	uint8_t disable			: 1;
161 	uint8_t disable_type1		: 1;
162 	uint8_t force_no_stored_km	: 1;
163 	uint8_t increase_h_prime_timeout: 1;
164 	uint8_t reserved		: 4;
165 };
166 
167 struct mod_hdcp_link_adjustment {
168 	uint8_t auth_delay;
169 	struct mod_hdcp_link_adjustment_hdcp1 hdcp1;
170 	struct mod_hdcp_link_adjustment_hdcp2 hdcp2;
171 };
172 
173 struct mod_hdcp_error {
174 	enum mod_hdcp_status status;
175 	uint8_t state_id;
176 };
177 
178 struct mod_hdcp_trace {
179 	struct mod_hdcp_error errors[MAX_NUM_OF_ERROR_TRACE];
180 	uint8_t error_count;
181 };
182 
183 enum mod_hdcp_encryption_status {
184 	MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF = 0,
185 	MOD_HDCP_ENCRYPTION_STATUS_HDCP1_ON,
186 	MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE0_ON,
187 	MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE1_ON
188 };
189 
190 /* per link events dm has to notify to hdcp module */
191 enum mod_hdcp_event {
192 	MOD_HDCP_EVENT_CALLBACK = 0,
193 	MOD_HDCP_EVENT_WATCHDOG_TIMEOUT,
194 	MOD_HDCP_EVENT_CPIRQ
195 };
196 
197 /* output flags from module requesting timer operations */
198 struct mod_hdcp_output {
199 	uint8_t callback_needed;
200 	uint8_t callback_stop;
201 	uint8_t watchdog_timer_needed;
202 	uint8_t watchdog_timer_stop;
203 	uint16_t callback_delay;
204 	uint16_t watchdog_timer_delay;
205 };
206 
207 /* used to represent per display info */
208 struct mod_hdcp_display {
209 	enum mod_hdcp_display_state state;
210 	uint8_t index;
211 	uint8_t controller;
212 	uint8_t dig_fe;
213 	union {
214 		uint8_t vc_id;
215 	};
216 	struct mod_hdcp_display_adjustment adjust;
217 };
218 
219 /* used to represent per link info */
220 /* in case a link has multiple displays, they share the same link info */
221 struct mod_hdcp_link {
222 	enum mod_hdcp_operation_mode mode;
223 	uint8_t dig_be;
224 	uint8_t ddc_line;
225 	union {
226 		struct mod_hdcp_displayport dp;
227 		struct mod_hdcp_hdmi hdmi;
228 	};
229 	struct mod_hdcp_link_adjustment adjust;
230 };
231 
232 /* a query structure for a display's hdcp information */
233 struct mod_hdcp_display_query {
234 	const struct mod_hdcp_display *display;
235 	const struct mod_hdcp_link *link;
236 	const struct mod_hdcp_trace *trace;
237 	enum mod_hdcp_encryption_status encryption_status;
238 };
239 
240 /* contains values per on external display configuration change */
241 struct mod_hdcp_config {
242 	struct mod_hdcp_psp psp;
243 	struct mod_hdcp_ddc ddc;
244 	uint8_t index;
245 };
246 
247 struct mod_hdcp;
248 
249 /* dm allocates memory of mod_hdcp per dc_link on dm init based on memory size*/
250 size_t mod_hdcp_get_memory_size(void);
251 
252 /* called per link on link creation */
253 enum mod_hdcp_status mod_hdcp_setup(struct mod_hdcp *hdcp,
254 		struct mod_hdcp_config *config);
255 
256 /* called per link on link destroy */
257 enum mod_hdcp_status mod_hdcp_teardown(struct mod_hdcp *hdcp);
258 
259 /* called per display on cp_desired set to true */
260 enum mod_hdcp_status mod_hdcp_add_display(struct mod_hdcp *hdcp,
261 		struct mod_hdcp_link *link, struct mod_hdcp_display *display,
262 		struct mod_hdcp_output *output);
263 
264 /* called per display on cp_desired set to false */
265 enum mod_hdcp_status mod_hdcp_remove_display(struct mod_hdcp *hdcp,
266 		uint8_t index, struct mod_hdcp_output *output);
267 
268 /* called to query hdcp information on a specific index */
269 enum mod_hdcp_status mod_hdcp_query_display(struct mod_hdcp *hdcp,
270 		uint8_t index, struct mod_hdcp_display_query *query);
271 
272 /* called per link on connectivity change */
273 enum mod_hdcp_status mod_hdcp_reset_connection(struct mod_hdcp *hdcp,
274 		struct mod_hdcp_output *output);
275 
276 /* called per link on events (i.e. callback, watchdog, CP_IRQ) */
277 enum mod_hdcp_status mod_hdcp_process_event(struct mod_hdcp *hdcp,
278 		enum mod_hdcp_event event, struct mod_hdcp_output *output);
279 
280 /* called to convert enum mod_hdcp_status to c string */
281 char *mod_hdcp_status_to_str(int32_t status);
282 
283 /* called to convert state id to c string */
284 char *mod_hdcp_state_id_to_str(int32_t id);
285 
286 /* called to convert signal type to operation mode */
287 enum mod_hdcp_operation_mode mod_hdcp_signal_type_to_operation_mode(
288 		enum signal_type signal);
289 #endif /* MOD_HDCP_H_ */
290