927 lines · 28.8 KB · C
RawDownload
// SPDX-License-Identifier: MIT/* * Copyright 2019 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Authors: AMD * */ #include "amdgpu_dm_hdcp.h"#include "amdgpu.h"#include "amdgpu_dm.h"#include "dc_fused_io.h"#include "dm_helpers.h"#include <drm/display/drm_hdcp_helper.h>#include "hdcp_psp.h" /* * If the SRM version being loaded is less than or equal to the * currently loaded SRM, psp will return 0xFFFF as the version */#define PSP_SRM_VERSION_MAX 0xFFFF STATIC_IFN_KUNIT bool lp_write_i2c(void *handle, uint32_t address, const uint8_t *data, uint32_t size){	struct dc_link *link = handle;	struct i2c_payload i2c_payloads[] = {{true, address, size, (void *)data} };	struct i2c_command cmd = {i2c_payloads, 1, I2C_COMMAND_ENGINE_HW,				  link->dc->caps.i2c_speed_in_khz}; 	return dm_helpers_submit_i2c(link->ctx, link, &cmd);}EXPORT_IF_KUNIT(lp_write_i2c); STATIC_IFN_KUNIT bool lp_read_i2c(void *handle, uint32_t address, uint8_t offset, uint8_t *data, uint32_t size){	struct dc_link *link = handle; 	struct i2c_payload i2c_payloads[] = {{true, address, 1, &offset},					     {false, address, size, data} };	struct i2c_command cmd = {i2c_payloads, 2, I2C_COMMAND_ENGINE_HW,				  link->dc->caps.i2c_speed_in_khz}; 	return dm_helpers_submit_i2c(link->ctx, link, &cmd);}EXPORT_IF_KUNIT(lp_read_i2c); STATIC_IFN_KUNIT bool lp_write_dpcd(void *handle, uint32_t address, const uint8_t *data, uint32_t size){	struct dc_link *link = handle; 	return dm_helpers_dp_write_dpcd(link->ctx, link, address, data, size);}EXPORT_IF_KUNIT(lp_write_dpcd); STATIC_IFN_KUNIT bool lp_read_dpcd(void *handle, uint32_t address, uint8_t *data, uint32_t size){	struct dc_link *link = handle; 	return dm_helpers_dp_read_dpcd(link->ctx, link, address, data, size);}EXPORT_IF_KUNIT(lp_read_dpcd); STATIC_IFN_KUNIT bool 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){	struct dc_link *link = handle; 	return dm_atomic_write_poll_read_i2c(link, write, poll, read, poll_timeout_us, poll_mask_msb);}EXPORT_IF_KUNIT(lp_atomic_write_poll_read_i2c); STATIC_IFN_KUNIT bool 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){	struct dc_link *link = handle; 	return dm_atomic_write_poll_read_aux(link, write, poll, read, poll_timeout_us, poll_mask_msb);}EXPORT_IF_KUNIT(lp_atomic_write_poll_read_aux); STATIC_IFN_KUNITuint8_t *psp_get_srm(struct psp_context *psp, uint32_t *srm_version, uint32_t *srm_size){	struct ta_hdcp_shared_memory *hdcp_cmd; 	if (!psp->hdcp_context.context.initialized) {		DRM_WARN("Failed to get hdcp srm. HDCP TA is not initialized.");		return NULL;	} 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory)); 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP_GET_SRM;	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id); 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)		return NULL; 	*srm_version = hdcp_cmd->out_msg.hdcp_get_srm.srm_version;	*srm_size = hdcp_cmd->out_msg.hdcp_get_srm.srm_buf_size; 	return hdcp_cmd->out_msg.hdcp_get_srm.srm_buf;}EXPORT_IF_KUNIT(psp_get_srm); STATIC_IFN_KUNITint psp_set_srm(struct psp_context *psp,		       u8 *srm, uint32_t srm_size, uint32_t *srm_version){	struct ta_hdcp_shared_memory *hdcp_cmd; 	if (!psp->hdcp_context.context.initialized) {		DRM_WARN("Failed to get hdcp srm. HDCP TA is not initialized.");		return -EINVAL;	} 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory)); 	memcpy(hdcp_cmd->in_msg.hdcp_set_srm.srm_buf, srm, srm_size);	hdcp_cmd->in_msg.hdcp_set_srm.srm_buf_size = srm_size;	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP_SET_SRM; 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id); 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS ||	    hdcp_cmd->out_msg.hdcp_set_srm.valid_signature != 1 ||	    hdcp_cmd->out_msg.hdcp_set_srm.srm_version == PSP_SRM_VERSION_MAX)		return -EINVAL; 	*srm_version = hdcp_cmd->out_msg.hdcp_set_srm.srm_version;	return 0;}EXPORT_IF_KUNIT(psp_set_srm); STATIC_IFN_KUNITvoid process_output(struct hdcp_workqueue *hdcp_work){	struct mod_hdcp_output output = hdcp_work->output; 	if (output.callback_stop)		cancel_delayed_work(&hdcp_work->callback_dwork); 	if (output.callback_needed)		schedule_delayed_work(&hdcp_work->callback_dwork,				      msecs_to_jiffies(output.callback_delay)); 	if (output.watchdog_timer_stop)		cancel_delayed_work(&hdcp_work->watchdog_timer_dwork); 	if (output.watchdog_timer_needed)		schedule_delayed_work(&hdcp_work->watchdog_timer_dwork,				      msecs_to_jiffies(output.watchdog_timer_delay)); 	schedule_delayed_work(&hdcp_work->property_validate_dwork, msecs_to_jiffies(0));}EXPORT_IF_KUNIT(process_output); STATIC_IFN_KUNITbool hdcp_get_content_protection_from_status(	unsigned int hdcp_content_type,	enum mod_hdcp_encryption_status encryption_status,	unsigned int *content_protection){	if (encryption_status == MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF) {		*content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;		return true;	} 	if (hdcp_content_type == DRM_MODE_HDCP_CONTENT_TYPE0 &&	    encryption_status <= MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE0_ON) {		*content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;		return true;	} 	if (hdcp_content_type == DRM_MODE_HDCP_CONTENT_TYPE1 &&	    encryption_status == MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE1_ON) {		*content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;		return true;	} 	return false;}EXPORT_IF_KUNIT(hdcp_get_content_protection_from_status); STATIC_IFN_KUNITvoid hdcp_get_link_display_adjustments(	bool enable_encryption,	u8 content_type,	bool fused_io_supported,	bool hdcp_lc_force_fw_enable,	bool hdcp_lc_enable_sw_fallback,	struct mod_hdcp_link_adjustment *link_adjust,	struct mod_hdcp_display_adjustment *display_adjust){	memset(link_adjust, 0, sizeof(*link_adjust));	memset(display_adjust, 0, sizeof(*display_adjust)); 	if (!enable_encryption) {		display_adjust->disable =			MOD_HDCP_DISPLAY_DISABLE_AUTHENTICATION;		return;	} 	display_adjust->disable = MOD_HDCP_DISPLAY_NOT_DISABLE;	link_adjust->auth_delay = 2;	link_adjust->retry_limit = MAX_NUM_OF_ATTEMPTS; 	if (content_type == DRM_MODE_HDCP_CONTENT_TYPE0) {		link_adjust->hdcp2.force_type = MOD_HDCP_FORCE_TYPE_0;	} else if (content_type == DRM_MODE_HDCP_CONTENT_TYPE1) {		link_adjust->hdcp1.disable = 1;		link_adjust->hdcp2.force_type = MOD_HDCP_FORCE_TYPE_1;	} 	link_adjust->hdcp2.use_fw_locality_check =		fused_io_supported || hdcp_lc_force_fw_enable;	link_adjust->hdcp2.use_sw_locality_fallback =		hdcp_lc_enable_sw_fallback;}EXPORT_IF_KUNIT(hdcp_get_link_display_adjustments); STATIC_IFN_KUNITvoid link_lock(struct hdcp_workqueue *work, bool lock){	int i = 0; 	for (i = 0; i < work->max_link; i++) {		if (lock)			mutex_lock(&work[i].mutex);		else			mutex_unlock(&work[i].mutex);	}}EXPORT_IF_KUNIT(link_lock); STATIC_IFN_KUNITvoid hdcp_update_display_encryption_control(struct hdcp_workqueue *hdcp_work,					    struct hdcp_workqueue *hdcp_w,					    unsigned int conn_index,					    bool enable_encryption){	if (enable_encryption) {		/* Explicitly set the saved SRM as sysfs call will be after we already enabled hdcp		 * (s3 resume case)		 */		if (hdcp_work->srm_size > 0)			psp_set_srm(hdcp_work->hdcp.config.psp.handle, hdcp_work->srm,				    hdcp_work->srm_size,				    &hdcp_work->srm_version); 		schedule_delayed_work(&hdcp_w->property_validate_dwork,				      msecs_to_jiffies(DRM_HDCP_CHECK_PERIOD_MS));	} else {		hdcp_w->encryption_status[conn_index] = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;		cancel_delayed_work(&hdcp_w->property_validate_dwork);	}}EXPORT_IF_KUNIT(hdcp_update_display_encryption_control); void hdcp_update_display(struct hdcp_workqueue *hdcp_work,			 unsigned int link_index,			 struct amdgpu_dm_connector *aconnector,			 u8 content_type,			 bool enable_encryption){	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];	struct mod_hdcp_link_adjustment link_adjust;	struct mod_hdcp_display_adjustment display_adjust;	unsigned int conn_index = aconnector->base.index;	const struct dc *dc = aconnector->dc_link->dc; 	guard(mutex)(&hdcp_w->mutex);	drm_connector_get(&aconnector->base);	if (hdcp_w->aconnector[conn_index])		drm_connector_put(&hdcp_w->aconnector[conn_index]->base);	hdcp_w->aconnector[conn_index] = aconnector; 	hdcp_get_link_display_adjustments(enable_encryption, content_type,			dc->caps.fused_io_supported,			dc->debug.hdcp_lc_force_fw_enable,			dc->debug.hdcp_lc_enable_sw_fallback,			&link_adjust, &display_adjust);	hdcp_update_display_encryption_control(hdcp_work, hdcp_w, conn_index,					      enable_encryption); 	mod_hdcp_update_display(&hdcp_w->hdcp, conn_index, &link_adjust, &display_adjust, &hdcp_w->output); 	process_output(hdcp_w);}EXPORT_IF_KUNIT(hdcp_update_display); STATIC_IFN_KUNITvoid hdcp_remove_display(struct hdcp_workqueue *hdcp_work,				unsigned int link_index,			 struct amdgpu_dm_connector *aconnector){	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];	struct drm_connector_state *conn_state = aconnector->base.state;	unsigned int conn_index = aconnector->base.index; 	guard(mutex)(&hdcp_w->mutex); 	/* the removal of display will invoke auth reset -> hdcp destroy and	 * we'd expect the Content Protection (CP) property changed back to	 * DESIRED if at the time ENABLED. CP property change should occur	 * before the element removed from linked list.	 */	if (conn_state && conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {		conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 		DRM_DEBUG_DRIVER("[HDCP_DM] display %d, CP 2 -> 1, type %u, DPMS %u\n",				 aconnector->base.index, conn_state->hdcp_content_type,				 aconnector->base.dpms);	} 	mod_hdcp_remove_display(&hdcp_w->hdcp, aconnector->base.index, &hdcp_w->output);	if (hdcp_w->aconnector[conn_index]) {		drm_connector_put(&hdcp_w->aconnector[conn_index]->base);		hdcp_w->aconnector[conn_index] = NULL;	}	process_output(hdcp_w);}EXPORT_IF_KUNIT(hdcp_remove_display); void hdcp_reset_display(struct hdcp_workqueue *hdcp_work, unsigned int link_index){	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index];	unsigned int conn_index; 	guard(mutex)(&hdcp_w->mutex); 	mod_hdcp_reset_connection(&hdcp_w->hdcp,  &hdcp_w->output); 	cancel_delayed_work(&hdcp_w->property_validate_dwork); 	for (conn_index = 0; conn_index < AMDGPU_DM_MAX_DISPLAY_COUNT; conn_index++) {		hdcp_w->encryption_status[conn_index] =			MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;		if (hdcp_w->aconnector[conn_index]) {			drm_connector_put(&hdcp_w->aconnector[conn_index]->base);			hdcp_w->aconnector[conn_index] = NULL;		}	} 	process_output(hdcp_w);}EXPORT_IF_KUNIT(hdcp_reset_display); void hdcp_handle_cpirq(struct hdcp_workqueue *hdcp_work, unsigned int link_index){	struct hdcp_workqueue *hdcp_w = &hdcp_work[link_index]; 	schedule_work(&hdcp_w->cpirq_work);}EXPORT_IF_KUNIT(hdcp_handle_cpirq); STATIC_IFN_KUNITvoid event_callback(struct work_struct *work){	struct hdcp_workqueue *hdcp_work; 	hdcp_work = container_of(to_delayed_work(work), struct hdcp_workqueue,				 callback_dwork); 	guard(mutex)(&hdcp_work->mutex); 	cancel_delayed_work(&hdcp_work->callback_dwork); 	mod_hdcp_process_event(&hdcp_work->hdcp, MOD_HDCP_EVENT_CALLBACK,			       &hdcp_work->output); 	process_output(hdcp_work);}EXPORT_IF_KUNIT(event_callback); STATIC_IFN_KUNITvoid event_property_update(struct work_struct *work){	struct hdcp_workqueue *hdcp_work = container_of(work, struct hdcp_workqueue,							property_update_work);	struct amdgpu_dm_connector *aconnector = NULL;	struct drm_device *dev;	unsigned int content_protection;	long ret;	unsigned int conn_index;	struct drm_connector *connector;	struct drm_connector_state *conn_state; 	for (conn_index = 0; conn_index < AMDGPU_DM_MAX_DISPLAY_COUNT; conn_index++) {		aconnector = hdcp_work->aconnector[conn_index]; 		if (!aconnector)			continue; 		connector = &aconnector->base; 		/* check if display connected */		if (connector->status != connector_status_connected)			continue; 		conn_state = aconnector->base.state; 		if (!conn_state)			continue; 		dev = connector->dev; 		if (!dev)			continue; 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);		guard(mutex)(&hdcp_work->mutex); 		if (conn_state->commit) {			ret = wait_for_completion_interruptible_timeout(&conn_state->commit->hw_done,									10 * HZ);			if (ret == 0) {				DRM_ERROR("HDCP state unknown! Setting it to DESIRED\n");				hdcp_work->encryption_status[conn_index] =					MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;			}		}		if (hdcp_get_content_protection_from_status(conn_state->hdcp_content_type,							    hdcp_work->encryption_status[conn_index],							    &content_protection)) {			if (content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)				DRM_DEBUG_DRIVER("[HDCP_DM] DRM_MODE_CONTENT_PROTECTION_ENABLED\n");			else				DRM_DEBUG_DRIVER("[HDCP_DM] DRM_MODE_CONTENT_PROTECTION_DESIRED\n"); 			drm_hdcp_update_content_protection(connector, content_protection);		}		drm_modeset_unlock(&dev->mode_config.connection_mutex);	}}EXPORT_IF_KUNIT(event_property_update); STATIC_IFN_KUNITvoid event_property_validate(struct work_struct *work){	struct hdcp_workqueue *hdcp_work =		container_of(to_delayed_work(work), struct hdcp_workqueue, property_validate_dwork);	struct mod_hdcp_display_query query;	struct amdgpu_dm_connector *aconnector;	unsigned int conn_index; 	guard(mutex)(&hdcp_work->mutex); 	for (conn_index = 0; conn_index < AMDGPU_DM_MAX_DISPLAY_COUNT;	     conn_index++) {		aconnector = hdcp_work->aconnector[conn_index]; 		if (!aconnector)			continue; 		/* check if display connected */		if (aconnector->base.status != connector_status_connected)			continue; 		if (!aconnector->base.state)			continue; 		query.encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;		mod_hdcp_query_display(&hdcp_work->hdcp, aconnector->base.index,				       &query); 		DRM_DEBUG_DRIVER("[HDCP_DM] disp %d, connector->CP %u, (query, work): (%d, %d)\n",				 aconnector->base.index,

Lines 1–500 of 927.