1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2023, Intel Corporation.
4  */
5 
6 #include <drm/i915_hdcp_interface.h>
7 
8 #include "gem/i915_gem_region.h"
9 #include "gt/intel_gt.h"
10 #include "gt/uc/intel_gsc_uc_heci_cmd_submit.h"
11 #include "i915_drv.h"
12 #include "i915_utils.h"
13 #include "intel_hdcp_gsc.h"
14 
15 bool intel_hdcp_gsc_cs_required(struct drm_i915_private *i915)
16 {
17 	return DISPLAY_VER(i915) >= 14;
18 }
19 
20 static int
21 gsc_hdcp_initiate_session(struct device *dev, struct hdcp_port_data *data,
22 			  struct hdcp2_ake_init *ake_data)
23 {
24 	struct wired_cmd_initiate_hdcp2_session_in session_init_in = { { 0 } };
25 	struct wired_cmd_initiate_hdcp2_session_out
26 						session_init_out = { { 0 } };
27 	struct drm_i915_private *i915;
28 	ssize_t byte;
29 
30 	if (!dev || !data || !ake_data)
31 		return -EINVAL;
32 
33 	i915 = kdev_to_i915(dev);
34 	if (!i915) {
35 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
36 		return -ENODEV;
37 	}
38 
39 	session_init_in.header.api_version = HDCP_API_VERSION;
40 	session_init_in.header.command_id = WIRED_INITIATE_HDCP2_SESSION;
41 	session_init_in.header.status = FW_HDCP_STATUS_SUCCESS;
42 	session_init_in.header.buffer_len =
43 				WIRED_CMD_BUF_LEN_INITIATE_HDCP2_SESSION_IN;
44 
45 	session_init_in.port.integrated_port_type = data->port_type;
46 	session_init_in.port.physical_port = (u8)data->hdcp_ddi;
47 	session_init_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
48 	session_init_in.protocol = data->protocol;
49 
50 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&session_init_in,
51 				       sizeof(session_init_in),
52 				       (u8 *)&session_init_out,
53 				       sizeof(session_init_out));
54 	if (byte < 0) {
55 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
56 		return byte;
57 	}
58 
59 	if (session_init_out.header.status != FW_HDCP_STATUS_SUCCESS) {
60 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X Failed. Status: 0x%X\n",
61 			    WIRED_INITIATE_HDCP2_SESSION,
62 			    session_init_out.header.status);
63 		return -EIO;
64 	}
65 
66 	ake_data->msg_id = HDCP_2_2_AKE_INIT;
67 	ake_data->tx_caps = session_init_out.tx_caps;
68 	memcpy(ake_data->r_tx, session_init_out.r_tx, HDCP_2_2_RTX_LEN);
69 
70 	return 0;
71 }
72 
73 static int
74 gsc_hdcp_verify_receiver_cert_prepare_km(struct device *dev,
75 					 struct hdcp_port_data *data,
76 					 struct hdcp2_ake_send_cert *rx_cert,
77 					 bool *km_stored,
78 					 struct hdcp2_ake_no_stored_km
79 								*ek_pub_km,
80 					 size_t *msg_sz)
81 {
82 	struct wired_cmd_verify_receiver_cert_in verify_rxcert_in = { { 0 } };
83 	struct wired_cmd_verify_receiver_cert_out verify_rxcert_out = { { 0 } };
84 	struct drm_i915_private *i915;
85 	ssize_t byte;
86 
87 	if (!dev || !data || !rx_cert || !km_stored || !ek_pub_km || !msg_sz)
88 		return -EINVAL;
89 
90 	i915 = kdev_to_i915(dev);
91 	if (!i915) {
92 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
93 		return -ENODEV;
94 	}
95 
96 	verify_rxcert_in.header.api_version = HDCP_API_VERSION;
97 	verify_rxcert_in.header.command_id = WIRED_VERIFY_RECEIVER_CERT;
98 	verify_rxcert_in.header.status = FW_HDCP_STATUS_SUCCESS;
99 	verify_rxcert_in.header.buffer_len =
100 				WIRED_CMD_BUF_LEN_VERIFY_RECEIVER_CERT_IN;
101 
102 	verify_rxcert_in.port.integrated_port_type = data->port_type;
103 	verify_rxcert_in.port.physical_port = (u8)data->hdcp_ddi;
104 	verify_rxcert_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
105 
106 	verify_rxcert_in.cert_rx = rx_cert->cert_rx;
107 	memcpy(verify_rxcert_in.r_rx, &rx_cert->r_rx, HDCP_2_2_RRX_LEN);
108 	memcpy(verify_rxcert_in.rx_caps, rx_cert->rx_caps, HDCP_2_2_RXCAPS_LEN);
109 
110 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&verify_rxcert_in,
111 				       sizeof(verify_rxcert_in),
112 				       (u8 *)&verify_rxcert_out,
113 				       sizeof(verify_rxcert_out));
114 	if (byte < 0) {
115 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed: %zd\n", byte);
116 		return byte;
117 	}
118 
119 	if (verify_rxcert_out.header.status != FW_HDCP_STATUS_SUCCESS) {
120 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X Failed. Status: 0x%X\n",
121 			    WIRED_VERIFY_RECEIVER_CERT,
122 			    verify_rxcert_out.header.status);
123 		return -EIO;
124 	}
125 
126 	*km_stored = !!verify_rxcert_out.km_stored;
127 	if (verify_rxcert_out.km_stored) {
128 		ek_pub_km->msg_id = HDCP_2_2_AKE_STORED_KM;
129 		*msg_sz = sizeof(struct hdcp2_ake_stored_km);
130 	} else {
131 		ek_pub_km->msg_id = HDCP_2_2_AKE_NO_STORED_KM;
132 		*msg_sz = sizeof(struct hdcp2_ake_no_stored_km);
133 	}
134 
135 	memcpy(ek_pub_km->e_kpub_km, &verify_rxcert_out.ekm_buff,
136 	       sizeof(verify_rxcert_out.ekm_buff));
137 
138 	return 0;
139 }
140 
141 static int
142 gsc_hdcp_verify_hprime(struct device *dev, struct hdcp_port_data *data,
143 		       struct hdcp2_ake_send_hprime *rx_hprime)
144 {
145 	struct wired_cmd_ake_send_hprime_in send_hprime_in = { { 0 } };
146 	struct wired_cmd_ake_send_hprime_out send_hprime_out = { { 0 } };
147 	struct drm_i915_private *i915;
148 	ssize_t byte;
149 
150 	if (!dev || !data || !rx_hprime)
151 		return -EINVAL;
152 
153 	i915 = kdev_to_i915(dev);
154 	if (!i915) {
155 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
156 		return -ENODEV;
157 	}
158 
159 	send_hprime_in.header.api_version = HDCP_API_VERSION;
160 	send_hprime_in.header.command_id = WIRED_AKE_SEND_HPRIME;
161 	send_hprime_in.header.status = FW_HDCP_STATUS_SUCCESS;
162 	send_hprime_in.header.buffer_len = WIRED_CMD_BUF_LEN_AKE_SEND_HPRIME_IN;
163 
164 	send_hprime_in.port.integrated_port_type = data->port_type;
165 	send_hprime_in.port.physical_port = (u8)data->hdcp_ddi;
166 	send_hprime_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
167 
168 	memcpy(send_hprime_in.h_prime, rx_hprime->h_prime,
169 	       HDCP_2_2_H_PRIME_LEN);
170 
171 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&send_hprime_in,
172 				       sizeof(send_hprime_in),
173 				       (u8 *)&send_hprime_out,
174 				       sizeof(send_hprime_out));
175 	if (byte < 0) {
176 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
177 		return byte;
178 	}
179 
180 	if (send_hprime_out.header.status != FW_HDCP_STATUS_SUCCESS) {
181 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X Failed. Status: 0x%X\n",
182 			    WIRED_AKE_SEND_HPRIME, send_hprime_out.header.status);
183 		return -EIO;
184 	}
185 
186 	return 0;
187 }
188 
189 static int
190 gsc_hdcp_store_pairing_info(struct device *dev, struct hdcp_port_data *data,
191 			    struct hdcp2_ake_send_pairing_info *pairing_info)
192 {
193 	struct wired_cmd_ake_send_pairing_info_in pairing_info_in = { { 0 } };
194 	struct wired_cmd_ake_send_pairing_info_out pairing_info_out = { { 0 } };
195 	struct drm_i915_private *i915;
196 	ssize_t byte;
197 
198 	if (!dev || !data || !pairing_info)
199 		return -EINVAL;
200 
201 	i915 = kdev_to_i915(dev);
202 	if (!i915) {
203 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
204 		return -ENODEV;
205 	}
206 
207 	pairing_info_in.header.api_version = HDCP_API_VERSION;
208 	pairing_info_in.header.command_id = WIRED_AKE_SEND_PAIRING_INFO;
209 	pairing_info_in.header.status = FW_HDCP_STATUS_SUCCESS;
210 	pairing_info_in.header.buffer_len =
211 					WIRED_CMD_BUF_LEN_SEND_PAIRING_INFO_IN;
212 
213 	pairing_info_in.port.integrated_port_type = data->port_type;
214 	pairing_info_in.port.physical_port = (u8)data->hdcp_ddi;
215 	pairing_info_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
216 
217 	memcpy(pairing_info_in.e_kh_km, pairing_info->e_kh_km,
218 	       HDCP_2_2_E_KH_KM_LEN);
219 
220 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&pairing_info_in,
221 				       sizeof(pairing_info_in),
222 				       (u8 *)&pairing_info_out,
223 				       sizeof(pairing_info_out));
224 	if (byte < 0) {
225 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
226 		return byte;
227 	}
228 
229 	if (pairing_info_out.header.status != FW_HDCP_STATUS_SUCCESS) {
230 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X failed. Status: 0x%X\n",
231 			    WIRED_AKE_SEND_PAIRING_INFO,
232 			    pairing_info_out.header.status);
233 		return -EIO;
234 	}
235 
236 	return 0;
237 }
238 
239 static int
240 gsc_hdcp_initiate_locality_check(struct device *dev,
241 				 struct hdcp_port_data *data,
242 				 struct hdcp2_lc_init *lc_init_data)
243 {
244 	struct wired_cmd_init_locality_check_in lc_init_in = { { 0 } };
245 	struct wired_cmd_init_locality_check_out lc_init_out = { { 0 } };
246 	struct drm_i915_private *i915;
247 	ssize_t byte;
248 
249 	if (!dev || !data || !lc_init_data)
250 		return -EINVAL;
251 
252 	i915 = kdev_to_i915(dev);
253 	if (!i915) {
254 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
255 		return -ENODEV;
256 	}
257 
258 	lc_init_in.header.api_version = HDCP_API_VERSION;
259 	lc_init_in.header.command_id = WIRED_INIT_LOCALITY_CHECK;
260 	lc_init_in.header.status = FW_HDCP_STATUS_SUCCESS;
261 	lc_init_in.header.buffer_len = WIRED_CMD_BUF_LEN_INIT_LOCALITY_CHECK_IN;
262 
263 	lc_init_in.port.integrated_port_type = data->port_type;
264 	lc_init_in.port.physical_port = (u8)data->hdcp_ddi;
265 	lc_init_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
266 
267 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&lc_init_in, sizeof(lc_init_in),
268 				       (u8 *)&lc_init_out, sizeof(lc_init_out));
269 	if (byte < 0) {
270 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
271 		return byte;
272 	}
273 
274 	if (lc_init_out.header.status != FW_HDCP_STATUS_SUCCESS) {
275 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X Failed. status: 0x%X\n",
276 			    WIRED_INIT_LOCALITY_CHECK, lc_init_out.header.status);
277 		return -EIO;
278 	}
279 
280 	lc_init_data->msg_id = HDCP_2_2_LC_INIT;
281 	memcpy(lc_init_data->r_n, lc_init_out.r_n, HDCP_2_2_RN_LEN);
282 
283 	return 0;
284 }
285 
286 static int
287 gsc_hdcp_verify_lprime(struct device *dev, struct hdcp_port_data *data,
288 		       struct hdcp2_lc_send_lprime *rx_lprime)
289 {
290 	struct wired_cmd_validate_locality_in verify_lprime_in = { { 0 } };
291 	struct wired_cmd_validate_locality_out verify_lprime_out = { { 0 } };
292 	struct drm_i915_private *i915;
293 	ssize_t byte;
294 
295 	if (!dev || !data || !rx_lprime)
296 		return -EINVAL;
297 
298 	i915 = kdev_to_i915(dev);
299 	if (!i915) {
300 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
301 		return -ENODEV;
302 	}
303 
304 	verify_lprime_in.header.api_version = HDCP_API_VERSION;
305 	verify_lprime_in.header.command_id = WIRED_VALIDATE_LOCALITY;
306 	verify_lprime_in.header.status = FW_HDCP_STATUS_SUCCESS;
307 	verify_lprime_in.header.buffer_len =
308 					WIRED_CMD_BUF_LEN_VALIDATE_LOCALITY_IN;
309 
310 	verify_lprime_in.port.integrated_port_type = data->port_type;
311 	verify_lprime_in.port.physical_port = (u8)data->hdcp_ddi;
312 	verify_lprime_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
313 
314 	memcpy(verify_lprime_in.l_prime, rx_lprime->l_prime,
315 	       HDCP_2_2_L_PRIME_LEN);
316 
317 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&verify_lprime_in,
318 				       sizeof(verify_lprime_in),
319 				       (u8 *)&verify_lprime_out,
320 				       sizeof(verify_lprime_out));
321 	if (byte < 0) {
322 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
323 		return byte;
324 	}
325 
326 	if (verify_lprime_out.header.status != FW_HDCP_STATUS_SUCCESS) {
327 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X failed. status: 0x%X\n",
328 			    WIRED_VALIDATE_LOCALITY,
329 			    verify_lprime_out.header.status);
330 		return -EIO;
331 	}
332 
333 	return 0;
334 }
335 
336 static int gsc_hdcp_get_session_key(struct device *dev,
337 				    struct hdcp_port_data *data,
338 				    struct hdcp2_ske_send_eks *ske_data)
339 {
340 	struct wired_cmd_get_session_key_in get_skey_in = { { 0 } };
341 	struct wired_cmd_get_session_key_out get_skey_out = { { 0 } };
342 	struct drm_i915_private *i915;
343 	ssize_t byte;
344 
345 	if (!dev || !data || !ske_data)
346 		return -EINVAL;
347 
348 	i915 = kdev_to_i915(dev);
349 	if (!i915) {
350 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
351 		return -ENODEV;
352 	}
353 
354 	get_skey_in.header.api_version = HDCP_API_VERSION;
355 	get_skey_in.header.command_id = WIRED_GET_SESSION_KEY;
356 	get_skey_in.header.status = FW_HDCP_STATUS_SUCCESS;
357 	get_skey_in.header.buffer_len = WIRED_CMD_BUF_LEN_GET_SESSION_KEY_IN;
358 
359 	get_skey_in.port.integrated_port_type = data->port_type;
360 	get_skey_in.port.physical_port = (u8)data->hdcp_ddi;
361 	get_skey_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
362 
363 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&get_skey_in, sizeof(get_skey_in),
364 				       (u8 *)&get_skey_out, sizeof(get_skey_out));
365 	if (byte < 0) {
366 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
367 		return byte;
368 	}
369 
370 	if (get_skey_out.header.status != FW_HDCP_STATUS_SUCCESS) {
371 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X failed. status: 0x%X\n",
372 			    WIRED_GET_SESSION_KEY, get_skey_out.header.status);
373 		return -EIO;
374 	}
375 
376 	ske_data->msg_id = HDCP_2_2_SKE_SEND_EKS;
377 	memcpy(ske_data->e_dkey_ks, get_skey_out.e_dkey_ks,
378 	       HDCP_2_2_E_DKEY_KS_LEN);
379 	memcpy(ske_data->riv, get_skey_out.r_iv, HDCP_2_2_RIV_LEN);
380 
381 	return 0;
382 }
383 
384 static int
385 gsc_hdcp_repeater_check_flow_prepare_ack(struct device *dev,
386 					 struct hdcp_port_data *data,
387 					 struct hdcp2_rep_send_receiverid_list
388 							*rep_topology,
389 					 struct hdcp2_rep_send_ack
390 							*rep_send_ack)
391 {
392 	struct wired_cmd_verify_repeater_in verify_repeater_in = { { 0 } };
393 	struct wired_cmd_verify_repeater_out verify_repeater_out = { { 0 } };
394 	struct drm_i915_private *i915;
395 	ssize_t byte;
396 
397 	if (!dev || !rep_topology || !rep_send_ack || !data)
398 		return -EINVAL;
399 
400 	i915 = kdev_to_i915(dev);
401 	if (!i915) {
402 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
403 		return -ENODEV;
404 	}
405 
406 	verify_repeater_in.header.api_version = HDCP_API_VERSION;
407 	verify_repeater_in.header.command_id = WIRED_VERIFY_REPEATER;
408 	verify_repeater_in.header.status = FW_HDCP_STATUS_SUCCESS;
409 	verify_repeater_in.header.buffer_len =
410 					WIRED_CMD_BUF_LEN_VERIFY_REPEATER_IN;
411 
412 	verify_repeater_in.port.integrated_port_type = data->port_type;
413 	verify_repeater_in.port.physical_port = (u8)data->hdcp_ddi;
414 	verify_repeater_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
415 
416 	memcpy(verify_repeater_in.rx_info, rep_topology->rx_info,
417 	       HDCP_2_2_RXINFO_LEN);
418 	memcpy(verify_repeater_in.seq_num_v, rep_topology->seq_num_v,
419 	       HDCP_2_2_SEQ_NUM_LEN);
420 	memcpy(verify_repeater_in.v_prime, rep_topology->v_prime,
421 	       HDCP_2_2_V_PRIME_HALF_LEN);
422 	memcpy(verify_repeater_in.receiver_ids, rep_topology->receiver_ids,
423 	       HDCP_2_2_RECEIVER_IDS_MAX_LEN);
424 
425 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&verify_repeater_in,
426 				       sizeof(verify_repeater_in),
427 				       (u8 *)&verify_repeater_out,
428 				       sizeof(verify_repeater_out));
429 	if (byte < 0) {
430 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
431 		return byte;
432 	}
433 
434 	if (verify_repeater_out.header.status != FW_HDCP_STATUS_SUCCESS) {
435 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X failed. status: 0x%X\n",
436 			    WIRED_VERIFY_REPEATER,
437 			    verify_repeater_out.header.status);
438 		return -EIO;
439 	}
440 
441 	memcpy(rep_send_ack->v, verify_repeater_out.v,
442 	       HDCP_2_2_V_PRIME_HALF_LEN);
443 	rep_send_ack->msg_id = HDCP_2_2_REP_SEND_ACK;
444 
445 	return 0;
446 }
447 
448 static int gsc_hdcp_verify_mprime(struct device *dev,
449 				  struct hdcp_port_data *data,
450 				  struct hdcp2_rep_stream_ready *stream_ready)
451 {
452 	struct wired_cmd_repeater_auth_stream_req_in *verify_mprime_in;
453 	struct wired_cmd_repeater_auth_stream_req_out
454 					verify_mprime_out = { { 0 } };
455 	struct drm_i915_private *i915;
456 	ssize_t byte;
457 	size_t cmd_size;
458 
459 	if (!dev || !stream_ready || !data)
460 		return -EINVAL;
461 
462 	i915 = kdev_to_i915(dev);
463 	if (!i915) {
464 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
465 		return -ENODEV;
466 	}
467 
468 	cmd_size = struct_size(verify_mprime_in, streams, data->k);
469 	if (cmd_size == SIZE_MAX)
470 		return -EINVAL;
471 
472 	verify_mprime_in = kzalloc(cmd_size, GFP_KERNEL);
473 	if (!verify_mprime_in)
474 		return -ENOMEM;
475 
476 	verify_mprime_in->header.api_version = HDCP_API_VERSION;
477 	verify_mprime_in->header.command_id = WIRED_REPEATER_AUTH_STREAM_REQ;
478 	verify_mprime_in->header.status = FW_HDCP_STATUS_SUCCESS;
479 	verify_mprime_in->header.buffer_len = cmd_size  - sizeof(verify_mprime_in->header);
480 
481 	verify_mprime_in->port.integrated_port_type = data->port_type;
482 	verify_mprime_in->port.physical_port = (u8)data->hdcp_ddi;
483 	verify_mprime_in->port.attached_transcoder = (u8)data->hdcp_transcoder;
484 
485 	memcpy(verify_mprime_in->m_prime, stream_ready->m_prime, HDCP_2_2_MPRIME_LEN);
486 	drm_hdcp_cpu_to_be24(verify_mprime_in->seq_num_m, data->seq_num_m);
487 
488 	memcpy(verify_mprime_in->streams, data->streams,
489 	       array_size(data->k, sizeof(*data->streams)));
490 
491 	verify_mprime_in->k = cpu_to_be16(data->k);
492 
493 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)verify_mprime_in, cmd_size,
494 				       (u8 *)&verify_mprime_out,
495 				       sizeof(verify_mprime_out));
496 	kfree(verify_mprime_in);
497 	if (byte < 0) {
498 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
499 		return byte;
500 	}
501 
502 	if (verify_mprime_out.header.status != FW_HDCP_STATUS_SUCCESS) {
503 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X failed. status: 0x%X\n",
504 			    WIRED_REPEATER_AUTH_STREAM_REQ,
505 			    verify_mprime_out.header.status);
506 		return -EIO;
507 	}
508 
509 	return 0;
510 }
511 
512 static int gsc_hdcp_enable_authentication(struct device *dev,
513 					  struct hdcp_port_data *data)
514 {
515 	struct wired_cmd_enable_auth_in enable_auth_in = { { 0 } };
516 	struct wired_cmd_enable_auth_out enable_auth_out = { { 0 } };
517 	struct drm_i915_private *i915;
518 	ssize_t byte;
519 
520 	if (!dev || !data)
521 		return -EINVAL;
522 
523 	i915 = kdev_to_i915(dev);
524 	if (!i915) {
525 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
526 		return -ENODEV;
527 	}
528 
529 	enable_auth_in.header.api_version = HDCP_API_VERSION;
530 	enable_auth_in.header.command_id = WIRED_ENABLE_AUTH;
531 	enable_auth_in.header.status = FW_HDCP_STATUS_SUCCESS;
532 	enable_auth_in.header.buffer_len = WIRED_CMD_BUF_LEN_ENABLE_AUTH_IN;
533 
534 	enable_auth_in.port.integrated_port_type = data->port_type;
535 	enable_auth_in.port.physical_port = (u8)data->hdcp_ddi;
536 	enable_auth_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
537 	enable_auth_in.stream_type = data->streams[0].stream_type;
538 
539 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&enable_auth_in,
540 				       sizeof(enable_auth_in),
541 				       (u8 *)&enable_auth_out,
542 				       sizeof(enable_auth_out));
543 	if (byte < 0) {
544 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
545 		return byte;
546 	}
547 
548 	if (enable_auth_out.header.status != FW_HDCP_STATUS_SUCCESS) {
549 		drm_dbg_kms(&i915->drm, "FW cmd 0x%08X failed. status: 0x%X\n",
550 			    WIRED_ENABLE_AUTH, enable_auth_out.header.status);
551 		return -EIO;
552 	}
553 
554 	return 0;
555 }
556 
557 static int
558 gsc_hdcp_close_session(struct device *dev, struct hdcp_port_data *data)
559 {
560 	struct wired_cmd_close_session_in session_close_in = { { 0 } };
561 	struct wired_cmd_close_session_out session_close_out = { { 0 } };
562 	struct drm_i915_private *i915;
563 	ssize_t byte;
564 
565 	if (!dev || !data)
566 		return -EINVAL;
567 
568 	i915 = kdev_to_i915(dev);
569 	if (!i915) {
570 		dev_err(dev, "DRM not initialized, aborting HDCP.\n");
571 		return -ENODEV;
572 	}
573 
574 	session_close_in.header.api_version = HDCP_API_VERSION;
575 	session_close_in.header.command_id = WIRED_CLOSE_SESSION;
576 	session_close_in.header.status = FW_HDCP_STATUS_SUCCESS;
577 	session_close_in.header.buffer_len =
578 				WIRED_CMD_BUF_LEN_CLOSE_SESSION_IN;
579 
580 	session_close_in.port.integrated_port_type = data->port_type;
581 	session_close_in.port.physical_port = (u8)data->hdcp_ddi;
582 	session_close_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
583 
584 	byte = intel_hdcp_gsc_msg_send(i915, (u8 *)&session_close_in,
585 				       sizeof(session_close_in),
586 				       (u8 *)&session_close_out,
587 				       sizeof(session_close_out));
588 	if (byte < 0) {
589 		drm_dbg_kms(&i915->drm, "intel_hdcp_gsc_msg_send failed. %zd\n", byte);
590 		return byte;
591 	}
592 
593 	if (session_close_out.header.status != FW_HDCP_STATUS_SUCCESS) {
594 		drm_dbg_kms(&i915->drm, "Session Close Failed. status: 0x%X\n",
595 			    session_close_out.header.status);
596 		return -EIO;
597 	}
598 
599 	return 0;
600 }
601 
602 static const struct i915_hdcp_ops gsc_hdcp_ops = {
603 	.initiate_hdcp2_session = gsc_hdcp_initiate_session,
604 	.verify_receiver_cert_prepare_km =
605 				gsc_hdcp_verify_receiver_cert_prepare_km,
606 	.verify_hprime = gsc_hdcp_verify_hprime,
607 	.store_pairing_info = gsc_hdcp_store_pairing_info,
608 	.initiate_locality_check = gsc_hdcp_initiate_locality_check,
609 	.verify_lprime = gsc_hdcp_verify_lprime,
610 	.get_session_key = gsc_hdcp_get_session_key,
611 	.repeater_check_flow_prepare_ack =
612 				gsc_hdcp_repeater_check_flow_prepare_ack,
613 	.verify_mprime = gsc_hdcp_verify_mprime,
614 	.enable_hdcp_authentication = gsc_hdcp_enable_authentication,
615 	.close_hdcp_session = gsc_hdcp_close_session,
616 };
617 
618 /*This function helps allocate memory for the command that we will send to gsc cs */
619 static int intel_hdcp_gsc_initialize_message(struct drm_i915_private *i915,
620 					     struct intel_hdcp_gsc_message *hdcp_message)
621 {
622 	struct intel_gt *gt = i915->media_gt;
623 	struct drm_i915_gem_object *obj = NULL;
624 	struct i915_vma *vma = NULL;
625 	void *cmd_in, *cmd_out;
626 	int err;
627 
628 	/* allocate object of two page for HDCP command memory and store it */
629 	obj = i915_gem_object_create_shmem(i915, 2 * PAGE_SIZE);
630 
631 	if (IS_ERR(obj)) {
632 		drm_err(&i915->drm, "Failed to allocate HDCP streaming command!\n");
633 		return PTR_ERR(obj);
634 	}
635 
636 	cmd_in = i915_gem_object_pin_map_unlocked(obj, intel_gt_coherent_map_type(gt, obj, true));
637 	if (IS_ERR(cmd_in)) {
638 		drm_err(&i915->drm, "Failed to map gsc message page!\n");
639 		err = PTR_ERR(cmd_in);
640 		goto out_unpin;
641 	}
642 
643 	cmd_out = cmd_in + PAGE_SIZE;
644 
645 	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
646 	if (IS_ERR(vma)) {
647 		err = PTR_ERR(vma);
648 		goto out_unmap;
649 	}
650 
651 	err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
652 	if (err)
653 		goto out_unmap;
654 
655 	memset(cmd_in, 0, obj->base.size);
656 
657 	hdcp_message->hdcp_cmd_in = cmd_in;
658 	hdcp_message->hdcp_cmd_out = cmd_out;
659 	hdcp_message->vma = vma;
660 
661 	return 0;
662 
663 out_unmap:
664 	i915_gem_object_unpin_map(obj);
665 out_unpin:
666 	i915_gem_object_put(obj);
667 	return err;
668 }
669 
670 static int intel_hdcp_gsc_hdcp2_init(struct drm_i915_private *i915)
671 {
672 	struct intel_hdcp_gsc_message *hdcp_message;
673 	int ret;
674 
675 	hdcp_message = kzalloc(sizeof(*hdcp_message), GFP_KERNEL);
676 
677 	if (!hdcp_message)
678 		return -ENOMEM;
679 
680 	/*
681 	 * NOTE: No need to lock the comp mutex here as it is already
682 	 * going to be taken before this function called
683 	 */
684 	i915->display.hdcp.hdcp_message = hdcp_message;
685 	ret = intel_hdcp_gsc_initialize_message(i915, hdcp_message);
686 
687 	if (ret)
688 		drm_err(&i915->drm, "Could not initialize hdcp_message\n");
689 
690 	return ret;
691 }
692 
693 static void intel_hdcp_gsc_free_message(struct drm_i915_private *i915)
694 {
695 	struct intel_hdcp_gsc_message *hdcp_message =
696 					i915->display.hdcp.hdcp_message;
697 
698 	hdcp_message->hdcp_cmd_in = NULL;
699 	hdcp_message->hdcp_cmd_out = NULL;
700 	i915_vma_unpin_and_release(&hdcp_message->vma, I915_VMA_RELEASE_MAP);
701 	kfree(hdcp_message);
702 }
703 
704 int intel_hdcp_gsc_init(struct drm_i915_private *i915)
705 {
706 	struct i915_hdcp_arbiter *data;
707 	int ret;
708 
709 	data = kzalloc(sizeof(struct i915_hdcp_arbiter), GFP_KERNEL);
710 	if (!data)
711 		return -ENOMEM;
712 
713 	mutex_lock(&i915->display.hdcp.hdcp_mutex);
714 	i915->display.hdcp.arbiter = data;
715 	i915->display.hdcp.arbiter->hdcp_dev = i915->drm.dev;
716 	i915->display.hdcp.arbiter->ops = &gsc_hdcp_ops;
717 	ret = intel_hdcp_gsc_hdcp2_init(i915);
718 	mutex_unlock(&i915->display.hdcp.hdcp_mutex);
719 
720 	return ret;
721 }
722 
723 void intel_hdcp_gsc_fini(struct drm_i915_private *i915)
724 {
725 	intel_hdcp_gsc_free_message(i915);
726 	kfree(i915->display.hdcp.arbiter);
727 }
728 
729 static int intel_gsc_send_sync(struct drm_i915_private *i915,
730 			       struct intel_gsc_mtl_header *header_in,
731 			       struct intel_gsc_mtl_header *header_out,
732 			       u64 addr_in, u64 addr_out,
733 			       size_t msg_out_len)
734 {
735 	struct intel_gt *gt = i915->media_gt;
736 	int ret;
737 
738 	ret = intel_gsc_uc_heci_cmd_submit_packet(&gt->uc.gsc, addr_in,
739 						  header_in->message_size,
740 						  addr_out,
741 						  msg_out_len + sizeof(*header_out));
742 	if (ret) {
743 		drm_err(&i915->drm, "failed to send gsc HDCP msg (%d)\n", ret);
744 		return ret;
745 	}
746 
747 	/*
748 	 * Checking validity marker and header status to see if some error has
749 	 * blocked us from sending message to gsc cs
750 	 */
751 	if (header_out->validity_marker != GSC_HECI_VALIDITY_MARKER) {
752 		drm_err(&i915->drm, "invalid validity marker\n");
753 		return -EINVAL;
754 	}
755 
756 	if (header_out->status != 0) {
757 		drm_err(&i915->drm, "header status indicates error %d\n",
758 			header_out->status);
759 		return -EINVAL;
760 	}
761 
762 	if (header_out->flags & GSC_OUTFLAG_MSG_PENDING) {
763 		header_in->gsc_message_handle = header_out->gsc_message_handle;
764 		return -EAGAIN;
765 	}
766 
767 	return 0;
768 }
769 
770 /*
771  * This function can now be used for sending requests and will also handle
772  * receipt of reply messages hence no different function of message retrieval
773  * is required. We will initialize intel_hdcp_gsc_message structure then add
774  * gsc cs memory header as stated in specs after which the normal HDCP payload
775  * will follow
776  */
777 ssize_t intel_hdcp_gsc_msg_send(struct drm_i915_private *i915, u8 *msg_in,
778 				size_t msg_in_len, u8 *msg_out,
779 				size_t msg_out_len)
780 {
781 	struct intel_gt *gt = i915->media_gt;
782 	struct intel_gsc_mtl_header *header_in, *header_out;
783 	const size_t max_msg_size = PAGE_SIZE - sizeof(*header_in);
784 	struct intel_hdcp_gsc_message *hdcp_message;
785 	u64 addr_in, addr_out, host_session_id;
786 	u32 reply_size, msg_size_in, msg_size_out;
787 	int ret, tries = 0;
788 
789 	if (!intel_uc_uses_gsc_uc(&gt->uc))
790 		return -ENODEV;
791 
792 	if (msg_in_len > max_msg_size || msg_out_len > max_msg_size)
793 		return -ENOSPC;
794 
795 	msg_size_in = msg_in_len + sizeof(*header_in);
796 	msg_size_out = msg_out_len + sizeof(*header_out);
797 	hdcp_message = i915->display.hdcp.hdcp_message;
798 	header_in = hdcp_message->hdcp_cmd_in;
799 	header_out = hdcp_message->hdcp_cmd_out;
800 	addr_in = i915_ggtt_offset(hdcp_message->vma);
801 	addr_out = addr_in + PAGE_SIZE;
802 
803 	memset(header_in, 0, msg_size_in);
804 	memset(header_out, 0, msg_size_out);
805 	get_random_bytes(&host_session_id, sizeof(u64));
806 	intel_gsc_uc_heci_cmd_emit_mtl_header(header_in, HECI_MEADDRESS_HDCP,
807 					      msg_size_in, host_session_id);
808 	memcpy(hdcp_message->hdcp_cmd_in + sizeof(*header_in), msg_in, msg_in_len);
809 
810 	/*
811 	 * Keep sending request in case the pending bit is set no need to add
812 	 * message handle as we are using same address hence loc. of header is
813 	 * same and it will contain the message handle. we will send the message
814 	 * 20 times each message 50 ms apart
815 	 */
816 	do {
817 		ret = intel_gsc_send_sync(i915, header_in, header_out, addr_in,
818 					  addr_out, msg_out_len);
819 
820 		/* Only try again if gsc says so */
821 		if (ret != -EAGAIN)
822 			break;
823 
824 		msleep(50);
825 
826 	} while (++tries < 20);
827 
828 	if (ret)
829 		goto err;
830 
831 	/* we use the same mem for the reply, so header is in the same loc */
832 	reply_size = header_out->message_size - sizeof(*header_out);
833 	if (reply_size > msg_out_len) {
834 		drm_warn(&i915->drm, "caller with insufficient HDCP reply size %u (%d)\n",
835 			 reply_size, (u32)msg_out_len);
836 		reply_size = msg_out_len;
837 	} else if (reply_size != msg_out_len) {
838 		drm_dbg_kms(&i915->drm, "caller unexpected HCDP reply size %u (%d)\n",
839 			    reply_size, (u32)msg_out_len);
840 	}
841 
842 	memcpy(msg_out, hdcp_message->hdcp_cmd_out + sizeof(*header_out), msg_out_len);
843 
844 err:
845 	return ret;
846 }
847