dmub_psr.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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 "dmub_psr.h"
#include "dc.h"
#include "dc_dmub_srv.h"
29
#include "dmub/dmub_srv.h"
30 31 32 33
#include "core_types.h"

#define MAX_PIPES 6

34
/*
35 36
 * Convert dmcub psr state to dmcu psr state.
 */
37
static enum dc_psr_state convert_psr_state(uint32_t raw_state)
38
{
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	enum dc_psr_state state = PSR_STATE0;

	if (raw_state == 0)
		state = PSR_STATE0;
	else if (raw_state == 0x10)
		state = PSR_STATE1;
	else if (raw_state == 0x11)
		state = PSR_STATE1a;
	else if (raw_state == 0x20)
		state = PSR_STATE2;
	else if (raw_state == 0x21)
		state = PSR_STATE2a;
	else if (raw_state == 0x30)
		state = PSR_STATE3;
	else if (raw_state == 0x31)
		state = PSR_STATE3Init;
	else if (raw_state == 0x40)
		state = PSR_STATE4;
	else if (raw_state == 0x41)
		state = PSR_STATE4a;
	else if (raw_state == 0x42)
		state = PSR_STATE4b;
	else if (raw_state == 0x43)
		state = PSR_STATE4c;
	else if (raw_state == 0x44)
		state = PSR_STATE4d;
	else if (raw_state == 0x50)
		state = PSR_STATE5;
	else if (raw_state == 0x51)
		state = PSR_STATE5a;
	else if (raw_state == 0x52)
		state = PSR_STATE5b;
	else if (raw_state == 0x53)
		state = PSR_STATE5c;

	return state;
75 76
}

77
/*
78 79
 * Get PSR state from firmware.
 */
80
static void dmub_psr_get_state(struct dmub_psr *dmub, enum dc_psr_state *state, uint8_t panel_inst)
81
{
82
	struct dmub_srv *srv = dmub->ctx->dmub_srv->dmub;
83 84
	uint32_t raw_state = 0;
	uint32_t retry_count = 0;
85
	enum dmub_status status;
86

87 88
	do {
		// Send gpint command and wait for ack
89
		status = dmub_srv_send_gpint_command(srv, DMUB_GPINT__GET_PSR_STATE, panel_inst, 30);
90

91 92 93 94 95 96 97 98 99 100 101 102
		if (status == DMUB_STATUS_OK) {
			// GPINT was executed, get response
			dmub_srv_get_gpint_response(srv, &raw_state);
			*state = convert_psr_state(raw_state);
		} else
			// Return invalid state when GPINT times out
			*state = PSR_STATE_INVALID;

		// Assert if max retry hit
		if (retry_count >= 1000)
			ASSERT(0);
	} while (++retry_count <= 1000 && *state == PSR_STATE_INVALID);
103 104
}

105
/*
106 107
 * Set PSR version.
 */
108
static bool dmub_psr_set_version(struct dmub_psr *dmub, struct dc_stream_state *stream, uint8_t panel_inst)
109 110 111 112
{
	union dmub_rb_cmd cmd;
	struct dc_context *dc = dmub->ctx;

113
	if (stream->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED)
114 115
		return false;

116
	memset(&cmd, 0, sizeof(cmd));
117 118
	cmd.psr_set_version.header.type = DMUB_CMD__PSR;
	cmd.psr_set_version.header.sub_type = DMUB_CMD__PSR_SET_VERSION;
119 120 121 122 123 124 125 126 127
	switch (stream->link->psr_settings.psr_version) {
	case DC_PSR_VERSION_1:
		cmd.psr_set_version.psr_set_version_data.version = PSR_VERSION_1;
		break;
	case DC_PSR_VERSION_UNSUPPORTED:
	default:
		cmd.psr_set_version.psr_set_version_data.version = PSR_VERSION_UNSUPPORTED;
		break;
	}
128 129
	cmd.psr_set_version.psr_set_version_data.cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
	cmd.psr_set_version.psr_set_version_data.panel_inst = panel_inst;
130
	cmd.psr_set_version.header.payload_bytes = sizeof(struct dmub_cmd_psr_set_version_data);
131

132
	dc_dmub_srv_cmd_queue(dc->dmub_srv, &cmd);
133 134
	dc_dmub_srv_cmd_execute(dc->dmub_srv);
	dc_dmub_srv_wait_idle(dc->dmub_srv);
135 136

	return true;
137 138
}

