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 "core_types.h"
27 #include "link_encoder.h"
28 #include "dce_dmcu.h"
29 #include "dm_services.h"
30 #include "reg_helper.h"
31 #include "fixed31_32.h"
32 #include "dc.h"
33 
34 #define TO_DCE_DMCU(dmcu)\
35 	container_of(dmcu, struct dce_dmcu, base)
36 
37 #define REG(reg) \
38 	(dmcu_dce->regs->reg)
39 
40 #undef FN
41 #define FN(reg_name, field_name) \
42 	dmcu_dce->dmcu_shift->field_name, dmcu_dce->dmcu_mask->field_name
43 
44 #define CTX \
45 	dmcu_dce->base.ctx
46 
47 /* PSR related commands */
48 #define PSR_ENABLE 0x20
49 #define PSR_EXIT 0x21
50 #define PSR_SET 0x23
51 #define PSR_SET_WAITLOOP 0x31
52 #define MCP_INIT_DMCU 0x88
53 #define MCP_INIT_IRAM 0x89
54 #define MCP_SYNC_PHY_LOCK 0x90
55 #define MCP_SYNC_PHY_UNLOCK 0x91
56 #define MCP_BL_SET_PWM_FRAC 0x6A  /* Enable or disable Fractional PWM */
57 #define MASTER_COMM_CNTL_REG__MASTER_COMM_INTERRUPT_MASK   0x00000001L
58 
59 static bool dce_dmcu_init(struct dmcu *dmcu)
60 {
61 	// Do nothing
62 	return true;
63 }
64 
65 bool dce_dmcu_load_iram(struct dmcu *dmcu,
66 		unsigned int start_offset,
67 		const char *src,
68 		unsigned int bytes)
69 {
70 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
71 	unsigned int count = 0;
72 
73 	/* Enable write access to IRAM */
74 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
75 			IRAM_HOST_ACCESS_EN, 1,
76 			IRAM_WR_ADDR_AUTO_INC, 1);
77 
78 	REG_WAIT(DCI_MEM_PWR_STATUS, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
79 
80 	REG_WRITE(DMCU_IRAM_WR_CTRL, start_offset);
81 
82 	for (count = 0; count < bytes; count++)
83 		REG_WRITE(DMCU_IRAM_WR_DATA, src[count]);
84 
85 	/* Disable write access to IRAM to allow dynamic sleep state */
86 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
87 			IRAM_HOST_ACCESS_EN, 0,
88 			IRAM_WR_ADDR_AUTO_INC, 0);
89 
90 	return true;
91 }
92 
93 static void dce_get_dmcu_psr_state(struct dmcu *dmcu, uint32_t *psr_state)
94 {
95 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
96 
97 	uint32_t psr_state_offset = 0xf0;
98 
99 	/* Enable write access to IRAM */
100 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 1);
101 
102 	REG_WAIT(DCI_MEM_PWR_STATUS, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
103 
104 	/* Write address to IRAM_RD_ADDR in DMCU_IRAM_RD_CTRL */
105 	REG_WRITE(DMCU_IRAM_RD_CTRL, psr_state_offset);
106 
107 	/* Read data from IRAM_RD_DATA in DMCU_IRAM_RD_DATA*/
108 	*psr_state = REG_READ(DMCU_IRAM_RD_DATA);
109 
110 	/* Disable write access to IRAM after finished using IRAM
111 	 * in order to allow dynamic sleep state
112 	 */
113 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 0);
114 }
115 
116 static void dce_dmcu_set_psr_enable(struct dmcu *dmcu, bool enable, bool wait)
117 {
118 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
119 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
120 	unsigned int dmcu_wait_reg_ready_interval = 100;
121 
122 	unsigned int retryCount;
123 	uint32_t psr_state = 0;
124 
125 	/* waitDMCUReadyForCmd */
126 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
127 				dmcu_wait_reg_ready_interval,
128 				dmcu_max_retry_on_wait_reg_ready);
129 
130 	/* setDMCUParam_Cmd */
131 	if (enable)
132 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
133 				PSR_ENABLE);
134 	else
135 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
136 				PSR_EXIT);
137 
138 	/* notifyDMCUMsg */
139 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
140 	if (wait == true) {
141 		for (retryCount = 0; retryCount <= 100; retryCount++) {
142 			dce_get_dmcu_psr_state(dmcu, &psr_state);
143 			if (enable) {
144 				if (psr_state != 0)
145 					break;
146 			} else {
147 				if (psr_state == 0)
148 					break;
149 			}
150 			udelay(10);
151 		}
152 	}
153 }
154 
155 static bool dce_dmcu_setup_psr(struct dmcu *dmcu,
156 		struct dc_link *link,
157 		struct psr_context *psr_context)
158 {
159 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
160 
161 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
162 	unsigned int dmcu_wait_reg_ready_interval = 100;
163 
164 	union dce_dmcu_psr_config_data_reg1 masterCmdData1;
165 	union dce_dmcu_psr_config_data_reg2 masterCmdData2;
166 	union dce_dmcu_psr_config_data_reg3 masterCmdData3;
167 
168 	link->link_enc->funcs->psr_program_dp_dphy_fast_training(link->link_enc,
169 			psr_context->psrExitLinkTrainingRequired);
170 
171 	/* Enable static screen interrupts for PSR supported display */
172 	/* Disable the interrupt coming from other displays. */
173 	REG_UPDATE_4(DMCU_INTERRUPT_TO_UC_EN_MASK,
174 			STATIC_SCREEN1_INT_TO_UC_EN, 0,
175 			STATIC_SCREEN2_INT_TO_UC_EN, 0,
176 			STATIC_SCREEN3_INT_TO_UC_EN, 0,
177 			STATIC_SCREEN4_INT_TO_UC_EN, 0);
178 
179 	switch (psr_context->controllerId) {
180 	/* Driver uses case 1 for unconfigured */
181 	case 1:
182 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
183 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
184 		break;
185 	case 2:
186 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
187 				STATIC_SCREEN2_INT_TO_UC_EN, 1);
188 		break;
189 	case 3:
190 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
191 				STATIC_SCREEN3_INT_TO_UC_EN, 1);
192 		break;
193 	case 4:
194 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
195 				STATIC_SCREEN4_INT_TO_UC_EN, 1);
196 		break;
197 	case 5:
198 		/* CZ/NL only has 4 CRTC!!
199 		 * really valid.
200 		 * There is no interrupt enable mask for these instances.
201 		 */
202 		break;
203 	case 6:
204 		/* CZ/NL only has 4 CRTC!!
205 		 * These are here because they are defined in HW regspec,
206 		 * but not really valid. There is no interrupt enable mask
207 		 * for these instances.
208 		 */
209 		break;
210 	default:
211 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
212 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
213 		break;
214 	}
215 
216 	link->link_enc->funcs->psr_program_secondary_packet(link->link_enc,
217 			psr_context->sdpTransmitLineNumDeadline);
218 
219 	/* waitDMCUReadyForCmd */
220 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
221 					dmcu_wait_reg_ready_interval,
222 					dmcu_max_retry_on_wait_reg_ready);
223 
224 	/* setDMCUParam_PSRHostConfigData */
225 	masterCmdData1.u32All = 0;
226 	masterCmdData1.bits.timehyst_frames = psr_context->timehyst_frames;
227 	masterCmdData1.bits.hyst_lines = psr_context->hyst_lines;
228 	masterCmdData1.bits.rfb_update_auto_en =
229 			psr_context->rfb_update_auto_en;
230 	masterCmdData1.bits.dp_port_num = psr_context->transmitterId;
231 	masterCmdData1.bits.dcp_sel = psr_context->controllerId;
232 	masterCmdData1.bits.phy_type  = psr_context->phyType;
233 	masterCmdData1.bits.frame_cap_ind =
234 			psr_context->psrFrameCaptureIndicationReq;
235 	masterCmdData1.bits.aux_chan = psr_context->channel;
236 	masterCmdData1.bits.aux_repeat = psr_context->aux_repeats;
237 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1),
238 					masterCmdData1.u32All);
239 
240 	masterCmdData2.u32All = 0;
241 	masterCmdData2.bits.dig_fe = psr_context->engineId;
242 	masterCmdData2.bits.dig_be = psr_context->transmitterId;
243 	masterCmdData2.bits.skip_wait_for_pll_lock =
244 			psr_context->skipPsrWaitForPllLock;
245 	masterCmdData2.bits.frame_delay = psr_context->frame_delay;
246 	masterCmdData2.bits.smu_phy_id = psr_context->smuPhyId;
247 	masterCmdData2.bits.num_of_controllers =
248 			psr_context->numberOfControllers;
249 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG2),
250 			masterCmdData2.u32All);
251 
252 	masterCmdData3.u32All = 0;
253 	masterCmdData3.bits.psr_level = psr_context->psr_level.u32all;
254 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG3),
255 			masterCmdData3.u32All);
256 
257 	/* setDMCUParam_Cmd */
258 	REG_UPDATE(MASTER_COMM_CMD_REG,
259 			MASTER_COMM_CMD_REG_BYTE0, PSR_SET);
260 
261 	/* notifyDMCUMsg */
262 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
263 
264 	return true;
265 }
266 
267 static bool dce_is_dmcu_initialized(struct dmcu *dmcu)
268 {
269 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
270 	unsigned int dmcu_uc_reset;
271 
272 	/* microcontroller is not running */
273 	REG_GET(DMCU_STATUS, UC_IN_RESET, &dmcu_uc_reset);
274 
275 	/* DMCU is not running */
276 	if (dmcu_uc_reset)
277 		return false;
278 
279 	return true;
280 }
281 
282 static void dce_psr_wait_loop(
283 	struct dmcu *dmcu,
284 	unsigned int wait_loop_number)
285 {
286 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
287 	union dce_dmcu_psr_config_data_wait_loop_reg1 masterCmdData1;
288 
289 	if (dmcu->cached_wait_loop_number == wait_loop_number)
290 		return;
291 
292 	/* DMCU is not running */
293 	if (!dce_is_dmcu_initialized(dmcu))
294 		return;
295 
296 	/* waitDMCUReadyForCmd */
297 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
298 
299 	masterCmdData1.u32 = 0;
300 	masterCmdData1.bits.wait_loop = wait_loop_number;
301 	dmcu->cached_wait_loop_number = wait_loop_number;
302 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1), masterCmdData1.u32);
303 
304 	/* setDMCUParam_Cmd */
305 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, PSR_SET_WAITLOOP);
306 
307 	/* notifyDMCUMsg */
308 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
309 }
310 
311 static void dce_get_psr_wait_loop(
312 		struct dmcu *dmcu, unsigned int *psr_wait_loop_number)
313 {
314 	*psr_wait_loop_number = dmcu->cached_wait_loop_number;
315 	return;
316 }
317 
318 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
319 static void dcn10_get_dmcu_version(struct dmcu *dmcu)
320 {
321 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
322 	uint32_t dmcu_version_offset = 0xf1;
323 
324 	/* Enable write access to IRAM */
325 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
326 			IRAM_HOST_ACCESS_EN, 1,
327 			IRAM_RD_ADDR_AUTO_INC, 1);
328 
329 	REG_WAIT(DMU_MEM_PWR_CNTL, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
330 
331 	/* Write address to IRAM_RD_ADDR and read from DATA register */
332 	REG_WRITE(DMCU_IRAM_RD_CTRL, dmcu_version_offset);
333 	dmcu->dmcu_version.interface_version = REG_READ(DMCU_IRAM_RD_DATA);
334 	dmcu->dmcu_version.abm_version = REG_READ(DMCU_IRAM_RD_DATA);
335 	dmcu->dmcu_version.psr_version = REG_READ(DMCU_IRAM_RD_DATA);
336 	dmcu->dmcu_version.build_version = ((REG_READ(DMCU_IRAM_RD_DATA) << 8) |
337 						REG_READ(DMCU_IRAM_RD_DATA));
338 
339 	/* Disable write access to IRAM to allow dynamic sleep state */
340 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
341 			IRAM_HOST_ACCESS_EN, 0,
342 			IRAM_RD_ADDR_AUTO_INC, 0);
343 }
344 
345 static void dcn10_dmcu_enable_fractional_pwm(struct dmcu *dmcu,
346 		uint32_t fractional_pwm)
347 {
348 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
349 
350 	/* Wait until microcontroller is ready to process interrupt */
351 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
352 
353 	/* Set PWM fractional enable/disable */
354 	REG_WRITE(MASTER_COMM_DATA_REG1, fractional_pwm);
355 
356 	/* Set command to enable or disable fractional PWM microcontroller */
357 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
358 			MCP_BL_SET_PWM_FRAC);
359 
360 	/* Notify microcontroller of new command */
361 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
362 
363 	/* Ensure command has been executed before continuing */
364 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
365 }
366 
367 static bool dcn10_dmcu_init(struct dmcu *dmcu)
368 {
369 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
370 	const struct dc_config *config = &dmcu->ctx->dc->config;
371 	bool status = false;
372 
373 	/*  Definition of DC_DMCU_SCRATCH
374 	 *  0 : firmare not loaded
375 	 *  1 : PSP load DMCU FW but not initialized
376 	 *  2 : Firmware already initialized
377 	 */
378 	dmcu->dmcu_state = REG_READ(DC_DMCU_SCRATCH);
379 
380 	switch (dmcu->dmcu_state) {
381 	case DMCU_UNLOADED:
382 		status = false;
383 		break;
384 	case DMCU_LOADED_UNINITIALIZED:
385 		/* Wait until microcontroller is ready to process interrupt */
386 		REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
387 
388 		/* Set initialized ramping boundary value */
389 		REG_WRITE(MASTER_COMM_DATA_REG1, 0xFFFF);
390 
391 		/* Set backlight ramping stepsize */
392 		REG_WRITE(MASTER_COMM_DATA_REG2, abm_gain_stepsize);
393 
394 		/* Set command to initialize microcontroller */
395 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
396 			MCP_INIT_DMCU);
397 
398 		/* Notify microcontroller of new command */
399 		REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
400 
401 		/* Ensure command has been executed before continuing */
402 		REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
403 
404 		// Check state is initialized
405 		dmcu->dmcu_state = REG_READ(DC_DMCU_SCRATCH);
406 
407 		// If microcontroller is not in running state, fail
408 		if (dmcu->dmcu_state == DMCU_RUNNING) {
409 			/* Retrieve and cache the DMCU firmware version. */
410 			dcn10_get_dmcu_version(dmcu);
411 
412 			/* Initialize DMCU to use fractional PWM or not */
413 			dcn10_dmcu_enable_fractional_pwm(dmcu,
414 				(config->disable_fractional_pwm == false) ? 1 : 0);
415 			status = true;
416 		} else {
417 			status = false;
418 		}
419 
420 		break;
421 	case DMCU_RUNNING:
422 		status = true;
423 		break;
424 	default:
425 		status = false;
426 		break;
427 	}
428 
429 	return status;
430 }
431 
432 
433 static bool dcn10_dmcu_load_iram(struct dmcu *dmcu,
434 		unsigned int start_offset,
435 		const char *src,
436 		unsigned int bytes)
437 {
438 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
439 	unsigned int count = 0;
440 
441 	/* If microcontroller is not running, do nothing */
442 	if (dmcu->dmcu_state != DMCU_RUNNING)
443 		return false;
444 
445 	/* Enable write access to IRAM */
446 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
447 			IRAM_HOST_ACCESS_EN, 1,
448 			IRAM_WR_ADDR_AUTO_INC, 1);
449 
450 	REG_WAIT(DMU_MEM_PWR_CNTL, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
451 
452 	REG_WRITE(DMCU_IRAM_WR_CTRL, start_offset);
453 
454 	for (count = 0; count < bytes; count++)
455 		REG_WRITE(DMCU_IRAM_WR_DATA, src[count]);
456 
457 	/* Disable write access to IRAM to allow dynamic sleep state */
458 	REG_UPDATE_2(DMCU_RAM_ACCESS_CTRL,
459 			IRAM_HOST_ACCESS_EN, 0,
460 			IRAM_WR_ADDR_AUTO_INC, 0);
461 
462 	/* Wait until microcontroller is ready to process interrupt */
463 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
464 
465 	/* Set command to signal IRAM is loaded and to initialize IRAM */
466 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
467 			MCP_INIT_IRAM);
468 
469 	/* Notify microcontroller of new command */
470 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
471 
472 	/* Ensure command has been executed before continuing */
473 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 100, 800);
474 
475 	return true;
476 }
477 
478 static void dcn10_get_dmcu_psr_state(struct dmcu *dmcu, uint32_t *psr_state)
479 {
480 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
481 
482 	uint32_t psr_state_offset = 0xf0;
483 
484 	/* If microcontroller is not running, do nothing */
485 	if (dmcu->dmcu_state != DMCU_RUNNING)
486 		return;
487 
488 	/* Enable write access to IRAM */
489 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 1);
490 
491 	REG_WAIT(DMU_MEM_PWR_CNTL, DMCU_IRAM_MEM_PWR_STATE, 0, 2, 10);
492 
493 	/* Write address to IRAM_RD_ADDR in DMCU_IRAM_RD_CTRL */
494 	REG_WRITE(DMCU_IRAM_RD_CTRL, psr_state_offset);
495 
496 	/* Read data from IRAM_RD_DATA in DMCU_IRAM_RD_DATA*/
497 	*psr_state = REG_READ(DMCU_IRAM_RD_DATA);
498 
499 	/* Disable write access to IRAM after finished using IRAM
500 	 * in order to allow dynamic sleep state
501 	 */
502 	REG_UPDATE(DMCU_RAM_ACCESS_CTRL, IRAM_HOST_ACCESS_EN, 0);
503 }
504 
505 static void dcn10_dmcu_set_psr_enable(struct dmcu *dmcu, bool enable, bool wait)
506 {
507 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
508 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
509 	unsigned int dmcu_wait_reg_ready_interval = 100;
510 
511 	unsigned int retryCount;
512 	uint32_t psr_state = 0;
513 
514 	/* If microcontroller is not running, do nothing */
515 	if (dmcu->dmcu_state != DMCU_RUNNING)
516 		return;
517 
518 	dcn10_get_dmcu_psr_state(dmcu, &psr_state);
519 	if (psr_state == 0 && !enable)
520 		return;
521 	/* waitDMCUReadyForCmd */
522 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
523 				dmcu_wait_reg_ready_interval,
524 				dmcu_max_retry_on_wait_reg_ready);
525 
526 	/* setDMCUParam_Cmd */
527 	if (enable)
528 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
529 				PSR_ENABLE);
530 	else
531 		REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0,
532 				PSR_EXIT);
533 
534 	/* notifyDMCUMsg */
535 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
536 
537 	/* Below loops 1000 x 500us = 500 ms.
538 	 *  Exit PSR may need to wait 1-2 frames to power up. Timeout after at
539 	 *  least a few frames. Should never hit the max retry assert below.
540 	 */
541 	if (wait == true) {
542 		for (retryCount = 0; retryCount <= 1000; retryCount++) {
543 			dcn10_get_dmcu_psr_state(dmcu, &psr_state);
544 			if (enable) {
545 				if (psr_state != 0)
546 					break;
547 			} else {
548 				if (psr_state == 0)
549 					break;
550 			}
551 			udelay(500);
552 		}
553 
554 		/* assert if max retry hit */
555 		if (retryCount >= 1000)
556 			ASSERT(0);
557 	}
558 }
559 
560 static bool dcn10_dmcu_setup_psr(struct dmcu *dmcu,
561 		struct dc_link *link,
562 		struct psr_context *psr_context)
563 {
564 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
565 
566 	unsigned int dmcu_max_retry_on_wait_reg_ready = 801;
567 	unsigned int dmcu_wait_reg_ready_interval = 100;
568 
569 	union dce_dmcu_psr_config_data_reg1 masterCmdData1;
570 	union dce_dmcu_psr_config_data_reg2 masterCmdData2;
571 	union dce_dmcu_psr_config_data_reg3 masterCmdData3;
572 
573 	/* If microcontroller is not running, do nothing */
574 	if (dmcu->dmcu_state != DMCU_RUNNING)
575 		return false;
576 
577 	link->link_enc->funcs->psr_program_dp_dphy_fast_training(link->link_enc,
578 			psr_context->psrExitLinkTrainingRequired);
579 
580 	/* Enable static screen interrupts for PSR supported display */
581 	/* Disable the interrupt coming from other displays. */
582 	REG_UPDATE_4(DMCU_INTERRUPT_TO_UC_EN_MASK,
583 			STATIC_SCREEN1_INT_TO_UC_EN, 0,
584 			STATIC_SCREEN2_INT_TO_UC_EN, 0,
585 			STATIC_SCREEN3_INT_TO_UC_EN, 0,
586 			STATIC_SCREEN4_INT_TO_UC_EN, 0);
587 
588 	switch (psr_context->controllerId) {
589 	/* Driver uses case 1 for unconfigured */
590 	case 1:
591 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
592 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
593 		break;
594 	case 2:
595 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
596 				STATIC_SCREEN2_INT_TO_UC_EN, 1);
597 		break;
598 	case 3:
599 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
600 				STATIC_SCREEN3_INT_TO_UC_EN, 1);
601 		break;
602 	case 4:
603 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
604 				STATIC_SCREEN4_INT_TO_UC_EN, 1);
605 		break;
606 	case 5:
607 		/* CZ/NL only has 4 CRTC!!
608 		 * really valid.
609 		 * There is no interrupt enable mask for these instances.
610 		 */
611 		break;
612 	case 6:
613 		/* CZ/NL only has 4 CRTC!!
614 		 * These are here because they are defined in HW regspec,
615 		 * but not really valid. There is no interrupt enable mask
616 		 * for these instances.
617 		 */
618 		break;
619 	default:
620 		REG_UPDATE(DMCU_INTERRUPT_TO_UC_EN_MASK,
621 				STATIC_SCREEN1_INT_TO_UC_EN, 1);
622 		break;
623 	}
624 
625 	link->link_enc->funcs->psr_program_secondary_packet(link->link_enc,
626 			psr_context->sdpTransmitLineNumDeadline);
627 
628 	if (psr_context->allow_smu_optimizations)
629 		REG_UPDATE(SMU_INTERRUPT_CONTROL, DC_SMU_INT_ENABLE, 1);
630 
631 	/* waitDMCUReadyForCmd */
632 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
633 			dmcu_wait_reg_ready_interval,
634 			dmcu_max_retry_on_wait_reg_ready);
635 
636 	/* setDMCUParam_PSRHostConfigData */
637 	masterCmdData1.u32All = 0;
638 	masterCmdData1.bits.timehyst_frames = psr_context->timehyst_frames;
639 	masterCmdData1.bits.hyst_lines = psr_context->hyst_lines;
640 	masterCmdData1.bits.rfb_update_auto_en =
641 			psr_context->rfb_update_auto_en;
642 	masterCmdData1.bits.dp_port_num = psr_context->transmitterId;
643 	masterCmdData1.bits.dcp_sel = psr_context->controllerId;
644 	masterCmdData1.bits.phy_type  = psr_context->phyType;
645 	masterCmdData1.bits.frame_cap_ind =
646 			psr_context->psrFrameCaptureIndicationReq;
647 	masterCmdData1.bits.aux_chan = psr_context->channel;
648 	masterCmdData1.bits.aux_repeat = psr_context->aux_repeats;
649 	masterCmdData1.bits.allow_smu_optimizations = psr_context->allow_smu_optimizations;
650 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1),
651 					masterCmdData1.u32All);
652 
653 	masterCmdData2.u32All = 0;
654 	masterCmdData2.bits.dig_fe = psr_context->engineId;
655 	masterCmdData2.bits.dig_be = psr_context->transmitterId;
656 	masterCmdData2.bits.skip_wait_for_pll_lock =
657 			psr_context->skipPsrWaitForPllLock;
658 	masterCmdData2.bits.frame_delay = psr_context->frame_delay;
659 	masterCmdData2.bits.smu_phy_id = psr_context->smuPhyId;
660 	masterCmdData2.bits.num_of_controllers =
661 			psr_context->numberOfControllers;
662 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG2),
663 			masterCmdData2.u32All);
664 
665 	masterCmdData3.u32All = 0;
666 	masterCmdData3.bits.psr_level = psr_context->psr_level.u32all;
667 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG3),
668 			masterCmdData3.u32All);
669 
670 
671 	/* setDMCUParam_Cmd */
672 	REG_UPDATE(MASTER_COMM_CMD_REG,
673 			MASTER_COMM_CMD_REG_BYTE0, PSR_SET);
674 
675 	/* notifyDMCUMsg */
676 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
677 
678 	/* waitDMCUReadyForCmd */
679 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
680 
681 	return true;
682 }
683 
684 static void dcn10_psr_wait_loop(
685 	struct dmcu *dmcu,
686 	unsigned int wait_loop_number)
687 {
688 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(dmcu);
689 	union dce_dmcu_psr_config_data_wait_loop_reg1 masterCmdData1;
690 
691 	/* If microcontroller is not running, do nothing */
692 	if (dmcu->dmcu_state != DMCU_RUNNING)
693 		return;
694 
695 	if (wait_loop_number != 0) {
696 	/* waitDMCUReadyForCmd */
697 	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 1, 10000);
698 
699 	masterCmdData1.u32 = 0;
700 	masterCmdData1.bits.wait_loop = wait_loop_number;
701 	dmcu->cached_wait_loop_number = wait_loop_number;
702 	dm_write_reg(dmcu->ctx, REG(MASTER_COMM_DATA_REG1), masterCmdData1.u32);
703 
704 	/* setDMCUParam_Cmd */
705 	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, PSR_SET_WAITLOOP);
706 
707 	/* notifyDMCUMsg */
708 	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
709 	}
710 }
711 
712 static void dcn10_get_psr_wait_loop(
713 		struct dmcu *dmcu, unsigned int *psr_wait_loop_number)
714 {
715 	*psr_wait_loop_number = dmcu->cached_wait_loop_number;
716 	return;
717 }
718 
719 static bool dcn10_is_dmcu_initialized(struct dmcu *dmcu)
720 {
721 	/* microcontroller is not running */
722 	if (dmcu->dmcu_state != DMCU_RUNNING)
723 		return false;
724 	return true;
725 }
726 
727 #endif //(CONFIG_DRM_AMD_DC_DCN1_0)
728 
729 static const struct dmcu_funcs dce_funcs = {
730 	.dmcu_init = dce_dmcu_init,
731 	.load_iram = dce_dmcu_load_iram,
732 	.set_psr_enable = dce_dmcu_set_psr_enable,
733 	.setup_psr = dce_dmcu_setup_psr,
734 	.get_psr_state = dce_get_dmcu_psr_state,
735 	.set_psr_wait_loop = dce_psr_wait_loop,
736 	.get_psr_wait_loop = dce_get_psr_wait_loop,
737 	.is_dmcu_initialized = dce_is_dmcu_initialized
738 };
739 
740 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
741 static const struct dmcu_funcs dcn10_funcs = {
742 	.dmcu_init = dcn10_dmcu_init,
743 	.load_iram = dcn10_dmcu_load_iram,
744 	.set_psr_enable = dcn10_dmcu_set_psr_enable,
745 	.setup_psr = dcn10_dmcu_setup_psr,
746 	.get_psr_state = dcn10_get_dmcu_psr_state,
747 	.set_psr_wait_loop = dcn10_psr_wait_loop,
748 	.get_psr_wait_loop = dcn10_get_psr_wait_loop,
749 	.is_dmcu_initialized = dcn10_is_dmcu_initialized
750 };
751 #endif
752 
753 static void dce_dmcu_construct(
754 	struct dce_dmcu *dmcu_dce,
755 	struct dc_context *ctx,
756 	const struct dce_dmcu_registers *regs,
757 	const struct dce_dmcu_shift *dmcu_shift,
758 	const struct dce_dmcu_mask *dmcu_mask)
759 {
760 	struct dmcu *base = &dmcu_dce->base;
761 
762 	base->ctx = ctx;
763 	base->funcs = &dce_funcs;
764 	base->cached_wait_loop_number = 0;
765 
766 	dmcu_dce->regs = regs;
767 	dmcu_dce->dmcu_shift = dmcu_shift;
768 	dmcu_dce->dmcu_mask = dmcu_mask;
769 }
770 
771 struct dmcu *dce_dmcu_create(
772 	struct dc_context *ctx,
773 	const struct dce_dmcu_registers *regs,
774 	const struct dce_dmcu_shift *dmcu_shift,
775 	const struct dce_dmcu_mask *dmcu_mask)
776 {
777 	struct dce_dmcu *dmcu_dce = kzalloc(sizeof(*dmcu_dce), GFP_KERNEL);
778 
779 	if (dmcu_dce == NULL) {
780 		BREAK_TO_DEBUGGER();
781 		return NULL;
782 	}
783 
784 	dce_dmcu_construct(
785 		dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
786 
787 	dmcu_dce->base.funcs = &dce_funcs;
788 
789 	return &dmcu_dce->base;
790 }
791 
792 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
793 struct dmcu *dcn10_dmcu_create(
794 	struct dc_context *ctx,
795 	const struct dce_dmcu_registers *regs,
796 	const struct dce_dmcu_shift *dmcu_shift,
797 	const struct dce_dmcu_mask *dmcu_mask)
798 {
799 	struct dce_dmcu *dmcu_dce = kzalloc(sizeof(*dmcu_dce), GFP_KERNEL);
800 
801 	if (dmcu_dce == NULL) {
802 		BREAK_TO_DEBUGGER();
803 		return NULL;
804 	}
805 
806 	dce_dmcu_construct(
807 		dmcu_dce, ctx, regs, dmcu_shift, dmcu_mask);
808 
809 	dmcu_dce->base.funcs = &dcn10_funcs;
810 
811 	return &dmcu_dce->base;
812 }
813 #endif
814 
815 void dce_dmcu_destroy(struct dmcu **dmcu)
816 {
817 	struct dce_dmcu *dmcu_dce = TO_DCE_DMCU(*dmcu);
818 
819 	if (dmcu_dce->base.dmcu_state == DMCU_RUNNING)
820 		dmcu_dce->base.funcs->set_psr_enable(*dmcu, false, true);
821 
822 	kfree(dmcu_dce);
823 	*dmcu = NULL;
824 }
825