xref: /linux/drivers/gpu/drm/amd/display/dc/dce/dce_dmcu.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 /*
2  * Copyright 2012-16 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 
29 #include "core_types.h"
30 #include "link_encoder.h"
31 #include "dce_dmcu.h"
32 #include "dm_services.h"
33 #include "reg_helper.h"
34 #include "fixed31_32.h"
35 #include "dc.h"
36 
37 #define TO_DCE_DMCU(dmcu)\
38 	container_of(dmcu, struct dce_dmcu, base)
39 
40 #define REG(reg) \
41 	(dmcu_dce->regs->reg)
42 
43 #undef FN
44 #define FN(reg_name, field_name) \
45 	dmcu_dce->dmcu_shift->field_name, dmcu_dce->dmcu_mask->field_name
46 
47 #define CTX \
48 	dmcu_dce->base.ctx
49 
50 /* PSR related commands */
51 #define PSR_ENABLE 0x20
52 #define PSR_EXIT 0x21
53 #define PSR_SET 0x23
54 #define PSR_SET_WAITLOOP 0x31
55 #define MCP_INIT_DMCU 0x88
56 #define MCP_INIT_IRAM 0x89
57 #define MCP_SYNC_PHY_LOCK 0x90
58 #define MCP_SYNC_PHY_UNLOCK 0x91
59 #define MCP_BL_SET_PWM_FRAC 0x6A  /* Enable or disable Fractional PWM */
60 #define MASTER_COMM_CNTL_REG__MASTER_COMM_INTERRUPT_MASK   0x00000001L
61 
62 // PSP FW version
63 #define mmMP0_SMN_C2PMSG_58				0x1607A
64 
65 //Register access policy version
66 #define mmMP0_SMN_C2PMSG_91				0x1609B
67 
68 static bool dce_dmcu_init(struct dmcu *dmcu)
69 {
70 	// Do nothing
71 	return true;
72 }
73 
74 bool dce_dmcu_load_iram(struct dmcu *dmcu,
75 		unsigned int start_offset,
76 		const char *src,
77 		unsigned int bytes)
78 {
79 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
80 	unsigned int count = 0;
81 
82 	/* Enable write access to IRAM */
83 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
84 			IRAM_HOST_ACCESS_EN, 1,
85 			IRAM_WR_ADDR_AUTO_INC, 1);
86 
87 	REG_WAIT(DCI_MEM_PWR_STATUS, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
88 
89 	REG_WRITE(DMCU_IRAM_WR_CTRL, start_offset);
90 
91 	for (count = 0; count < bytes; count++)
92 		REG_WRITE(DMCU_IRAM_WR_DATA, src[count]);
93 
94 	/* Disable write access to IRAM to allow dynamic sleep state */
95 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
96 			IRAM_HOST_ACCESS_EN, 0,
97 			IRAM_WR_ADDR_AUTO_INC, 0);
98 
99 	return true;
100 }
101 
102 static void dce_get_dmcu_psr_state(struct dmcu *dmcu, uint32_t *psr_state)
103 {
104 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
105 
106 	uint32_t psr_state_offset = 0xf0;
107 
108 	/* Enable write access to IRAM */
109 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 1);
110 
111 	REG_WAIT(DCI_MEM_PWR_STATUS, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
112 
113 	/* Write address to IRAM_RD_ADDR in DMCU_IRAM_RD_CTRL */
114 	REG_WRITE(DMCU_IRAM_RD_CTRL, psr_state_offset);
115 
116 	/* Read data from IRAM_RD_DATA in DMCU_IRAM_RD_DATA*/
117 	*psr_state = REG_READ(DMCU_IRAM_RD_DATA);
118 
119 	/* Disable write access to IRAM after finished using IRAM
120 	 * in order to allow dynamic sleep state
121 	 */
122 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 0);
123 }
124 
125 static void dce_dmcu_set_psr_enable(struct dmcu *dmcu, bool enable, bool wait)
126 {
127 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
128 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
129 	unsigned int dmcu_wait_reg_ready_interval = 100;
130 
131 	unsigned int retryCount;
132 	uint32_t psr_state = 0;
133 
134 	/* waitDMCUReadyForCmd */
135 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
136 				dmcu_wait_reg_ready_interval,
137 				dmcu_max_retry_on_wait_reg_ready);
138 
139 	/* setDMCUParam_Cmd */
140 	if (enable)
141 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
142 				PSR_ENABLE);
143 	else
144 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
145 				PSR_EXIT);
146 
147 	/* notifyDMCUMsg */
148 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
149 	if (wait == true) {
150 		for (retryCount = 0; retryCount <= 100; retryCount++) {
151 			dce_get_dmcu_psr_state(dmcu, &psr_state);
152 			if (enable) {
153 				if (psr_state != 0)
154 					break;
155 			} else {
156 				if (psr_state == 0)
157 					break;
158 			}
159 			udelay(10);
160 		}
161 	}
162 }
163 
164 static bool dce_dmcu_setup_psr(struct dmcu *dmcu,
165 		struct dc_link *link,
166 		struct psr_context *psr_context)
167 {
168 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
169 
170 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
171 	unsigned int dmcu_wait_reg_ready_interval = 100;
172 
173 	union dce_dmcu_psr_config_data_reg1 masterCmdData1;
174 	union dce_dmcu_psr_config_data_reg2 masterCmdData2;
175 	union dce_dmcu_psr_config_data_reg3 masterCmdData3;
176 
177 	link->link_enc->funcs->psr_program_dp_dphy_fast_training(link->link_enc,
178 			psr_context->psrExitLinkTrainingRequired);
179 
180 	/* Enable static screen interrupts for PSR supported display */
181 	/* Disable the interrupt coming from other displays. */
182 	REG_UPDATE_4(DMCU_INTERRUPT_TO_UC_EN_MASK,
183 			STATIC_SCREEN1_INT_TO_UC_EN, 0,
184 			STATIC_SCREEN2_INT_TO_UC_EN, 0,
185 			STATIC_SCREEN3_INT_TO_UC_EN, 0,
186 			STATIC_SCREEN4_INT_TO_UC_EN, 0);
187 
188 	switch (psr_context->controllerId) {
189 	/* Driver uses case 1 for unconfigured */
190 	case 1:
191 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
192 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
193 		break;
194 	case 2:
195 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
196 				STATIC_SCREEN2_INT_TO_UC_EN, 1);
197 		break;
198 	case 3:
199 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
200 				STATIC_SCREEN3_INT_TO_UC_EN, 1);
201 		break;
202 	case 4:
203 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
204 				STATIC_SCREEN4_INT_TO_UC_EN, 1);
205 		break;
206 	case 5:
207 		/* CZ/NL only has 4 CRTC!!
208 		 * really valid.
209 		 * There is no interrupt enable mask for these instances.
210 		 */
211 		break;
212 	case 6:
213 		/* CZ/NL only has 4 CRTC!!
214 		 * These are here because they are defined in HW regspec,
215 		 * but not really valid. There is no interrupt enable mask
216 		 * for these instances.
217 		 */
218 		break;
219 	default:
220 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
221 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
222 		break;
223 	}
224 
225 	link->link_enc->funcs->psr_program_secondary_packet(link->link_enc,
226 			psr_context->sdpTransmitLineNumDeadline);
227 
228 	/* waitDMCUReadyForCmd */
229 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
230 					dmcu_wait_reg_ready_interval,
231 					dmcu_max_retry_on_wait_reg_ready);
232 
233 	/* setDMCUParam_PSRHostConfigData */
234 	masterCmdData1.u32All = 0;
235 	masterCmdData1.bits.timehyst_frames = psr_context->timehyst_frames;
236 	masterCmdData1.bits.hyst_lines = psr_context->hyst_lines;
237 	masterCmdData1.bits.rfb_update_auto_en =
238 			psr_context->rfb_update_auto_en;
239 	masterCmdData1.bits.dp_port_num = psr_context->transmitterId;
240 	masterCmdData1.bits.dcp_sel = psr_context->controllerId;
241 	masterCmdData1.bits.phy_type  = psr_context->phyType;
242 	masterCmdData1.bits.frame_cap_ind =
243 			psr_context->psrFrameCaptureIndicationReq;
244 	masterCmdData1.bits.aux_chan = psr_context->channel;
245 	masterCmdData1.bits.aux_repeat = psr_context->aux_repeats;
246 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1),
247 					masterCmdData1.u32All);
248 
249 	masterCmdData2.u32All = 0;
250 	masterCmdData2.bits.dig_fe = psr_context->engineId;
251 	masterCmdData2.bits.dig_be = psr_context->transmitterId;
252 	masterCmdData2.bits.skip_wait_for_pll_lock =
253 			psr_context->skipPsrWaitForPllLock;
254 	masterCmdData2.bits.frame_delay = psr_context->frame_delay;
255 	masterCmdData2.bits.smu_phy_id = psr_context->smuPhyId;
256 	masterCmdData2.bits.num_of_controllers =
257 			psr_context->numberOfControllers;
258 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG2),
259 			masterCmdData2.u32All);
260 
261 	masterCmdData3.u32All = 0;
262 	masterCmdData3.bits.psr_level = psr_context->psr_level.u32all;
263 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG3),
264 			masterCmdData3.u32All);
265 
266 	/* setDMCUParam_Cmd */
267 	REG_UPDATE(MASTER_COMM_CMD_REG,
268 			MASTER_COMM_CMD_REG_BYTE0, PSR_SET);
269 
270 	/* notifyDMCUMsg */
271 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
272 
273 	return true;
274 }
275 
276 static bool dce_is_dmcu_initialized(struct dmcu *dmcu)
277 {
278 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
279 	unsigned int dmcu_uc_reset;
280 
281 	/* microcontroller is not running */
282 	REG_GET(DMCU_STATUS, UC_IN_RESET, &dmcu_uc_reset);
283 
284 	/* DMCU is not running */
285 	if (dmcu_uc_reset)
286 		return false;
287 
288 	return true;
289 }
290 
291 static void dce_psr_wait_loop(
292 	struct dmcu *dmcu,
293 	unsigned int wait_loop_number)
294 {
295 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
296 	union dce_dmcu_psr_config_data_wait_loop_reg1 masterCmdData1;
297 
298 	if (dmcu->cached_wait_loop_number == wait_loop_number)
299 		return;
300 
301 	/* DMCU is not running */
302 	if (!dce_is_dmcu_initialized(dmcu))
303 		return;
304 
305 	/* waitDMCUReadyForCmd */
306 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
307 
308 	masterCmdData1.u32 = 0;
309 	masterCmdData1.bits.wait_loop = wait_loop_number;
310 	dmcu->cached_wait_loop_number = wait_loop_number;
311 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1), masterCmdData1.u32);
312 
313 	/* setDMCUParam_Cmd */
314 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, PSR_SET_WAITLOOP);
315 
316 	/* notifyDMCUMsg */
317 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
318 }
319 
320 static void dce_get_psr_wait_loop(
321 		struct dmcu *dmcu, unsigned int *psr_wait_loop_number)
322 {
323 	*psr_wait_loop_number = dmcu->cached_wait_loop_number;
324 	return;
325 }
326 
327 #if defined(CONFIG_DRM_AMD_DC_DCN)
328 static void dcn10_get_dmcu_version(struct dmcu *dmcu)
329 {
330 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
331 	uint32_t dmcu_version_offset = 0xf1;
332 
333 	/* Enable write access to IRAM */
334 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
335 			IRAM_HOST_ACCESS_EN, 1,
336 			IRAM_RD_ADDR_AUTO_INC, 1);
337 
338 	REG_WAIT(DMU_MEM_PWR_CNTL, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
339 
340 	/* Write address to IRAM_RD_ADDR and read from DATA register */
341 	REG_WRITE(DMCU_IRAM_RD_CTRL, dmcu_version_offset);
342 	dmcu->dmcu_version.interface_version = REG_READ(DMCU_IRAM_RD_DATA);
343 	dmcu->dmcu_version.abm_version = REG_READ(DMCU_IRAM_RD_DATA);
344 	dmcu->dmcu_version.psr_version = REG_READ(DMCU_IRAM_RD_DATA);
345 	dmcu->dmcu_version.build_version = ((REG_READ(DMCU_IRAM_RD_DATA) << 8) |
346 						REG_READ(DMCU_IRAM_RD_DATA));
347 
348 	/* Disable write access to IRAM to allow dynamic sleep state */
349 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
350 			IRAM_HOST_ACCESS_EN, 0,
351 			IRAM_RD_ADDR_AUTO_INC, 0);
352 }
353 
354 static void dcn10_dmcu_enable_fractional_pwm(struct dmcu *dmcu,
355 		uint32_t fractional_pwm)
356 {
357 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
358 
359 	/* Wait until microcontroller is ready to process interrupt */
360 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
361 
362 	/* Set PWM fractional enable/disable */
363 	REG_WRITE(MASTER_COMM_DATA_REG1, fractional_pwm);
364 
365 	/* Set command to enable or disable fractional PWM microcontroller */
366 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
367 			MCP_BL_SET_PWM_FRAC);
368 
369 	/* Notify microcontroller of new command */
370 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
371 
372 	/* Ensure command has been executed before continuing */
373 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
374 }
375 
376 static bool dcn10_dmcu_init(struct dmcu *dmcu)
377 {
378 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
379 	const struct dc_config *config = &dmcu->ctx->dc->config;
380 	bool status = false;
381 
382 	PERF_TRACE();
383 	/*  Definition of DC_DMCU_SCRATCH
384 	 *  0 : firmare not loaded
385 	 *  1 : PSP load DMCU FW but not initialized
386 	 *  2 : Firmware already initialized
387 	 */
388 	dmcu->dmcu_state = REG_READ(DC_DMCU_SCRATCH);
389 
390 	switch (dmcu->dmcu_state) {
391 	case DMCU_UNLOADED:
392 		status = false;
393 		break;
394 	case DMCU_LOADED_UNINITIALIZED:
395 		/* Wait until microcontroller is ready to process interrupt */
396 		REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
397 
398 		/* Set initialized ramping boundary value */
399 		REG_WRITE(MASTER_COMM_DATA_REG1, 0xFFFF);
400 
401 		/* Set backlight ramping stepsize */
402 		REG_WRITE(MASTER_COMM_DATA_REG2, abm_gain_stepsize);
403 
404 		/* Set command to initialize microcontroller */
405 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
406 			MCP_INIT_DMCU);
407 
408 		/* Notify microcontroller of new command */
409 		REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
410 
411 		/* Ensure command has been executed before continuing */
412 		REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
413 
414 		// Check state is initialized
415 		dmcu->dmcu_state = REG_READ(DC_DMCU_SCRATCH);
416 
417 		// If microcontroller is not in running state, fail
418 		if (dmcu->dmcu_state == DMCU_RUNNING) {
419 			/* Retrieve and cache the DMCU firmware version. */
420 			dcn10_get_dmcu_version(dmcu);
421 
422 			/* Initialize DMCU to use fractional PWM or not */
423 			dcn10_dmcu_enable_fractional_pwm(dmcu,
424 				(config->disable_fractional_pwm == false) ? 1 : 0);
425 			status = true;
426 		} else {
427 			status = false;
428 		}
429 
430 		break;
431 	case DMCU_RUNNING:
432 		status = true;
433 		break;
434 	default:
435 		status = false;
436 		break;
437 	}
438 
439 	PERF_TRACE();
440 	return status;
441 }
442 
443 static bool dcn21_dmcu_init(struct dmcu *dmcu)
444 {
445 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
446 	uint32_t dmcub_psp_version = REG_READ(DMCUB_SCRATCH15);
447 
448 	if (dmcu->auto_load_dmcu && dmcub_psp_version == 0) {
449 		return false;
450 	}
451 
452 	return dcn10_dmcu_init(dmcu);
453 }
454 
455 static bool dcn10_dmcu_load_iram(struct dmcu *dmcu,
456 		unsigned int start_offset,
457 		const char *src,
458 		unsigned int bytes)
459 {
460 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
461 	unsigned int count = 0;
462 
463 	/* If microcontroller is not running, do nothing */
464 	if (dmcu->dmcu_state != DMCU_RUNNING)
465 		return false;
466 
467 	/* Enable write access to IRAM */
468 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
469 			IRAM_HOST_ACCESS_EN, 1,
470 			IRAM_WR_ADDR_AUTO_INC, 1);
471 
472 	REG_WAIT(DMU_MEM_PWR_CNTL, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
473 
474 	REG_WRITE(DMCU_IRAM_WR_CTRL, start_offset);
475 
476 	for (count = 0; count < bytes; count++)
477 		REG_WRITE(DMCU_IRAM_WR_DATA, src[count]);
478 
479 	/* Disable write access to IRAM to allow dynamic sleep state */
480 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
481 			IRAM_HOST_ACCESS_EN, 0,
482 			IRAM_WR_ADDR_AUTO_INC, 0);
483 
484 	/* Wait until microcontroller is ready to process interrupt */
485 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
486 
487 	/* Set command to signal IRAM is loaded and to initialize IRAM */
488 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
489 			MCP_INIT_IRAM);
490 
491 	/* Notify microcontroller of new command */
492 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
493 
494 	/* Ensure command has been executed before continuing */
495 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
496 
497 	return true;
498 }
499 
500 static void dcn10_get_dmcu_psr_state(struct dmcu *dmcu, uint32_t *psr_state)
501 {
502 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
503 
504 	uint32_t psr_state_offset = 0xf0;
505 
506 	/* If microcontroller is not running, do nothing */
507 	if (dmcu->dmcu_state != DMCU_RUNNING)
508 		return;
509 
510 	/* Enable write access to IRAM */
511 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 1);
512 
513 	REG_WAIT(DMU_MEM_PWR_CNTL, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
514 
515 	/* Write address to IRAM_RD_ADDR in DMCU_IRAM_RD_CTRL */
516 	REG_WRITE(DMCU_IRAM_RD_CTRL, psr_state_offset);
517 
518 	/* Read data from IRAM_RD_DATA in DMCU_IRAM_RD_DATA*/
519 	*psr_state = REG_READ(DMCU_IRAM_RD_DATA);
520 
521 	/* Disable write access to IRAM after finished using IRAM
522 	 * in order to allow dynamic sleep state
523 	 */
524 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 0);
525 }
526 
527 static void dcn10_dmcu_set_psr_enable(struct dmcu *dmcu, bool enable, bool wait)
528 {
529 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
530 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
531 	unsigned int dmcu_wait_reg_ready_interval = 100;
532 
533 	unsigned int retryCount;
534 	uint32_t psr_state = 0;
535 
536 	/* If microcontroller is not running, do nothing */
537 	if (dmcu->dmcu_state != DMCU_RUNNING)
538 		return;
539 
540 	dcn10_get_dmcu_psr_state(dmcu, &psr_state);
541 	if (psr_state == 0 && !enable)
542 		return;
543 	/* waitDMCUReadyForCmd */
544 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
545 				dmcu_wait_reg_ready_interval,
546 				dmcu_max_retry_on_wait_reg_ready);
547 
548 	/* setDMCUParam_Cmd */
549 	if (enable)
550 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
551 				PSR_ENABLE);
552 	else
553 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
554 				PSR_EXIT);
555 
556 	/* notifyDMCUMsg */
557 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
558 
559 	/* Below loops 1000 x 500us = 500 ms.
560 	 *  Exit PSR may need to wait 1-2 frames to power up. Timeout after at
561 	 *  least a few frames. Should never hit the max retry assert below.
562 	 */
563 	if (wait == true) {
564 		for (retryCount = 0; retryCount <= 1000; retryCount++) {
565 			dcn10_get_dmcu_psr_state(dmcu, &psr_state);
566 			if (enable) {
567 				if (psr_state != 0)
568 					break;
569 			} else {
570 				if (psr_state == 0)
571 					break;
572 			}
573 			udelay(500);
574 		}
575 
576 		/* assert if max retry hit */
577 		if (retryCount >= 1000)
578 			ASSERT(0);
579 	}
580 }
581 
582 static bool dcn10_dmcu_setup_psr(struct dmcu *dmcu,
583 		struct dc_link *link,
584 		struct psr_context *psr_context)
585 {
586 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
587 
588 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
589 	unsigned int dmcu_wait_reg_ready_interval = 100;
590 
591 	union dce_dmcu_psr_config_data_reg1 masterCmdData1;
592 	union dce_dmcu_psr_config_data_reg2 masterCmdData2;
593 	union dce_dmcu_psr_config_data_reg3 masterCmdData3;
594 
595 	/* If microcontroller is not running, do nothing */
596 	if (dmcu->dmcu_state != DMCU_RUNNING)
597 		return false;
598 
599 	link->link_enc->funcs->psr_program_dp_dphy_fast_training(link->link_enc,
600 			psr_context->psrExitLinkTrainingRequired);
601 
602 	/* Enable static screen interrupts for PSR supported display */
603 	/* Disable the interrupt coming from other displays. */
604 	REG_UPDATE_4(DMCU_INTERRUPT_TO_UC_EN_MASK,
605 			STATIC_SCREEN1_INT_TO_UC_EN, 0,
606 			STATIC_SCREEN2_INT_TO_UC_EN, 0,
607 			STATIC_SCREEN3_INT_TO_UC_EN, 0,
608 			STATIC_SCREEN4_INT_TO_UC_EN, 0);
609 
610 	switch (psr_context->controllerId) {
611 	/* Driver uses case 1 for unconfigured */
612 	case 1:
613 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
614 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
615 		break;
616 	case 2:
617 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
618 				STATIC_SCREEN2_INT_TO_UC_EN, 1);
619 		break;
620 	case 3:
621 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
622 				STATIC_SCREEN3_INT_TO_UC_EN, 1);
623 		break;
624 	case 4:
625 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
626 				STATIC_SCREEN4_INT_TO_UC_EN, 1);
627 		break;
628 	case 5:
629 		/* CZ/NL only has 4 CRTC!!
630 		 * really valid.
631 		 * There is no interrupt enable mask for these instances.
632 		 */
633 		break;
634 	case 6:
635 		/* CZ/NL only has 4 CRTC!!
636 		 * These are here because they are defined in HW regspec,
637 		 * but not really valid. There is no interrupt enable mask
638 		 * for these instances.
639 		 */
640 		break;
641 	default:
642 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
643 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
644 		break;
645 	}
646 
647 	link->link_enc->funcs->psr_program_secondary_packet(link->link_enc,
648 			psr_context->sdpTransmitLineNumDeadline);
649 
650 	if (psr_context->allow_smu_optimizations)
651 		REG_UPDATE(SMU_INTERRUPT_CONTROL, DC_SMU_INT_ENABLE, 1);
652 
653 	/* waitDMCUReadyForCmd */
654 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
655 			dmcu_wait_reg_ready_interval,
656 			dmcu_max_retry_on_wait_reg_ready);
657 
658 	/* setDMCUParam_PSRHostConfigData */
659 	masterCmdData1.u32All = 0;
660 	masterCmdData1.bits.timehyst_frames = psr_context->timehyst_frames;
661 	masterCmdData1.bits.hyst_lines = psr_context->hyst_lines;
662 	masterCmdData1.bits.rfb_update_auto_en =
663 			psr_context->rfb_update_auto_en;
664 	masterCmdData1.bits.dp_port_num = psr_context->transmitterId;
665 	masterCmdData1.bits.dcp_sel = psr_context->controllerId;
666 	masterCmdData1.bits.phy_type  = psr_context->phyType;
667 	masterCmdData1.bits.frame_cap_ind =
668 			psr_context->psrFrameCaptureIndicationReq;
669 	masterCmdData1.bits.aux_chan = psr_context->channel;
670 	masterCmdData1.bits.aux_repeat = psr_context->aux_repeats;
671 	masterCmdData1.bits.allow_smu_optimizations = psr_context->allow_smu_optimizations;
672 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1),
673 					masterCmdData1.u32All);
674 
675 	masterCmdData2.u32All = 0;
676 	masterCmdData2.bits.dig_fe = psr_context->engineId;
677 	masterCmdData2.bits.dig_be = psr_context->transmitterId;
678 	masterCmdData2.bits.skip_wait_for_pll_lock =
679 			psr_context->skipPsrWaitForPllLock;
680 	masterCmdData2.bits.frame_delay = psr_context->frame_delay;
681 	masterCmdData2.bits.smu_phy_id = psr_context->smuPhyId;
682 	masterCmdData2.bits.num_of_controllers =
683 			psr_context->numberOfControllers;
684 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG2),
685 			masterCmdData2.u32All);
686 
687 	masterCmdData3.u32All = 0;
688 	masterCmdData3.bits.psr_level = psr_context->psr_level.u32all;
689 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG3),
690 			masterCmdData3.u32All);
691 
692 
693 	/* setDMCUParam_Cmd */
694 	REG_UPDATE(MASTER_COMM_CMD_REG,
695 			MASTER_COMM_CMD_REG_BYTE0, PSR_SET);
696 
697 	/* notifyDMCUMsg */
698 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
699 
700 	/* waitDMCUReadyForCmd */
701 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
702 
703 	return true;
704 }
705 
706 static void dcn10_psr_wait_loop(
707 	struct dmcu *dmcu,
708 	unsigned int wait_loop_number)
709 {
710 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
711 	union dce_dmcu_psr_config_data_wait_loop_reg1 masterCmdData1;
712 
713 	/* If microcontroller is not running, do nothing */
714 	if (dmcu->dmcu_state != DMCU_RUNNING)
715 		return;
716 
717 	if (wait_loop_number != 0) {
718 	/* waitDMCUReadyForCmd */
719 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
720 
721 	masterCmdData1.u32 = 0;
722 	masterCmdData1.bits.wait_loop = wait_loop_number;
723 	dmcu->cached_wait_loop_number = wait_loop_number;
724 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1), masterCmdData1.u32);
725 
726 	/* setDMCUParam_Cmd */
727 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, PSR_SET_WAITLOOP);
728 
729 	/* notifyDMCUMsg */
730 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
731 	}
732 }
733 
734 static void dcn10_get_psr_wait_loop(
735 		struct dmcu *dmcu, unsigned int *psr_wait_loop_number)
736 {
737 	*psr_wait_loop_number = dmcu->cached_wait_loop_number;
738 	return;
739 }
740 
741 static bool dcn10_is_dmcu_initialized(struct dmcu *dmcu)
742 {
743 	/* microcontroller is not running */
744 	if (dmcu->dmcu_state != DMCU_RUNNING)
745 		return false;
746 	return true;
747 }
748 
749 
750 
751 static bool dcn20_lock_phy(struct dmcu *dmcu)
752 {
753 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
754 
755 	/* If microcontroller is not running, do nothing */
756 	if (dmcu->dmcu_state != DMCU_RUNNING)
757 		return false;
758 
759 	/* waitDMCUReadyForCmd */
760 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
761 
762 	/* setDMCUParam_Cmd */
763 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, MCP_SYNC_PHY_LOCK);
764 
765 	/* notifyDMCUMsg */
766 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
767 
768 	/* waitDMCUReadyForCmd */
769 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
770 
771 	return true;
772 }
773 
774 static bool dcn20_unlock_phy(struct dmcu *dmcu)
775 {
776 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
777 
778 	/* If microcontroller is not running, do nothing */
779 	if (dmcu->dmcu_state != DMCU_RUNNING)
780 		return false;
781 
782 	/* waitDMCUReadyForCmd */
783 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
784 
785 	/* setDMCUParam_Cmd */
786 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, MCP_SYNC_PHY_UNLOCK);
787 
788 	/* notifyDMCUMsg */
789 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
790 
791 	/* waitDMCUReadyForCmd */
792 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
793 
794 	return true;
795 }
796 
797 #endif //(CONFIG_DRM_AMD_DC_DCN)
798 
799 static const struct dmcu_funcs dce_funcs = {
800 	.dmcu_init = dce_dmcu_init,
801 	.load_iram = dce_dmcu_load_iram,
802 	.set_psr_enable = dce_dmcu_set_psr_enable,
803 	.setup_psr = dce_dmcu_setup_psr,
804 	.get_psr_state = dce_get_dmcu_psr_state,
805 	.set_psr_wait_loop = dce_psr_wait_loop,
806 	.get_psr_wait_loop = dce_get_psr_wait_loop,
807 	.is_dmcu_initialized = dce_is_dmcu_initialized
808 };
809 
810 #if defined(CONFIG_DRM_AMD_DC_DCN)
811 static const struct dmcu_funcs dcn10_funcs = {
812 	.dmcu_init = dcn10_dmcu_init,
813 	.load_iram = dcn10_dmcu_load_iram,
814 	.set_psr_enable = dcn10_dmcu_set_psr_enable,
815 	.setup_psr = dcn10_dmcu_setup_psr,
816 	.get_psr_state = dcn10_get_dmcu_psr_state,
817 	.set_psr_wait_loop = dcn10_psr_wait_loop,
818 	.get_psr_wait_loop = dcn10_get_psr_wait_loop,
819 	.is_dmcu_initialized = dcn10_is_dmcu_initialized
820 };
821 
822 static const struct dmcu_funcs dcn20_funcs = {
823 	.dmcu_init = dcn10_dmcu_init,
824 	.load_iram = dcn10_dmcu_load_iram,
825 	.set_psr_enable = dcn10_dmcu_set_psr_enable,
826 	.setup_psr = dcn10_dmcu_setup_psr,
827 	.get_psr_state = dcn10_get_dmcu_psr_state,
828 	.set_psr_wait_loop = dcn10_psr_wait_loop,
829 	.get_psr_wait_loop = dcn10_get_psr_wait_loop,
830 	.is_dmcu_initialized = dcn10_is_dmcu_initialized,
831 	.lock_phy = dcn20_lock_phy,
832 	.unlock_phy = dcn20_unlock_phy
833 };
834 
835 static const struct dmcu_funcs dcn21_funcs = {
836 	.dmcu_init = dcn21_dmcu_init,
837 	.load_iram = dcn10_dmcu_load_iram,
838 	.set_psr_enable = dcn10_dmcu_set_psr_enable,
839 	.setup_psr = dcn10_dmcu_setup_psr,
840 	.get_psr_state = dcn10_get_dmcu_psr_state,
841 	.set_psr_wait_loop = dcn10_psr_wait_loop,
842 	.get_psr_wait_loop = dcn10_get_psr_wait_loop,
843 	.is_dmcu_initialized = dcn10_is_dmcu_initialized,
844 	.lock_phy = dcn20_lock_phy,
845 	.unlock_phy = dcn20_unlock_phy
846 };
847 #endif
848 
849 static void dce_dmcu_construct(
850 	struct dce_dmcu *dmcu_dce,
851 	struct dc_context *ctx,
852 	const struct dce_dmcu_registers *regs,
853 	const struct dce_dmcu_shift *dmcu_shift,
854 	const struct dce_dmcu_mask *dmcu_mask)
855 {
856 	struct dmcu *base = &dmcu_dce->base;
857 
858 	base->ctx = ctx;
859 	base->funcs = &dce_funcs;
860 	base->cached_wait_loop_number = 0;
861 
862 	dmcu_dce->regs = regs;
863 	dmcu_dce->dmcu_shift = dmcu_shift;
864 	dmcu_dce->dmcu_mask = dmcu_mask;
865 }
866 
867 #if defined(CONFIG_DRM_AMD_DC_DCN)
868 static void dcn21_dmcu_construct(
869 		struct dce_dmcu *dmcu_dce,
870 		struct dc_context *ctx,
871 		const struct dce_dmcu_registers *regs,
872 		const struct dce_dmcu_shift *dmcu_shift,
873 		const struct dce_dmcu_mask *dmcu_mask)
874 {
875 	uint32_t psp_version = 0;
876 
877 	dce_dmcu_construct(dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
878 
879 	if (!IS_FPGA_MAXIMUS_DC(ctx->dce_environment)) {
880 		psp_version = dm_read_reg(ctx, mmMP0_SMN_C2PMSG_58);
881 		dmcu_dce->base.auto_load_dmcu = ((psp_version & 0x00FF00FF) > 0x00110029);
882 		dmcu_dce->base.psp_version = psp_version;
883 	}
884 }
885 #endif
886 
887 struct dmcu *dce_dmcu_create(
888 	struct dc_context *ctx,
889 	const struct dce_dmcu_registers *regs,
890 	const struct dce_dmcu_shift *dmcu_shift,
891 	const struct dce_dmcu_mask *dmcu_mask)
892 {
893 	struct dce_dmcu *dmcu_dce = kzalloc(sizeof(*dmcu_dce), GFP_KERNEL);
894 
895 	if (dmcu_dce == NULL) {
896 		BREAK_TO_DEBUGGER();
897 		return NULL;
898 	}
899 
900 	dce_dmcu_construct(
901 		dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
902 
903 	dmcu_dce->base.funcs = &dce_funcs;
904 
905 	return &dmcu_dce->base;
906 }
907 
908 #if defined(CONFIG_DRM_AMD_DC_DCN)
909 struct dmcu *dcn10_dmcu_create(
910 	struct dc_context *ctx,
911 	const struct dce_dmcu_registers *regs,
912 	const struct dce_dmcu_shift *dmcu_shift,
913 	const struct dce_dmcu_mask *dmcu_mask)
914 {
915 	struct dce_dmcu *dmcu_dce = kzalloc(sizeof(*dmcu_dce), GFP_KERNEL);
916 
917 	if (dmcu_dce == NULL) {
918 		BREAK_TO_DEBUGGER();
919 		return NULL;
920 	}
921 
922 	dce_dmcu_construct(
923 		dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
924 
925 	dmcu_dce->base.funcs = &dcn10_funcs;
926 
927 	return &dmcu_dce->base;
928 }
929 
930 struct dmcu *dcn20_dmcu_create(
931 	struct dc_context *ctx,
932 	const struct dce_dmcu_registers *regs,
933 	const struct dce_dmcu_shift *dmcu_shift,
934 	const struct dce_dmcu_mask *dmcu_mask)
935 {
936 	struct dce_dmcu *dmcu_dce = kzalloc(sizeof(*dmcu_dce), GFP_KERNEL);
937 
938 	if (dmcu_dce == NULL) {
939 		BREAK_TO_DEBUGGER();
940 		return NULL;
941 	}
942 
943 	dce_dmcu_construct(
944 		dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
945 
946 	dmcu_dce->base.funcs = &dcn20_funcs;
947 
948 	return &dmcu_dce->base;
949 }
950 
951 struct dmcu *dcn21_dmcu_create(
952 	struct dc_context *ctx,
953 	const struct dce_dmcu_registers *regs,
954 	const struct dce_dmcu_shift *dmcu_shift,
955 	const struct dce_dmcu_mask *dmcu_mask)
956 {
957 	struct dce_dmcu *dmcu_dce = kzalloc(sizeof(*dmcu_dce), GFP_KERNEL);
958 
959 	if (dmcu_dce == NULL) {
960 		BREAK_TO_DEBUGGER();
961 		return NULL;
962 	}
963 
964 	dcn21_dmcu_construct(
965 		dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
966 
967 	dmcu_dce->base.funcs = &dcn21_funcs;
968 
969 	return &dmcu_dce->base;
970 }
971 #endif
972 
973 void dce_dmcu_destroy(struct dmcu **dmcu)
974 {
975 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(*dmcu);
976 
977 	kfree(dmcu_dce);
978 	*dmcu = NULL;
979 }
980