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