139
/*
140 141
 * Enable/Disable PSR.
 */
142
static void dmub_psr_enable(struct dmub_psr *dmub, bool enable, bool wait, uint8_t panel_inst)
143 144 145
{
	union dmub_rb_cmd cmd;
	struct dc_context *dc = dmub->ctx;
146 147 148
	uint32_t retry_count;
	enum dc_psr_state state = PSR_STATE0;

149
	memset(&cmd, 0, sizeof(cmd));
150 151
	cmd.psr_enable.header.type = DMUB_CMD__PSR;

152 153 154
	cmd.psr_enable.data.cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
	cmd.psr_enable.data.panel_inst = panel_inst;

155 156 157 158 159 160 161
	if (enable)
		cmd.psr_enable.header.sub_type = DMUB_CMD__PSR_ENABLE;
	else
		cmd.psr_enable.header.sub_type = DMUB_CMD__PSR_DISABLE;

	cmd.psr_enable.header.payload_bytes = 0; // Send header only

162
	dc_dmub_srv_cmd_queue(dc->dmub_srv, &cmd);
163 164
	dc_dmub_srv_cmd_execute(dc->dmub_srv);
	dc_dmub_srv_wait_idle(dc->dmub_srv);
165 166 167 168 169 170 171

	/* Below loops 1000 x 500us = 500 ms.
	 *  Exit PSR may need to wait 1-2 frames to power up. Timeout after at
	 *  least a few frames. Should never hit the max retry assert below.
	 */
	if (wait) {
		for (retry_count = 0; retry_count <= 1000; retry_count++) {
172
			dmub_psr_get_state(dmub, &state, panel_inst);
173 174

			if (enable) {
175
				if (state != PSR_STATE0)
176 177
					break;
			} else {
178
				if (state == PSR_STATE0)
179 180 181 182 183 184 185 186 187 188
					break;
			}

			udelay(500);
		}

		/* assert if max retry hit */
		if (retry_count >= 1000)
			ASSERT(0);
	}
189 190
}

191
/*
192 193
 * Set PSR level.
 */
194
static void dmub_psr_set_level(struct dmub_psr *dmub, uint16_t psr_level, uint8_t panel_inst)
195 196
{
	union dmub_rb_cmd cmd;
197
	enum dc_psr_state state = PSR_STATE0;
198 199
	struct dc_context *dc = dmub->ctx;

200
	dmub_psr_get_state(dmub, &state, panel_inst);
201

202
	if (state == PSR_STATE0)
203 204
		return;

205
	memset(&cmd, 0, sizeof(cmd));
206 207 208 209
	cmd.psr_set_level.header.type = DMUB_CMD__PSR;
	cmd.psr_set_level.header.sub_type = DMUB_CMD__PSR_SET_LEVEL;
	cmd.psr_set_level.header.payload_bytes = sizeof(struct dmub_cmd_psr_set_level_data);
	cmd.psr_set_level.psr_set_level_data.psr_level = psr_level;
210 211
	cmd.psr_set_level.psr_set_level_data.cmd_version = PSR_VERSION_1;
	cmd.psr_set_level.psr_set_level_data.panel_inst = panel_inst;
212
	dc_dmub_srv_cmd_queue(dc->dmub_srv, &cmd);
213 214 215 216
	dc_dmub_srv_cmd_execute(dc->dmub_srv);
	dc_dmub_srv_wait_idle(dc->dmub_srv);
}

