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