xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_hdcp.c (revision 5c9b8b27a883822fe5812bed23bca7ffeb4301f7)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2019 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 #include "amdgpu_dm_hdcp.h"
28 #include "amdgpu.h"
29 #include "amdgpu_dm.h"
30 #include "dc_fused_io.h"
31 #include "dm_helpers.h"
32 #include <drm/display/drm_hdcp_helper.h>
33 #include "hdcp_psp.h"
34 #include "amdgpu_dm_kunit_helpers.h"
35 
36 /*
37  * If the SRM version being loaded is less than or equal to the
38  * currently loaded SRM, psp will return 0xFFFF as the version
39  */
40 #define PSP_SRM_VERSION_MAX 0xFFFF
41 
42 static bool
43 lp_write_i2c(void *handle, uint32_t address, const uint8_t *data, uint32_t size)
44 {
45 	struct dc_link *link = handle;
46 	struct i2c_payload i2c_payloads[] = {{true, address, size, (void *)data} };
47 	struct i2c_command cmd = {i2c_payloads, 1, I2C_COMMAND_ENGINE_HW,
48 				  link->dc->caps.i2c_speed_in_khz};
49 
50 	return dm_helpers_submit_i2c(link->ctx, link, &cmd);
51 }
52 
53 static bool
54 lp_read_i2c(void *handle, uint32_t address, uint8_t offset, uint8_t *data, uint32_t size)
55 {
56 	struct dc_link *link = handle;
57 
58 	struct i2c_payload i2c_payloads[] = {{true, address, 1, &offset},
59 					     {false, address, size, data} };
60 	struct i2c_command cmd = {i2c_payloads, 2, I2C_COMMAND_ENGINE_HW,
61 				  link->dc->caps.i2c_speed_in_khz};
62 
63 	return dm_helpers_submit_i2c(link->ctx, link, &cmd);
64 }
65 
66 static bool
67 lp_write_dpcd(void *handle, uint32_t address, const uint8_t *data, uint32_t size)
68 {
69 	struct dc_link *link = handle;
70 
71 	return dm_helpers_dp_write_dpcd(link->ctx, link, address, data, size);
72 }
73 
74 static bool
75 lp_read_dpcd(void *handle, uint32_t address, uint8_t *data, uint32_t size)
76 {
77 	struct dc_link *link = handle;
78 
79 	return dm_helpers_dp_read_dpcd(link->ctx, link, address, data, size);
80 }
81 
82 static bool lp_atomic_write_poll_read_i2c(
83 		void *handle,
84 		const struct mod_hdcp_atomic_op_i2c *write,
85 		const struct mod_hdcp_atomic_op_i2c *poll,
86 		struct mod_hdcp_atomic_op_i2c *read,
87 		uint32_t poll_timeout_us,
88 		uint8_t poll_mask_msb
89 )
90 {
91 	struct dc_link *link = handle;
92 
93 	return dm_atomic_write_poll_read_i2c(link, write, poll, read, poll_timeout_us, poll_mask_msb);
94 }
95 
96 static bool lp_atomic_write_poll_read_aux(
97 		void *handle,
98 		const struct mod_hdcp_atomic_op_aux *write,
99 		const struct mod_hdcp_atomic_op_aux *poll,
100 		struct mod_hdcp_atomic_op_aux *read,
101 		uint32_t poll_timeout_us,
102 		uint8_t poll_mask_msb
103 )
104 {
105 	struct dc_link *link = handle;
106 
107 	return dm_atomic_write_poll_read_aux(link, write, poll, read, poll_timeout_us, poll_mask_msb);
108 }
109 
110 static uint8_t *psp_get_srm(struct psp_context *psp, uint32_t *srm_version, uint32_t *srm_size)
111 {
112 	struct ta_hdcp_shared_memory *hdcp_cmd;
113 
114 	if (!psp->hdcp_context.context.initialized) {
115 		DRM_WARN("Failed to get hdcp srm. HDCP TA is not initialized.");
116 		return NULL;
117 	}
118 
119 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
120 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
121 
122 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP_GET_SRM;
123 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
124 
125 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
126 		return NULL;
127 
128 	*srm_version = hdcp_cmd->out_msg.hdcp_get_srm.srm_version;
129 	*srm_size = hdcp_cmd->out_msg.hdcp_get_srm.srm_buf_size;
130 
131 	return hdcp_cmd->out_msg.hdcp_get_srm.srm_buf;
132 }
133 
134 static int psp_set_srm(struct psp_context *psp,
135 		       u8 *srm, uint32_t srm_size, uint32_t *srm_version)
136 {
137 	struct ta_hdcp_shared_memory *hdcp_cmd;
138 
139 	if (!psp->hdcp_context.context.initialized) {
140 		DRM_WARN("Failed to get hdcp srm. HDCP TA is not initialized.");
141 		return -EINVAL;
142 	}
143 
144 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
145 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
146 
147 	memcpy(hdcp_cmd->in_msg.hdcp_set_srm.srm_buf, srm, srm_size);
148 	hdcp_cmd->in_msg.hdcp_set_srm.srm_buf_size = srm_size;
149 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP_SET_SRM;
150 
151 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
152 
153 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS ||
154 	    hdcp_cmd->out_msg.hdcp_set_srm.valid_signature != 1 ||
155 	    hdcp_cmd->out_msg.hdcp_set_srm.srm_version == PSP_SRM_VERSION_MAX)
156 		return -EINVAL;
157 
158 	*srm_version = hdcp_cmd->out_msg.hdcp_set_srm.srm_version;
159 	return 0;
160 }
161 
162 STATIC_IFN_KUNIT
163 void process_output(struct hdcp_workqueue *hdcp_work)
164 {
165 	struct mod_hdcp_output output = hdcp_work->output;
166 
167 	if (output.callback_stop)
168 		cancel_delayed_work(&hdcp_work->callback_dwork);
169 
170 	if (output.callback_needed)
171 		schedule_delayed_work(&hdcp_work->callback_dwork,
172 				      msecs_to_jiffies(output.callback_delay));
173 
174 	if (output.watchdog_timer_stop)
175 		cancel_delayed_work(&hdcp_work->watchdog_timer_dwork);
176 
177 	if (output.watchdog_timer_needed)
178 		schedule_delayed_work(&hdcp_work->watchdog_timer_dwork,
179 				      msecs_to_jiffies(output.watchdog_timer_delay));
180 
181 	schedule_delayed_work(&hdcp_work->property_validate_dwork, msecs_to_jiffies(0));
182 }
183 EXPORT_IF_KUNIT(process_output);
184 
185 static void link_lock(struct hdcp_workqueue *work, bool lock)
186 {
187 	int i = 0;
188 
189 	for (i = 0; i < work->max_link; i++) {
190 		if (lock)
191 			mutex_lock(&work[i].mutex);
192 		else
193 			mutex_unlock(&work[i].mutex);
194 	}
195 }
196 
197 void hdcp_update_display(struct hdcp_workqueue *hdcp_work,
198 			 unsigned int link_index,
199 			 struct amdgpu_dm_connector *aconnector,
200 			 u8 content_type,
201 			 bool enable_encryption)
202 {
203 	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];
204 	struct mod_hdcp_link_adjustment link_adjust;
205 	struct mod_hdcp_display_adjustment display_adjust;
206 	unsigned int conn_index = aconnector->base.index;
207 	const struct dc *dc = aconnector->dc_link->dc;
208 
209 	guard(mutex)(&hdcp_w->mutex);
210 	drm_connector_get(&aconnector->base);
211 	if (hdcp_w->aconnector[conn_index])
212 		drm_connector_put(&hdcp_w->aconnector[conn_index]->base);
213 	hdcp_w->aconnector[conn_index] = aconnector;
214 
215 	memset(&link_adjust, 0, sizeof(link_adjust));
216 	memset(&display_adjust, 0, sizeof(display_adjust));
217 
218 	if (enable_encryption) {
219 		/* Explicitly set the saved SRM as sysfs call will be after we already enabled hdcp
220 		 * (s3 resume case)
221 		 */
222 		if (hdcp_work->srm_size > 0)
223 			psp_set_srm(hdcp_work->hdcp.config.psp.handle, hdcp_work->srm,
224 				    hdcp_work->srm_size,
225 				    &hdcp_work->srm_version);
226 
227 		display_adjust.disable = MOD_HDCP_DISPLAY_NOT_DISABLE;
228 
229 		link_adjust.auth_delay = 2;
230 		link_adjust.retry_limit = MAX_NUM_OF_ATTEMPTS;
231 
232 		if (content_type == DRM_MODE_HDCP_CONTENT_TYPE0) {
233 			link_adjust.hdcp2.force_type = MOD_HDCP_FORCE_TYPE_0;
234 		} else if (content_type == DRM_MODE_HDCP_CONTENT_TYPE1) {
235 			link_adjust.hdcp1.disable = 1;
236 			link_adjust.hdcp2.force_type = MOD_HDCP_FORCE_TYPE_1;
237 		}
238 		link_adjust.hdcp2.use_fw_locality_check =
239 				(dc->caps.fused_io_supported || dc->debug.hdcp_lc_force_fw_enable);
240 		link_adjust.hdcp2.use_sw_locality_fallback = dc->debug.hdcp_lc_enable_sw_fallback;
241 
242 		schedule_delayed_work(&hdcp_w->property_validate_dwork,
243 				      msecs_to_jiffies(DRM_HDCP_CHECK_PERIOD_MS));
244 	} else {
245 		display_adjust.disable = MOD_HDCP_DISPLAY_DISABLE_AUTHENTICATION;
246 		hdcp_w->encryption_status[conn_index] = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
247 		cancel_delayed_work(&hdcp_w->property_validate_dwork);
248 	}
249 
250 	mod_hdcp_update_display(&hdcp_w->hdcp, conn_index, &link_adjust, &display_adjust, &hdcp_w->output);
251 
252 	process_output(hdcp_w);
253 }
254 
255 static void hdcp_remove_display(struct hdcp_workqueue *hdcp_work,
256 				unsigned int link_index,
257 			 struct amdgpu_dm_connector *aconnector)
258 {
259 	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];
260 	struct drm_connector_state *conn_state = aconnector->base.state;
261 	unsigned int conn_index = aconnector->base.index;
262 
263 	guard(mutex)(&hdcp_w->mutex);
264 
265 	/* the removal of display will invoke auth reset -> hdcp destroy and
266 	 * we'd expect the Content Protection (CP) property changed back to
267 	 * DESIRED if at the time ENABLED. CP property change should occur
268 	 * before the element removed from linked list.
269 	 */
270 	if (conn_state && conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
271 		conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
272 
273 		DRM_DEBUG_DRIVER("[HDCP_DM] display %d, CP 2 -> 1, type %u, DPMS %u\n",
274 				 aconnector->base.index, conn_state->hdcp_content_type,
275 				 aconnector->base.dpms);
276 	}
277 
278 	mod_hdcp_remove_display(&hdcp_w->hdcp, aconnector->base.index, &hdcp_w->output);
279 	if (hdcp_w->aconnector[conn_index]) {
280 		drm_connector_put(&hdcp_w->aconnector[conn_index]->base);
281 		hdcp_w->aconnector[conn_index] = NULL;
282 	}
283 	process_output(hdcp_w);
284 }
285 
286 void hdcp_reset_display(struct hdcp_workqueue *hdcp_work, unsigned int link_index)
287 {
288 	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];
289 	unsigned int conn_index;
290 
291 	guard(mutex)(&hdcp_w->mutex);
292 
293 	mod_hdcp_reset_connection(&hdcp_w->hdcp,  &hdcp_w->output);
294 
295 	cancel_delayed_work(&hdcp_w->property_validate_dwork);
296 
297 	for (conn_index = 0; conn_index < AMDGPU_DM_MAX_DISPLAY_INDEX; conn_index++) {
298 		hdcp_w->encryption_status[conn_index] =
299 			MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
300 		if (hdcp_w->aconnector[conn_index]) {
301 			drm_connector_put(&hdcp_w->aconnector[conn_index]->base);
302 			hdcp_w->aconnector[conn_index] = NULL;
303 		}
304 	}
305 
306 	process_output(hdcp_w);
307 }
308 
309 void hdcp_handle_cpirq(struct hdcp_workqueue *hdcp_work, unsigned int link_index)
310 {
311 	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];
312 
313 	schedule_work(&hdcp_w->cpirq_work);
314 }
315 
316 static void event_callback(struct work_struct *work)
317 {
318 	struct hdcp_workqueue *hdcp_work;
319 
320 	hdcp_work = container_of(to_delayed_work(work), struct hdcp_workqueue,
321 				 callback_dwork);
322 
323 	guard(mutex)(&hdcp_work->mutex);
324 
325 	cancel_delayed_work(&hdcp_work->callback_dwork);
326 
327 	mod_hdcp_process_event(&hdcp_work->hdcp, MOD_HDCP_EVENT_CALLBACK,
328 			       &hdcp_work->output);
329 
330 	process_output(hdcp_work);
331 }
332 
333 static void event_property_update(struct work_struct *work)
334 {
335 	struct hdcp_workqueue *hdcp_work = container_of(work, struct hdcp_workqueue,
336 							property_update_work);
337 	struct amdgpu_dm_connector *aconnector = NULL;
338 	struct drm_device *dev;
339 	long ret;
340 	unsigned int conn_index;
341 	struct drm_connector *connector;
342 	struct drm_connector_state *conn_state;
343 
344 	for (conn_index = 0; conn_index < AMDGPU_DM_MAX_DISPLAY_INDEX; conn_index++) {
345 		aconnector = hdcp_work->aconnector[conn_index];
346 
347 		if (!aconnector)
348 			continue;
349 
350 		connector = &aconnector->base;
351 
352 		/* check if display connected */
353 		if (connector->status != connector_status_connected)
354 			continue;
355 
356 		conn_state = aconnector->base.state;
357 
358 		if (!conn_state)
359 			continue;
360 
361 		dev = connector->dev;
362 
363 		if (!dev)
364 			continue;
365 
366 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
367 		guard(mutex)(&hdcp_work->mutex);
368 
369 		if (conn_state->commit) {
370 			ret = wait_for_completion_interruptible_timeout(&conn_state->commit->hw_done,
371 									10 * HZ);
372 			if (ret == 0) {
373 				DRM_ERROR("HDCP state unknown! Setting it to DESIRED\n");
374 				hdcp_work->encryption_status[conn_index] =
375 					MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
376 			}
377 		}
378 		if (hdcp_work->encryption_status[conn_index] !=
379 			MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF) {
380 			if (conn_state->hdcp_content_type ==
381 				DRM_MODE_HDCP_CONTENT_TYPE0 &&
382 				hdcp_work->encryption_status[conn_index] <=
383 				MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE0_ON) {
384 				DRM_DEBUG_DRIVER("[HDCP_DM] DRM_MODE_CONTENT_PROTECTION_ENABLED\n");
385 				drm_hdcp_update_content_protection(connector,
386 								   DRM_MODE_CONTENT_PROTECTION_ENABLED);
387 			} else if (conn_state->hdcp_content_type ==
388 					DRM_MODE_HDCP_CONTENT_TYPE1 &&
389 					hdcp_work->encryption_status[conn_index] ==
390 					MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE1_ON) {
391 				drm_hdcp_update_content_protection(connector,
392 								   DRM_MODE_CONTENT_PROTECTION_ENABLED);
393 			}
394 		} else {
395 			DRM_DEBUG_DRIVER("[HDCP_DM] DRM_MODE_CONTENT_PROTECTION_DESIRED\n");
396 			drm_hdcp_update_content_protection(connector,
397 							   DRM_MODE_CONTENT_PROTECTION_DESIRED);
398 		}
399 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
400 	}
401 }
402 
403 static void event_property_validate(struct work_struct *work)
404 {
405 	struct hdcp_workqueue *hdcp_work =
406 		container_of(to_delayed_work(work), struct hdcp_workqueue, property_validate_dwork);
407 	struct mod_hdcp_display_query query;
408 	struct amdgpu_dm_connector *aconnector;
409 	unsigned int conn_index;
410 
411 	guard(mutex)(&hdcp_work->mutex);
412 
413 	for (conn_index = 0; conn_index < AMDGPU_DM_MAX_DISPLAY_INDEX;
414 	     conn_index++) {
415 		aconnector = hdcp_work->aconnector[conn_index];
416 
417 		if (!aconnector)
418 			continue;
419 
420 		/* check if display connected */
421 		if (aconnector->base.status != connector_status_connected)
422 			continue;
423 
424 		if (!aconnector->base.state)
425 			continue;
426 
427 		query.encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
428 		mod_hdcp_query_display(&hdcp_work->hdcp, aconnector->base.index,
429 				       &query);
430 
431 		DRM_DEBUG_DRIVER("[HDCP_DM] disp %d, connector->CP %u, (query, work): (%d, %d)\n",
432 				 aconnector->base.index,
433 			aconnector->base.state->content_protection,
434 			query.encryption_status,
435 			hdcp_work->encryption_status[conn_index]);
436 
437 		if (query.encryption_status !=
438 		    hdcp_work->encryption_status[conn_index]) {
439 			DRM_DEBUG_DRIVER("[HDCP_DM] encryption_status change from %x to %x\n",
440 					 hdcp_work->encryption_status[conn_index],
441 					 query.encryption_status);
442 
443 			hdcp_work->encryption_status[conn_index] =
444 				query.encryption_status;
445 
446 			DRM_DEBUG_DRIVER("[HDCP_DM] trigger property_update_work\n");
447 
448 			schedule_work(&hdcp_work->property_update_work);
449 		}
450 	}
451 }
452 
453 static void event_watchdog_timer(struct work_struct *work)
454 {
455 	struct hdcp_workqueue *hdcp_work;
456 
457 	hdcp_work = container_of(to_delayed_work(work),
458 				 struct hdcp_workqueue,
459 				      watchdog_timer_dwork);
460 
461 	guard(mutex)(&hdcp_work->mutex);
462 
463 	cancel_delayed_work(&hdcp_work->watchdog_timer_dwork);
464 
465 	mod_hdcp_process_event(&hdcp_work->hdcp,
466 			       MOD_HDCP_EVENT_WATCHDOG_TIMEOUT,
467 			       &hdcp_work->output);
468 
469 	process_output(hdcp_work);
470 }
471 
472 static void event_cpirq(struct work_struct *work)
473 {
474 	struct hdcp_workqueue *hdcp_work;
475 
476 	hdcp_work = container_of(work, struct hdcp_workqueue, cpirq_work);
477 
478 	guard(mutex)(&hdcp_work->mutex);
479 
480 	mod_hdcp_process_event(&hdcp_work->hdcp, MOD_HDCP_EVENT_CPIRQ, &hdcp_work->output);
481 
482 	process_output(hdcp_work);
483 }
484 
485 void hdcp_destroy(struct kobject *kobj, struct hdcp_workqueue *hdcp_work)
486 {
487 	int i = 0;
488 
489 	for (i = 0; i < hdcp_work->max_link; i++) {
490 		cancel_delayed_work_sync(&hdcp_work[i].callback_dwork);
491 		cancel_delayed_work_sync(&hdcp_work[i].watchdog_timer_dwork);
492 		cancel_delayed_work_sync(&hdcp_work[i].property_validate_dwork);
493 	}
494 
495 	sysfs_remove_bin_file(kobj, &hdcp_work[0].attr);
496 	kfree(hdcp_work->srm);
497 	kfree(hdcp_work->srm_temp);
498 	kfree(hdcp_work);
499 }
500 
501 static bool enable_assr(void *handle, struct dc_link *link)
502 {
503 	struct hdcp_workqueue *hdcp_work = handle;
504 	struct mod_hdcp hdcp = hdcp_work->hdcp;
505 	struct psp_context *psp = hdcp.config.psp.handle;
506 	struct ta_dtm_shared_memory *dtm_cmd;
507 
508 	if (!psp->dtm_context.context.initialized) {
509 		drm_info(adev_to_drm(psp->adev),
510 			 "Failed to enable ASSR, DTM TA is not initialized.");
511 		return false;
512 	}
513 
514 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
515 
516 	guard(mutex)(&psp->dtm_context.mutex);
517 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
518 
519 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_ASSR_ENABLE;
520 	dtm_cmd->dtm_in_message.topology_assr_enable.display_topology_dig_be_index =
521 		link->link_enc_hw_inst;
522 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
523 
524 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
525 
526 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
527 		drm_info(adev_to_drm(psp->adev),
528 			 "Failed to enable ASSR");
529 		return false;
530 	}
531 
532 	return true;
533 }
534 
535 static void update_config(void *handle, struct cp_psp_stream_config *config)
536 {
537 	struct hdcp_workqueue *hdcp_work = handle;
538 	struct amdgpu_dm_connector *aconnector = config->dm_stream_ctx;
539 	int link_index = aconnector->dc_link->link_index;
540 	unsigned int conn_index = aconnector->base.index;
541 	struct mod_hdcp_display *display = &hdcp_work[link_index].display;
542 	struct mod_hdcp_link *link = &hdcp_work[link_index].link;
543 	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];
544 	struct dc_sink *sink = NULL;
545 	bool link_is_hdcp14 = false;
546 	const struct dc *dc = aconnector->dc_link->dc;
547 
548 	if (config->dpms_off) {
549 		hdcp_remove_display(hdcp_work, link_index, aconnector);
550 		return;
551 	}
552 
553 	memset(display, 0, sizeof(*display));
554 	memset(link, 0, sizeof(*link));
555 
556 	display->index = aconnector->base.index;
557 	display->state = MOD_HDCP_DISPLAY_ACTIVE;
558 
559 	if (aconnector->dc_sink)
560 		sink = aconnector->dc_sink;
561 	else if (aconnector->dc_em_sink)
562 		sink = aconnector->dc_em_sink;
563 
564 	if (sink)
565 		link->mode = mod_hdcp_signal_type_to_operation_mode(sink->sink_signal);
566 
567 	display->controller = CONTROLLER_ID_D0 + config->otg_inst;
568 	display->dig_fe = config->dig_fe;
569 	link->dig_be = config->dig_be;
570 	link->ddc_line = aconnector->dc_link->ddc_hw_inst + 1;
571 	display->stream_enc_idx = config->stream_enc_idx;
572 	link->link_enc_idx = config->link_enc_idx;
573 	link->dio_output_id = config->dio_output_idx;
574 	link->phy_idx = config->phy_idx;
575 
576 	if (sink)
577 		link_is_hdcp14 = dc_link_is_hdcp14(aconnector->dc_link, sink->sink_signal);
578 	link->hdcp_supported_informational = link_is_hdcp14;
579 	link->dp.rev = aconnector->dc_link->dpcd_caps.dpcd_rev.raw;
580 	link->dp.assr_enabled = config->assr_enabled;
581 	link->dp.mst_enabled = config->mst_enabled;
582 	link->dp.dp2_enabled = config->dp2_enabled;
583 	link->dp.usb4_enabled = config->usb4_enabled;
584 	if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_HDMI_FRL)
585 		link->hdmi.frl_enabled = config->frl_enabled;
586 	display->adjust.disable = MOD_HDCP_DISPLAY_DISABLE_AUTHENTICATION;
587 	link->adjust.auth_delay = 2;
588 	link->adjust.retry_limit = MAX_NUM_OF_ATTEMPTS;
589 	link->adjust.hdcp1.disable = 0;
590 	link->adjust.hdcp2.use_fw_locality_check = (dc->caps.fused_io_supported || dc->debug.hdcp_lc_force_fw_enable);
591 	link->adjust.hdcp2.use_sw_locality_fallback = dc->debug.hdcp_lc_enable_sw_fallback;
592 	hdcp_w->encryption_status[display->index] = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
593 
594 	DRM_DEBUG_DRIVER("[HDCP_DM] display %d, CP %d, type %d\n", aconnector->base.index,
595 			 (!!aconnector->base.state) ?
596 			 aconnector->base.state->content_protection : -1,
597 			 (!!aconnector->base.state) ?
598 			 aconnector->base.state->hdcp_content_type : -1);
599 
600 	guard(mutex)(&hdcp_w->mutex);
601 
602 	mod_hdcp_add_display(&hdcp_w->hdcp, link, display, &hdcp_w->output);
603 	drm_connector_get(&aconnector->base);
604 	if (hdcp_w->aconnector[conn_index])
605 		drm_connector_put(&hdcp_w->aconnector[conn_index]->base);
606 	hdcp_w->aconnector[conn_index] = aconnector;
607 	process_output(hdcp_w);
608 }
609 
610 /**
611  * DOC: Add sysfs interface for set/get srm
612  *
613  * NOTE: From the usermodes prospective you only need to call write *ONCE*, the kernel
614  *      will automatically call once or twice depending on the size
615  *
616  * call: "cat file > /sys/class/drm/card0/device/hdcp_srm" from usermode no matter what the size is
617  *
618  * The kernel can only send PAGE_SIZE at once and since MAX_SRM_FILE(5120) > PAGE_SIZE(4096),
619  * srm_data_write can be called multiple times.
620  *
621  * sysfs interface doesn't tell us the size we will get so we are sending partial SRMs to psp and on
622  * the last call we will send the full SRM. PSP will fail on every call before the last.
623  *
624  * This means we don't know if the SRM is good until the last call. And because of this
625  * limitation we cannot throw errors early as it will stop the kernel from writing to sysfs
626  *
627  * Example 1:
628  *	Good SRM size = 5096
629  *	first call to write 4096 -> PSP fails
630  *	Second call to write 1000 -> PSP Pass -> SRM is set
631  *
632  * Example 2:
633  *	Bad SRM size = 4096
634  *	first call to write 4096 -> PSP fails (This is the same as above, but we don't know if this
635  *	is the last call)
636  *
637  * Solution?:
638  *	1: Parse the SRM? -> It is signed so we don't know the EOF
639  *	2: We can have another sysfs that passes the size before calling set. -> simpler solution
640  *	below
641  *
642  * Easy Solution:
643  * Always call get after Set to verify if set was successful.
644  * +----------------------+
645  * |   Why it works:      |
646  * +----------------------+
647  * PSP will only update its srm if its older than the one we are trying to load.
648  * Always do set first than get.
649  *	-if we try to "1. SET" a older version PSP will reject it and we can "2. GET" the newer
650  *	version and save it
651  *
652  *	-if we try to "1. SET" a newer version PSP will accept it and we can "2. GET" the
653  *	same(newer) version back and save it
654  *
655  *	-if we try to "1. SET" a newer version and PSP rejects it. That means the format is
656  *	incorrect/corrupted and we should correct our SRM by getting it from PSP
657  */
658 static ssize_t srm_data_write(struct file *filp, struct kobject *kobj,
659 			      const struct bin_attribute *bin_attr, char *buffer,
660 			      loff_t pos, size_t count)
661 {
662 	struct hdcp_workqueue *work;
663 	u32 srm_version = 0;
664 
665 	work = container_of(bin_attr, struct hdcp_workqueue, attr);
666 	link_lock(work, true);
667 
668 	memcpy(work->srm_temp + pos, buffer, count);
669 
670 	if (!psp_set_srm(work->hdcp.config.psp.handle, work->srm_temp, pos + count, &srm_version)) {
671 		DRM_DEBUG_DRIVER("HDCP SRM SET version 0x%X", srm_version);
672 		memcpy(work->srm, work->srm_temp, pos + count);
673 		work->srm_size = pos + count;
674 		work->srm_version = srm_version;
675 	}
676 
677 	link_lock(work, false);
678 
679 	return count;
680 }
681 
682 static ssize_t srm_data_read(struct file *filp, struct kobject *kobj,
683 			     const struct bin_attribute *bin_attr, char *buffer,
684 			     loff_t pos, size_t count)
685 {
686 	struct hdcp_workqueue *work;
687 	u8 *srm = NULL;
688 	u32 srm_version;
689 	u32 srm_size;
690 	size_t ret = count;
691 
692 	work = container_of(bin_attr, struct hdcp_workqueue, attr);
693 
694 	link_lock(work, true);
695 
696 	srm = psp_get_srm(work->hdcp.config.psp.handle, &srm_version, &srm_size);
697 
698 	if (!srm) {
699 		ret = -EINVAL;
700 		goto ret;
701 	}
702 
703 	if (pos >= srm_size)
704 		ret = 0;
705 
706 	if (srm_size - pos < count) {
707 		memcpy(buffer, srm + pos, srm_size - pos);
708 		ret = srm_size - pos;
709 		goto ret;
710 	}
711 
712 	memcpy(buffer, srm + pos, count);
713 
714 ret:
715 	link_lock(work, false);
716 	return ret;
717 }
718 
719 /* From the hdcp spec (5.Renewability) SRM needs to be stored in a non-volatile memory.
720  *
721  * For example,
722  *	if Application "A" sets the SRM (ver 2) and we reboot/suspend and later when Application "B"
723  *	needs to use HDCP, the version in PSP should be SRM(ver 2). So SRM should be persistent
724  *	across boot/reboots/suspend/resume/shutdown
725  *
726  * Currently when the system goes down (suspend/shutdown) the SRM is cleared from PSP. For HDCP
727  * we need to make the SRM persistent.
728  *
729  * -PSP owns the checking of SRM but doesn't have the ability to store it in a non-volatile memory.
730  * -The kernel cannot write to the file systems.
731  * -So we need usermode to do this for us, which is why an interface for usermode is needed
732  *
733  *
734  *
735  * Usermode can read/write to/from PSP using the sysfs interface
736  * For example:
737  *	to save SRM from PSP to storage : cat /sys/class/drm/card0/device/hdcp_srm > srmfile
738  *	to load from storage to PSP: cat srmfile > /sys/class/drm/card0/device/hdcp_srm
739  */
740 static const struct bin_attribute data_attr = {
741 	.attr = {.name = "hdcp_srm", .mode = 0664},
742 	.size = PSP_HDCP_SRM_FIRST_GEN_MAX_SIZE, /* Limit SRM size */
743 	.write = srm_data_write,
744 	.read = srm_data_read,
745 };
746 
747 struct hdcp_workqueue *hdcp_create_workqueue(struct amdgpu_device *adev,
748 					     struct cp_psp *cp_psp, struct dc *dc)
749 {
750 	int max_caps = dc->caps.max_links;
751 	struct hdcp_workqueue *hdcp_work;
752 	int i = 0;
753 
754 	hdcp_work = kzalloc_objs(*hdcp_work, max_caps);
755 	if (ZERO_OR_NULL_PTR(hdcp_work))
756 		return NULL;
757 
758 	hdcp_work->srm = kcalloc(PSP_HDCP_SRM_FIRST_GEN_MAX_SIZE,
759 				 sizeof(*hdcp_work->srm), GFP_KERNEL);
760 
761 	if (!hdcp_work->srm)
762 		goto fail_alloc_context;
763 
764 	hdcp_work->srm_temp = kcalloc(PSP_HDCP_SRM_FIRST_GEN_MAX_SIZE,
765 				      sizeof(*hdcp_work->srm_temp), GFP_KERNEL);
766 
767 	if (!hdcp_work->srm_temp)
768 		goto fail_alloc_context;
769 
770 	hdcp_work->max_link = max_caps;
771 
772 	for (i = 0; i < max_caps; i++) {
773 		mutex_init(&hdcp_work[i].mutex);
774 
775 		INIT_WORK(&hdcp_work[i].cpirq_work, event_cpirq);
776 		INIT_WORK(&hdcp_work[i].property_update_work, event_property_update);
777 		INIT_DELAYED_WORK(&hdcp_work[i].callback_dwork, event_callback);
778 		INIT_DELAYED_WORK(&hdcp_work[i].watchdog_timer_dwork, event_watchdog_timer);
779 		INIT_DELAYED_WORK(&hdcp_work[i].property_validate_dwork, event_property_validate);
780 
781 		struct mod_hdcp_config *config = &hdcp_work[i].hdcp.config;
782 		struct mod_hdcp_ddc_funcs *ddc_funcs = &config->ddc.funcs;
783 
784 		config->psp.handle = &adev->psp;
785 		if (dc->ctx->dce_version == DCN_VERSION_3_1  ||
786 		    dc->ctx->dce_version == DCN_VERSION_3_14 ||
787 		    dc->ctx->dce_version == DCN_VERSION_3_15 ||
788 		    dc->ctx->dce_version == DCN_VERSION_3_16 ||
789 		    dc->ctx->dce_version == DCN_VERSION_3_2  ||
790 		    dc->ctx->dce_version == DCN_VERSION_3_21 ||
791 		    dc->ctx->dce_version == DCN_VERSION_3_5  ||
792 		    dc->ctx->dce_version == DCN_VERSION_3_51 ||
793 		    dc->ctx->dce_version == DCN_VERSION_3_6  ||
794 		    dc->ctx->dce_version == DCN_VERSION_4_01)
795 			config->psp.caps.dtm_v3_supported = 1;
796 
797 		config->ddc.handle = dc_get_link_at_index(dc, i);
798 
799 		ddc_funcs->write_i2c = lp_write_i2c;
800 		ddc_funcs->read_i2c = lp_read_i2c;
801 		ddc_funcs->write_dpcd = lp_write_dpcd;
802 		ddc_funcs->read_dpcd = lp_read_dpcd;
803 		ddc_funcs->atomic_write_poll_read_i2c = lp_atomic_write_poll_read_i2c;
804 		ddc_funcs->atomic_write_poll_read_aux = lp_atomic_write_poll_read_aux;
805 
806 		memset(hdcp_work[i].aconnector, 0,
807 		       sizeof(struct amdgpu_dm_connector *) *
808 			       AMDGPU_DM_MAX_DISPLAY_INDEX);
809 		memset(hdcp_work[i].encryption_status, 0,
810 		       sizeof(enum mod_hdcp_encryption_status) *
811 			       AMDGPU_DM_MAX_DISPLAY_INDEX);
812 	}
813 
814 	cp_psp->funcs.update_stream_config = update_config;
815 	cp_psp->funcs.enable_assr = enable_assr;
816 	cp_psp->handle = hdcp_work;
817 
818 	/* File created at /sys/class/drm/card0/device/hdcp_srm*/
819 	hdcp_work[0].attr = data_attr;
820 	sysfs_bin_attr_init(&hdcp_work[0].attr);
821 
822 	if (sysfs_create_bin_file(&adev->dev->kobj, &hdcp_work[0].attr))
823 		drm_warn(adev_to_drm(adev), "Failed to create device file hdcp_srm\n");
824 
825 	return hdcp_work;
826 
827 fail_alloc_context:
828 	kfree(hdcp_work->srm);
829 	kfree(hdcp_work->srm_temp);
830 	kfree(hdcp_work);
831 
832 	return NULL;
833 }
834 
835