xref: /linux/drivers/gpu/drm/amd/display/dc/resource/dce110/dce110_resource.c (revision 26df51adf30b3d440293eed38d01f953ae0bb6f4)
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 
26 #include "dm_services.h"
27 
28 #include "link_encoder.h"
29 #include "stream_encoder.h"
30 
31 #include "resource.h"
32 #include "dce110/dce110_resource.h"
33 #include "include/irq_service_interface.h"
34 #include "dce/dce_audio.h"
35 #include "dce110/dce110_timing_generator.h"
36 #include "irq/dce110/irq_service_dce110.h"
37 #include "dce110/dce110_timing_generator_v.h"
38 #include "dce/dce_link_encoder.h"
39 #include "dce/dce_stream_encoder.h"
40 #include "dce/dce_mem_input.h"
41 #include "dce110/dce110_mem_input_v.h"
42 #include "dce/dce_ipp.h"
43 #include "dce/dce_transform.h"
44 #include "dce110/dce110_transform_v.h"
45 #include "dce/dce_opp.h"
46 #include "dce110/dce110_opp_v.h"
47 #include "dce/dce_clock_source.h"
48 #include "dce/dce_hwseq.h"
49 #include "dce110/dce110_hwseq.h"
50 #include "dce/dce_aux.h"
51 #include "dce/dce_abm.h"
52 #include "dce/dce_dmcu.h"
53 #include "dce/dce_i2c.h"
54 #include "dce/dce_panel_cntl.h"
55 
56 #define DC_LOGGER \
57 		dc->ctx->logger
58 
59 #include "dce110/dce110_compressor.h"
60 
61 #include "reg_helper.h"
62 
63 #include "dce/dce_11_0_d.h"
64 #include "dce/dce_11_0_sh_mask.h"
65 
66 #ifndef mmMC_HUB_RDREQ_DMIF_LIMIT
67 #include "gmc/gmc_8_2_d.h"
68 #include "gmc/gmc_8_2_sh_mask.h"
69 #endif
70 
71 #ifndef mmDP_DPHY_INTERNAL_CTRL
72 	#define mmDP_DPHY_INTERNAL_CTRL 0x4aa7
73 	#define mmDP0_DP_DPHY_INTERNAL_CTRL 0x4aa7
74 	#define mmDP1_DP_DPHY_INTERNAL_CTRL 0x4ba7
75 	#define mmDP2_DP_DPHY_INTERNAL_CTRL 0x4ca7
76 	#define mmDP3_DP_DPHY_INTERNAL_CTRL 0x4da7
77 	#define mmDP4_DP_DPHY_INTERNAL_CTRL 0x4ea7
78 	#define mmDP5_DP_DPHY_INTERNAL_CTRL 0x4fa7
79 	#define mmDP6_DP_DPHY_INTERNAL_CTRL 0x54a7
80 	#define mmDP7_DP_DPHY_INTERNAL_CTRL 0x56a7
81 	#define mmDP8_DP_DPHY_INTERNAL_CTRL 0x57a7
82 #endif
83 
84 #ifndef mmBIOS_SCRATCH_2
85 	#define mmBIOS_SCRATCH_0 0x05C9
86 	#define mmBIOS_SCRATCH_2 0x05CB
87 	#define mmBIOS_SCRATCH_3 0x05CC
88 	#define mmBIOS_SCRATCH_6 0x05CF
89 #endif
90 
91 #ifndef mmDP_DPHY_BS_SR_SWAP_CNTL
92 	#define mmDP_DPHY_BS_SR_SWAP_CNTL                       0x4ADC
93 	#define mmDP0_DP_DPHY_BS_SR_SWAP_CNTL                   0x4ADC
94 	#define mmDP1_DP_DPHY_BS_SR_SWAP_CNTL                   0x4BDC
95 	#define mmDP2_DP_DPHY_BS_SR_SWAP_CNTL                   0x4CDC
96 	#define mmDP3_DP_DPHY_BS_SR_SWAP_CNTL                   0x4DDC
97 	#define mmDP4_DP_DPHY_BS_SR_SWAP_CNTL                   0x4EDC
98 	#define mmDP5_DP_DPHY_BS_SR_SWAP_CNTL                   0x4FDC
99 	#define mmDP6_DP_DPHY_BS_SR_SWAP_CNTL                   0x54DC
100 #endif
101 
102 #ifndef mmDP_DPHY_FAST_TRAINING
103 	#define mmDP_DPHY_FAST_TRAINING                         0x4ABC
104 	#define mmDP0_DP_DPHY_FAST_TRAINING                     0x4ABC
105 	#define mmDP1_DP_DPHY_FAST_TRAINING                     0x4BBC
106 	#define mmDP2_DP_DPHY_FAST_TRAINING                     0x4CBC
107 	#define mmDP3_DP_DPHY_FAST_TRAINING                     0x4DBC
108 	#define mmDP4_DP_DPHY_FAST_TRAINING                     0x4EBC
109 	#define mmDP5_DP_DPHY_FAST_TRAINING                     0x4FBC
110 	#define mmDP6_DP_DPHY_FAST_TRAINING                     0x54BC
111 #endif
112 
113 #ifndef DPHY_RX_FAST_TRAINING_CAPABLE
114 	#define DPHY_RX_FAST_TRAINING_CAPABLE 0x1
115 #endif
116 
117 static const struct dce110_timing_generator_offsets dce110_tg_offsets[] = {
118 	{
119 		.crtc = (mmCRTC0_CRTC_CONTROL - mmCRTC_CONTROL),
120 		.dcp =  (mmDCP0_GRPH_CONTROL - mmGRPH_CONTROL),
121 	},
122 	{
123 		.crtc = (mmCRTC1_CRTC_CONTROL - mmCRTC_CONTROL),
124 		.dcp = (mmDCP1_GRPH_CONTROL - mmGRPH_CONTROL),
125 	},
126 	{
127 		.crtc = (mmCRTC2_CRTC_CONTROL - mmCRTC_CONTROL),
128 		.dcp = (mmDCP2_GRPH_CONTROL - mmGRPH_CONTROL),
129 	},
130 	{
131 		.crtc = (mmCRTC3_CRTC_CONTROL - mmCRTC_CONTROL),
132 		.dcp =  (mmDCP3_GRPH_CONTROL - mmGRPH_CONTROL),
133 	},
134 	{
135 		.crtc = (mmCRTC4_CRTC_CONTROL - mmCRTC_CONTROL),
136 		.dcp = (mmDCP4_GRPH_CONTROL - mmGRPH_CONTROL),
137 	},
138 	{
139 		.crtc = (mmCRTC5_CRTC_CONTROL - mmCRTC_CONTROL),
140 		.dcp = (mmDCP5_GRPH_CONTROL - mmGRPH_CONTROL),
141 	}
142 };
143 
144 /* set register offset */
145 #define SR(reg_name)\
146 	.reg_name = mm ## reg_name
147 
148 /* set register offset with instance */
149 #define SRI(reg_name, block, id)\
150 	.reg_name = mm ## block ## id ## _ ## reg_name
151 
152 static const struct dce_dmcu_registers dmcu_regs = {
153 		DMCU_DCE110_COMMON_REG_LIST()
154 };
155 
156 static const struct dce_dmcu_shift dmcu_shift = {
157 		DMCU_MASK_SH_LIST_DCE110(__SHIFT)
158 };
159 
160 static const struct dce_dmcu_mask dmcu_mask = {
161 		DMCU_MASK_SH_LIST_DCE110(_MASK)
162 };
163 
164 static const struct dce_abm_registers abm_regs = {
165 		ABM_DCE110_COMMON_REG_LIST()
166 };
167 
168 static const struct dce_abm_shift abm_shift = {
169 		ABM_MASK_SH_LIST_DCE110(__SHIFT)
170 };
171 
172 static const struct dce_abm_mask abm_mask = {
173 		ABM_MASK_SH_LIST_DCE110(_MASK)
174 };
175 
176 #define ipp_regs(id)\
177 [id] = {\
178 		IPP_DCE110_REG_LIST_DCE_BASE(id)\
179 }
180 
181 static const struct dce_ipp_registers ipp_regs[] = {
182 		ipp_regs(0),
183 		ipp_regs(1),
184 		ipp_regs(2)
185 };
186 
187 static const struct dce_ipp_shift ipp_shift = {
188 		IPP_DCE100_MASK_SH_LIST_DCE_COMMON_BASE(__SHIFT)
189 };
190 
191 static const struct dce_ipp_mask ipp_mask = {
192 		IPP_DCE100_MASK_SH_LIST_DCE_COMMON_BASE(_MASK)
193 };
194 
195 #define transform_regs(id)\
196 [id] = {\
197 		XFM_COMMON_REG_LIST_DCE110(id)\
198 }
199 
200 static const struct dce_transform_registers xfm_regs[] = {
201 		transform_regs(0),
202 		transform_regs(1),
203 		transform_regs(2)
204 };
205 
206 static const struct dce_transform_shift xfm_shift = {
207 		XFM_COMMON_MASK_SH_LIST_DCE110(__SHIFT)
208 };
209 
210 static const struct dce_transform_mask xfm_mask = {
211 		XFM_COMMON_MASK_SH_LIST_DCE110(_MASK)
212 };
213 
214 #define aux_regs(id)\
215 [id] = {\
216 	AUX_REG_LIST(id)\
217 }
218 
219 static const struct dce110_link_enc_aux_registers link_enc_aux_regs[] = {
220 		aux_regs(0),
221 		aux_regs(1),
222 		aux_regs(2),
223 		aux_regs(3),
224 		aux_regs(4),
225 		aux_regs(5)
226 };
227 
228 #define hpd_regs(id)\
229 [id] = {\
230 	HPD_REG_LIST(id)\
231 }
232 
233 static const struct dce110_link_enc_hpd_registers link_enc_hpd_regs[] = {
234 		hpd_regs(0),
235 		hpd_regs(1),
236 		hpd_regs(2),
237 		hpd_regs(3),
238 		hpd_regs(4),
239 		hpd_regs(5)
240 };
241 
242 
243 #define link_regs(id)\
244 [id] = {\
245 	LE_DCE110_REG_LIST(id)\
246 }
247 
248 static const struct dce110_link_enc_registers link_enc_regs[] = {
249 	link_regs(0),
250 	link_regs(1),
251 	link_regs(2),
252 	link_regs(3),
253 	link_regs(4),
254 	link_regs(5),
255 	link_regs(6),
256 };
257 
258 #define stream_enc_regs(id)\
259 [id] = {\
260 	SE_COMMON_REG_LIST(id),\
261 	.TMDS_CNTL = 0,\
262 }
263 
264 static const struct dce110_stream_enc_registers stream_enc_regs[] = {
265 	stream_enc_regs(0),
266 	stream_enc_regs(1),
267 	stream_enc_regs(2)
268 };
269 
270 static const struct dce_stream_encoder_shift se_shift = {
271 		SE_COMMON_MASK_SH_LIST_DCE110(__SHIFT)
272 };
273 
274 static const struct dce_stream_encoder_mask se_mask = {
275 		SE_COMMON_MASK_SH_LIST_DCE110(_MASK)
276 };
277 
278 static const struct dce_panel_cntl_registers panel_cntl_regs[] = {
279 	{ DCE_PANEL_CNTL_REG_LIST() }
280 };
281 
282 static const struct dce_panel_cntl_shift panel_cntl_shift = {
283 	DCE_PANEL_CNTL_MASK_SH_LIST(__SHIFT)
284 };
285 
286 static const struct dce_panel_cntl_mask panel_cntl_mask = {
287 	DCE_PANEL_CNTL_MASK_SH_LIST(_MASK)
288 };
289 
290 static const struct dce110_aux_registers_shift aux_shift = {
291 	DCE_AUX_MASK_SH_LIST(__SHIFT)
292 };
293 
294 static const struct dce110_aux_registers_mask aux_mask = {
295 	DCE_AUX_MASK_SH_LIST(_MASK)
296 };
297 
298 #define opp_regs(id)\
299 [id] = {\
300 	OPP_DCE_110_REG_LIST(id),\
301 }
302 
303 static const struct dce_opp_registers opp_regs[] = {
304 	opp_regs(0),
305 	opp_regs(1),
306 	opp_regs(2),
307 	opp_regs(3),
308 	opp_regs(4),
309 	opp_regs(5)
310 };
311 
312 static const struct dce_opp_shift opp_shift = {
313 	OPP_COMMON_MASK_SH_LIST_DCE_110(__SHIFT)
314 };
315 
316 static const struct dce_opp_mask opp_mask = {
317 	OPP_COMMON_MASK_SH_LIST_DCE_110(_MASK)
318 };
319 
320 #define aux_engine_regs(id)\
321 [id] = {\
322 	AUX_COMMON_REG_LIST(id), \
323 	.AUX_RESET_MASK = 0 \
324 }
325 
326 static const struct dce110_aux_registers aux_engine_regs[] = {
327 		aux_engine_regs(0),
328 		aux_engine_regs(1),
329 		aux_engine_regs(2),
330 		aux_engine_regs(3),
331 		aux_engine_regs(4),
332 		aux_engine_regs(5)
333 };
334 
335 #define audio_regs(id)\
336 [id] = {\
337 	AUD_COMMON_REG_LIST(id)\
338 }
339 
340 static const struct dce_audio_registers audio_regs[] = {
341 	audio_regs(0),
342 	audio_regs(1),
343 	audio_regs(2),
344 	audio_regs(3),
345 	audio_regs(4),
346 	audio_regs(5),
347 	audio_regs(6),
348 };
349 
350 static const struct dce_audio_shift audio_shift = {
351 		AUD_COMMON_MASK_SH_LIST(__SHIFT)
352 };
353 
354 static const struct dce_audio_mask audio_mask = {
355 		AUD_COMMON_MASK_SH_LIST(_MASK)
356 };
357 
358 /* AG TBD Needs to be reduced back to 3 pipes once dce10 hw sequencer implemented. */
359 
360 
361 #define clk_src_regs(id)\
362 [id] = {\
363 	CS_COMMON_REG_LIST_DCE_100_110(id),\
364 }
365 
366 static const struct dce110_clk_src_regs clk_src_regs[] = {
367 	clk_src_regs(0),
368 	clk_src_regs(1),
369 	clk_src_regs(2)
370 };
371 
372 static const struct dce110_clk_src_shift cs_shift = {
373 		CS_COMMON_MASK_SH_LIST_DCE_COMMON_BASE(__SHIFT)
374 };
375 
376 static const struct dce110_clk_src_mask cs_mask = {
377 		CS_COMMON_MASK_SH_LIST_DCE_COMMON_BASE(_MASK)
378 };
379 
380 static const struct bios_registers bios_regs = {
381 	.BIOS_SCRATCH_0 = mmBIOS_SCRATCH_0,
382 	.BIOS_SCRATCH_3 = mmBIOS_SCRATCH_3,
383 	.BIOS_SCRATCH_6 = mmBIOS_SCRATCH_6
384 };
385 
386 static const struct resource_caps carrizo_resource_cap = {
387 		.num_timing_generator = 3,
388 		.num_video_plane = 1,
389 		.num_audio = 3,
390 		.num_stream_encoder = 3,
391 		.num_pll = 2,
392 		.num_ddc = 3,
393 };
394 
395 static const struct resource_caps stoney_resource_cap = {
396 		.num_timing_generator = 2,
397 		.num_video_plane = 1,
398 		.num_audio = 3,
399 		.num_stream_encoder = 3,
400 		.num_pll = 2,
401 		.num_ddc = 3,
402 };
403 
404 static const struct dc_plane_cap plane_cap = {
405 		.type = DC_PLANE_TYPE_DCE_RGB,
406 		.per_pixel_alpha = 1,
407 
408 		.pixel_format_support = {
409 				.argb8888 = true,
410 				.nv12 = false,
411 				.fp16 = true
412 		},
413 
414 		.max_upscale_factor = {
415 				.argb8888 = 16000,
416 				.nv12 = 1,
417 				.fp16 = 1
418 		},
419 
420 		.max_downscale_factor = {
421 				.argb8888 = 250,
422 				.nv12 = 1,
423 				.fp16 = 1
424 		},
425 		64,
426 		64
427 };
428 
429 static const struct dc_debug_options debug_defaults = { 0 };
430 
431 static const struct dc_check_config config_defaults = {
432 		.enable_legacy_fast_update = true,
433 };
434 
435 static const struct dc_plane_cap underlay_plane_cap = {
436 		.type = DC_PLANE_TYPE_DCE_UNDERLAY,
437 		.per_pixel_alpha = 1,
438 
439 		.pixel_format_support = {
440 				.argb8888 = false,
441 				.nv12 = true,
442 				.fp16 = false
443 		},
444 
445 		.max_upscale_factor = {
446 				.argb8888 = 1,
447 				.nv12 = 16000,
448 				.fp16 = 1
449 		},
450 
451 		.max_downscale_factor = {
452 				.argb8888 = 1,
453 				.nv12 = 250,
454 				.fp16 = 1
455 		},
456 		64,
457 		64
458 };
459 
460 #define CTX  ctx
461 #define REG(reg) mm ## reg
462 
463 #ifndef mmCC_DC_HDMI_STRAPS
464 #define mmCC_DC_HDMI_STRAPS 0x4819
465 #define CC_DC_HDMI_STRAPS__HDMI_DISABLE_MASK 0x40
466 #define CC_DC_HDMI_STRAPS__HDMI_DISABLE__SHIFT 0x6
467 #define CC_DC_HDMI_STRAPS__AUDIO_STREAM_NUMBER_MASK 0x700
468 #define CC_DC_HDMI_STRAPS__AUDIO_STREAM_NUMBER__SHIFT 0x8
469 #endif
470 
map_transmitter_id_to_phy_instance(enum transmitter transmitter)471 static int map_transmitter_id_to_phy_instance(
472 	enum transmitter transmitter)
473 {
474 	switch (transmitter) {
475 	case TRANSMITTER_UNIPHY_A:
476 		return 0;
477 	case TRANSMITTER_UNIPHY_B:
478 		return 1;
479 	case TRANSMITTER_UNIPHY_C:
480 		return 2;
481 	case TRANSMITTER_UNIPHY_D:
482 		return 3;
483 	case TRANSMITTER_UNIPHY_E:
484 		return 4;
485 	case TRANSMITTER_UNIPHY_F:
486 		return 5;
487 	case TRANSMITTER_UNIPHY_G:
488 		return 6;
489 	default:
490 		ASSERT(0);
491 		return 0;
492 	}
493 }
494 
read_dce_straps(struct dc_context * ctx,struct resource_straps * straps)495 static void read_dce_straps(
496 	struct dc_context *ctx,
497 	struct resource_straps *straps)
498 {
499 	REG_GET_2(CC_DC_HDMI_STRAPS,
500 			HDMI_DISABLE, &straps->hdmi_disable,
501 			AUDIO_STREAM_NUMBER, &straps->audio_stream_number);
502 
503 	REG_GET(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO, &straps->dc_pinstraps_audio);
504 }
505 
create_audio(struct dc_context * ctx,unsigned int inst)506 static struct audio *create_audio(
507 		struct dc_context *ctx, unsigned int inst)
508 {
509 	return dce_audio_create(ctx, inst,
510 			&audio_regs[inst], &audio_shift, &audio_mask);
511 }
512 
dce110_timing_generator_create(struct dc_context * ctx,uint32_t instance,const struct dce110_timing_generator_offsets * offsets)513 static struct timing_generator *dce110_timing_generator_create(
514 		struct dc_context *ctx,
515 		uint32_t instance,
516 		const struct dce110_timing_generator_offsets *offsets)
517 {
518 	struct dce110_timing_generator *tg110 =
519 		kzalloc_obj(struct dce110_timing_generator);
520 
521 	if (!tg110)
522 		return NULL;
523 
524 	dce110_timing_generator_construct(tg110, ctx, instance, offsets);
525 	return &tg110->base;
526 }
527 
dce110_stream_encoder_create(enum engine_id eng_id,struct dc_context * ctx)528 static struct stream_encoder *dce110_stream_encoder_create(
529 	enum engine_id eng_id,
530 	struct dc_context *ctx)
531 {
532 	struct dce110_stream_encoder *enc110 =
533 		kzalloc_obj(struct dce110_stream_encoder);
534 
535 	if (!enc110)
536 		return NULL;
537 
538 	dce110_stream_encoder_construct(enc110, ctx, ctx->dc_bios, eng_id,
539 					&stream_enc_regs[eng_id],
540 					&se_shift, &se_mask);
541 	return &enc110->base;
542 }
543 
544 #define SRII(reg_name, block, id)\
545 	.reg_name[id] = mm ## block ## id ## _ ## reg_name
546 
547 static const struct dce_hwseq_registers hwseq_stoney_reg = {
548 		HWSEQ_ST_REG_LIST()
549 };
550 
551 static const struct dce_hwseq_registers hwseq_cz_reg = {
552 		HWSEQ_CZ_REG_LIST()
553 };
554 
555 static const struct dce_hwseq_shift hwseq_shift = {
556 		HWSEQ_DCE11_MASK_SH_LIST(__SHIFT),
557 };
558 
559 static const struct dce_hwseq_mask hwseq_mask = {
560 		HWSEQ_DCE11_MASK_SH_LIST(_MASK),
561 };
562 
dce110_hwseq_create(struct dc_context * ctx)563 static struct dce_hwseq *dce110_hwseq_create(
564 	struct dc_context *ctx)
565 {
566 	struct dce_hwseq *hws = kzalloc_obj(struct dce_hwseq);
567 
568 	if (hws) {
569 		hws->ctx = ctx;
570 		hws->regs = ASIC_REV_IS_STONEY(ctx->asic_id.hw_internal_rev) ?
571 				&hwseq_stoney_reg : &hwseq_cz_reg;
572 		hws->shifts = &hwseq_shift;
573 		hws->masks = &hwseq_mask;
574 		hws->wa.blnd_crtc_trigger = true;
575 	}
576 	return hws;
577 }
578 
579 static const struct resource_create_funcs res_create_funcs = {
580 	.read_dce_straps = read_dce_straps,
581 	.create_audio = create_audio,
582 	.create_stream_encoder = dce110_stream_encoder_create,
583 	.create_hwseq = dce110_hwseq_create,
584 };
585 
586 #define mi_inst_regs(id) { \
587 	MI_DCE11_REG_LIST(id), \
588 	.MC_HUB_RDREQ_DMIF_LIMIT = mmMC_HUB_RDREQ_DMIF_LIMIT \
589 }
590 static const struct dce_mem_input_registers mi_regs[] = {
591 		mi_inst_regs(0),
592 		mi_inst_regs(1),
593 		mi_inst_regs(2),
594 };
595 
596 static const struct dce_mem_input_shift mi_shifts = {
597 		MI_DCE11_MASK_SH_LIST(__SHIFT),
598 		.ENABLE = MC_HUB_RDREQ_DMIF_LIMIT__ENABLE__SHIFT
599 };
600 
601 static const struct dce_mem_input_mask mi_masks = {
602 		MI_DCE11_MASK_SH_LIST(_MASK),
603 		.ENABLE = MC_HUB_RDREQ_DMIF_LIMIT__ENABLE_MASK
604 };
605 
606 
dce110_mem_input_create(struct dc_context * ctx,uint32_t inst)607 static struct mem_input *dce110_mem_input_create(
608 	struct dc_context *ctx,
609 	uint32_t inst)
610 {
611 	struct dce_mem_input *dce_mi = kzalloc_obj(struct dce_mem_input);
612 
613 	if (!dce_mi) {
614 		BREAK_TO_DEBUGGER();
615 		return NULL;
616 	}
617 
618 	dce_mem_input_construct(dce_mi, ctx, inst, &mi_regs[inst], &mi_shifts, &mi_masks);
619 	dce_mi->wa.single_head_rdreq_dmif_limit = 3;
620 	return &dce_mi->base;
621 }
622 
dce110_transform_destroy(struct transform ** xfm)623 static void dce110_transform_destroy(struct transform **xfm)
624 {
625 	kfree(TO_DCE_TRANSFORM(*xfm));
626 	*xfm = NULL;
627 }
628 
dce110_transform_create(struct dc_context * ctx,uint32_t inst)629 static struct transform *dce110_transform_create(
630 	struct dc_context *ctx,
631 	uint32_t inst)
632 {
633 	struct dce_transform *transform =
634 		kzalloc_obj(struct dce_transform);
635 
636 	if (!transform)
637 		return NULL;
638 
639 	dce_transform_construct(transform, ctx, inst,
640 				&xfm_regs[inst], &xfm_shift, &xfm_mask);
641 	return &transform->base;
642 }
643 
dce110_ipp_create(struct dc_context * ctx,uint32_t inst)644 static struct input_pixel_processor *dce110_ipp_create(
645 	struct dc_context *ctx, uint32_t inst)
646 {
647 	struct dce_ipp *ipp = kzalloc_obj(struct dce_ipp);
648 
649 	if (!ipp) {
650 		BREAK_TO_DEBUGGER();
651 		return NULL;
652 	}
653 
654 	dce_ipp_construct(ipp, ctx, inst,
655 			&ipp_regs[inst], &ipp_shift, &ipp_mask);
656 	return &ipp->base;
657 }
658 
659 static const struct encoder_feature_support link_enc_feature = {
660 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
661 		.max_hdmi_pixel_clock = 300000,
662 		.flags.bits.IS_HBR2_CAPABLE = true,
663 		.flags.bits.IS_TPS3_CAPABLE = true
664 };
665 
dce110_link_encoder_create(struct dc_context * ctx,const struct encoder_init_data * enc_init_data)666 static struct link_encoder *dce110_link_encoder_create(
667 	struct dc_context *ctx,
668 	const struct encoder_init_data *enc_init_data)
669 {
670 	struct dce110_link_encoder *enc110 =
671 		kzalloc_obj(struct dce110_link_encoder);
672 	int link_regs_id;
673 
674 	if (!enc110)
675 		return NULL;
676 
677 	link_regs_id =
678 		map_transmitter_id_to_phy_instance(enc_init_data->transmitter);
679 
680 	dce110_link_encoder_construct(enc110,
681 				      enc_init_data,
682 				      &link_enc_feature,
683 				      &link_enc_regs[link_regs_id],
684 				      &link_enc_aux_regs[enc_init_data->channel - 1],
685 				      enc_init_data->hpd_source >= ARRAY_SIZE(link_enc_hpd_regs) ?
686 				      NULL : &link_enc_hpd_regs[enc_init_data->hpd_source]);
687 	return &enc110->base;
688 }
689 
dce110_panel_cntl_create(const struct panel_cntl_init_data * init_data)690 static struct panel_cntl *dce110_panel_cntl_create(const struct panel_cntl_init_data *init_data)
691 {
692 	struct dce_panel_cntl *panel_cntl =
693 		kzalloc_obj(struct dce_panel_cntl);
694 
695 	if (!panel_cntl)
696 		return NULL;
697 
698 	dce_panel_cntl_construct(panel_cntl,
699 			init_data,
700 			&panel_cntl_regs[init_data->inst],
701 			&panel_cntl_shift,
702 			&panel_cntl_mask);
703 
704 	return &panel_cntl->base;
705 }
706 
dce110_opp_create(struct dc_context * ctx,uint32_t inst)707 static struct output_pixel_processor *dce110_opp_create(
708 	struct dc_context *ctx,
709 	uint32_t inst)
710 {
711 	struct dce110_opp *opp =
712 		kzalloc_obj(struct dce110_opp);
713 
714 	if (!opp)
715 		return NULL;
716 
717 	dce110_opp_construct(opp,
718 			     ctx, inst, &opp_regs[inst], &opp_shift, &opp_mask);
719 	return &opp->base;
720 }
721 
dce110_aux_engine_create(struct dc_context * ctx,uint32_t inst)722 static struct dce_aux *dce110_aux_engine_create(
723 	struct dc_context *ctx,
724 	uint32_t inst)
725 {
726 	struct aux_engine_dce110 *aux_engine =
727 		kzalloc_obj(struct aux_engine_dce110);
728 
729 	if (!aux_engine)
730 		return NULL;
731 
732 	dce110_aux_engine_construct(aux_engine, ctx, inst,
733 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
734 				    &aux_engine_regs[inst],
735 					&aux_mask,
736 					&aux_shift,
737 					ctx->dc->caps.extended_aux_timeout_support);
738 
739 	return &aux_engine->base;
740 }
741 #define i2c_inst_regs(id) { I2C_HW_ENGINE_COMMON_REG_LIST(id) }
742 
743 static const struct dce_i2c_registers i2c_hw_regs[] = {
744 		i2c_inst_regs(1),
745 		i2c_inst_regs(2),
746 		i2c_inst_regs(3),
747 		i2c_inst_regs(4),
748 		i2c_inst_regs(5),
749 		i2c_inst_regs(6),
750 };
751 
752 static const struct dce_i2c_shift i2c_shifts = {
753 		I2C_COMMON_MASK_SH_LIST_DCE110(__SHIFT)
754 };
755 
756 static const struct dce_i2c_mask i2c_masks = {
757 		I2C_COMMON_MASK_SH_LIST_DCE110(_MASK)
758 };
759 
dce110_i2c_hw_create(struct dc_context * ctx,uint32_t inst)760 static struct dce_i2c_hw *dce110_i2c_hw_create(
761 	struct dc_context *ctx,
762 	uint32_t inst)
763 {
764 	struct dce_i2c_hw *dce_i2c_hw =
765 		kzalloc_obj(struct dce_i2c_hw);
766 
767 	if (!dce_i2c_hw)
768 		return NULL;
769 
770 	dce100_i2c_hw_construct(dce_i2c_hw, ctx, inst,
771 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
772 
773 	return dce_i2c_hw;
774 }
dce110_clock_source_create(struct dc_context * ctx,struct dc_bios * bios,enum clock_source_id id,const struct dce110_clk_src_regs * regs,bool dp_clk_src)775 static struct clock_source *dce110_clock_source_create(
776 	struct dc_context *ctx,
777 	struct dc_bios *bios,
778 	enum clock_source_id id,
779 	const struct dce110_clk_src_regs *regs,
780 	bool dp_clk_src)
781 {
782 	struct dce110_clk_src *clk_src =
783 		kzalloc_obj(struct dce110_clk_src);
784 
785 	if (!clk_src)
786 		return NULL;
787 
788 	if (dce110_clk_src_construct(clk_src, ctx, bios, id,
789 			regs, &cs_shift, &cs_mask)) {
790 		clk_src->base.dp_clk_src = dp_clk_src;
791 		return &clk_src->base;
792 	}
793 
794 	kfree(clk_src);
795 	BREAK_TO_DEBUGGER();
796 	return NULL;
797 }
798 
dce110_clock_source_destroy(struct clock_source ** clk_src)799 static void dce110_clock_source_destroy(struct clock_source **clk_src)
800 {
801 	struct dce110_clk_src *dce110_clk_src;
802 
803 	if (!clk_src)
804 		return;
805 
806 	dce110_clk_src = TO_DCE110_CLK_SRC(*clk_src);
807 
808 	kfree(dce110_clk_src->dp_ss_params);
809 	kfree(dce110_clk_src->hdmi_ss_params);
810 	kfree(dce110_clk_src->dvi_ss_params);
811 
812 	kfree(dce110_clk_src);
813 	*clk_src = NULL;
814 }
815 
dce110_resource_destruct(struct dce110_resource_pool * pool)816 static void dce110_resource_destruct(struct dce110_resource_pool *pool)
817 {
818 	unsigned int i;
819 
820 	for (i = 0; i < pool->base.pipe_count; i++) {
821 		if (pool->base.opps[i] != NULL)
822 			dce110_opp_destroy(&pool->base.opps[i]);
823 
824 		if (pool->base.transforms[i] != NULL)
825 			dce110_transform_destroy(&pool->base.transforms[i]);
826 
827 		if (pool->base.ipps[i] != NULL)
828 			dce_ipp_destroy(&pool->base.ipps[i]);
829 
830 		if (pool->base.mis[i] != NULL) {
831 			kfree(TO_DCE_MEM_INPUT(pool->base.mis[i]));
832 			pool->base.mis[i] = NULL;
833 		}
834 
835 		if (pool->base.timing_generators[i] != NULL)	{
836 			kfree(DCE110TG_FROM_TG(pool->base.timing_generators[i]));
837 			pool->base.timing_generators[i] = NULL;
838 		}
839 	}
840 
841 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
842 		if (pool->base.engines[i] != NULL)
843 			dce110_engine_destroy(&pool->base.engines[i]);
844 		if (pool->base.hw_i2cs[i] != NULL) {
845 			kfree(pool->base.hw_i2cs[i]);
846 			pool->base.hw_i2cs[i] = NULL;
847 		}
848 		if (pool->base.sw_i2cs[i] != NULL) {
849 			kfree(pool->base.sw_i2cs[i]);
850 			pool->base.sw_i2cs[i] = NULL;
851 		}
852 	}
853 
854 	for (i = 0; i < pool->base.stream_enc_count; i++) {
855 		if (pool->base.stream_enc[i] != NULL)
856 			kfree(DCE110STRENC_FROM_STRENC(pool->base.stream_enc[i]));
857 	}
858 
859 	for (i = 0; i < pool->base.clk_src_count; i++) {
860 		if (pool->base.clock_sources[i] != NULL) {
861 			dce110_clock_source_destroy(&pool->base.clock_sources[i]);
862 		}
863 	}
864 
865 	if (pool->base.dp_clock_source != NULL)
866 		dce110_clock_source_destroy(&pool->base.dp_clock_source);
867 
868 	for (i = 0; i < pool->base.audio_count; i++)	{
869 		if (pool->base.audios[i] != NULL) {
870 			dce_aud_destroy(&pool->base.audios[i]);
871 		}
872 	}
873 
874 	if (pool->base.abm != NULL)
875 		dce_abm_destroy(&pool->base.abm);
876 
877 	if (pool->base.dmcu != NULL)
878 		dce_dmcu_destroy(&pool->base.dmcu);
879 
880 	if (pool->base.irqs != NULL) {
881 		dal_irq_service_destroy(&pool->base.irqs);
882 	}
883 }
884 
885 
get_pixel_clock_parameters(const struct pipe_ctx * pipe_ctx,struct pixel_clk_params * pixel_clk_params)886 static void get_pixel_clock_parameters(
887 	const struct pipe_ctx *pipe_ctx,
888 	struct pixel_clk_params *pixel_clk_params)
889 {
890 	const struct dc_stream_state *stream = pipe_ctx->stream;
891 
892 	/*TODO: is this halved for YCbCr 420? in that case we might want to move
893 	 * the pixel clock normalization for hdmi up to here instead of doing it
894 	 * in pll_adjust_pix_clk
895 	 */
896 	pixel_clk_params->requested_pix_clk_100hz = stream->timing.pix_clk_100hz;
897 	pixel_clk_params->encoder_object_id = stream->link->link_enc->id;
898 	if (dc_is_rgb_signal(pipe_ctx->stream->signal))
899 		pixel_clk_params->encoder_object_id = stream->link->link_enc->analog_id;
900 	pixel_clk_params->signal_type = pipe_ctx->stream->signal;
901 	pixel_clk_params->controller_id = pipe_ctx->stream_res.tg->inst + 1;
902 	/* TODO: un-hardcode*/
903 	pixel_clk_params->requested_sym_clk = LINK_RATE_LOW *
904 						LINK_RATE_REF_FREQ_IN_KHZ;
905 	pixel_clk_params->flags.ENABLE_SS = 0;
906 	pixel_clk_params->color_depth =
907 		stream->timing.display_color_depth;
908 	pixel_clk_params->flags.DISPLAY_BLANKED = 1;
909 	pixel_clk_params->flags.SUPPORT_YCBCR420 = (stream->timing.pixel_encoding ==
910 			PIXEL_ENCODING_YCBCR420);
911 	pixel_clk_params->pixel_encoding = stream->timing.pixel_encoding;
912 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422) {
913 		pixel_clk_params->color_depth = COLOR_DEPTH_888;
914 	}
915 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420) {
916 		pixel_clk_params->requested_pix_clk_100hz  = pixel_clk_params->requested_pix_clk_100hz / 2;
917 	}
918 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
919 		pixel_clk_params->requested_pix_clk_100hz *= 2;
920 
921 }
922 
dce110_resource_build_pipe_hw_param(struct pipe_ctx * pipe_ctx)923 void dce110_resource_build_pipe_hw_param(struct pipe_ctx *pipe_ctx)
924 {
925 	get_pixel_clock_parameters(pipe_ctx, &pipe_ctx->stream_res.pix_clk_params);
926 	pipe_ctx->clock_source->funcs->get_pix_clk_dividers(
927 		pipe_ctx->clock_source,
928 		&pipe_ctx->stream_res.pix_clk_params,
929 		&pipe_ctx->pll_settings);
930 	resource_build_bit_depth_reduction_params(pipe_ctx->stream,
931 			&pipe_ctx->stream->bit_depth_params);
932 	pipe_ctx->stream->clamping.pixel_encoding = pipe_ctx->stream->timing.pixel_encoding;
933 }
934 
is_surface_pixel_format_supported(struct pipe_ctx * pipe_ctx,unsigned int underlay_idx)935 static bool is_surface_pixel_format_supported(struct pipe_ctx *pipe_ctx, unsigned int underlay_idx)
936 {
937 	if (pipe_ctx->pipe_idx != underlay_idx)
938 		return true;
939 	if (!pipe_ctx->plane_state)
940 		return false;
941 	if (pipe_ctx->plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
942 		return false;
943 	return true;
944 }
945 
build_mapped_resource(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)946 static enum dc_status build_mapped_resource(
947 		const struct dc *dc,
948 		struct dc_state *context,
949 		struct dc_stream_state *stream)
950 {
951 	struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(&context->res_ctx, stream);
952 
953 	if (!pipe_ctx)
954 		return DC_ERROR_UNEXPECTED;
955 
956 	if (!is_surface_pixel_format_supported(pipe_ctx,
957 			dc->res_pool->underlay_pipe_index))
958 		return DC_SURFACE_PIXEL_FORMAT_UNSUPPORTED;
959 
960 	dce110_resource_build_pipe_hw_param(pipe_ctx);
961 
962 	/* TODO: validate audio ASIC caps, encoder */
963 
964 	resource_build_info_frame(pipe_ctx);
965 
966 	return DC_OK;
967 }
968 
dce110_validate_bandwidth(struct dc * dc,struct dc_state * context,enum dc_validate_mode validate_mode)969 static enum dc_status dce110_validate_bandwidth(
970 	struct dc *dc,
971 	struct dc_state *context,
972 	enum dc_validate_mode validate_mode)
973 {
974 	bool result = false;
975 
976 	DC_LOG_BANDWIDTH_CALCS(
977 		"%s: start",
978 		__func__);
979 
980 	if (bw_calcs(
981 			dc->ctx,
982 			dc->bw_dceip,
983 			dc->bw_vbios,
984 			context->res_ctx.pipe_ctx,
985 			dc->res_pool->pipe_count,
986 			&context->bw_ctx.bw.dce))
987 		result =  true;
988 
989 	if (!result)
990 		DC_LOG_BANDWIDTH_VALIDATION("%s: %dx%d@%d Bandwidth validation failed!\n",
991 			__func__,
992 			context->streams[0]->timing.h_addressable,
993 			context->streams[0]->timing.v_addressable,
994 			context->streams[0]->timing.pix_clk_100hz / 10);
995 
996 	if (memcmp(&dc->current_state->bw_ctx.bw.dce,
997 			&context->bw_ctx.bw.dce, sizeof(context->bw_ctx.bw.dce))) {
998 
999 		DC_LOG_BANDWIDTH_CALCS(
1000 			"%s: finish,\n"
1001 			"nbpMark_b: %d nbpMark_a: %d urgentMark_b: %d urgentMark_a: %d\n"
1002 			"stutMark_b: %d stutMark_a: %d\n"
1003 			"nbpMark_b: %d nbpMark_a: %d urgentMark_b: %d urgentMark_a: %d\n"
1004 			"stutMark_b: %d stutMark_a: %d\n"
1005 			"nbpMark_b: %d nbpMark_a: %d urgentMark_b: %d urgentMark_a: %d\n"
1006 			"stutMark_b: %d stutMark_a: %d stutter_mode_enable: %d\n"
1007 			"cstate: %d pstate: %d nbpstate: %d sync: %d dispclk: %d\n"
1008 			"sclk: %d sclk_sleep: %d yclk: %d blackout_recovery_time_us: %d\n"
1009 			,
1010 			__func__,
1011 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[0].b_mark,
1012 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[0].a_mark,
1013 			context->bw_ctx.bw.dce.urgent_wm_ns[0].b_mark,
1014 			context->bw_ctx.bw.dce.urgent_wm_ns[0].a_mark,
1015 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[0].b_mark,
1016 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[0].a_mark,
1017 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[1].b_mark,
1018 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[1].a_mark,
1019 			context->bw_ctx.bw.dce.urgent_wm_ns[1].b_mark,
1020 			context->bw_ctx.bw.dce.urgent_wm_ns[1].a_mark,
1021 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[1].b_mark,
1022 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[1].a_mark,
1023 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[2].b_mark,
1024 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[2].a_mark,
1025 			context->bw_ctx.bw.dce.urgent_wm_ns[2].b_mark,
1026 			context->bw_ctx.bw.dce.urgent_wm_ns[2].a_mark,
1027 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[2].b_mark,
1028 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[2].a_mark,
1029 			context->bw_ctx.bw.dce.stutter_mode_enable,
1030 			context->bw_ctx.bw.dce.cpuc_state_change_enable,
1031 			context->bw_ctx.bw.dce.cpup_state_change_enable,
1032 			context->bw_ctx.bw.dce.nbp_state_change_enable,
1033 			context->bw_ctx.bw.dce.all_displays_in_sync,
1034 			context->bw_ctx.bw.dce.dispclk_khz,
1035 			context->bw_ctx.bw.dce.sclk_khz,
1036 			context->bw_ctx.bw.dce.sclk_deep_sleep_khz,
1037 			context->bw_ctx.bw.dce.yclk_khz,
1038 			context->bw_ctx.bw.dce.blackout_recovery_time_us);
1039 	}
1040 	return result ? DC_OK : DC_FAIL_BANDWIDTH_VALIDATE;
1041 }
1042 
dce110_validate_plane(const struct dc_plane_state * plane_state,struct dc_caps * caps)1043 static enum dc_status dce110_validate_plane(const struct dc_plane_state *plane_state,
1044 					    struct dc_caps *caps)
1045 {
1046 	if (((plane_state->dst_rect.width * 2) < plane_state->src_rect.width) ||
1047 	    ((plane_state->dst_rect.height * 2) < plane_state->src_rect.height))
1048 		return DC_FAIL_SURFACE_VALIDATE;
1049 
1050 	return DC_OK;
1051 }
1052 
dce110_validate_surface_sets(struct dc_state * context)1053 static bool dce110_validate_surface_sets(
1054 		struct dc_state *context)
1055 {
1056 	int i, j;
1057 
1058 	for (i = 0; i < context->stream_count; i++) {
1059 		if (context->stream_status[i].plane_count == 0)
1060 			continue;
1061 
1062 		if (context->stream_status[i].plane_count > 2)
1063 			return false;
1064 
1065 		for (j = 0; j < context->stream_status[i].plane_count; j++) {
1066 			struct dc_plane_state *plane =
1067 				context->stream_status[i].plane_states[j];
1068 
1069 			/* underlay validation */
1070 			if (plane->format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
1071 
1072 				if ((plane->src_rect.width > 1920 ||
1073 					plane->src_rect.height > 1080))
1074 					return false;
1075 
1076 				/* we don't have the logic to support underlay
1077 				 * only yet so block the use case where we get
1078 				 * NV12 plane as top layer
1079 				 */
1080 				if (j == 0)
1081 					return false;
1082 
1083 				/* irrespective of plane format,
1084 				 * stream should be RGB encoded
1085 				 */
1086 				if (context->streams[i]->timing.pixel_encoding
1087 						!= PIXEL_ENCODING_RGB)
1088 					return false;
1089 
1090 			}
1091 
1092 		}
1093 	}
1094 
1095 	return true;
1096 }
1097 
dce110_validate_global(struct dc * dc,struct dc_state * context)1098 static enum dc_status dce110_validate_global(
1099 		struct dc *dc,
1100 		struct dc_state *context)
1101 {
1102 	if (!dce110_validate_surface_sets(context))
1103 		return DC_FAIL_SURFACE_VALIDATE;
1104 
1105 	return DC_OK;
1106 }
1107 
dce110_add_stream_to_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * dc_stream)1108 static enum dc_status dce110_add_stream_to_ctx(
1109 		struct dc *dc,
1110 		struct dc_state *new_ctx,
1111 		struct dc_stream_state *dc_stream)
1112 {
1113 	enum dc_status result = DC_ERROR_UNEXPECTED;
1114 
1115 	result = resource_map_pool_resources(dc, new_ctx, dc_stream);
1116 
1117 	if (result == DC_OK)
1118 		result = resource_map_clock_resources(dc, new_ctx, dc_stream);
1119 
1120 
1121 	if (result == DC_OK)
1122 		result = build_mapped_resource(dc, new_ctx, dc_stream);
1123 
1124 	return result;
1125 }
1126 
dce110_acquire_underlay(const struct dc_state * cur_ctx,struct dc_state * new_ctx,const struct resource_pool * pool,const struct pipe_ctx * opp_head_pipe)1127 static struct pipe_ctx *dce110_acquire_underlay(
1128 		const struct dc_state *cur_ctx,
1129 		struct dc_state *new_ctx,
1130 		const struct resource_pool *pool,
1131 		const struct pipe_ctx *opp_head_pipe)
1132 {
1133 	struct dc_stream_state *stream = opp_head_pipe->stream;
1134 	struct dc *dc = stream->ctx->dc;
1135 	struct dce_hwseq *hws = dc->hwseq;
1136 	struct resource_context *res_ctx = &new_ctx->res_ctx;
1137 	unsigned int underlay_idx = pool->underlay_pipe_index;
1138 	struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[underlay_idx];
1139 
1140 	if (res_ctx->pipe_ctx[underlay_idx].stream)
1141 		return NULL;
1142 
1143 	pipe_ctx->stream_res.tg = pool->timing_generators[underlay_idx];
1144 	pipe_ctx->plane_res.mi = pool->mis[underlay_idx];
1145 	/*pipe_ctx->plane_res.ipp = res_ctx->pool->ipps[underlay_idx];*/
1146 	pipe_ctx->plane_res.xfm = pool->transforms[underlay_idx];
1147 	pipe_ctx->stream_res.opp = pool->opps[underlay_idx];
1148 	pipe_ctx->pipe_idx = underlay_idx;
1149 
1150 	pipe_ctx->stream = stream;
1151 
1152 	if (!dc->current_state->res_ctx.pipe_ctx[underlay_idx].stream) {
1153 		struct tg_color black_color = {0};
1154 		struct dc_bios *dcb = dc->ctx->dc_bios;
1155 
1156 		hws->funcs.enable_display_power_gating(
1157 				dc,
1158 				pipe_ctx->stream_res.tg->inst,
1159 				dcb, PIPE_GATING_CONTROL_DISABLE);
1160 
1161 		/*
1162 		 * This is for powering on underlay, so crtc does not
1163 		 * need to be enabled
1164 		 */
1165 
1166 		pipe_ctx->stream_res.tg->funcs->program_timing(pipe_ctx->stream_res.tg,
1167 				&stream->timing,
1168 				0,
1169 				0,
1170 				0,
1171 				0,
1172 				0,
1173 				pipe_ctx->stream->signal,
1174 				false);
1175 
1176 		pipe_ctx->stream_res.tg->funcs->enable_advanced_request(
1177 				pipe_ctx->stream_res.tg,
1178 				true,
1179 				&stream->timing);
1180 
1181 		pipe_ctx->plane_res.mi->funcs->allocate_mem_input(pipe_ctx->plane_res.mi,
1182 				stream->timing.h_total,
1183 				stream->timing.v_total,
1184 				stream->timing.pix_clk_100hz / 10,
1185 				new_ctx->stream_count);
1186 
1187 		color_space_to_black_color(dc,
1188 				COLOR_SPACE_YCBCR601, &black_color);
1189 		pipe_ctx->stream_res.tg->funcs->set_blank_color(
1190 				pipe_ctx->stream_res.tg,
1191 				&black_color);
1192 	}
1193 
1194 	return pipe_ctx;
1195 }
1196 
dce110_destroy_resource_pool(struct resource_pool ** pool)1197 static void dce110_destroy_resource_pool(struct resource_pool **pool)
1198 {
1199 	struct dce110_resource_pool *dce110_pool = TO_DCE110_RES_POOL(*pool);
1200 
1201 	dce110_resource_destruct(dce110_pool);
1202 	kfree(dce110_pool);
1203 	*pool = NULL;
1204 }
1205 
dce110_find_first_free_match_stream_enc_for_link(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)1206 struct stream_encoder *dce110_find_first_free_match_stream_enc_for_link(
1207 		struct resource_context *res_ctx,
1208 		const struct resource_pool *pool,
1209 		struct dc_stream_state *stream)
1210 {
1211 	int i;
1212 	int j = -1;
1213 	struct dc_link *link = stream->link;
1214 
1215 	for (i = 0; i < pool->stream_enc_count; i++) {
1216 		if (!res_ctx->is_stream_enc_acquired[i] &&
1217 				pool->stream_enc[i]) {
1218 			/* Store first available for MST second display
1219 			 * in daisy chain use case
1220 			 */
1221 			j = i;
1222 			if (pool->stream_enc[i]->id ==
1223 					link->link_enc->preferred_engine)
1224 				return pool->stream_enc[i];
1225 		}
1226 	}
1227 
1228 	/*
1229 	 * For CZ and later, we can allow DIG FE and BE to differ for all display types
1230 	 */
1231 
1232 	if (j >= 0)
1233 		return pool->stream_enc[j];
1234 
1235 	return NULL;
1236 }
1237 
1238 
1239 static const struct resource_funcs dce110_res_pool_funcs = {
1240 	.destroy = dce110_destroy_resource_pool,
1241 	.link_enc_create = dce110_link_encoder_create,
1242 	.panel_cntl_create = dce110_panel_cntl_create,
1243 	.validate_bandwidth = dce110_validate_bandwidth,
1244 	.validate_plane = dce110_validate_plane,
1245 	.acquire_free_pipe_as_secondary_dpp_pipe = dce110_acquire_underlay,
1246 	.add_stream_to_ctx = dce110_add_stream_to_ctx,
1247 	.validate_global = dce110_validate_global,
1248 	.find_first_free_match_stream_enc_for_link = dce110_find_first_free_match_stream_enc_for_link
1249 };
1250 
underlay_create(struct dc_context * ctx,struct resource_pool * pool)1251 static bool underlay_create(struct dc_context *ctx, struct resource_pool *pool)
1252 {
1253 	struct dce110_timing_generator *dce110_tgv = kzalloc_obj(*dce110_tgv);
1254 	struct dce_transform *dce110_xfmv = kzalloc_obj(*dce110_xfmv);
1255 	struct dce_mem_input *dce110_miv = kzalloc_obj(*dce110_miv);
1256 	struct dce110_opp *dce110_oppv = kzalloc_obj(*dce110_oppv);
1257 
1258 	if (!dce110_tgv || !dce110_xfmv || !dce110_miv || !dce110_oppv) {
1259 		kfree(dce110_tgv);
1260 		kfree(dce110_xfmv);
1261 		kfree(dce110_miv);
1262 		kfree(dce110_oppv);
1263 		return false;
1264 	}
1265 
1266 	dce110_opp_v_construct(dce110_oppv, ctx);
1267 
1268 	dce110_timing_generator_v_construct(dce110_tgv, ctx);
1269 	dce110_mem_input_v_construct(dce110_miv, ctx);
1270 	dce110_transform_v_construct(dce110_xfmv, ctx);
1271 
1272 	pool->opps[pool->pipe_count] = &dce110_oppv->base;
1273 	pool->timing_generators[pool->pipe_count] = &dce110_tgv->base;
1274 	pool->mis[pool->pipe_count] = &dce110_miv->base;
1275 	pool->transforms[pool->pipe_count] = &dce110_xfmv->base;
1276 	pool->pipe_count++;
1277 
1278 	/* update the public caps to indicate an underlay is available */
1279 	ctx->dc->caps.max_slave_planes = 1;
1280 	ctx->dc->caps.max_slave_yuv_planes = 1;
1281 	ctx->dc->caps.max_slave_rgb_planes = 0;
1282 
1283 	return true;
1284 }
1285 
bw_calcs_data_update_from_pplib(struct dc * dc)1286 static void bw_calcs_data_update_from_pplib(struct dc *dc)
1287 {
1288 	struct dm_pp_clock_levels clks = {0};
1289 
1290 	/*do system clock*/
1291 	dm_pp_get_clock_levels_by_type(
1292 			dc->ctx,
1293 			DM_PP_CLOCK_TYPE_ENGINE_CLK,
1294 			&clks);
1295 	/* convert all the clock fro kHz to fix point mHz */
1296 	dc->bw_vbios->high_sclk = bw_frc_to_fixed(
1297 			clks.clocks_in_khz[clks.num_levels-1], 1000);
1298 	dc->bw_vbios->mid1_sclk  = bw_frc_to_fixed(
1299 			clks.clocks_in_khz[clks.num_levels/8], 1000);
1300 	dc->bw_vbios->mid2_sclk  = bw_frc_to_fixed(
1301 			clks.clocks_in_khz[clks.num_levels*2/8], 1000);
1302 	dc->bw_vbios->mid3_sclk  = bw_frc_to_fixed(
1303 			clks.clocks_in_khz[clks.num_levels*3/8], 1000);
1304 	dc->bw_vbios->mid4_sclk  = bw_frc_to_fixed(
1305 			clks.clocks_in_khz[clks.num_levels*4/8], 1000);
1306 	dc->bw_vbios->mid5_sclk  = bw_frc_to_fixed(
1307 			clks.clocks_in_khz[clks.num_levels*5/8], 1000);
1308 	dc->bw_vbios->mid6_sclk  = bw_frc_to_fixed(
1309 			clks.clocks_in_khz[clks.num_levels*6/8], 1000);
1310 	dc->bw_vbios->low_sclk  = bw_frc_to_fixed(
1311 			clks.clocks_in_khz[0], 1000);
1312 	dc->sclk_lvls = clks;
1313 
1314 	/*do display clock*/
1315 	dm_pp_get_clock_levels_by_type(
1316 			dc->ctx,
1317 			DM_PP_CLOCK_TYPE_DISPLAY_CLK,
1318 			&clks);
1319 	dc->bw_vbios->high_voltage_max_dispclk = bw_frc_to_fixed(
1320 			clks.clocks_in_khz[clks.num_levels-1], 1000);
1321 	dc->bw_vbios->mid_voltage_max_dispclk  = bw_frc_to_fixed(
1322 			clks.clocks_in_khz[clks.num_levels>>1], 1000);
1323 	dc->bw_vbios->low_voltage_max_dispclk  = bw_frc_to_fixed(
1324 			clks.clocks_in_khz[0], 1000);
1325 
1326 	/*do memory clock*/
1327 	dm_pp_get_clock_levels_by_type(
1328 			dc->ctx,
1329 			DM_PP_CLOCK_TYPE_MEMORY_CLK,
1330 			&clks);
1331 
1332 	dc->bw_vbios->low_yclk = bw_frc_to_fixed(
1333 		clks.clocks_in_khz[0] * MEMORY_TYPE_MULTIPLIER_CZ, 1000);
1334 	dc->bw_vbios->mid_yclk = bw_frc_to_fixed(
1335 		clks.clocks_in_khz[clks.num_levels>>1] * MEMORY_TYPE_MULTIPLIER_CZ,
1336 		1000);
1337 	dc->bw_vbios->high_yclk = bw_frc_to_fixed(
1338 		clks.clocks_in_khz[clks.num_levels-1] * MEMORY_TYPE_MULTIPLIER_CZ,
1339 		1000);
1340 }
1341 
dce110_resource_cap(struct hw_asic_id * asic_id)1342 static const struct resource_caps *dce110_resource_cap(
1343 	struct hw_asic_id *asic_id)
1344 {
1345 	if (ASIC_REV_IS_STONEY(asic_id->hw_internal_rev))
1346 		return &stoney_resource_cap;
1347 	else
1348 		return &carrizo_resource_cap;
1349 }
1350 
dce110_resource_construct(uint8_t num_virtual_links,struct dc * dc,struct dce110_resource_pool * pool,struct hw_asic_id asic_id)1351 static bool dce110_resource_construct(
1352 	uint8_t num_virtual_links,
1353 	struct dc *dc,
1354 	struct dce110_resource_pool *pool,
1355 	struct hw_asic_id asic_id)
1356 {
1357 	unsigned int i;
1358 	struct dc_context *ctx = dc->ctx;
1359 	struct dc_bios *bp;
1360 
1361 	ctx->dc_bios->regs = &bios_regs;
1362 
1363 	pool->base.res_cap = dce110_resource_cap(&ctx->asic_id);
1364 	pool->base.funcs = &dce110_res_pool_funcs;
1365 
1366 	/*************************************************
1367 	 *  Resource + asic cap harcoding                *
1368 	 *************************************************/
1369 
1370 	pool->base.pipe_count = pool->base.res_cap->num_timing_generator;
1371 	pool->base.underlay_pipe_index = pool->base.pipe_count;
1372 	pool->base.timing_generator_count = pool->base.res_cap->num_timing_generator;
1373 	dc->caps.max_downscale_ratio = 150;
1374 	dc->caps.i2c_speed_in_khz = 40;
1375 	dc->caps.i2c_speed_in_khz_hdcp = 40;
1376 	dc->caps.max_cursor_size = 128;
1377 	dc->caps.min_horizontal_blanking_period = 80;
1378 	dc->caps.is_apu = true;
1379 	dc->caps.extended_aux_timeout_support = false;
1380 	dc->debug = debug_defaults;
1381 	dc->check_config = config_defaults;
1382 
1383 	/*************************************************
1384 	 *  Create resources                             *
1385 	 *************************************************/
1386 
1387 	bp = ctx->dc_bios;
1388 
1389 	if (bp->fw_info_valid && bp->fw_info.external_clock_source_frequency_for_dp != 0) {
1390 		pool->base.dp_clock_source =
1391 				dce110_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_EXTERNAL, NULL, true);
1392 
1393 		pool->base.clock_sources[0] =
1394 				dce110_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL0,
1395 						&clk_src_regs[0], false);
1396 		pool->base.clock_sources[1] =
1397 				dce110_clock_source_create(ctx, bp, CLOCK_SOURCE_ID_PLL1,
1398 						&clk_src_regs[1], false);
1399 
1400 		pool->base.clk_src_count = 2;
1401 
1402 		/* TODO: find out if CZ support 3 PLLs */
1403 	}
1404 
1405 	if (pool->base.dp_clock_source == NULL) {
1406 		dm_error("DC: failed to create dp clock source!\n");
1407 		BREAK_TO_DEBUGGER();
1408 		goto res_create_fail;
1409 	}
1410 
1411 	for (i = 0; i < pool->base.clk_src_count; i++) {
1412 		if (pool->base.clock_sources[i] == NULL) {
1413 			dm_error("DC: failed to create clock sources!\n");
1414 			BREAK_TO_DEBUGGER();
1415 			goto res_create_fail;
1416 		}
1417 	}
1418 
1419 	pool->base.dmcu = dce_dmcu_create(ctx,
1420 			&dmcu_regs,
1421 			&dmcu_shift,
1422 			&dmcu_mask);
1423 	if (pool->base.dmcu == NULL) {
1424 		dm_error("DC: failed to create dmcu!\n");
1425 		BREAK_TO_DEBUGGER();
1426 		goto res_create_fail;
1427 	}
1428 
1429 	pool->base.abm = dce_abm_create(ctx,
1430 			&abm_regs,
1431 			&abm_shift,
1432 			&abm_mask);
1433 	if (pool->base.abm == NULL) {
1434 		dm_error("DC: failed to create abm!\n");
1435 		BREAK_TO_DEBUGGER();
1436 		goto res_create_fail;
1437 	}
1438 
1439 	{
1440 		struct irq_service_init_data init_data;
1441 		init_data.ctx = dc->ctx;
1442 		pool->base.irqs = dal_irq_service_dce110_create(&init_data);
1443 		if (!pool->base.irqs)
1444 			goto res_create_fail;
1445 	}
1446 
1447 	for (i = 0; i < pool->base.pipe_count; i++) {
1448 		pool->base.timing_generators[i] = dce110_timing_generator_create(
1449 				ctx, i, &dce110_tg_offsets[i]);
1450 		if (pool->base.timing_generators[i] == NULL) {
1451 			BREAK_TO_DEBUGGER();
1452 			dm_error("DC: failed to create tg!\n");
1453 			goto res_create_fail;
1454 		}
1455 
1456 		pool->base.mis[i] = dce110_mem_input_create(ctx, i);
1457 		if (pool->base.mis[i] == NULL) {
1458 			BREAK_TO_DEBUGGER();
1459 			dm_error(
1460 				"DC: failed to create memory input!\n");
1461 			goto res_create_fail;
1462 		}
1463 
1464 		pool->base.ipps[i] = dce110_ipp_create(ctx, i);
1465 		if (pool->base.ipps[i] == NULL) {
1466 			BREAK_TO_DEBUGGER();
1467 			dm_error(
1468 				"DC: failed to create input pixel processor!\n");
1469 			goto res_create_fail;
1470 		}
1471 
1472 		pool->base.transforms[i] = dce110_transform_create(ctx, i);
1473 		if (pool->base.transforms[i] == NULL) {
1474 			BREAK_TO_DEBUGGER();
1475 			dm_error(
1476 				"DC: failed to create transform!\n");
1477 			goto res_create_fail;
1478 		}
1479 
1480 		pool->base.opps[i] = dce110_opp_create(ctx, i);
1481 		if (pool->base.opps[i] == NULL) {
1482 			BREAK_TO_DEBUGGER();
1483 			dm_error(
1484 				"DC: failed to create output pixel processor!\n");
1485 			goto res_create_fail;
1486 		}
1487 	}
1488 
1489 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1490 		pool->base.engines[i] = dce110_aux_engine_create(ctx, i);
1491 		if (pool->base.engines[i] == NULL) {
1492 			BREAK_TO_DEBUGGER();
1493 			dm_error(
1494 				"DC:failed to create aux engine!!\n");
1495 			goto res_create_fail;
1496 		}
1497 		pool->base.hw_i2cs[i] = dce110_i2c_hw_create(ctx, i);
1498 		if (pool->base.hw_i2cs[i] == NULL) {
1499 			BREAK_TO_DEBUGGER();
1500 			dm_error(
1501 				"DC:failed to create i2c engine!!\n");
1502 			goto res_create_fail;
1503 		}
1504 		pool->base.sw_i2cs[i] = NULL;
1505 	}
1506 
1507 	if (dc->config.fbc_support)
1508 		dc->fbc_compressor = dce110_compressor_create(ctx);
1509 
1510 	if (!underlay_create(ctx, &pool->base))
1511 		goto res_create_fail;
1512 
1513 	if (!resource_construct(num_virtual_links, dc, &pool->base,
1514 			&res_create_funcs))
1515 		goto res_create_fail;
1516 
1517 	/* Create hardware sequencer */
1518 	dce110_hw_sequencer_construct(dc);
1519 
1520 	dc->caps.max_planes =  pool->base.pipe_count;
1521 
1522 	for (i = 0; i < pool->base.underlay_pipe_index; ++i)
1523 		dc->caps.planes[i] = plane_cap;
1524 
1525 	dc->caps.planes[pool->base.underlay_pipe_index] = underlay_plane_cap;
1526 
1527 	bw_calcs_init(dc->bw_dceip, dc->bw_vbios, dc->ctx->asic_id);
1528 
1529 	bw_calcs_data_update_from_pplib(dc);
1530 
1531 	return true;
1532 
1533 res_create_fail:
1534 	dce110_resource_destruct(pool);
1535 	return false;
1536 }
1537 
dce110_create_resource_pool(uint8_t num_virtual_links,struct dc * dc,struct hw_asic_id asic_id)1538 struct resource_pool *dce110_create_resource_pool(
1539 	uint8_t num_virtual_links,
1540 	struct dc *dc,
1541 	struct hw_asic_id asic_id)
1542 {
1543 	struct dce110_resource_pool *pool =
1544 		kzalloc_obj(struct dce110_resource_pool);
1545 
1546 	if (!pool)
1547 		return NULL;
1548 
1549 	if (dce110_resource_construct(num_virtual_links, dc, pool, asic_id))
1550 		return &pool->base;
1551 
1552 	kfree(pool);
1553 	BREAK_TO_DEBUGGER();
1554 	return NULL;
1555 }
1556