217
/*
218 219
 * Setup PSR by programming phy registers and sending psr hw context values to firmware.
 */
220
static bool dmub_psr_copy_settings(struct dmub_psr *dmub,
221
		struct dc_link *link,
222 223
		struct psr_context *psr_context,
		uint8_t panel_inst)
224 225 226 227 228 229 230
{
	union dmub_rb_cmd cmd;
	struct dc_context *dc = dmub->ctx;
	struct dmub_cmd_psr_copy_settings_data *copy_settings_data
		= &cmd.psr_copy_settings.psr_copy_settings_data;
	struct pipe_ctx *pipe_ctx = NULL;
	struct resource_context *res_ctx = &link->ctx->dc->current_state->res_ctx;
231
	int i = 0;
232

233
	for (i = 0; i < MAX_PIPES; i++) {
234 235 236
		if (res_ctx->pipe_ctx[i].stream &&
		    res_ctx->pipe_ctx[i].stream->link == link &&
		    res_ctx->pipe_ctx[i].stream->link->connector_signal == SIGNAL_TYPE_EDP) {
237
			pipe_ctx = &res_ctx->pipe_ctx[i];
238
			//TODO: refactor for multi edp support
239 240 241 242
			break;
		}
	}

243
	if (!pipe_ctx)
244 245
		return false;

246
	// First, set the psr version
247
	if (!dmub_psr_set_version(dmub, pipe_ctx->stream, panel_inst))
248 249
		return false;

250 251 252 253 254 255 256 257
	// Program DP DPHY fast training registers
	link->link_enc->funcs->psr_program_dp_dphy_fast_training(link->link_enc,
			psr_context->psrExitLinkTrainingRequired);

	// Program DP_SEC_CNTL1 register to set transmission GPS0 line num and priority to high
	link->link_enc->funcs->psr_program_secondary_packet(link->link_enc,
			psr_context->sdpTransmitLineNumDeadline);

258
	memset(&cmd, 0, sizeof(cmd));
259 260 261 262 263
	cmd.psr_copy_settings.header.type = DMUB_CMD__PSR;
	cmd.psr_copy_settings.header.sub_type = DMUB_CMD__PSR_COPY_SETTINGS;
	cmd.psr_copy_settings.header.payload_bytes = sizeof(struct dmub_cmd_psr_copy_settings_data);

	// Hw insts
264
	copy_settings_data->dpphy_inst				= psr_context->transmitterId;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	copy_settings_data->aux_inst				= psr_context->channel;
	copy_settings_data->digfe_inst				= psr_context->engineId;
	copy_settings_data->digbe_inst				= psr_context->transmitterId;

	copy_settings_data->mpcc_inst				= pipe_ctx->plane_res.mpcc_inst;

	if (pipe_ctx->plane_res.dpp)
		copy_settings_data->dpp_inst			= pipe_ctx->plane_res.dpp->inst;
	else
		copy_settings_data->dpp_inst			= 0;
	if (pipe_ctx->stream_res.opp)
		copy_settings_data->opp_inst			= pipe_ctx->stream_res.opp->inst;
	else
		copy_settings_data->opp_inst			= 0;
	if (pipe_ctx->stream_res.tg)
		copy_settings_data->otg_inst			= pipe_ctx->stream_res.tg->inst;
	else
		copy_settings_data->otg_inst			= 0;

	// Misc
	copy_settings_data->psr_level				= psr_context->psr_level.u32all;
286
	copy_settings_data->smu_optimizations_en		= psr_context->allow_smu_optimizations;
287
	copy_settings_data->multi_disp_optimizations_en	= psr_context->allow_multi_disp_optimizations;
288 289
	copy_settings_data->frame_delay				= psr_context->frame_delay;
	copy_settings_data->frame_cap_ind			= psr_context->psrFrameCaptureIndicationReq;
290 291
	copy_settings_data->init_sdp_deadline			= psr_context->sdpTransmitLineNumDeadline;
	copy_settings_data->debug.u32All = 0;
292
	copy_settings_data->debug.bitfields.visual_confirm	= dc->dc->debug.visual_confirm == VISUAL_CONFIRM_PSR;
293
	copy_settings_data->debug.bitfields.use_hw_lock_mgr		= 1;
294 295
	copy_settings_data->fec_enable_status = (link->fec_state == dc_link_fec_enabled);
	copy_settings_data->fec_enable_delay_in100us = link->dc->debug.fec_enable_delay_in100us;
296 297
	copy_settings_data->cmd_version =  PSR_VERSION_1;
	copy_settings_data->panel_inst = panel_inst;
298

299
	dc_dmub_srv_cmd_queue(dc->dmub_srv, &cmd);
300 301 302 303 304 305
	dc_dmub_srv_cmd_execute(dc->dmub_srv);
	dc_dmub_srv_wait_idle(dc->dmub_srv);

	return true;
}

