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