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