306
/*
307 308
 * Send command to PSR to force static ENTER and ignore all state changes until exit
 */
309
static void dmub_psr_force_static(struct dmub_psr *dmub, uint8_t panel_inst)
310 311 312 313
{
	union dmub_rb_cmd cmd;
	struct dc_context *dc = dmub->ctx;

314
	memset(&cmd, 0, sizeof(cmd));
315 316 317

	cmd.psr_force_static.psr_force_static_data.panel_inst = panel_inst;
	cmd.psr_force_static.psr_force_static_data.cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
318 319 320 321 322 323 324 325 326
	cmd.psr_force_static.header.type = DMUB_CMD__PSR;
	cmd.psr_force_static.header.sub_type = DMUB_CMD__PSR_FORCE_STATIC;
	cmd.psr_enable.header.payload_bytes = 0;

	dc_dmub_srv_cmd_queue(dc->dmub_srv, &cmd);
	dc_dmub_srv_cmd_execute(dc->dmub_srv);
	dc_dmub_srv_wait_idle(dc->dmub_srv);
}

327
/*
328 329
 * Get PSR residency from firmware.
 */
330
static void dmub_psr_get_residency(struct dmub_psr *dmub, uint32_t *residency, uint8_t panel_inst)
331 332
{
	struct dmub_srv *srv = dmub->ctx->dmub_srv->dmub;
333
	uint16_t param = (uint16_t)(panel_inst << 8);
334

335 336
	/* Send gpint command and wait for ack */
	dmub_srv_send_gpint_command(srv, DMUB_GPINT__PSR_RESIDENCY, param, 30);
337 338 339 340

	dmub_srv_get_gpint_response(srv, residency);
}

341
static const struct dmub_psr_funcs psr_funcs = {
342 343 344 345
	.psr_copy_settings		= dmub_psr_copy_settings,
	.psr_enable			= dmub_psr_enable,
	.psr_get_state			= dmub_psr_get_state,
	.psr_set_level			= dmub_psr_set_level,
346
	.psr_force_static		= dmub_psr_force_static,
347
	.psr_get_residency		= dmub_psr_get_residency,
348 349
};

350
/*
351 352 353 354 355 356 357 358
 * Construct PSR object.
 */
static void dmub_psr_construct(struct dmub_psr *psr, struct dc_context *ctx)
{
	psr->ctx = ctx;
	psr->funcs = &psr_funcs;
}

359
/*
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
 * Allocate and initialize PSR object.
 */
struct dmub_psr *dmub_psr_create(struct dc_context *ctx)
{
	struct dmub_psr *psr = kzalloc(sizeof(struct dmub_psr), GFP_KERNEL);

	if (psr == NULL) {
		BREAK_TO_DEBUGGER();
		return NULL;
	}

	dmub_psr_construct(psr, ctx);

	return psr;
}

376
/*
377 378 379 380
 * Deallocate PSR object.
 */
void dmub_psr_destroy(struct dmub_psr **dmub)
{
381
	kfree(*dmub);
382 383
	*dmub = NULL;
}