xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/dce100/dce100_resource.c (revision 5ef12cb4a3a78ffb331c03a795a15eea4ae35155)
1 /*
2  * Copyright 2012-15 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 #include "dm_services.h"
26 
27 #include "link_encoder.h"
28 #include "stream_encoder.h"
29 
30 #include "resource.h"
31 #include "include/irq_service_interface.h"
32 #include "../virtual/virtual_stream_encoder.h"
33 #include "dce110/dce110_resource.h"
34 #include "dce110/dce110_timing_generator.h"
35 #include "irq/dce110/irq_service_dce110.h"
36 #include "dce/dce_link_encoder.h"
37 #include "dce/dce_stream_encoder.h"
38 
39 #include "dce/dce_mem_input.h"
40 #include "dce/dce_ipp.h"
41 #include "dce/dce_transform.h"
42 #include "dce/dce_opp.h"
43 #include "dce/dce_clocks.h"
44 #include "dce/dce_clock_source.h"
45 #include "dce/dce_audio.h"
46 #include "dce/dce_hwseq.h"
47 #include "dce100/dce100_hw_sequencer.h"
48 
49 #include "reg_helper.h"
50 
51 #include "dce/dce_10_0_d.h"
52 #include "dce/dce_10_0_sh_mask.h"
53 
54 #include "dce/dce_dmcu.h"
55 #include "dce/dce_abm.h"
56 
57 #ifndef mmMC_HUB_RDREQ_DMIF_LIMIT
58 #include "gmc/gmc_8_2_d.h"
59 #include "gmc/gmc_8_2_sh_mask.h"
60 #endif
61 
62 #ifndef mmDP_DPHY_INTERNAL_CTRL
63 	#define mmDP_DPHY_INTERNAL_CTRL 0x4aa7
64 	#define mmDP0_DP_DPHY_INTERNAL_CTRL 0x4aa7
65 	#define mmDP1_DP_DPHY_INTERNAL_CTRL 0x4ba7
66 	#define mmDP2_DP_DPHY_INTERNAL_CTRL 0x4ca7
67 	#define mmDP3_DP_DPHY_INTERNAL_CTRL 0x4da7
68 	#define mmDP4_DP_DPHY_INTERNAL_CTRL 0x4ea7
69 	#define mmDP5_DP_DPHY_INTERNAL_CTRL 0x4fa7
70 	#define mmDP6_DP_DPHY_INTERNAL_CTRL 0x54a7
71 	#define mmDP7_DP_DPHY_INTERNAL_CTRL 0x56a7
72 	#define mmDP8_DP_DPHY_INTERNAL_CTRL 0x57a7
73 #endif
74 
75 #ifndef mmBIOS_SCRATCH_2
76 	#define mmBIOS_SCRATCH_2 0x05CB
77 	#define mmBIOS_SCRATCH_6 0x05CF
78 #endif
79 
80 #ifndef mmDP_DPHY_BS_SR_SWAP_CNTL
81 	#define mmDP_DPHY_BS_SR_SWAP_CNTL                       0x4ADC
82 	#define mmDP0_DP_DPHY_BS_SR_SWAP_CNTL                   0x4ADC
83 	#define mmDP1_DP_DPHY_BS_SR_SWAP_CNTL                   0x4BDC
84 	#define mmDP2_DP_DPHY_BS_SR_SWAP_CNTL                   0x4CDC
85 	#define mmDP3_DP_DPHY_BS_SR_SWAP_CNTL                   0x4DDC
86 	#define mmDP4_DP_DPHY_BS_SR_SWAP_CNTL                   0x4EDC
87 	#define mmDP5_DP_DPHY_BS_SR_SWAP_CNTL                   0x4FDC
88 	#define mmDP6_DP_DPHY_BS_SR_SWAP_CNTL                   0x54DC
89 #endif
90 
91 #ifndef mmDP_DPHY_FAST_TRAINING
92 	#define mmDP_DPHY_FAST_TRAINING                         0x4ABC
93 	#define mmDP0_DP_DPHY_FAST_TRAINING                     0x4ABC
94 	#define mmDP1_DP_DPHY_FAST_TRAINING                     0x4BBC
95 	#define mmDP2_DP_DPHY_FAST_TRAINING                     0x4CBC
96 	#define mmDP3_DP_DPHY_FAST_TRAINING                     0x4DBC
97 	#define mmDP4_DP_DPHY_FAST_TRAINING                     0x4EBC
98 	#define mmDP5_DP_DPHY_FAST_TRAINING                     0x4FBC
99 	#define mmDP6_DP_DPHY_FAST_TRAINING                     0x54BC
100 #endif
101 
102 static const struct dce110_timing_generator_offsets dce100_tg_offsets[] = {
103 	{
104 		.crtc = (mmCRTC0_CRTC_CONTROL - mmCRTC_CONTROL),
105 		.dcp =  (mmDCP0_GRPH_CONTROL - mmGRPH_CONTROL),
106 	},
107 	{
108 		.crtc = (mmCRTC1_CRTC_CONTROL - mmCRTC_CONTROL),
109 		.dcp = (mmDCP1_GRPH_CONTROL - mmGRPH_CONTROL),
110 	},
111 	{
112 		.crtc = (mmCRTC2_CRTC_CONTROL - mmCRTC_CONTROL),
113 		.dcp = (mmDCP2_GRPH_CONTROL - mmGRPH_CONTROL),
114 	},
115 	{
116 		.crtc = (mmCRTC3_CRTC_CONTROL - mmCRTC_CONTROL),
117 		.dcp =  (mmDCP3_GRPH_CONTROL - mmGRPH_CONTROL),
118 	},
119 	{
120 		.crtc = (mmCRTC4_CRTC_CONTROL - mmCRTC_CONTROL),
121 		.dcp = (mmDCP4_GRPH_CONTROL - mmGRPH_CONTROL),
122 	},
123 	{
124 		.crtc = (mmCRTC5_CRTC_CONTROL - mmCRTC_CONTROL),
125 		.dcp = (mmDCP5_GRPH_CONTROL - mmGRPH_CONTROL),
126 	}
127 };
128 
129 /* set register offset */
130 #define SR(reg_name)\
131 	.reg_name = mm ## reg_name
132 
133 /* set register offset with instance */
134 #define SRI(reg_name, block, id)\
135 	.reg_name = mm ## block ## id ## _ ## reg_name
136 
137 
138 static const struct dce_disp_clk_registers disp_clk_regs = {
139 		CLK_COMMON_REG_LIST_DCE_BASE()
140 };
141 
142 static const struct dce_disp_clk_shift disp_clk_shift = {
143 		CLK_COMMON_MASK_SH_LIST_DCE_COMMON_BASE(__SHIFT)
144 };
145 
146 static const struct dce_disp_clk_mask disp_clk_mask = {
147 		CLK_COMMON_MASK_SH_LIST_DCE_COMMON_BASE(_MASK)
148 };
149 
150 #define ipp_regs(id)\
151 [id] = {\
152 		IPP_DCE100_REG_LIST_DCE_BASE(id)\
153 }
154 
155 static const struct dce_ipp_registers ipp_regs[] = {
156 		ipp_regs(0),
157 		ipp_regs(1),
158 		ipp_regs(2),
159 		ipp_regs(3),
160 		ipp_regs(4),
161 		ipp_regs(5)
162 };
163 
164 static const struct dce_ipp_shift ipp_shift = {
165 		IPP_DCE100_MASK_SH_LIST_DCE_COMMON_BASE(__SHIFT)
166 };
167 
168 static const struct dce_ipp_mask ipp_mask = {
169 		IPP_DCE100_MASK_SH_LIST_DCE_COMMON_BASE(_MASK)
170 };
171 
172 #define transform_regs(id)\
173 [id] = {\
174 		XFM_COMMON_REG_LIST_DCE100(id)\
175 }
176 
177 static const struct dce_transform_registers xfm_regs[] = {
178 		transform_regs(0),
179 		transform_regs(1),
180 		transform_regs(2),
181 		transform_regs(3),
182 		transform_regs(4),
183 		transform_regs(5)
184 };
185 
186 static const struct dce_transform_shift xfm_shift = {
187 		XFM_COMMON_MASK_SH_LIST_DCE110(__SHIFT)
188 };
189 
190 static const struct dce_transform_mask xfm_mask = {
191 		XFM_COMMON_MASK_SH_LIST_DCE110(_MASK)
192 };
193 
194 #define aux_regs(id)\
195 [id] = {\
196 	AUX_REG_LIST(id)\
197 }
198 
199 static const struct dce110_link_enc_aux_registers link_enc_aux_regs[] = {
200 		aux_regs(0),
201 		aux_regs(1),
202 		aux_regs(2),
203 		aux_regs(3),
204 		aux_regs(4),
205 		aux_regs(5)
206 };
207 
208 #define hpd_regs(id)\
209 [id] = {\
210 	HPD_REG_LIST(id)\
211 }
212 
213 static const struct dce110_link_enc_hpd_registers link_enc_hpd_regs[] = {
214 		hpd_regs(0),
215 		hpd_regs(1),
216 		hpd_regs(2),
217 		hpd_regs(3),
218 		hpd_regs(4),
219 		hpd_regs(5)
220 };
221 
222 #define link_regs(id)\
223 [id] = {\
224 	LE_DCE100_REG_LIST(id)\
225 }
226 
227 static const struct dce110_link_enc_registers link_enc_regs[] = {
228 	link_regs(0),
229 	link_regs(1),
230 	link_regs(2),
231 	link_regs(3),
232 	link_regs(4),
233 	link_regs(5),
234 	link_regs(6),
235 };
236 
237 #define stream_enc_regs(id)\
238 [id] = {\
239 	SE_COMMON_REG_LIST_DCE_BASE(id),\
240 	.AFMT_CNTL = 0,\
241 }
242 
243 static const struct dce110_stream_enc_registers stream_enc_regs[] = {
244 	stream_enc_regs(0),
245 	stream_enc_regs(1),
246 	stream_enc_regs(2),
247 	stream_enc_regs(3),
248 	stream_enc_regs(4),
249 	stream_enc_regs(5),
250 	stream_enc_regs(6)
251 };
252 
253 static const struct dce_stream_encoder_shift se_shift = {
254 		SE_COMMON_MASK_SH_LIST_DCE80_100(__SHIFT)
255 };
256 
257 static const struct dce_stream_encoder_mask se_mask = {
258 		SE_COMMON_MASK_SH_LIST_DCE80_100(_MASK)
259 };
260 
261 #define opp_regs(id)\
262 [id] = {\
263 	OPP_DCE_100_REG_LIST(id),\
264 }
265 
266 static const struct dce_opp_registers opp_regs[] = {
267 	opp_regs(0),
268 	opp_regs(1),
269 	opp_regs(2),
270 	opp_regs(3),
271 	opp_regs(4),
272 	opp_regs(5)
273 };
274 
275 static const struct dce_opp_shift opp_shift = {
276 	OPP_COMMON_MASK_SH_LIST_DCE_100(__SHIFT)
277 };
278 
279 static const struct dce_opp_mask opp_mask = {
280 	OPP_COMMON_MASK_SH_LIST_DCE_100(_MASK)
281 };
282 
283 
284 #define audio_regs(id)\
285 [id] = {\
286 	AUD_COMMON_REG_LIST(id)\
287 }
288 
289 static const struct dce_audio_registers audio_regs[] = {
290 	audio_regs(0),
291 	audio_regs(1),
292 	audio_regs(2),
293 	audio_regs(3),
294 	audio_regs(4),
295 	audio_regs(5),
296 	audio_regs(6),
297 };
298 
299 static const struct dce_audio_shift audio_shift = {
300 		AUD_COMMON_MASK_SH_LIST(__SHIFT)
301 };
302 
303 static const struct dce_aduio_mask audio_mask = {
304 		AUD_COMMON_MASK_SH_LIST(_MASK)
305 };
306 
307 #define clk_src_regs(id)\
308 [id] = {\
309 	CS_COMMON_REG_LIST_DCE_100_110(id),\
310 }
311 
312 static const struct dce110_clk_src_regs clk_src_regs[] = {
313 	clk_src_regs(0),
314 	clk_src_regs(1),
315 	clk_src_regs(2)
316 };
317 
318 static const struct dce110_clk_src_shift cs_shift = {
319 		CS_COMMON_MASK_SH_LIST_DCE_COMMON_BASE(__SHIFT)
320 };
321 
322 static const struct dce110_clk_src_mask cs_mask = {
323 		CS_COMMON_MASK_SH_LIST_DCE_COMMON_BASE(_MASK)
324 };
325 
326 static const struct dce_dmcu_registers dmcu_regs = {
327 		DMCU_DCE110_COMMON_REG_LIST()
328 };
329 
330 static const struct dce_dmcu_shift dmcu_shift = {
331 		DMCU_MASK_SH_LIST_DCE110(__SHIFT)
332 };
333 
334 static const struct dce_dmcu_mask dmcu_mask = {
335 		DMCU_MASK_SH_LIST_DCE110(_MASK)
336 };
337 
338 static const struct dce_abm_registers abm_regs = {
339 		ABM_DCE110_COMMON_REG_LIST()
340 };
341 
342 static const struct dce_abm_shift abm_shift = {
343 		ABM_MASK_SH_LIST_DCE110(__SHIFT)
344 };
345 
346 static const struct dce_abm_mask abm_mask = {
347 		ABM_MASK_SH_LIST_DCE110(_MASK)
348 };
349 
350 #define DCFE_MEM_PWR_CTRL_REG_BASE 0x1b03
351 
352 static const struct bios_registers bios_regs = {
353 	.BIOS_SCRATCH_6 = mmBIOS_SCRATCH_6
354 };
355 
356 static const struct resource_caps res_cap = {
357 	.num_timing_generator = 6,
358 	.num_audio = 6,
359 	.num_stream_encoder = 6,
360 	.num_pll = 3
361 };
362 
363 #define CTX  ctx
364 #define REG(reg) mm ## reg
365 
366 #ifndef mmCC_DC_HDMI_STRAPS
367 #define mmCC_DC_HDMI_STRAPS 0x1918
368 #define CC_DC_HDMI_STRAPS__HDMI_DISABLE_MASK 0x40
369 #define CC_DC_HDMI_STRAPS__HDMI_DISABLE__SHIFT 0x6
370 #define CC_DC_HDMI_STRAPS__AUDIO_STREAM_NUMBER_MASK 0x700
371 #define CC_DC_HDMI_STRAPS__AUDIO_STREAM_NUMBER__SHIFT 0x8
372 #endif
373 
374 static void read_dce_straps(
375 	struct dc_context *ctx,
376 	struct resource_straps *straps)
377 {
378 	REG_GET_2(CC_DC_HDMI_STRAPS,
379 			HDMI_DISABLE, &straps->hdmi_disable,
380 			AUDIO_STREAM_NUMBER, &straps->audio_stream_number);
381 
382 	REG_GET(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO, &straps->dc_pinstraps_audio);
383 }
384 
385 static struct audio *create_audio(
386 		struct dc_context *ctx, unsigned int inst)
387 {
388 	return dce_audio_create(ctx, inst,
389 			&audio_regs[inst], &audio_shift, &audio_mask);
390 }
391 
392 static struct timing_generator *dce100_timing_generator_create(
393 		struct dc_context *ctx,
394 		uint32_t instance,
395 		const struct dce110_timing_generator_offsets *offsets)
396 {
397 	struct dce110_timing_generator *tg110 =
398 		kzalloc(sizeof(struct dce110_timing_generator), GFP_KERNEL);
399 
400 	if (!tg110)
401 		return NULL;
402 
403 	dce110_timing_generator_construct(tg110, ctx, instance, offsets);
404 	return &tg110->base;
405 }
406 
407 static struct stream_encoder *dce100_stream_encoder_create(
408 	enum engine_id eng_id,
409 	struct dc_context *ctx)
410 {
411 	struct dce110_stream_encoder *enc110 =
412 		kzalloc(sizeof(struct dce110_stream_encoder), GFP_KERNEL);
413 
414 	if (!enc110)
415 		return NULL;
416 
417 	dce110_stream_encoder_construct(enc110, ctx, ctx->dc_bios, eng_id,
418 					&stream_enc_regs[eng_id], &se_shift, &se_mask);
419 	return &enc110->base;
420 }
421 
422 #define SRII(reg_name, block, id)\
423 	.reg_name[id] = mm ## block ## id ## _ ## reg_name
424 
425 static const struct dce_hwseq_registers hwseq_reg = {
426 		HWSEQ_DCE10_REG_LIST()
427 };
428 
429 static const struct dce_hwseq_shift hwseq_shift = {
430 		HWSEQ_DCE10_MASK_SH_LIST(__SHIFT)
431 };
432 
433 static const struct dce_hwseq_mask hwseq_mask = {
434 		HWSEQ_DCE10_MASK_SH_LIST(_MASK)
435 };
436 
437 static struct dce_hwseq *dce100_hwseq_create(
438 	struct dc_context *ctx)
439 {
440 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
441 
442 	if (hws) {
443 		hws->ctx = ctx;
444 		hws->regs = &hwseq_reg;
445 		hws->shifts = &hwseq_shift;
446 		hws->masks = &hwseq_mask;
447 	}
448 	return hws;
449 }
450 
451 static const struct resource_create_funcs res_create_funcs = {
452 	.read_dce_straps = read_dce_straps,
453 	.create_audio = create_audio,
454 	.create_stream_encoder = dce100_stream_encoder_create,
455 	.create_hwseq = dce100_hwseq_create,
456 };
457 
458 #define mi_inst_regs(id) { \
459 	MI_DCE8_REG_LIST(id), \
460 	.MC_HUB_RDREQ_DMIF_LIMIT = mmMC_HUB_RDREQ_DMIF_LIMIT \
461 }
462 static const struct dce_mem_input_registers mi_regs[] = {
463 		mi_inst_regs(0),
464 		mi_inst_regs(1),
465 		mi_inst_regs(2),
466 		mi_inst_regs(3),
467 		mi_inst_regs(4),
468 		mi_inst_regs(5),
469 };
470 
471 static const struct dce_mem_input_shift mi_shifts = {
472 		MI_DCE8_MASK_SH_LIST(__SHIFT),
473 		.ENABLE = MC_HUB_RDREQ_DMIF_LIMIT__ENABLE__SHIFT
474 };
475 
476 static const struct dce_mem_input_mask mi_masks = {
477 		MI_DCE8_MASK_SH_LIST(_MASK),
478 		.ENABLE = MC_HUB_RDREQ_DMIF_LIMIT__ENABLE_MASK
479 };
480 
481 static struct mem_input *dce100_mem_input_create(
482 	struct dc_context *ctx,
483 	uint32_t inst)
484 {
485 	struct dce_mem_input *dce_mi = kzalloc(sizeof(struct dce_mem_input),
486 					       GFP_KERNEL);
487 
488 	if (!dce_mi) {
489 		BREAK_TO_DEBUGGER();
490 		return NULL;
491 	}
492 
493 	dce_mem_input_construct(dce_mi, ctx, inst, &mi_regs[inst], &mi_shifts, &mi_masks);
494 	dce_mi->wa.single_head_rdreq_dmif_limit = 2;
495 	return &dce_mi->base;
496 }
497 
498 static void dce100_transform_destroy(struct transform **xfm)
499 {
500 	kfree(TO_DCE_TRANSFORM(*xfm));
501 	*xfm = NULL;
502 }
503 
504 static struct transform *dce100_transform_create(
505 	struct dc_context *ctx,
506 	uint32_t inst)
507 {
508 	struct dce_transform *transform =
509 		kzalloc(sizeof(struct dce_transform), GFP_KERNEL);
510 
511 	if (!transform)
512 		return NULL;
513 
514 	dce_transform_construct(transform, ctx, inst,
515 				&xfm_regs[inst], &xfm_shift, &xfm_mask);
516 	return &transform->base;
517 }
518 
519 static struct input_pixel_processor *dce100_ipp_create(
520 	struct dc_context *ctx, uint32_t inst)
521 {
522 	struct dce_ipp *ipp = kzalloc(sizeof(struct dce_ipp), GFP_KERNEL);
523 
524 	if (!ipp) {
525 		BREAK_TO_DEBUGGER();
526 		return NULL;
527 	}
528 
529 	dce_ipp_construct(ipp, ctx, inst,
530 			&ipp_regs[inst], &ipp_shift, &ipp_mask);
531 	return &ipp->base;
532 }
533 
534 static const struct encoder_feature_support link_enc_feature = {
535 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
536 		.max_hdmi_pixel_clock = 300000,
537 		.flags.bits.IS_HBR2_CAPABLE = true,
538 		.flags.bits.IS_TPS3_CAPABLE = true,
539 		.flags.bits.IS_YCBCR_CAPABLE = true
540 };
541 
542 struct link_encoder *dce100_link_encoder_create(
543 	const struct encoder_init_data *enc_init_data)
544 {
545 	struct dce110_link_encoder *enc110 =
546 		kzalloc(sizeof(struct dce110_link_encoder), GFP_KERNEL);
547 
548 	if (!enc110)
549 		return NULL;
550 
551 	dce110_link_encoder_construct(enc110,
552 				      enc_init_data,
553 				      &link_enc_feature,
554 				      &link_enc_regs[enc_init_data->transmitter],
555 				      &link_enc_aux_regs[enc_init_data->channel - 1],
556 				      &link_enc_hpd_regs[enc_init_data->hpd_source]);
557 	return &enc110->base;
558 }
559 
560 struct output_pixel_processor *dce100_opp_create(
561 	struct dc_context *ctx,
562 	uint32_t inst)
563 {
564 	struct dce110_opp *opp =
565 		kzalloc(sizeof(struct dce110_opp), GFP_KERNEL);
566 
567 	if (!opp)
568 		return NULL;
569 
570 	dce110_opp_construct(opp,
571 			     ctx, inst, &opp_regs[inst], &opp_shift, &opp_mask);
572 	return &opp->base;
573 }
574 
575 struct clock_source *dce100_clock_source_create(
576 	struct dc_context *ctx,
577 	struct dc_bios *bios,
578 	enum clock_source_id id,
579 	const struct dce110_clk_src_regs *regs,
580 	bool dp_clk_src)
581 {
582 	struct dce110_clk_src *clk_src =
583 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
584 
585 	if (!clk_src)
586 		return NULL;
587 
588 	if (dce110_clk_src_construct(clk_src, ctx, bios, id,
589 			regs, &cs_shift, &cs_mask)) {
590 		clk_src->base.dp_clk_src = dp_clk_src;
591 		return &clk_src->base;
592 	}
593 
594 	BREAK_TO_DEBUGGER();
595 	return NULL;
596 }
597 
598 void dce100_clock_source_destroy(struct clock_source **clk_src)
599 {
600 	kfree(TO_DCE110_CLK_SRC(*clk_src));
601 	*clk_src = NULL;
602 }
603 
604 static void destruct(struct dce110_resource_pool *pool)
605 {
606 	unsigned int i;
607 
608 	for (i = 0; i < pool->base.pipe_count; i++) {
609 		if (pool->base.opps[i] != NULL)
610 			dce110_opp_destroy(&pool->base.opps[i]);
611 
612 		if (pool->base.transforms[i] != NULL)
613 			dce100_transform_destroy(&pool->base.transforms[i]);
614 
615 		if (pool->base.ipps[i] != NULL)
616 			dce_ipp_destroy(&pool->base.ipps[i]);
617 
618 		if (pool->base.mis[i] != NULL) {
619 			kfree(TO_DCE_MEM_INPUT(pool->base.mis[i]));
620 			pool->base.mis[i] = NULL;
621 		}
622 
623 		if (pool->base.timing_generators[i] != NULL)	{
624 			kfree(DCE110TG_FROM_TG(pool->base.timing_generators[i]));
625 			pool->base.timing_generators[i] = NULL;
626 		}
627 	}
628 
629 	for (i = 0; i < pool->base.stream_enc_count; i++) {
630 		if (pool->base.stream_enc[i] != NULL)
631 			kfree(DCE110STRENC_FROM_STRENC(pool->base.stream_enc[i]));
632 	}
633 
634 	for (i = 0; i < pool->base.clk_src_count; i++) {
635 		if (pool->base.clock_sources[i] != NULL)
636 			dce100_clock_source_destroy(&pool->base.clock_sources[i]);
637 	}
638 
639 	if (pool->base.dp_clock_source != NULL)
640 		dce100_clock_source_destroy(&pool->base.dp_clock_source);
641 
642 	for (i = 0; i < pool->base.audio_count; i++)	{
643 		if (pool->base.audios[i] != NULL)
644 			dce_aud_destroy(&pool->base.audios[i]);
645 	}
646 
647 	if (pool->base.display_clock != NULL)
648 		dce_disp_clk_destroy(&pool->base.display_clock);
649 
650 	if (pool->base.abm != NULL)
651 				dce_abm_destroy(&pool->base.abm);
652 
653 	if (pool->base.dmcu != NULL)
654 			dce_dmcu_destroy(&pool->base.dmcu);
655 
656 	if (pool->base.irqs != NULL)
657 		dal_irq_service_destroy(&pool->base.irqs);
658 }
659 
660 static enum dc_status build_mapped_resource(
661 		const struct dc  *dc,
662 		struct dc_state *context,
663 		struct dc_stream_state *stream)
664 {
665 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
666 
667 	if (!pipe_ctx)
668 		return DC_ERROR_UNEXPECTED;
669 
670 	dce110_resource_build_pipe_hw_param(pipe_ctx);
671 
672 	resource_build_info_frame(pipe_ctx);
673 
674 	return DC_OK;
675 }
676 
677 bool dce100_validate_bandwidth(
678 	struct dc  *dc,
679 	struct dc_state *context)
680 {
681 	/* TODO implement when needed but for now hardcode max value*/
682 	context->bw.dce.dispclk_khz = 681000;
683 	context->bw.dce.yclk_khz = 250000 * MEMORY_TYPE_MULTIPLIER;
684 
685 	return true;
686 }
687 
688 static bool dce100_validate_surface_sets(
689 		struct dc_state *context)
690 {
691 	int i;
692 
693 	for (i = 0; i < context->stream_count; i++) {
694 		if (context->stream_status[i].plane_count == 0)
695 			continue;
696 
697 		if (context->stream_status[i].plane_count > 1)
698 			return false;
699 
700 		if (context->stream_status[i].plane_states[0]->format
701 				>= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
702 			return false;
703 	}
704 
705 	return true;
706 }
707 
708 enum dc_status dce100_validate_global(
709 		struct dc  *dc,
710 		struct dc_state *context)
711 {
712 	if (!dce100_validate_surface_sets(context))
713 		return DC_FAIL_SURFACE_VALIDATE;
714 
715 	return DC_OK;
716 }
717 
718 enum dc_status dce100_add_stream_to_ctx(
719 		struct dc *dc,
720 		struct dc_state *new_ctx,
721 		struct dc_stream_state *dc_stream)
722 {
723 	enum dc_status result = DC_ERROR_UNEXPECTED;
724 
725 	result = resource_map_pool_resources(dc, new_ctx, dc_stream);
726 
727 	if (result == DC_OK)
728 		result = resource_map_clock_resources(dc, new_ctx, dc_stream);
729 
730 	if (result == DC_OK)
731 		result = build_mapped_resource(dc, new_ctx, dc_stream);
732 
733 	return result;
734 }
735 
736 enum dc_status dce100_validate_guaranteed(
737 		struct dc  *dc,
738 		struct dc_stream_state *dc_stream,
739 		struct dc_state *context)
740 {
741 	enum dc_status result = DC_ERROR_UNEXPECTED;
742 
743 	context->streams[0] = dc_stream;
744 	dc_stream_retain(context->streams[0]);
745 	context->stream_count++;
746 
747 	result = resource_map_pool_resources(dc, context, dc_stream);
748 
749 	if (result == DC_OK)
750 		result = resource_map_clock_resources(dc, context, dc_stream);
751 
752 	if (result == DC_OK)
753 		result = build_mapped_resource(dc, context, dc_stream);
754 
755 	if (result == DC_OK) {
756 		validate_guaranteed_copy_streams(
757 				context, dc->caps.max_streams);
758 		result = resource_build_scaling_params_for_context(dc, context);
759 	}
760 
761 	if (result == DC_OK)
762 		if (!dce100_validate_bandwidth(dc, context))
763 			result = DC_FAIL_BANDWIDTH_VALIDATE;
764 
765 	return result;
766 }
767 
768 static void dce100_destroy_resource_pool(struct resource_pool **pool)
769 {
770 	struct dce110_resource_pool *dce110_pool = TO_DCE110_RES_POOL(*pool);
771 
772 	destruct(dce110_pool);
773 	kfree(dce110_pool);
774 	*pool = NULL;
775 }
776 
777 enum dc_status dce100_validate_plane(const struct dc_plane_state *plane_state, struct dc_caps *caps)
778 {
779 
780 	if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
781 		return DC_OK;
782 
783 	return DC_FAIL_SURFACE_VALIDATE;
784 }
785 
786 static const struct resource_funcs dce100_res_pool_funcs = {
787 	.destroy = dce100_destroy_resource_pool,
788 	.link_enc_create = dce100_link_encoder_create,
789 	.validate_guaranteed = dce100_validate_guaranteed,
790 	.validate_bandwidth = dce100_validate_bandwidth,
791 	.validate_plane = dce100_validate_plane,
792 	.add_stream_to_ctx = dce100_add_stream_to_ctx,
793 	.validate_global = dce100_validate_global
794 };
795 
796 static bool construct(
797 	uint8_t num_virtual_links,
798 	struct dc  *dc,
799 	struct dce110_resource_pool *pool)
800 {
801 	unsigned int i;
802 	struct dc_context *ctx = dc->ctx;
803 	struct dc_firmware_info info;
804 	struct dc_bios *bp;
805 	struct dm_pp_static_clock_info static_clk_info = {0};
806 
807 	ctx->dc_bios->regs = &bios_regs;
808 
809 	pool->base.res_cap = &res_cap;
810 	pool->base.funcs = &dce100_res_pool_funcs;
811 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
812 
813 	bp = ctx->dc_bios;
814 
815 	if ((bp->funcs->get_firmware_info(bp, &info) == BP_RESULT_OK) &&
816 		info.external_clock_source_frequency_for_dp != 0) {
817 		pool->base.dp_clock_source =
818 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_EXTERNAL, NULL, true);
819 
820 		pool->base.clock_sources[0] =
821 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL0, &clk_src_regs[0], false);
822 		pool->base.clock_sources[1] =
823 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL1, &clk_src_regs[1], false);
824 		pool->base.clock_sources[2] =
825 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL2, &clk_src_regs[2], false);
826 		pool->base.clk_src_count = 3;
827 
828 	} else {
829 		pool->base.dp_clock_source =
830 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL0, &clk_src_regs[0], true);
831 
832 		pool->base.clock_sources[0] =
833 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL1, &clk_src_regs[1], false);
834 		pool->base.clock_sources[1] =
835 				dce100_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL2, &clk_src_regs[2], false);
836 		pool->base.clk_src_count = 2;
837 	}
838 
839 	if (pool->base.dp_clock_source == NULL) {
840 		dm_error("DC: failed to create dp clock source!\n");
841 		BREAK_TO_DEBUGGER();
842 		goto res_create_fail;
843 	}
844 
845 	for (i = 0; i < pool->base.clk_src_count; i++) {
846 		if (pool->base.clock_sources[i] == NULL) {
847 			dm_error("DC: failed to create clock sources!\n");
848 			BREAK_TO_DEBUGGER();
849 			goto res_create_fail;
850 		}
851 	}
852 
853 	pool->base.display_clock = dce_disp_clk_create(ctx,
854 			&disp_clk_regs,
855 			&disp_clk_shift,
856 			&disp_clk_mask);
857 	if (pool->base.display_clock == NULL) {
858 		dm_error("DC: failed to create display clock!\n");
859 		BREAK_TO_DEBUGGER();
860 		goto res_create_fail;
861 	}
862 
863 	pool->base.dmcu = dce_dmcu_create(ctx,
864 			&dmcu_regs,
865 			&dmcu_shift,
866 			&dmcu_mask);
867 	if (pool->base.dmcu == NULL) {
868 		dm_error("DC: failed to create dmcu!\n");
869 		BREAK_TO_DEBUGGER();
870 		goto res_create_fail;
871 	}
872 
873 	pool->base.abm = dce_abm_create(ctx,
874 				&abm_regs,
875 				&abm_shift,
876 				&abm_mask);
877 	if (pool->base.abm == NULL) {
878 		dm_error("DC: failed to create abm!\n");
879 		BREAK_TO_DEBUGGER();
880 		goto res_create_fail;
881 	}
882 
883 	/* get static clock information for PPLIB or firmware, save
884 	 * max_clock_state
885 	 */
886 	if (dm_pp_get_static_clocks(ctx, &static_clk_info))
887 		pool->base.display_clock->max_clks_state =
888 					static_clk_info.max_clocks_state;
889 	{
890 		struct irq_service_init_data init_data;
891 		init_data.ctx = dc->ctx;
892 		pool->base.irqs = dal_irq_service_dce110_create(&init_data);
893 		if (!pool->base.irqs)
894 			goto res_create_fail;
895 	}
896 
897 	/*************************************************
898 	*  Resource + asic cap harcoding                *
899 	*************************************************/
900 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
901 	pool->base.pipe_count = res_cap.num_timing_generator;
902 	pool->base.timing_generator_count = pool->base.res_cap->num_timing_generator;
903 	dc->caps.max_downscale_ratio = 200;
904 	dc->caps.i2c_speed_in_khz = 40;
905 	dc->caps.max_cursor_size = 128;
906 	dc->caps.dual_link_dvi = true;
907 
908 	for (i = 0; i < pool->base.pipe_count; i++) {
909 		pool->base.timing_generators[i] =
910 			dce100_timing_generator_create(
911 				ctx,
912 				i,
913 				&dce100_tg_offsets[i]);
914 		if (pool->base.timing_generators[i] == NULL) {
915 			BREAK_TO_DEBUGGER();
916 			dm_error("DC: failed to create tg!\n");
917 			goto res_create_fail;
918 		}
919 
920 		pool->base.mis[i] = dce100_mem_input_create(ctx, i);
921 		if (pool->base.mis[i] == NULL) {
922 			BREAK_TO_DEBUGGER();
923 			dm_error(
924 				"DC: failed to create memory input!\n");
925 			goto res_create_fail;
926 		}
927 
928 		pool->base.ipps[i] = dce100_ipp_create(ctx, i);
929 		if (pool->base.ipps[i] == NULL) {
930 			BREAK_TO_DEBUGGER();
931 			dm_error(
932 				"DC: failed to create input pixel processor!\n");
933 			goto res_create_fail;
934 		}
935 
936 		pool->base.transforms[i] = dce100_transform_create(ctx, i);
937 		if (pool->base.transforms[i] == NULL) {
938 			BREAK_TO_DEBUGGER();
939 			dm_error(
940 				"DC: failed to create transform!\n");
941 			goto res_create_fail;
942 		}
943 
944 		pool->base.opps[i] = dce100_opp_create(ctx, i);
945 		if (pool->base.opps[i] == NULL) {
946 			BREAK_TO_DEBUGGER();
947 			dm_error(
948 				"DC: failed to create output pixel processor!\n");
949 			goto res_create_fail;
950 		}
951 	}
952 
953 	dc->caps.max_planes =  pool->base.pipe_count;
954 
955 	if (!resource_construct(num_virtual_links, dc, &pool->base,
956 			&res_create_funcs))
957 		goto res_create_fail;
958 
959 	/* Create hardware sequencer */
960 	dce100_hw_sequencer_construct(dc);
961 	return true;
962 
963 res_create_fail:
964 	destruct(pool);
965 
966 	return false;
967 }
968 
969 struct resource_pool *dce100_create_resource_pool(
970 	uint8_t num_virtual_links,
971 	struct dc  *dc)
972 {
973 	struct dce110_resource_pool *pool =
974 		kzalloc(sizeof(struct dce110_resource_pool), GFP_KERNEL);
975 
976 	if (!pool)
977 		return NULL;
978 
979 	if (construct(num_virtual_links, dc, pool))
980 		return &pool->base;
981 
982 	BREAK_TO_DEBUGGER();
983 	return NULL;
984 }
985 
986