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 HDCP_H_
27 #define HDCP_H_
28 
29 #include "mod_hdcp.h"
30 #include "hdcp_log.h"
31 
32 #define BCAPS_READY_MASK				0x20
33 #define BCAPS_REPEATER_MASK				0x40
34 #define BSTATUS_DEVICE_COUNT_MASK			0X007F
35 #define BSTATUS_MAX_DEVS_EXCEEDED_MASK			0x0080
36 #define BSTATUS_MAX_CASCADE_EXCEEDED_MASK		0x0800
37 #define BCAPS_HDCP_CAPABLE_MASK_DP			0x01
38 #define BCAPS_REPEATER_MASK_DP				0x02
39 #define BSTATUS_READY_MASK_DP				0x01
40 #define BSTATUS_R0_P_AVAILABLE_MASK_DP			0x02
41 #define BSTATUS_LINK_INTEGRITY_FAILURE_MASK_DP		0x04
42 #define BSTATUS_REAUTH_REQUEST_MASK_DP			0x08
43 #define BINFO_DEVICE_COUNT_MASK_DP			0X007F
44 #define BINFO_MAX_DEVS_EXCEEDED_MASK_DP			0x0080
45 #define BINFO_MAX_CASCADE_EXCEEDED_MASK_DP		0x0800
46 
47 #define RXSTATUS_MSG_SIZE_MASK				0x03FF
48 #define RXSTATUS_READY_MASK				0x0400
49 #define RXSTATUS_REAUTH_REQUEST_MASK			0x0800
50 #define RXIDLIST_DEVICE_COUNT_LOWER_MASK		0xf0
51 #define RXIDLIST_DEVICE_COUNT_UPPER_MASK		0x01
52 #define RXCAPS_BYTE0_HDCP_CAPABLE_MASK_DP		0x02
53 #define RXSTATUS_READY_MASK_DP				0x0001
54 #define RXSTATUS_H_P_AVAILABLE_MASK_DP			0x0002
55 #define RXSTATUS_PAIRING_AVAILABLE_MASK_DP		0x0004
56 #define RXSTATUS_REAUTH_REQUEST_MASK_DP			0x0008
57 #define RXSTATUS_LINK_INTEGRITY_FAILURE_MASK_DP		0x0010
58 
59 enum mod_hdcp_trans_input_result {
60 	UNKNOWN = 0,
61 	PASS,
62 	FAIL
63 };
64 
65 struct mod_hdcp_transition_input_hdcp1 {
66 	uint8_t bksv_read;
67 	uint8_t bksv_validation;
68 	uint8_t add_topology;
69 	uint8_t create_session;
70 	uint8_t an_write;
71 	uint8_t aksv_write;
72 	uint8_t ainfo_write;
73 	uint8_t bcaps_read;
74 	uint8_t r0p_read;
75 	uint8_t rx_validation;
76 	uint8_t encryption;
77 	uint8_t link_maintenance;
78 	uint8_t ready_check;
79 	uint8_t bstatus_read;
80 	uint8_t max_cascade_check;
81 	uint8_t max_devs_check;
82 	uint8_t device_count_check;
83 	uint8_t ksvlist_read;
84 	uint8_t vp_read;
85 	uint8_t ksvlist_vp_validation;
86 
87 	uint8_t hdcp_capable_dp;
88 	uint8_t binfo_read_dp;
89 	uint8_t r0p_available_dp;
90 	uint8_t link_integiry_check;
91 	uint8_t reauth_request_check;
92 	uint8_t stream_encryption_dp;
93 };
94 
95 union mod_hdcp_transition_input {
96 	struct mod_hdcp_transition_input_hdcp1 hdcp1;
97 };
98 
99 struct mod_hdcp_message_hdcp1 {
100 	uint8_t		an[8];
101 	uint8_t		aksv[5];
102 	uint8_t		ainfo;
103 	uint8_t		bksv[5];
104 	uint16_t	r0p;
105 	uint8_t		bcaps;
106 	uint16_t	bstatus;
107 	uint8_t		ksvlist[635];
108 	uint16_t	ksvlist_size;
109 	uint8_t		vp[20];
110 
111 	uint16_t	binfo_dp;
112 };
113 
114 union mod_hdcp_message {
115 	struct mod_hdcp_message_hdcp1 hdcp1;
116 };
117 
118 struct mod_hdcp_auth_counters {
119 	uint8_t stream_management_retry_count;
120 };
121 
122 /* contains values per connection */
123 struct mod_hdcp_connection {
124 	struct mod_hdcp_link link;
125 	struct mod_hdcp_display displays[MAX_NUM_OF_DISPLAYS];
126 	uint8_t is_repeater;
127 	uint8_t is_km_stored;
128 	struct mod_hdcp_trace trace;
129 	uint8_t hdcp1_retry_count;
130 };
131 
132 /* contains values per authentication cycle */
133 struct mod_hdcp_authentication {
134 	uint32_t id;
135 	union mod_hdcp_message msg;
136 	union mod_hdcp_transition_input trans_input;
137 	struct mod_hdcp_auth_counters count;
138 };
139 
140 /* contains values per state change */
141 struct mod_hdcp_state {
142 	uint8_t id;
143 	uint32_t stay_count;
144 };
145 
146 /* per event in a state */
147 struct mod_hdcp_event_context {
148 	enum mod_hdcp_event event;
149 	uint8_t rx_id_list_ready;
150 	uint8_t unexpected_event;
151 };
152 
153 struct mod_hdcp {
154 	/* per link */
155 	struct mod_hdcp_config config;
156 	/* per connection */
157 	struct mod_hdcp_connection connection;
158 	/* per authentication attempt */
159 	struct mod_hdcp_authentication auth;
160 	/* per state in an authentication */
161 	struct mod_hdcp_state state;
162 	/* reserved memory buffer */
163 	uint8_t buf[2025];
164 };
165 
166 enum mod_hdcp_initial_state_id {
167 	HDCP_UNINITIALIZED = 0x0,
168 	HDCP_INITIAL_STATE_START = HDCP_UNINITIALIZED,
169 	HDCP_INITIALIZED,
170 	HDCP_CP_NOT_DESIRED,
171 	HDCP_INITIAL_STATE_END = HDCP_CP_NOT_DESIRED
172 };
173 
174 enum mod_hdcp_hdcp1_state_id {
175 	HDCP1_STATE_START = HDCP_INITIAL_STATE_END,
176 	H1_A0_WAIT_FOR_ACTIVE_RX,
177 	H1_A1_EXCHANGE_KSVS,
178 	H1_A2_COMPUTATIONS_A3_VALIDATE_RX_A6_TEST_FOR_REPEATER,
179 	H1_A45_AUTHENTICATED,
180 	H1_A8_WAIT_FOR_READY,
181 	H1_A9_READ_KSV_LIST,
182 	HDCP1_STATE_END = H1_A9_READ_KSV_LIST
183 };
184 
185 enum mod_hdcp_hdcp1_dp_state_id {
186 	HDCP1_DP_STATE_START = HDCP1_STATE_END,
187 	D1_A0_DETERMINE_RX_HDCP_CAPABLE,
188 	D1_A1_EXCHANGE_KSVS,
189 	D1_A23_WAIT_FOR_R0_PRIME,
190 	D1_A2_COMPUTATIONS_A3_VALIDATE_RX_A5_TEST_FOR_REPEATER,
191 	D1_A4_AUTHENTICATED,
192 	D1_A6_WAIT_FOR_READY,
193 	D1_A7_READ_KSV_LIST,
194 	HDCP1_DP_STATE_END = D1_A7_READ_KSV_LIST,
195 };
196 
197 /* hdcp1 executions and transitions */
198 typedef enum mod_hdcp_status (*mod_hdcp_action)(struct mod_hdcp *hdcp);
199 uint8_t mod_hdcp_execute_and_set(
200 		mod_hdcp_action func, uint8_t *flag,
201 		enum mod_hdcp_status *status, struct mod_hdcp *hdcp, char *str);
202 enum mod_hdcp_status mod_hdcp_hdcp1_execution(struct mod_hdcp *hdcp,
203 	struct mod_hdcp_event_context *event_ctx,
204 	struct mod_hdcp_transition_input_hdcp1 *input);
205 enum mod_hdcp_status mod_hdcp_hdcp1_dp_execution(struct mod_hdcp *hdcp,
206 	struct mod_hdcp_event_context *event_ctx,
207 	struct mod_hdcp_transition_input_hdcp1 *input);
208 enum mod_hdcp_status mod_hdcp_hdcp1_transition(struct mod_hdcp *hdcp,
209 	struct mod_hdcp_event_context *event_ctx,
210 	struct mod_hdcp_transition_input_hdcp1 *input,
211 	struct mod_hdcp_output *output);
212 enum mod_hdcp_status mod_hdcp_hdcp1_dp_transition(struct mod_hdcp *hdcp,
213 	struct mod_hdcp_event_context *event_ctx,
214 	struct mod_hdcp_transition_input_hdcp1 *input,
215 	struct mod_hdcp_output *output);
216 
217 /* log functions */
218 void mod_hdcp_dump_binary_message(uint8_t *msg, uint32_t msg_size,
219 		uint8_t *buf, uint32_t buf_size);
220 /* TODO: add adjustment log */
221 
222 /* psp functions */
223 enum mod_hdcp_status mod_hdcp_add_display_topology(
224 		struct mod_hdcp *hdcp);
225 enum mod_hdcp_status mod_hdcp_remove_display_topology(
226 		struct mod_hdcp *hdcp);
227 enum mod_hdcp_status mod_hdcp_hdcp1_create_session(struct mod_hdcp *hdcp);
228 enum mod_hdcp_status mod_hdcp_hdcp1_destroy_session(struct mod_hdcp *hdcp);
229 enum mod_hdcp_status mod_hdcp_hdcp1_validate_rx(struct mod_hdcp *hdcp);
230 enum mod_hdcp_status mod_hdcp_hdcp1_enable_encryption(struct mod_hdcp *hdcp);
231 enum mod_hdcp_status mod_hdcp_hdcp1_validate_ksvlist_vp(struct mod_hdcp *hdcp);
232 enum mod_hdcp_status mod_hdcp_hdcp1_enable_dp_stream_encryption(
233 	struct mod_hdcp *hdcp);
234 enum mod_hdcp_status mod_hdcp_hdcp1_link_maintenance(struct mod_hdcp *hdcp);
235 enum mod_hdcp_status mod_hdcp_hdcp1_get_link_encryption_status(struct mod_hdcp *hdcp,
236 							       enum mod_hdcp_encryption_status *encryption_status);
237 /* ddc functions */
238 enum mod_hdcp_status mod_hdcp_read_bksv(struct mod_hdcp *hdcp);
239 enum mod_hdcp_status mod_hdcp_read_bcaps(struct mod_hdcp *hdcp);
240 enum mod_hdcp_status mod_hdcp_read_bstatus(struct mod_hdcp *hdcp);
241 enum mod_hdcp_status mod_hdcp_read_r0p(struct mod_hdcp *hdcp);
242 enum mod_hdcp_status mod_hdcp_read_ksvlist(struct mod_hdcp *hdcp);
243 enum mod_hdcp_status mod_hdcp_read_vp(struct mod_hdcp *hdcp);
244 enum mod_hdcp_status mod_hdcp_read_binfo(struct mod_hdcp *hdcp);
245 enum mod_hdcp_status mod_hdcp_write_aksv(struct mod_hdcp *hdcp);
246 enum mod_hdcp_status mod_hdcp_write_ainfo(struct mod_hdcp *hdcp);
247 enum mod_hdcp_status mod_hdcp_write_an(struct mod_hdcp *hdcp);
248 enum mod_hdcp_status mod_hdcp_read_rxcaps(struct mod_hdcp *hdcp);
249 enum mod_hdcp_status mod_hdcp_read_rxstatus(struct mod_hdcp *hdcp);
250 enum mod_hdcp_status mod_hdcp_read_ake_cert(struct mod_hdcp *hdcp);
251 enum mod_hdcp_status mod_hdcp_read_h_prime(struct mod_hdcp *hdcp);
252 enum mod_hdcp_status mod_hdcp_read_pairing_info(struct mod_hdcp *hdcp);
253 enum mod_hdcp_status mod_hdcp_read_l_prime(struct mod_hdcp *hdcp);
254 enum mod_hdcp_status mod_hdcp_read_rx_id_list(struct mod_hdcp *hdcp);
255 enum mod_hdcp_status mod_hdcp_read_stream_ready(struct mod_hdcp *hdcp);
256 enum mod_hdcp_status mod_hdcp_write_ake_init(struct mod_hdcp *hdcp);
257 enum mod_hdcp_status mod_hdcp_write_no_stored_km(struct mod_hdcp *hdcp);
258 enum mod_hdcp_status mod_hdcp_write_stored_km(struct mod_hdcp *hdcp);
259 enum mod_hdcp_status mod_hdcp_write_lc_init(struct mod_hdcp *hdcp);
260 enum mod_hdcp_status mod_hdcp_write_eks(struct mod_hdcp *hdcp);
261 enum mod_hdcp_status mod_hdcp_write_repeater_auth_ack(struct mod_hdcp *hdcp);
262 enum mod_hdcp_status mod_hdcp_write_stream_manage(struct mod_hdcp *hdcp);
263 enum mod_hdcp_status mod_hdcp_write_content_type(struct mod_hdcp *hdcp);
264 
265 /* hdcp version helpers */
266 static inline uint8_t is_dp_hdcp(struct mod_hdcp *hdcp)
267 {
268 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP ||
269 			hdcp->connection.link.mode == MOD_HDCP_MODE_DP_MST);
270 }
271 
272 static inline uint8_t is_dp_mst_hdcp(struct mod_hdcp *hdcp)
273 {
274 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP_MST);
275 }
276 
277 static inline uint8_t is_hdmi_dvi_sl_hdcp(struct mod_hdcp *hdcp)
278 {
279 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DEFAULT);
280 }
281 
282 /* hdcp state helpers */
283 static inline uint8_t current_state(struct mod_hdcp *hdcp)
284 {
285 	return hdcp->state.id;
286 }
287 
288 static inline void set_state_id(struct mod_hdcp *hdcp,
289 		struct mod_hdcp_output *output, uint8_t id)
290 {
291 	memset(&hdcp->state, 0, sizeof(hdcp->state));
292 	hdcp->state.id = id;
293 	/* callback timer should be reset per state */
294 	output->callback_stop = 1;
295 	output->watchdog_timer_stop = 1;
296 	HDCP_NEXT_STATE_TRACE(hdcp, id, output);
297 }
298 
299 static inline uint8_t is_in_hdcp1_states(struct mod_hdcp *hdcp)
300 {
301 	return (current_state(hdcp) > HDCP1_STATE_START &&
302 			current_state(hdcp) <= HDCP1_STATE_END);
303 }
304 
305 static inline uint8_t is_in_hdcp1_dp_states(struct mod_hdcp *hdcp)
306 {
307 	return (current_state(hdcp) > HDCP1_DP_STATE_START &&
308 			current_state(hdcp) <= HDCP1_DP_STATE_END);
309 }
310 
311 static inline uint8_t is_hdcp1(struct mod_hdcp *hdcp)
312 {
313 	return (is_in_hdcp1_states(hdcp) || is_in_hdcp1_dp_states(hdcp));
314 }
315 
316 static inline uint8_t is_in_cp_not_desired_state(struct mod_hdcp *hdcp)
317 {
318 	return current_state(hdcp) == HDCP_CP_NOT_DESIRED;
319 }
320 
321 static inline uint8_t is_in_initialized_state(struct mod_hdcp *hdcp)
322 {
323 	return current_state(hdcp) == HDCP_INITIALIZED;
324 }
325 
326 /* transition operation helpers */
327 static inline void increment_stay_counter(struct mod_hdcp *hdcp)
328 {
329 	hdcp->state.stay_count++;
330 }
331 
332 static inline void fail_and_restart_in_ms(uint16_t time,
333 		enum mod_hdcp_status *status,
334 		struct mod_hdcp_output *output)
335 {
336 	output->callback_needed = 1;
337 	output->callback_delay = time;
338 	output->watchdog_timer_needed = 0;
339 	output->watchdog_timer_delay = 0;
340 	*status = MOD_HDCP_STATUS_RESET_NEEDED;
341 }
342 
343 static inline void callback_in_ms(uint16_t time, struct mod_hdcp_output *output)
344 {
345 	output->callback_needed = 1;
346 	output->callback_delay = time;
347 }
348 
349 static inline void set_watchdog_in_ms(struct mod_hdcp *hdcp, uint16_t time,
350 		struct mod_hdcp_output *output)
351 {
352 	output->watchdog_timer_needed = 1;
353 	output->watchdog_timer_delay = time;
354 }
355 
356 /* connection topology helpers */
357 static inline uint8_t is_display_active(struct mod_hdcp_display *display)
358 {
359 	return display->state >= MOD_HDCP_DISPLAY_ACTIVE;
360 }
361 
362 static inline uint8_t is_display_added(struct mod_hdcp_display *display)
363 {
364 	return display->state >= MOD_HDCP_DISPLAY_ACTIVE_AND_ADDED;
365 }
366 
367 static inline uint8_t is_display_encryption_enabled(struct mod_hdcp_display *display)
368 {
369 	return display->state >= MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
370 }
371 
372 static inline uint8_t get_active_display_count(struct mod_hdcp *hdcp)
373 {
374 	uint8_t added_count = 0;
375 	uint8_t i;
376 
377 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
378 		if (is_display_active(&hdcp->connection.displays[i]))
379 			added_count++;
380 	return added_count;
381 }
382 
383 static inline uint8_t get_added_display_count(struct mod_hdcp *hdcp)
384 {
385 	uint8_t added_count = 0;
386 	uint8_t i;
387 
388 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
389 		if (is_display_added(&hdcp->connection.displays[i]))
390 			added_count++;
391 	return added_count;
392 }
393 
394 static inline struct mod_hdcp_display *get_first_added_display(
395 		struct mod_hdcp *hdcp)
396 {
397 	uint8_t i;
398 	struct mod_hdcp_display *display = NULL;
399 
400 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
401 		if (is_display_added(&hdcp->connection.displays[i])) {
402 			display = &hdcp->connection.displays[i];
403 			break;
404 		}
405 	return display;
406 }
407 
408 static inline struct mod_hdcp_display *get_active_display_at_index(
409 		struct mod_hdcp *hdcp, uint8_t index)
410 {
411 	uint8_t i;
412 	struct mod_hdcp_display *display = NULL;
413 
414 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
415 		if (hdcp->connection.displays[i].index == index &&
416 				is_display_active(&hdcp->connection.displays[i])) {
417 			display = &hdcp->connection.displays[i];
418 			break;
419 		}
420 	return display;
421 }
422 
423 static inline struct mod_hdcp_display *get_empty_display_container(
424 		struct mod_hdcp *hdcp)
425 {
426 	uint8_t i;
427 	struct mod_hdcp_display *display = NULL;
428 
429 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
430 		if (!is_display_active(&hdcp->connection.displays[i])) {
431 			display = &hdcp->connection.displays[i];
432 			break;
433 		}
434 	return display;
435 }
436 
437 static inline void reset_retry_counts(struct mod_hdcp *hdcp)
438 {
439 	hdcp->connection.hdcp1_retry_count = 0;
440 }
441 
442 #endif /* HDCP_H_ */
443