xref: /linux/drivers/gpu/drm/amd/amdgpu/dce_v8_0.c (revision 994aeacbb3c039b4f3e02e76e6d39407920e76c6)
1a2e73f56SAlex Deucher /*
2a2e73f56SAlex Deucher  * Copyright 2014 Advanced Micro Devices, Inc.
3a2e73f56SAlex Deucher  *
4a2e73f56SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
5a2e73f56SAlex Deucher  * copy of this software and associated documentation files (the "Software"),
6a2e73f56SAlex Deucher  * to deal in the Software without restriction, including without limitation
7a2e73f56SAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a2e73f56SAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
9a2e73f56SAlex Deucher  * Software is furnished to do so, subject to the following conditions:
10a2e73f56SAlex Deucher  *
11a2e73f56SAlex Deucher  * The above copyright notice and this permission notice shall be included in
12a2e73f56SAlex Deucher  * all copies or substantial portions of the Software.
13a2e73f56SAlex Deucher  *
14a2e73f56SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15a2e73f56SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16a2e73f56SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17a2e73f56SAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18a2e73f56SAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19a2e73f56SAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20a2e73f56SAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
21a2e73f56SAlex Deucher  *
22a2e73f56SAlex Deucher  */
2347b757fbSSam Ravnborg 
24d9501844SJani Nikula #include <drm/drm_edid.h>
2547b757fbSSam Ravnborg #include <drm/drm_fourcc.h>
26973ad627SThomas Zimmermann #include <drm/drm_modeset_helper.h>
27973ad627SThomas Zimmermann #include <drm/drm_modeset_helper_vtables.h>
2847b757fbSSam Ravnborg #include <drm/drm_vblank.h>
2947b757fbSSam Ravnborg 
30a2e73f56SAlex Deucher #include "amdgpu.h"
31a2e73f56SAlex Deucher #include "amdgpu_pm.h"
32a2e73f56SAlex Deucher #include "amdgpu_i2c.h"
33a2e73f56SAlex Deucher #include "cikd.h"
34a2e73f56SAlex Deucher #include "atom.h"
35a2e73f56SAlex Deucher #include "amdgpu_atombios.h"
36a2e73f56SAlex Deucher #include "atombios_crtc.h"
37a2e73f56SAlex Deucher #include "atombios_encoders.h"
38a2e73f56SAlex Deucher #include "amdgpu_pll.h"
39a2e73f56SAlex Deucher #include "amdgpu_connectors.h"
405df58525SHuang Rui #include "amdgpu_display.h"
41356aee30SBaoyou Xie #include "dce_v8_0.h"
42a2e73f56SAlex Deucher 
43a2e73f56SAlex Deucher #include "dce/dce_8_0_d.h"
44a2e73f56SAlex Deucher #include "dce/dce_8_0_sh_mask.h"
45a2e73f56SAlex Deucher 
46a2e73f56SAlex Deucher #include "gca/gfx_7_2_enum.h"
47a2e73f56SAlex Deucher 
48a2e73f56SAlex Deucher #include "gmc/gmc_7_1_d.h"
49a2e73f56SAlex Deucher #include "gmc/gmc_7_1_sh_mask.h"
50a2e73f56SAlex Deucher 
51a2e73f56SAlex Deucher #include "oss/oss_2_0_d.h"
52a2e73f56SAlex Deucher #include "oss/oss_2_0_sh_mask.h"
53a2e73f56SAlex Deucher 
54a2e73f56SAlex Deucher static void dce_v8_0_set_display_funcs(struct amdgpu_device *adev);
55a2e73f56SAlex Deucher static void dce_v8_0_set_irq_funcs(struct amdgpu_device *adev);
56a2e73f56SAlex Deucher 
5718ef7544SRan Sun static const u32 crtc_offsets[6] = {
58a2e73f56SAlex Deucher 	CRTC0_REGISTER_OFFSET,
59a2e73f56SAlex Deucher 	CRTC1_REGISTER_OFFSET,
60a2e73f56SAlex Deucher 	CRTC2_REGISTER_OFFSET,
61a2e73f56SAlex Deucher 	CRTC3_REGISTER_OFFSET,
62a2e73f56SAlex Deucher 	CRTC4_REGISTER_OFFSET,
63a2e73f56SAlex Deucher 	CRTC5_REGISTER_OFFSET
64a2e73f56SAlex Deucher };
65a2e73f56SAlex Deucher 
6618ef7544SRan Sun static const u32 hpd_offsets[] = {
672285b91cSAlex Deucher 	HPD0_REGISTER_OFFSET,
682285b91cSAlex Deucher 	HPD1_REGISTER_OFFSET,
692285b91cSAlex Deucher 	HPD2_REGISTER_OFFSET,
702285b91cSAlex Deucher 	HPD3_REGISTER_OFFSET,
712285b91cSAlex Deucher 	HPD4_REGISTER_OFFSET,
722285b91cSAlex Deucher 	HPD5_REGISTER_OFFSET
732285b91cSAlex Deucher };
742285b91cSAlex Deucher 
75a2e73f56SAlex Deucher static const uint32_t dig_offsets[] = {
76a2e73f56SAlex Deucher 	CRTC0_REGISTER_OFFSET,
77a2e73f56SAlex Deucher 	CRTC1_REGISTER_OFFSET,
78a2e73f56SAlex Deucher 	CRTC2_REGISTER_OFFSET,
79a2e73f56SAlex Deucher 	CRTC3_REGISTER_OFFSET,
80a2e73f56SAlex Deucher 	CRTC4_REGISTER_OFFSET,
81a2e73f56SAlex Deucher 	CRTC5_REGISTER_OFFSET,
82a2e73f56SAlex Deucher 	(0x13830 - 0x7030) >> 2,
83a2e73f56SAlex Deucher };
84a2e73f56SAlex Deucher 
85a2e73f56SAlex Deucher static const struct {
86a2e73f56SAlex Deucher 	uint32_t	reg;
87a2e73f56SAlex Deucher 	uint32_t	vblank;
88a2e73f56SAlex Deucher 	uint32_t	vline;
89a2e73f56SAlex Deucher 	uint32_t	hpd;
90a2e73f56SAlex Deucher 
91a2e73f56SAlex Deucher } interrupt_status_offsets[6] = { {
92a2e73f56SAlex Deucher 	.reg = mmDISP_INTERRUPT_STATUS,
93a2e73f56SAlex Deucher 	.vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
94a2e73f56SAlex Deucher 	.vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
95a2e73f56SAlex Deucher 	.hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
96a2e73f56SAlex Deucher }, {
97a2e73f56SAlex Deucher 	.reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
98a2e73f56SAlex Deucher 	.vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
99a2e73f56SAlex Deucher 	.vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
100a2e73f56SAlex Deucher 	.hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
101a2e73f56SAlex Deucher }, {
102a2e73f56SAlex Deucher 	.reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
103a2e73f56SAlex Deucher 	.vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
104a2e73f56SAlex Deucher 	.vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
105a2e73f56SAlex Deucher 	.hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
106a2e73f56SAlex Deucher }, {
107a2e73f56SAlex Deucher 	.reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
108a2e73f56SAlex Deucher 	.vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
109a2e73f56SAlex Deucher 	.vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
110a2e73f56SAlex Deucher 	.hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
111a2e73f56SAlex Deucher }, {
112a2e73f56SAlex Deucher 	.reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
113a2e73f56SAlex Deucher 	.vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
114a2e73f56SAlex Deucher 	.vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
115a2e73f56SAlex Deucher 	.hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
116a2e73f56SAlex Deucher }, {
117a2e73f56SAlex Deucher 	.reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
118a2e73f56SAlex Deucher 	.vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
119a2e73f56SAlex Deucher 	.vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
120a2e73f56SAlex Deucher 	.hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
121a2e73f56SAlex Deucher } };
122a2e73f56SAlex Deucher 
dce_v8_0_audio_endpt_rreg(struct amdgpu_device * adev,u32 block_offset,u32 reg)123a2e73f56SAlex Deucher static u32 dce_v8_0_audio_endpt_rreg(struct amdgpu_device *adev,
124a2e73f56SAlex Deucher 				     u32 block_offset, u32 reg)
125a2e73f56SAlex Deucher {
126a2e73f56SAlex Deucher 	unsigned long flags;
127a2e73f56SAlex Deucher 	u32 r;
128a2e73f56SAlex Deucher 
129a2e73f56SAlex Deucher 	spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
130a2e73f56SAlex Deucher 	WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
131a2e73f56SAlex Deucher 	r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
132a2e73f56SAlex Deucher 	spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
133a2e73f56SAlex Deucher 
134a2e73f56SAlex Deucher 	return r;
135a2e73f56SAlex Deucher }
136a2e73f56SAlex Deucher 
dce_v8_0_audio_endpt_wreg(struct amdgpu_device * adev,u32 block_offset,u32 reg,u32 v)137a2e73f56SAlex Deucher static void dce_v8_0_audio_endpt_wreg(struct amdgpu_device *adev,
138a2e73f56SAlex Deucher 				      u32 block_offset, u32 reg, u32 v)
139a2e73f56SAlex Deucher {
140a2e73f56SAlex Deucher 	unsigned long flags;
141a2e73f56SAlex Deucher 
142a2e73f56SAlex Deucher 	spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
143a2e73f56SAlex Deucher 	WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
144a2e73f56SAlex Deucher 	WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
145a2e73f56SAlex Deucher 	spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
146a2e73f56SAlex Deucher }
147a2e73f56SAlex Deucher 
dce_v8_0_vblank_get_counter(struct amdgpu_device * adev,int crtc)148a2e73f56SAlex Deucher static u32 dce_v8_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
149a2e73f56SAlex Deucher {
150a2e73f56SAlex Deucher 	if (crtc >= adev->mode_info.num_crtc)
151a2e73f56SAlex Deucher 		return 0;
152a2e73f56SAlex Deucher 	else
153a2e73f56SAlex Deucher 		return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
154a2e73f56SAlex Deucher }
155a2e73f56SAlex Deucher 
dce_v8_0_pageflip_interrupt_init(struct amdgpu_device * adev)156f6c7aba4SMichel Dänzer static void dce_v8_0_pageflip_interrupt_init(struct amdgpu_device *adev)
157f6c7aba4SMichel Dänzer {
158f6c7aba4SMichel Dänzer 	unsigned i;
159f6c7aba4SMichel Dänzer 
160f6c7aba4SMichel Dänzer 	/* Enable pflip interrupts */
161f6c7aba4SMichel Dänzer 	for (i = 0; i < adev->mode_info.num_crtc; i++)
162f6c7aba4SMichel Dänzer 		amdgpu_irq_get(adev, &adev->pageflip_irq, i);
163f6c7aba4SMichel Dänzer }
164f6c7aba4SMichel Dänzer 
dce_v8_0_pageflip_interrupt_fini(struct amdgpu_device * adev)165f6c7aba4SMichel Dänzer static void dce_v8_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
166f6c7aba4SMichel Dänzer {
167f6c7aba4SMichel Dänzer 	unsigned i;
168f6c7aba4SMichel Dänzer 
169f6c7aba4SMichel Dänzer 	/* Disable pflip interrupts */
170f6c7aba4SMichel Dänzer 	for (i = 0; i < adev->mode_info.num_crtc; i++)
171f6c7aba4SMichel Dänzer 		amdgpu_irq_put(adev, &adev->pageflip_irq, i);
172f6c7aba4SMichel Dänzer }
173f6c7aba4SMichel Dänzer 
174a2e73f56SAlex Deucher /**
175a2e73f56SAlex Deucher  * dce_v8_0_page_flip - pageflip callback.
176a2e73f56SAlex Deucher  *
177a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
178a2e73f56SAlex Deucher  * @crtc_id: crtc to cleanup pageflip on
179a2e73f56SAlex Deucher  * @crtc_base: new address of the crtc (GPU MC address)
1802b7a8cf3SLee Jones  * @async: asynchronous flip
181a2e73f56SAlex Deucher  *
18282326860SAlex Deucher  * Triggers the actual pageflip by updating the primary
18382326860SAlex Deucher  * surface base address.
184a2e73f56SAlex Deucher  */
dce_v8_0_page_flip(struct amdgpu_device * adev,int crtc_id,u64 crtc_base,bool async)185a2e73f56SAlex Deucher static void dce_v8_0_page_flip(struct amdgpu_device *adev,
186cb9e59d7SAlex Deucher 			       int crtc_id, u64 crtc_base, bool async)
187a2e73f56SAlex Deucher {
188a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
189965ebe3dSMichel Dänzer 	struct drm_framebuffer *fb = amdgpu_crtc->base.primary->fb;
190a2e73f56SAlex Deucher 
191cb9e59d7SAlex Deucher 	/* flip at hsync for async, default is vsync */
192cb9e59d7SAlex Deucher 	WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, async ?
193cb9e59d7SAlex Deucher 	       GRPH_FLIP_CONTROL__GRPH_SURFACE_UPDATE_H_RETRACE_EN_MASK : 0);
194965ebe3dSMichel Dänzer 	/* update pitch */
195965ebe3dSMichel Dänzer 	WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset,
196965ebe3dSMichel Dänzer 	       fb->pitches[0] / fb->format->cpp[0]);
19782326860SAlex Deucher 	/* update the primary scanout addresses */
198a2e73f56SAlex Deucher 	WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
199a2e73f56SAlex Deucher 	       upper_32_bits(crtc_base));
20082326860SAlex Deucher 	/* writing to the low address triggers the update */
201a2e73f56SAlex Deucher 	WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
20282326860SAlex Deucher 	       lower_32_bits(crtc_base));
20382326860SAlex Deucher 	/* post the write */
20482326860SAlex Deucher 	RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
205a2e73f56SAlex Deucher }
206a2e73f56SAlex Deucher 
dce_v8_0_crtc_get_scanoutpos(struct amdgpu_device * adev,int crtc,u32 * vbl,u32 * position)207a2e73f56SAlex Deucher static int dce_v8_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
208a2e73f56SAlex Deucher 					u32 *vbl, u32 *position)
209a2e73f56SAlex Deucher {
210a2e73f56SAlex Deucher 	if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
211a2e73f56SAlex Deucher 		return -EINVAL;
212a2e73f56SAlex Deucher 
213a2e73f56SAlex Deucher 	*vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
214a2e73f56SAlex Deucher 	*position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
215a2e73f56SAlex Deucher 
216a2e73f56SAlex Deucher 	return 0;
217a2e73f56SAlex Deucher }
218a2e73f56SAlex Deucher 
219a2e73f56SAlex Deucher /**
220a2e73f56SAlex Deucher  * dce_v8_0_hpd_sense - hpd sense callback.
221a2e73f56SAlex Deucher  *
222a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
223a2e73f56SAlex Deucher  * @hpd: hpd (hotplug detect) pin
224a2e73f56SAlex Deucher  *
225a2e73f56SAlex Deucher  * Checks if a digital monitor is connected (evergreen+).
226a2e73f56SAlex Deucher  * Returns true if connected, false if not connected.
227a2e73f56SAlex Deucher  */
dce_v8_0_hpd_sense(struct amdgpu_device * adev,enum amdgpu_hpd_id hpd)228a2e73f56SAlex Deucher static bool dce_v8_0_hpd_sense(struct amdgpu_device *adev,
229a2e73f56SAlex Deucher 			       enum amdgpu_hpd_id hpd)
230a2e73f56SAlex Deucher {
231a2e73f56SAlex Deucher 	bool connected = false;
232a2e73f56SAlex Deucher 
2332285b91cSAlex Deucher 	if (hpd >= adev->mode_info.num_hpd)
2342285b91cSAlex Deucher 		return connected;
2352285b91cSAlex Deucher 
2362285b91cSAlex Deucher 	if (RREG32(mmDC_HPD1_INT_STATUS + hpd_offsets[hpd]) &
2372285b91cSAlex Deucher 	    DC_HPD1_INT_STATUS__DC_HPD1_SENSE_MASK)
238a2e73f56SAlex Deucher 		connected = true;
239a2e73f56SAlex Deucher 
240a2e73f56SAlex Deucher 	return connected;
241a2e73f56SAlex Deucher }
242a2e73f56SAlex Deucher 
243a2e73f56SAlex Deucher /**
244a2e73f56SAlex Deucher  * dce_v8_0_hpd_set_polarity - hpd set polarity callback.
245a2e73f56SAlex Deucher  *
246a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
247a2e73f56SAlex Deucher  * @hpd: hpd (hotplug detect) pin
248a2e73f56SAlex Deucher  *
249a2e73f56SAlex Deucher  * Set the polarity of the hpd pin (evergreen+).
250a2e73f56SAlex Deucher  */
dce_v8_0_hpd_set_polarity(struct amdgpu_device * adev,enum amdgpu_hpd_id hpd)251a2e73f56SAlex Deucher static void dce_v8_0_hpd_set_polarity(struct amdgpu_device *adev,
252a2e73f56SAlex Deucher 				      enum amdgpu_hpd_id hpd)
253a2e73f56SAlex Deucher {
254a2e73f56SAlex Deucher 	u32 tmp;
255a2e73f56SAlex Deucher 	bool connected = dce_v8_0_hpd_sense(adev, hpd);
256a2e73f56SAlex Deucher 
2572285b91cSAlex Deucher 	if (hpd >= adev->mode_info.num_hpd)
2582285b91cSAlex Deucher 		return;
2592285b91cSAlex Deucher 
2602285b91cSAlex Deucher 	tmp = RREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[hpd]);
261a2e73f56SAlex Deucher 	if (connected)
262a2e73f56SAlex Deucher 		tmp &= ~DC_HPD1_INT_CONTROL__DC_HPD1_INT_POLARITY_MASK;
263a2e73f56SAlex Deucher 	else
264a2e73f56SAlex Deucher 		tmp |= DC_HPD1_INT_CONTROL__DC_HPD1_INT_POLARITY_MASK;
2652285b91cSAlex Deucher 	WREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[hpd], tmp);
266a2e73f56SAlex Deucher }
267a2e73f56SAlex Deucher 
dce_v8_0_hpd_int_ack(struct amdgpu_device * adev,int hpd)268aeaf3e6cSQiang Ma static void dce_v8_0_hpd_int_ack(struct amdgpu_device *adev,
269aeaf3e6cSQiang Ma 				 int hpd)
270aeaf3e6cSQiang Ma {
271aeaf3e6cSQiang Ma 	u32 tmp;
272aeaf3e6cSQiang Ma 
273aeaf3e6cSQiang Ma 	if (hpd >= adev->mode_info.num_hpd) {
274aeaf3e6cSQiang Ma 		DRM_DEBUG("invalid hdp %d\n", hpd);
275aeaf3e6cSQiang Ma 		return;
276aeaf3e6cSQiang Ma 	}
277aeaf3e6cSQiang Ma 
278aeaf3e6cSQiang Ma 	tmp = RREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[hpd]);
279aeaf3e6cSQiang Ma 	tmp |= DC_HPD1_INT_CONTROL__DC_HPD1_INT_ACK_MASK;
280aeaf3e6cSQiang Ma 	WREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[hpd], tmp);
281aeaf3e6cSQiang Ma }
282aeaf3e6cSQiang Ma 
283a2e73f56SAlex Deucher /**
284a2e73f56SAlex Deucher  * dce_v8_0_hpd_init - hpd setup callback.
285a2e73f56SAlex Deucher  *
286a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
287a2e73f56SAlex Deucher  *
288a2e73f56SAlex Deucher  * Setup the hpd pins used by the card (evergreen+).
289a2e73f56SAlex Deucher  * Enable the pin, set the polarity, and enable the hpd interrupts.
290a2e73f56SAlex Deucher  */
dce_v8_0_hpd_init(struct amdgpu_device * adev)291a2e73f56SAlex Deucher static void dce_v8_0_hpd_init(struct amdgpu_device *adev)
292a2e73f56SAlex Deucher {
2934a580877SLuben Tuikov 	struct drm_device *dev = adev_to_drm(adev);
294a2e73f56SAlex Deucher 	struct drm_connector *connector;
295f8d2d39eSLyude Paul 	struct drm_connector_list_iter iter;
29603ae23b9SAlex Deucher 	u32 tmp;
297a2e73f56SAlex Deucher 
298f8d2d39eSLyude Paul 	drm_connector_list_iter_begin(dev, &iter);
299f8d2d39eSLyude Paul 	drm_for_each_connector_iter(connector, &iter) {
300a2e73f56SAlex Deucher 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
301a2e73f56SAlex Deucher 
3022285b91cSAlex Deucher 		if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
3032285b91cSAlex Deucher 			continue;
3042285b91cSAlex Deucher 
30503ae23b9SAlex Deucher 		tmp = RREG32(mmDC_HPD1_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
30603ae23b9SAlex Deucher 		tmp |= DC_HPD1_CONTROL__DC_HPD1_EN_MASK;
3072285b91cSAlex Deucher 		WREG32(mmDC_HPD1_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
30832408258SAlex Deucher 
30932408258SAlex Deucher 		if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
31032408258SAlex Deucher 		    connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
31132408258SAlex Deucher 			/* don't try to enable hpd on eDP or LVDS avoid breaking the
31232408258SAlex Deucher 			 * aux dp channel on imac and help (but not completely fix)
31332408258SAlex Deucher 			 * https://bugzilla.redhat.com/show_bug.cgi?id=726143
31432408258SAlex Deucher 			 * also avoid interrupt storms during dpms.
31532408258SAlex Deucher 			 */
3162285b91cSAlex Deucher 			tmp = RREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
3172285b91cSAlex Deucher 			tmp &= ~DC_HPD1_INT_CONTROL__DC_HPD1_INT_EN_MASK;
3182285b91cSAlex Deucher 			WREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
31932408258SAlex Deucher 			continue;
32032408258SAlex Deucher 		}
32132408258SAlex Deucher 
322aeaf3e6cSQiang Ma 		dce_v8_0_hpd_int_ack(adev, amdgpu_connector->hpd.hpd);
323a2e73f56SAlex Deucher 		dce_v8_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
324a2e73f56SAlex Deucher 		amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
325a2e73f56SAlex Deucher 	}
326f8d2d39eSLyude Paul 	drm_connector_list_iter_end(&iter);
327a2e73f56SAlex Deucher }
328a2e73f56SAlex Deucher 
329a2e73f56SAlex Deucher /**
330a2e73f56SAlex Deucher  * dce_v8_0_hpd_fini - hpd tear down callback.
331a2e73f56SAlex Deucher  *
332a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
333a2e73f56SAlex Deucher  *
334a2e73f56SAlex Deucher  * Tear down the hpd pins used by the card (evergreen+).
335a2e73f56SAlex Deucher  * Disable the hpd interrupts.
336a2e73f56SAlex Deucher  */
dce_v8_0_hpd_fini(struct amdgpu_device * adev)337a2e73f56SAlex Deucher static void dce_v8_0_hpd_fini(struct amdgpu_device *adev)
338a2e73f56SAlex Deucher {
3394a580877SLuben Tuikov 	struct drm_device *dev = adev_to_drm(adev);
340a2e73f56SAlex Deucher 	struct drm_connector *connector;
341f8d2d39eSLyude Paul 	struct drm_connector_list_iter iter;
34203ae23b9SAlex Deucher 	u32 tmp;
343a2e73f56SAlex Deucher 
344f8d2d39eSLyude Paul 	drm_connector_list_iter_begin(dev, &iter);
345f8d2d39eSLyude Paul 	drm_for_each_connector_iter(connector, &iter) {
346a2e73f56SAlex Deucher 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
347a2e73f56SAlex Deucher 
3482285b91cSAlex Deucher 		if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
3492285b91cSAlex Deucher 			continue;
3502285b91cSAlex Deucher 
35103ae23b9SAlex Deucher 		tmp = RREG32(mmDC_HPD1_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
35203ae23b9SAlex Deucher 		tmp &= ~DC_HPD1_CONTROL__DC_HPD1_EN_MASK;
35340835624SMaíra Canal 		WREG32(mmDC_HPD1_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
3542285b91cSAlex Deucher 
355a2e73f56SAlex Deucher 		amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
356a2e73f56SAlex Deucher 	}
357f8d2d39eSLyude Paul 	drm_connector_list_iter_end(&iter);
358a2e73f56SAlex Deucher }
359a2e73f56SAlex Deucher 
dce_v8_0_hpd_get_gpio_reg(struct amdgpu_device * adev)360a2e73f56SAlex Deucher static u32 dce_v8_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
361a2e73f56SAlex Deucher {
362a2e73f56SAlex Deucher 	return mmDC_GPIO_HPD_A;
363a2e73f56SAlex Deucher }
364a2e73f56SAlex Deucher 
dce_v8_0_is_display_hung(struct amdgpu_device * adev)365a2e73f56SAlex Deucher static bool dce_v8_0_is_display_hung(struct amdgpu_device *adev)
366a2e73f56SAlex Deucher {
367a2e73f56SAlex Deucher 	u32 crtc_hung = 0;
368a2e73f56SAlex Deucher 	u32 crtc_status[6];
369a2e73f56SAlex Deucher 	u32 i, j, tmp;
370a2e73f56SAlex Deucher 
371a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
372a2e73f56SAlex Deucher 		if (RREG32(mmCRTC_CONTROL + crtc_offsets[i]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK) {
373a2e73f56SAlex Deucher 			crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
374a2e73f56SAlex Deucher 			crtc_hung |= (1 << i);
375a2e73f56SAlex Deucher 		}
376a2e73f56SAlex Deucher 	}
377a2e73f56SAlex Deucher 
378a2e73f56SAlex Deucher 	for (j = 0; j < 10; j++) {
379a2e73f56SAlex Deucher 		for (i = 0; i < adev->mode_info.num_crtc; i++) {
380a2e73f56SAlex Deucher 			if (crtc_hung & (1 << i)) {
381a2e73f56SAlex Deucher 				tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
382a2e73f56SAlex Deucher 				if (tmp != crtc_status[i])
383a2e73f56SAlex Deucher 					crtc_hung &= ~(1 << i);
384a2e73f56SAlex Deucher 			}
385a2e73f56SAlex Deucher 		}
386a2e73f56SAlex Deucher 		if (crtc_hung == 0)
387a2e73f56SAlex Deucher 			return false;
388a2e73f56SAlex Deucher 		udelay(100);
389a2e73f56SAlex Deucher 	}
390a2e73f56SAlex Deucher 
391a2e73f56SAlex Deucher 	return true;
392a2e73f56SAlex Deucher }
393a2e73f56SAlex Deucher 
dce_v8_0_set_vga_render_state(struct amdgpu_device * adev,bool render)394a2e73f56SAlex Deucher static void dce_v8_0_set_vga_render_state(struct amdgpu_device *adev,
395a2e73f56SAlex Deucher 					  bool render)
396a2e73f56SAlex Deucher {
397a2e73f56SAlex Deucher 	u32 tmp;
398a2e73f56SAlex Deucher 
399a2e73f56SAlex Deucher 	/* Lockout access through VGA aperture*/
400a2e73f56SAlex Deucher 	tmp = RREG32(mmVGA_HDP_CONTROL);
401a2e73f56SAlex Deucher 	if (render)
402a2e73f56SAlex Deucher 		tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
403a2e73f56SAlex Deucher 	else
404a2e73f56SAlex Deucher 		tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
405a2e73f56SAlex Deucher 	WREG32(mmVGA_HDP_CONTROL, tmp);
406a2e73f56SAlex Deucher 
407a2e73f56SAlex Deucher 	/* disable VGA render */
408a2e73f56SAlex Deucher 	tmp = RREG32(mmVGA_RENDER_CONTROL);
409a2e73f56SAlex Deucher 	if (render)
410a2e73f56SAlex Deucher 		tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
411a2e73f56SAlex Deucher 	else
412a2e73f56SAlex Deucher 		tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
413a2e73f56SAlex Deucher 	WREG32(mmVGA_RENDER_CONTROL, tmp);
414a2e73f56SAlex Deucher }
415a2e73f56SAlex Deucher 
dce_v8_0_get_num_crtc(struct amdgpu_device * adev)41683c9b025SEmily Deng static int dce_v8_0_get_num_crtc(struct amdgpu_device *adev)
41783c9b025SEmily Deng {
41883c9b025SEmily Deng 	int num_crtc = 0;
41983c9b025SEmily Deng 
42083c9b025SEmily Deng 	switch (adev->asic_type) {
42183c9b025SEmily Deng 	case CHIP_BONAIRE:
42283c9b025SEmily Deng 	case CHIP_HAWAII:
42383c9b025SEmily Deng 		num_crtc = 6;
42483c9b025SEmily Deng 		break;
42583c9b025SEmily Deng 	case CHIP_KAVERI:
42683c9b025SEmily Deng 		num_crtc = 4;
42783c9b025SEmily Deng 		break;
42883c9b025SEmily Deng 	case CHIP_KABINI:
42983c9b025SEmily Deng 	case CHIP_MULLINS:
43083c9b025SEmily Deng 		num_crtc = 2;
43183c9b025SEmily Deng 		break;
43283c9b025SEmily Deng 	default:
43383c9b025SEmily Deng 		num_crtc = 0;
43483c9b025SEmily Deng 	}
43583c9b025SEmily Deng 	return num_crtc;
43683c9b025SEmily Deng }
43783c9b025SEmily Deng 
dce_v8_0_disable_dce(struct amdgpu_device * adev)43883c9b025SEmily Deng void dce_v8_0_disable_dce(struct amdgpu_device *adev)
43983c9b025SEmily Deng {
44083c9b025SEmily Deng 	/*Disable VGA render and enabled crtc, if has DCE engine*/
44183c9b025SEmily Deng 	if (amdgpu_atombios_has_dce_engine_info(adev)) {
44283c9b025SEmily Deng 		u32 tmp;
44383c9b025SEmily Deng 		int crtc_enabled, i;
44483c9b025SEmily Deng 
44583c9b025SEmily Deng 		dce_v8_0_set_vga_render_state(adev, false);
44683c9b025SEmily Deng 
44783c9b025SEmily Deng 		/*Disable crtc*/
44883c9b025SEmily Deng 		for (i = 0; i < dce_v8_0_get_num_crtc(adev); i++) {
44983c9b025SEmily Deng 			crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
45083c9b025SEmily Deng 									 CRTC_CONTROL, CRTC_MASTER_EN);
45183c9b025SEmily Deng 			if (crtc_enabled) {
45283c9b025SEmily Deng 				WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
45383c9b025SEmily Deng 				tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
45483c9b025SEmily Deng 				tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
45583c9b025SEmily Deng 				WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
45683c9b025SEmily Deng 				WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
45783c9b025SEmily Deng 			}
45883c9b025SEmily Deng 		}
45983c9b025SEmily Deng 	}
46083c9b025SEmily Deng }
46183c9b025SEmily Deng 
dce_v8_0_program_fmt(struct drm_encoder * encoder)462a2e73f56SAlex Deucher static void dce_v8_0_program_fmt(struct drm_encoder *encoder)
463a2e73f56SAlex Deucher {
464a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
4651348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
466a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
467a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
468a2e73f56SAlex Deucher 	struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
469a2e73f56SAlex Deucher 	int bpc = 0;
470a2e73f56SAlex Deucher 	u32 tmp = 0;
471a2e73f56SAlex Deucher 	enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
472a2e73f56SAlex Deucher 
473a2e73f56SAlex Deucher 	if (connector) {
474a2e73f56SAlex Deucher 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
475a2e73f56SAlex Deucher 		bpc = amdgpu_connector_get_monitor_bpc(connector);
476a2e73f56SAlex Deucher 		dither = amdgpu_connector->dither;
477a2e73f56SAlex Deucher 	}
478a2e73f56SAlex Deucher 
479a2e73f56SAlex Deucher 	/* LVDS/eDP FMT is set up by atom */
480a2e73f56SAlex Deucher 	if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
481a2e73f56SAlex Deucher 		return;
482a2e73f56SAlex Deucher 
483a2e73f56SAlex Deucher 	/* not needed for analog */
484a2e73f56SAlex Deucher 	if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
485a2e73f56SAlex Deucher 	    (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
486a2e73f56SAlex Deucher 		return;
487a2e73f56SAlex Deucher 
488a2e73f56SAlex Deucher 	if (bpc == 0)
489a2e73f56SAlex Deucher 		return;
490a2e73f56SAlex Deucher 
491a2e73f56SAlex Deucher 	switch (bpc) {
492a2e73f56SAlex Deucher 	case 6:
493a2e73f56SAlex Deucher 		if (dither == AMDGPU_FMT_DITHER_ENABLE)
494a2e73f56SAlex Deucher 			/* XXX sort out optimal dither settings */
495a2e73f56SAlex Deucher 			tmp |= (FMT_BIT_DEPTH_CONTROL__FMT_FRAME_RANDOM_ENABLE_MASK |
496a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_HIGHPASS_RANDOM_ENABLE_MASK |
497a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_SPATIAL_DITHER_EN_MASK |
498a2e73f56SAlex Deucher 				(0 << FMT_BIT_DEPTH_CONTROL__FMT_SPATIAL_DITHER_DEPTH__SHIFT));
499a2e73f56SAlex Deucher 		else
500a2e73f56SAlex Deucher 			tmp |= (FMT_BIT_DEPTH_CONTROL__FMT_TRUNCATE_EN_MASK |
501a2e73f56SAlex Deucher 			(0 << FMT_BIT_DEPTH_CONTROL__FMT_TRUNCATE_DEPTH__SHIFT));
502a2e73f56SAlex Deucher 		break;
503a2e73f56SAlex Deucher 	case 8:
504a2e73f56SAlex Deucher 		if (dither == AMDGPU_FMT_DITHER_ENABLE)
505a2e73f56SAlex Deucher 			/* XXX sort out optimal dither settings */
506a2e73f56SAlex Deucher 			tmp |= (FMT_BIT_DEPTH_CONTROL__FMT_FRAME_RANDOM_ENABLE_MASK |
507a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_HIGHPASS_RANDOM_ENABLE_MASK |
508a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_RGB_RANDOM_ENABLE_MASK |
509a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_SPATIAL_DITHER_EN_MASK |
510a2e73f56SAlex Deucher 				(1 << FMT_BIT_DEPTH_CONTROL__FMT_SPATIAL_DITHER_DEPTH__SHIFT));
511a2e73f56SAlex Deucher 		else
512a2e73f56SAlex Deucher 			tmp |= (FMT_BIT_DEPTH_CONTROL__FMT_TRUNCATE_EN_MASK |
513a2e73f56SAlex Deucher 			(1 << FMT_BIT_DEPTH_CONTROL__FMT_TRUNCATE_DEPTH__SHIFT));
514a2e73f56SAlex Deucher 		break;
515a2e73f56SAlex Deucher 	case 10:
516a2e73f56SAlex Deucher 		if (dither == AMDGPU_FMT_DITHER_ENABLE)
517a2e73f56SAlex Deucher 			/* XXX sort out optimal dither settings */
518a2e73f56SAlex Deucher 			tmp |= (FMT_BIT_DEPTH_CONTROL__FMT_FRAME_RANDOM_ENABLE_MASK |
519a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_HIGHPASS_RANDOM_ENABLE_MASK |
520a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_RGB_RANDOM_ENABLE_MASK |
521a2e73f56SAlex Deucher 				FMT_BIT_DEPTH_CONTROL__FMT_SPATIAL_DITHER_EN_MASK |
522a2e73f56SAlex Deucher 				(2 << FMT_BIT_DEPTH_CONTROL__FMT_SPATIAL_DITHER_DEPTH__SHIFT));
523a2e73f56SAlex Deucher 		else
524a2e73f56SAlex Deucher 			tmp |= (FMT_BIT_DEPTH_CONTROL__FMT_TRUNCATE_EN_MASK |
525a2e73f56SAlex Deucher 			(2 << FMT_BIT_DEPTH_CONTROL__FMT_TRUNCATE_DEPTH__SHIFT));
526a2e73f56SAlex Deucher 		break;
527a2e73f56SAlex Deucher 	default:
528a2e73f56SAlex Deucher 		/* not needed */
529a2e73f56SAlex Deucher 		break;
530a2e73f56SAlex Deucher 	}
531a2e73f56SAlex Deucher 
532a2e73f56SAlex Deucher 	WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
533a2e73f56SAlex Deucher }
534a2e73f56SAlex Deucher 
535a2e73f56SAlex Deucher 
536a2e73f56SAlex Deucher /* display watermark setup */
537a2e73f56SAlex Deucher /**
538a2e73f56SAlex Deucher  * dce_v8_0_line_buffer_adjust - Set up the line buffer
539a2e73f56SAlex Deucher  *
540a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
541a2e73f56SAlex Deucher  * @amdgpu_crtc: the selected display controller
542a2e73f56SAlex Deucher  * @mode: the current display mode on the selected display
543a2e73f56SAlex Deucher  * controller
544a2e73f56SAlex Deucher  *
545a2e73f56SAlex Deucher  * Setup up the line buffer allocation for
546a2e73f56SAlex Deucher  * the selected display controller (CIK).
547a2e73f56SAlex Deucher  * Returns the line buffer size in pixels.
548a2e73f56SAlex Deucher  */
dce_v8_0_line_buffer_adjust(struct amdgpu_device * adev,struct amdgpu_crtc * amdgpu_crtc,struct drm_display_mode * mode)549a2e73f56SAlex Deucher static u32 dce_v8_0_line_buffer_adjust(struct amdgpu_device *adev,
550a2e73f56SAlex Deucher 				       struct amdgpu_crtc *amdgpu_crtc,
551a2e73f56SAlex Deucher 				       struct drm_display_mode *mode)
552a2e73f56SAlex Deucher {
553a2e73f56SAlex Deucher 	u32 tmp, buffer_alloc, i;
554a2e73f56SAlex Deucher 	u32 pipe_offset = amdgpu_crtc->crtc_id * 0x8;
555a2e73f56SAlex Deucher 	/*
556a2e73f56SAlex Deucher 	 * Line Buffer Setup
557a2e73f56SAlex Deucher 	 * There are 6 line buffers, one for each display controllers.
558a2e73f56SAlex Deucher 	 * There are 3 partitions per LB. Select the number of partitions
559a2e73f56SAlex Deucher 	 * to enable based on the display width.  For display widths larger
560a2e73f56SAlex Deucher 	 * than 4096, you need use to use 2 display controllers and combine
561a2e73f56SAlex Deucher 	 * them using the stereo blender.
562a2e73f56SAlex Deucher 	 */
563a2e73f56SAlex Deucher 	if (amdgpu_crtc->base.enabled && mode) {
564a2e73f56SAlex Deucher 		if (mode->crtc_hdisplay < 1920) {
565a2e73f56SAlex Deucher 			tmp = 1;
566a2e73f56SAlex Deucher 			buffer_alloc = 2;
567a2e73f56SAlex Deucher 		} else if (mode->crtc_hdisplay < 2560) {
568a2e73f56SAlex Deucher 			tmp = 2;
569a2e73f56SAlex Deucher 			buffer_alloc = 2;
570a2e73f56SAlex Deucher 		} else if (mode->crtc_hdisplay < 4096) {
571a2e73f56SAlex Deucher 			tmp = 0;
5722f7d10b3SJammy Zhou 			buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
573a2e73f56SAlex Deucher 		} else {
574a2e73f56SAlex Deucher 			DRM_DEBUG_KMS("Mode too big for LB!\n");
575a2e73f56SAlex Deucher 			tmp = 0;
5762f7d10b3SJammy Zhou 			buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
577a2e73f56SAlex Deucher 		}
578a2e73f56SAlex Deucher 	} else {
579a2e73f56SAlex Deucher 		tmp = 1;
580a2e73f56SAlex Deucher 		buffer_alloc = 0;
581a2e73f56SAlex Deucher 	}
582a2e73f56SAlex Deucher 
583a2e73f56SAlex Deucher 	WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset,
584a2e73f56SAlex Deucher 	      (tmp << LB_MEMORY_CTRL__LB_MEMORY_CONFIG__SHIFT) |
585a2e73f56SAlex Deucher 	      (0x6B0 << LB_MEMORY_CTRL__LB_MEMORY_SIZE__SHIFT));
586a2e73f56SAlex Deucher 
587a2e73f56SAlex Deucher 	WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset,
588a2e73f56SAlex Deucher 	       (buffer_alloc << PIPE0_DMIF_BUFFER_CONTROL__DMIF_BUFFERS_ALLOCATED__SHIFT));
589a2e73f56SAlex Deucher 	for (i = 0; i < adev->usec_timeout; i++) {
590a2e73f56SAlex Deucher 		if (RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset) &
591a2e73f56SAlex Deucher 		    PIPE0_DMIF_BUFFER_CONTROL__DMIF_BUFFERS_ALLOCATION_COMPLETED_MASK)
592a2e73f56SAlex Deucher 			break;
593a2e73f56SAlex Deucher 		udelay(1);
594a2e73f56SAlex Deucher 	}
595a2e73f56SAlex Deucher 
596a2e73f56SAlex Deucher 	if (amdgpu_crtc->base.enabled && mode) {
597a2e73f56SAlex Deucher 		switch (tmp) {
598a2e73f56SAlex Deucher 		case 0:
599a2e73f56SAlex Deucher 		default:
600a2e73f56SAlex Deucher 			return 4096 * 2;
601a2e73f56SAlex Deucher 		case 1:
602a2e73f56SAlex Deucher 			return 1920 * 2;
603a2e73f56SAlex Deucher 		case 2:
604a2e73f56SAlex Deucher 			return 2560 * 2;
605a2e73f56SAlex Deucher 		}
606a2e73f56SAlex Deucher 	}
607a2e73f56SAlex Deucher 
608a2e73f56SAlex Deucher 	/* controller not enabled, so no lb used */
609a2e73f56SAlex Deucher 	return 0;
610a2e73f56SAlex Deucher }
611a2e73f56SAlex Deucher 
612a2e73f56SAlex Deucher /**
613a2e73f56SAlex Deucher  * cik_get_number_of_dram_channels - get the number of dram channels
614a2e73f56SAlex Deucher  *
615a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
616a2e73f56SAlex Deucher  *
617a2e73f56SAlex Deucher  * Look up the number of video ram channels (CIK).
618a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
619a2e73f56SAlex Deucher  * Returns the number of dram channels
620a2e73f56SAlex Deucher  */
cik_get_number_of_dram_channels(struct amdgpu_device * adev)621a2e73f56SAlex Deucher static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
622a2e73f56SAlex Deucher {
623a2e73f56SAlex Deucher 	u32 tmp = RREG32(mmMC_SHARED_CHMAP);
624a2e73f56SAlex Deucher 
625a2e73f56SAlex Deucher 	switch ((tmp & MC_SHARED_CHMAP__NOOFCHAN_MASK) >> MC_SHARED_CHMAP__NOOFCHAN__SHIFT) {
626a2e73f56SAlex Deucher 	case 0:
627a2e73f56SAlex Deucher 	default:
628a2e73f56SAlex Deucher 		return 1;
629a2e73f56SAlex Deucher 	case 1:
630a2e73f56SAlex Deucher 		return 2;
631a2e73f56SAlex Deucher 	case 2:
632a2e73f56SAlex Deucher 		return 4;
633a2e73f56SAlex Deucher 	case 3:
634a2e73f56SAlex Deucher 		return 8;
635a2e73f56SAlex Deucher 	case 4:
636a2e73f56SAlex Deucher 		return 3;
637a2e73f56SAlex Deucher 	case 5:
638a2e73f56SAlex Deucher 		return 6;
639a2e73f56SAlex Deucher 	case 6:
640a2e73f56SAlex Deucher 		return 10;
641a2e73f56SAlex Deucher 	case 7:
642a2e73f56SAlex Deucher 		return 12;
643a2e73f56SAlex Deucher 	case 8:
644a2e73f56SAlex Deucher 		return 16;
645a2e73f56SAlex Deucher 	}
646a2e73f56SAlex Deucher }
647a2e73f56SAlex Deucher 
648a2e73f56SAlex Deucher struct dce8_wm_params {
649a2e73f56SAlex Deucher 	u32 dram_channels; /* number of dram channels */
650a2e73f56SAlex Deucher 	u32 yclk;          /* bandwidth per dram data pin in kHz */
651a2e73f56SAlex Deucher 	u32 sclk;          /* engine clock in kHz */
652a2e73f56SAlex Deucher 	u32 disp_clk;      /* display clock in kHz */
653a2e73f56SAlex Deucher 	u32 src_width;     /* viewport width */
654a2e73f56SAlex Deucher 	u32 active_time;   /* active display time in ns */
655a2e73f56SAlex Deucher 	u32 blank_time;    /* blank time in ns */
656a2e73f56SAlex Deucher 	bool interlaced;    /* mode is interlaced */
657a2e73f56SAlex Deucher 	fixed20_12 vsc;    /* vertical scale ratio */
658a2e73f56SAlex Deucher 	u32 num_heads;     /* number of active crtcs */
659a2e73f56SAlex Deucher 	u32 bytes_per_pixel; /* bytes per pixel display + overlay */
660a2e73f56SAlex Deucher 	u32 lb_size;       /* line buffer allocated to pipe */
661a2e73f56SAlex Deucher 	u32 vtaps;         /* vertical scaler taps */
662a2e73f56SAlex Deucher };
663a2e73f56SAlex Deucher 
664a2e73f56SAlex Deucher /**
665a2e73f56SAlex Deucher  * dce_v8_0_dram_bandwidth - get the dram bandwidth
666a2e73f56SAlex Deucher  *
667a2e73f56SAlex Deucher  * @wm: watermark calculation data
668a2e73f56SAlex Deucher  *
669a2e73f56SAlex Deucher  * Calculate the raw dram bandwidth (CIK).
670a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
671a2e73f56SAlex Deucher  * Returns the dram bandwidth in MBytes/s
672a2e73f56SAlex Deucher  */
dce_v8_0_dram_bandwidth(struct dce8_wm_params * wm)673a2e73f56SAlex Deucher static u32 dce_v8_0_dram_bandwidth(struct dce8_wm_params *wm)
674a2e73f56SAlex Deucher {
675a2e73f56SAlex Deucher 	/* Calculate raw DRAM Bandwidth */
676a2e73f56SAlex Deucher 	fixed20_12 dram_efficiency; /* 0.7 */
677a2e73f56SAlex Deucher 	fixed20_12 yclk, dram_channels, bandwidth;
678a2e73f56SAlex Deucher 	fixed20_12 a;
679a2e73f56SAlex Deucher 
680a2e73f56SAlex Deucher 	a.full = dfixed_const(1000);
681a2e73f56SAlex Deucher 	yclk.full = dfixed_const(wm->yclk);
682a2e73f56SAlex Deucher 	yclk.full = dfixed_div(yclk, a);
683a2e73f56SAlex Deucher 	dram_channels.full = dfixed_const(wm->dram_channels * 4);
684a2e73f56SAlex Deucher 	a.full = dfixed_const(10);
685a2e73f56SAlex Deucher 	dram_efficiency.full = dfixed_const(7);
686a2e73f56SAlex Deucher 	dram_efficiency.full = dfixed_div(dram_efficiency, a);
687a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(dram_channels, yclk);
688a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
689a2e73f56SAlex Deucher 
690a2e73f56SAlex Deucher 	return dfixed_trunc(bandwidth);
691a2e73f56SAlex Deucher }
692a2e73f56SAlex Deucher 
693a2e73f56SAlex Deucher /**
694a2e73f56SAlex Deucher  * dce_v8_0_dram_bandwidth_for_display - get the dram bandwidth for display
695a2e73f56SAlex Deucher  *
696a2e73f56SAlex Deucher  * @wm: watermark calculation data
697a2e73f56SAlex Deucher  *
698a2e73f56SAlex Deucher  * Calculate the dram bandwidth used for display (CIK).
699a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
700a2e73f56SAlex Deucher  * Returns the dram bandwidth for display in MBytes/s
701a2e73f56SAlex Deucher  */
dce_v8_0_dram_bandwidth_for_display(struct dce8_wm_params * wm)702a2e73f56SAlex Deucher static u32 dce_v8_0_dram_bandwidth_for_display(struct dce8_wm_params *wm)
703a2e73f56SAlex Deucher {
704a2e73f56SAlex Deucher 	/* Calculate DRAM Bandwidth and the part allocated to display. */
705a2e73f56SAlex Deucher 	fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
706a2e73f56SAlex Deucher 	fixed20_12 yclk, dram_channels, bandwidth;
707a2e73f56SAlex Deucher 	fixed20_12 a;
708a2e73f56SAlex Deucher 
709a2e73f56SAlex Deucher 	a.full = dfixed_const(1000);
710a2e73f56SAlex Deucher 	yclk.full = dfixed_const(wm->yclk);
711a2e73f56SAlex Deucher 	yclk.full = dfixed_div(yclk, a);
712a2e73f56SAlex Deucher 	dram_channels.full = dfixed_const(wm->dram_channels * 4);
713a2e73f56SAlex Deucher 	a.full = dfixed_const(10);
714a2e73f56SAlex Deucher 	disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
715a2e73f56SAlex Deucher 	disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
716a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(dram_channels, yclk);
717a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
718a2e73f56SAlex Deucher 
719a2e73f56SAlex Deucher 	return dfixed_trunc(bandwidth);
720a2e73f56SAlex Deucher }
721a2e73f56SAlex Deucher 
722a2e73f56SAlex Deucher /**
723a2e73f56SAlex Deucher  * dce_v8_0_data_return_bandwidth - get the data return bandwidth
724a2e73f56SAlex Deucher  *
725a2e73f56SAlex Deucher  * @wm: watermark calculation data
726a2e73f56SAlex Deucher  *
727a2e73f56SAlex Deucher  * Calculate the data return bandwidth used for display (CIK).
728a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
729a2e73f56SAlex Deucher  * Returns the data return bandwidth in MBytes/s
730a2e73f56SAlex Deucher  */
dce_v8_0_data_return_bandwidth(struct dce8_wm_params * wm)731a2e73f56SAlex Deucher static u32 dce_v8_0_data_return_bandwidth(struct dce8_wm_params *wm)
732a2e73f56SAlex Deucher {
733a2e73f56SAlex Deucher 	/* Calculate the display Data return Bandwidth */
734a2e73f56SAlex Deucher 	fixed20_12 return_efficiency; /* 0.8 */
735a2e73f56SAlex Deucher 	fixed20_12 sclk, bandwidth;
736a2e73f56SAlex Deucher 	fixed20_12 a;
737a2e73f56SAlex Deucher 
738a2e73f56SAlex Deucher 	a.full = dfixed_const(1000);
739a2e73f56SAlex Deucher 	sclk.full = dfixed_const(wm->sclk);
740a2e73f56SAlex Deucher 	sclk.full = dfixed_div(sclk, a);
741a2e73f56SAlex Deucher 	a.full = dfixed_const(10);
742a2e73f56SAlex Deucher 	return_efficiency.full = dfixed_const(8);
743a2e73f56SAlex Deucher 	return_efficiency.full = dfixed_div(return_efficiency, a);
744a2e73f56SAlex Deucher 	a.full = dfixed_const(32);
745a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(a, sclk);
746a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
747a2e73f56SAlex Deucher 
748a2e73f56SAlex Deucher 	return dfixed_trunc(bandwidth);
749a2e73f56SAlex Deucher }
750a2e73f56SAlex Deucher 
751a2e73f56SAlex Deucher /**
752a2e73f56SAlex Deucher  * dce_v8_0_dmif_request_bandwidth - get the dmif bandwidth
753a2e73f56SAlex Deucher  *
754a2e73f56SAlex Deucher  * @wm: watermark calculation data
755a2e73f56SAlex Deucher  *
756a2e73f56SAlex Deucher  * Calculate the dmif bandwidth used for display (CIK).
757a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
758a2e73f56SAlex Deucher  * Returns the dmif bandwidth in MBytes/s
759a2e73f56SAlex Deucher  */
dce_v8_0_dmif_request_bandwidth(struct dce8_wm_params * wm)760a2e73f56SAlex Deucher static u32 dce_v8_0_dmif_request_bandwidth(struct dce8_wm_params *wm)
761a2e73f56SAlex Deucher {
762a2e73f56SAlex Deucher 	/* Calculate the DMIF Request Bandwidth */
763a2e73f56SAlex Deucher 	fixed20_12 disp_clk_request_efficiency; /* 0.8 */
764a2e73f56SAlex Deucher 	fixed20_12 disp_clk, bandwidth;
765a2e73f56SAlex Deucher 	fixed20_12 a, b;
766a2e73f56SAlex Deucher 
767a2e73f56SAlex Deucher 	a.full = dfixed_const(1000);
768a2e73f56SAlex Deucher 	disp_clk.full = dfixed_const(wm->disp_clk);
769a2e73f56SAlex Deucher 	disp_clk.full = dfixed_div(disp_clk, a);
770a2e73f56SAlex Deucher 	a.full = dfixed_const(32);
771a2e73f56SAlex Deucher 	b.full = dfixed_mul(a, disp_clk);
772a2e73f56SAlex Deucher 
773a2e73f56SAlex Deucher 	a.full = dfixed_const(10);
774a2e73f56SAlex Deucher 	disp_clk_request_efficiency.full = dfixed_const(8);
775a2e73f56SAlex Deucher 	disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
776a2e73f56SAlex Deucher 
777a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
778a2e73f56SAlex Deucher 
779a2e73f56SAlex Deucher 	return dfixed_trunc(bandwidth);
780a2e73f56SAlex Deucher }
781a2e73f56SAlex Deucher 
782a2e73f56SAlex Deucher /**
783a2e73f56SAlex Deucher  * dce_v8_0_available_bandwidth - get the min available bandwidth
784a2e73f56SAlex Deucher  *
785a2e73f56SAlex Deucher  * @wm: watermark calculation data
786a2e73f56SAlex Deucher  *
787a2e73f56SAlex Deucher  * Calculate the min available bandwidth used for display (CIK).
788a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
789a2e73f56SAlex Deucher  * Returns the min available bandwidth in MBytes/s
790a2e73f56SAlex Deucher  */
dce_v8_0_available_bandwidth(struct dce8_wm_params * wm)791a2e73f56SAlex Deucher static u32 dce_v8_0_available_bandwidth(struct dce8_wm_params *wm)
792a2e73f56SAlex Deucher {
793a2e73f56SAlex Deucher 	/* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
794a2e73f56SAlex Deucher 	u32 dram_bandwidth = dce_v8_0_dram_bandwidth(wm);
795a2e73f56SAlex Deucher 	u32 data_return_bandwidth = dce_v8_0_data_return_bandwidth(wm);
796a2e73f56SAlex Deucher 	u32 dmif_req_bandwidth = dce_v8_0_dmif_request_bandwidth(wm);
797a2e73f56SAlex Deucher 
798a2e73f56SAlex Deucher 	return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
799a2e73f56SAlex Deucher }
800a2e73f56SAlex Deucher 
801a2e73f56SAlex Deucher /**
802a2e73f56SAlex Deucher  * dce_v8_0_average_bandwidth - get the average available bandwidth
803a2e73f56SAlex Deucher  *
804a2e73f56SAlex Deucher  * @wm: watermark calculation data
805a2e73f56SAlex Deucher  *
806a2e73f56SAlex Deucher  * Calculate the average available bandwidth used for display (CIK).
807a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
808a2e73f56SAlex Deucher  * Returns the average available bandwidth in MBytes/s
809a2e73f56SAlex Deucher  */
dce_v8_0_average_bandwidth(struct dce8_wm_params * wm)810a2e73f56SAlex Deucher static u32 dce_v8_0_average_bandwidth(struct dce8_wm_params *wm)
811a2e73f56SAlex Deucher {
812a2e73f56SAlex Deucher 	/* Calculate the display mode Average Bandwidth
813a2e73f56SAlex Deucher 	 * DisplayMode should contain the source and destination dimensions,
814a2e73f56SAlex Deucher 	 * timing, etc.
815a2e73f56SAlex Deucher 	 */
816a2e73f56SAlex Deucher 	fixed20_12 bpp;
817a2e73f56SAlex Deucher 	fixed20_12 line_time;
818a2e73f56SAlex Deucher 	fixed20_12 src_width;
819a2e73f56SAlex Deucher 	fixed20_12 bandwidth;
820a2e73f56SAlex Deucher 	fixed20_12 a;
821a2e73f56SAlex Deucher 
822a2e73f56SAlex Deucher 	a.full = dfixed_const(1000);
823a2e73f56SAlex Deucher 	line_time.full = dfixed_const(wm->active_time + wm->blank_time);
824a2e73f56SAlex Deucher 	line_time.full = dfixed_div(line_time, a);
825a2e73f56SAlex Deucher 	bpp.full = dfixed_const(wm->bytes_per_pixel);
826a2e73f56SAlex Deucher 	src_width.full = dfixed_const(wm->src_width);
827a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(src_width, bpp);
828a2e73f56SAlex Deucher 	bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
829a2e73f56SAlex Deucher 	bandwidth.full = dfixed_div(bandwidth, line_time);
830a2e73f56SAlex Deucher 
831a2e73f56SAlex Deucher 	return dfixed_trunc(bandwidth);
832a2e73f56SAlex Deucher }
833a2e73f56SAlex Deucher 
834a2e73f56SAlex Deucher /**
835a2e73f56SAlex Deucher  * dce_v8_0_latency_watermark - get the latency watermark
836a2e73f56SAlex Deucher  *
837a2e73f56SAlex Deucher  * @wm: watermark calculation data
838a2e73f56SAlex Deucher  *
839a2e73f56SAlex Deucher  * Calculate the latency watermark (CIK).
840a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
841a2e73f56SAlex Deucher  * Returns the latency watermark in ns
842a2e73f56SAlex Deucher  */
dce_v8_0_latency_watermark(struct dce8_wm_params * wm)843a2e73f56SAlex Deucher static u32 dce_v8_0_latency_watermark(struct dce8_wm_params *wm)
844a2e73f56SAlex Deucher {
845a2e73f56SAlex Deucher 	/* First calculate the latency in ns */
846a2e73f56SAlex Deucher 	u32 mc_latency = 2000; /* 2000 ns. */
847a2e73f56SAlex Deucher 	u32 available_bandwidth = dce_v8_0_available_bandwidth(wm);
848a2e73f56SAlex Deucher 	u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
849a2e73f56SAlex Deucher 	u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
850a2e73f56SAlex Deucher 	u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
851a2e73f56SAlex Deucher 	u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
852a2e73f56SAlex Deucher 		(wm->num_heads * cursor_line_pair_return_time);
853a2e73f56SAlex Deucher 	u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
854a2e73f56SAlex Deucher 	u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
855a2e73f56SAlex Deucher 	u32 tmp, dmif_size = 12288;
856a2e73f56SAlex Deucher 	fixed20_12 a, b, c;
857a2e73f56SAlex Deucher 
858a2e73f56SAlex Deucher 	if (wm->num_heads == 0)
859a2e73f56SAlex Deucher 		return 0;
860a2e73f56SAlex Deucher 
861a2e73f56SAlex Deucher 	a.full = dfixed_const(2);
862a2e73f56SAlex Deucher 	b.full = dfixed_const(1);
863a2e73f56SAlex Deucher 	if ((wm->vsc.full > a.full) ||
864a2e73f56SAlex Deucher 	    ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
865a2e73f56SAlex Deucher 	    (wm->vtaps >= 5) ||
866a2e73f56SAlex Deucher 	    ((wm->vsc.full >= a.full) && wm->interlaced))
867a2e73f56SAlex Deucher 		max_src_lines_per_dst_line = 4;
868a2e73f56SAlex Deucher 	else
869a2e73f56SAlex Deucher 		max_src_lines_per_dst_line = 2;
870a2e73f56SAlex Deucher 
871a2e73f56SAlex Deucher 	a.full = dfixed_const(available_bandwidth);
872a2e73f56SAlex Deucher 	b.full = dfixed_const(wm->num_heads);
873a2e73f56SAlex Deucher 	a.full = dfixed_div(a, b);
874e190ed1eSMario Kleiner 	tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
875e190ed1eSMario Kleiner 	tmp = min(dfixed_trunc(a), tmp);
876a2e73f56SAlex Deucher 
877e190ed1eSMario Kleiner 	lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
878a2e73f56SAlex Deucher 
879a2e73f56SAlex Deucher 	a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
880a2e73f56SAlex Deucher 	b.full = dfixed_const(1000);
881a2e73f56SAlex Deucher 	c.full = dfixed_const(lb_fill_bw);
882a2e73f56SAlex Deucher 	b.full = dfixed_div(c, b);
883a2e73f56SAlex Deucher 	a.full = dfixed_div(a, b);
884a2e73f56SAlex Deucher 	line_fill_time = dfixed_trunc(a);
885a2e73f56SAlex Deucher 
886a2e73f56SAlex Deucher 	if (line_fill_time < wm->active_time)
887a2e73f56SAlex Deucher 		return latency;
888a2e73f56SAlex Deucher 	else
889a2e73f56SAlex Deucher 		return latency + (line_fill_time - wm->active_time);
890a2e73f56SAlex Deucher 
891a2e73f56SAlex Deucher }
892a2e73f56SAlex Deucher 
893a2e73f56SAlex Deucher /**
894a2e73f56SAlex Deucher  * dce_v8_0_average_bandwidth_vs_dram_bandwidth_for_display - check
895a2e73f56SAlex Deucher  * average and available dram bandwidth
896a2e73f56SAlex Deucher  *
897a2e73f56SAlex Deucher  * @wm: watermark calculation data
898a2e73f56SAlex Deucher  *
899a2e73f56SAlex Deucher  * Check if the display average bandwidth fits in the display
900a2e73f56SAlex Deucher  * dram bandwidth (CIK).
901a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
902a2e73f56SAlex Deucher  * Returns true if the display fits, false if not.
903a2e73f56SAlex Deucher  */
dce_v8_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce8_wm_params * wm)904a2e73f56SAlex Deucher static bool dce_v8_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce8_wm_params *wm)
905a2e73f56SAlex Deucher {
906a2e73f56SAlex Deucher 	if (dce_v8_0_average_bandwidth(wm) <=
907a2e73f56SAlex Deucher 	    (dce_v8_0_dram_bandwidth_for_display(wm) / wm->num_heads))
908a2e73f56SAlex Deucher 		return true;
909a2e73f56SAlex Deucher 	else
910a2e73f56SAlex Deucher 		return false;
911a2e73f56SAlex Deucher }
912a2e73f56SAlex Deucher 
913a2e73f56SAlex Deucher /**
914a2e73f56SAlex Deucher  * dce_v8_0_average_bandwidth_vs_available_bandwidth - check
915a2e73f56SAlex Deucher  * average and available bandwidth
916a2e73f56SAlex Deucher  *
917a2e73f56SAlex Deucher  * @wm: watermark calculation data
918a2e73f56SAlex Deucher  *
919a2e73f56SAlex Deucher  * Check if the display average bandwidth fits in the display
920a2e73f56SAlex Deucher  * available bandwidth (CIK).
921a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
922a2e73f56SAlex Deucher  * Returns true if the display fits, false if not.
923a2e73f56SAlex Deucher  */
dce_v8_0_average_bandwidth_vs_available_bandwidth(struct dce8_wm_params * wm)924a2e73f56SAlex Deucher static bool dce_v8_0_average_bandwidth_vs_available_bandwidth(struct dce8_wm_params *wm)
925a2e73f56SAlex Deucher {
926a2e73f56SAlex Deucher 	if (dce_v8_0_average_bandwidth(wm) <=
927a2e73f56SAlex Deucher 	    (dce_v8_0_available_bandwidth(wm) / wm->num_heads))
928a2e73f56SAlex Deucher 		return true;
929a2e73f56SAlex Deucher 	else
930a2e73f56SAlex Deucher 		return false;
931a2e73f56SAlex Deucher }
932a2e73f56SAlex Deucher 
933a2e73f56SAlex Deucher /**
934a2e73f56SAlex Deucher  * dce_v8_0_check_latency_hiding - check latency hiding
935a2e73f56SAlex Deucher  *
936a2e73f56SAlex Deucher  * @wm: watermark calculation data
937a2e73f56SAlex Deucher  *
938a2e73f56SAlex Deucher  * Check latency hiding (CIK).
939a2e73f56SAlex Deucher  * Used for display watermark bandwidth calculations
940a2e73f56SAlex Deucher  * Returns true if the display fits, false if not.
941a2e73f56SAlex Deucher  */
dce_v8_0_check_latency_hiding(struct dce8_wm_params * wm)942a2e73f56SAlex Deucher static bool dce_v8_0_check_latency_hiding(struct dce8_wm_params *wm)
943a2e73f56SAlex Deucher {
944a2e73f56SAlex Deucher 	u32 lb_partitions = wm->lb_size / wm->src_width;
945a2e73f56SAlex Deucher 	u32 line_time = wm->active_time + wm->blank_time;
946a2e73f56SAlex Deucher 	u32 latency_tolerant_lines;
947a2e73f56SAlex Deucher 	u32 latency_hiding;
948a2e73f56SAlex Deucher 	fixed20_12 a;
949a2e73f56SAlex Deucher 
950a2e73f56SAlex Deucher 	a.full = dfixed_const(1);
951a2e73f56SAlex Deucher 	if (wm->vsc.full > a.full)
952a2e73f56SAlex Deucher 		latency_tolerant_lines = 1;
953a2e73f56SAlex Deucher 	else {
954a2e73f56SAlex Deucher 		if (lb_partitions <= (wm->vtaps + 1))
955a2e73f56SAlex Deucher 			latency_tolerant_lines = 1;
956a2e73f56SAlex Deucher 		else
957a2e73f56SAlex Deucher 			latency_tolerant_lines = 2;
958a2e73f56SAlex Deucher 	}
959a2e73f56SAlex Deucher 
960a2e73f56SAlex Deucher 	latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
961a2e73f56SAlex Deucher 
962a2e73f56SAlex Deucher 	if (dce_v8_0_latency_watermark(wm) <= latency_hiding)
963a2e73f56SAlex Deucher 		return true;
964a2e73f56SAlex Deucher 	else
965a2e73f56SAlex Deucher 		return false;
966a2e73f56SAlex Deucher }
967a2e73f56SAlex Deucher 
968a2e73f56SAlex Deucher /**
969a2e73f56SAlex Deucher  * dce_v8_0_program_watermarks - program display watermarks
970a2e73f56SAlex Deucher  *
971a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
972a2e73f56SAlex Deucher  * @amdgpu_crtc: the selected display controller
973a2e73f56SAlex Deucher  * @lb_size: line buffer size
974a2e73f56SAlex Deucher  * @num_heads: number of display controllers in use
975a2e73f56SAlex Deucher  *
976a2e73f56SAlex Deucher  * Calculate and program the display watermarks for the
977a2e73f56SAlex Deucher  * selected display controller (CIK).
978a2e73f56SAlex Deucher  */
dce_v8_0_program_watermarks(struct amdgpu_device * adev,struct amdgpu_crtc * amdgpu_crtc,u32 lb_size,u32 num_heads)979a2e73f56SAlex Deucher static void dce_v8_0_program_watermarks(struct amdgpu_device *adev,
980a2e73f56SAlex Deucher 					struct amdgpu_crtc *amdgpu_crtc,
981a2e73f56SAlex Deucher 					u32 lb_size, u32 num_heads)
982a2e73f56SAlex Deucher {
983a2e73f56SAlex Deucher 	struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
984a2e73f56SAlex Deucher 	struct dce8_wm_params wm_low, wm_high;
985d63c277dSMario Kleiner 	u32 active_time;
986a2e73f56SAlex Deucher 	u32 line_time = 0;
987a2e73f56SAlex Deucher 	u32 latency_watermark_a = 0, latency_watermark_b = 0;
9888e36f9d3SAlex Deucher 	u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
989a2e73f56SAlex Deucher 
990a2e73f56SAlex Deucher 	if (amdgpu_crtc->base.enabled && num_heads && mode) {
991bea10413SMario Kleiner 		active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
992bea10413SMario Kleiner 					    (u32)mode->clock);
993bea10413SMario Kleiner 		line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
994bea10413SMario Kleiner 					  (u32)mode->clock);
995eb3b214cSSrinivasan Shanmugam 		line_time = min_t(u32, line_time, 65535);
996a2e73f56SAlex Deucher 
997a2e73f56SAlex Deucher 		/* watermark for high clocks */
998a2e73f56SAlex Deucher 		if (adev->pm.dpm_enabled) {
999a2e73f56SAlex Deucher 			wm_high.yclk =
1000a2e73f56SAlex Deucher 				amdgpu_dpm_get_mclk(adev, false) * 10;
1001a2e73f56SAlex Deucher 			wm_high.sclk =
1002a2e73f56SAlex Deucher 				amdgpu_dpm_get_sclk(adev, false) * 10;
1003a2e73f56SAlex Deucher 		} else {
1004a2e73f56SAlex Deucher 			wm_high.yclk = adev->pm.current_mclk * 10;
1005a2e73f56SAlex Deucher 			wm_high.sclk = adev->pm.current_sclk * 10;
1006a2e73f56SAlex Deucher 		}
1007a2e73f56SAlex Deucher 
1008a2e73f56SAlex Deucher 		wm_high.disp_clk = mode->clock;
1009a2e73f56SAlex Deucher 		wm_high.src_width = mode->crtc_hdisplay;
1010d63c277dSMario Kleiner 		wm_high.active_time = active_time;
1011a2e73f56SAlex Deucher 		wm_high.blank_time = line_time - wm_high.active_time;
1012a2e73f56SAlex Deucher 		wm_high.interlaced = false;
1013a2e73f56SAlex Deucher 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1014a2e73f56SAlex Deucher 			wm_high.interlaced = true;
1015a2e73f56SAlex Deucher 		wm_high.vsc = amdgpu_crtc->vsc;
1016a2e73f56SAlex Deucher 		wm_high.vtaps = 1;
1017a2e73f56SAlex Deucher 		if (amdgpu_crtc->rmx_type != RMX_OFF)
1018a2e73f56SAlex Deucher 			wm_high.vtaps = 2;
1019a2e73f56SAlex Deucher 		wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1020a2e73f56SAlex Deucher 		wm_high.lb_size = lb_size;
1021a2e73f56SAlex Deucher 		wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1022a2e73f56SAlex Deucher 		wm_high.num_heads = num_heads;
1023a2e73f56SAlex Deucher 
1024a2e73f56SAlex Deucher 		/* set for high clocks */
1025eb3b214cSSrinivasan Shanmugam 		latency_watermark_a = min_t(u32, dce_v8_0_latency_watermark(&wm_high), 65535);
1026a2e73f56SAlex Deucher 
1027a2e73f56SAlex Deucher 		/* possibly force display priority to high */
1028a2e73f56SAlex Deucher 		/* should really do this at mode validation time... */
1029a2e73f56SAlex Deucher 		if (!dce_v8_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1030a2e73f56SAlex Deucher 		    !dce_v8_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1031a2e73f56SAlex Deucher 		    !dce_v8_0_check_latency_hiding(&wm_high) ||
1032a2e73f56SAlex Deucher 		    (adev->mode_info.disp_priority == 2)) {
1033a2e73f56SAlex Deucher 			DRM_DEBUG_KMS("force priority to high\n");
1034a2e73f56SAlex Deucher 		}
1035a2e73f56SAlex Deucher 
1036a2e73f56SAlex Deucher 		/* watermark for low clocks */
1037a2e73f56SAlex Deucher 		if (adev->pm.dpm_enabled) {
1038a2e73f56SAlex Deucher 			wm_low.yclk =
1039a2e73f56SAlex Deucher 				amdgpu_dpm_get_mclk(adev, true) * 10;
1040a2e73f56SAlex Deucher 			wm_low.sclk =
1041a2e73f56SAlex Deucher 				amdgpu_dpm_get_sclk(adev, true) * 10;
1042a2e73f56SAlex Deucher 		} else {
1043a2e73f56SAlex Deucher 			wm_low.yclk = adev->pm.current_mclk * 10;
1044a2e73f56SAlex Deucher 			wm_low.sclk = adev->pm.current_sclk * 10;
1045a2e73f56SAlex Deucher 		}
1046a2e73f56SAlex Deucher 
1047a2e73f56SAlex Deucher 		wm_low.disp_clk = mode->clock;
1048a2e73f56SAlex Deucher 		wm_low.src_width = mode->crtc_hdisplay;
1049d63c277dSMario Kleiner 		wm_low.active_time = active_time;
1050a2e73f56SAlex Deucher 		wm_low.blank_time = line_time - wm_low.active_time;
1051a2e73f56SAlex Deucher 		wm_low.interlaced = false;
1052a2e73f56SAlex Deucher 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1053a2e73f56SAlex Deucher 			wm_low.interlaced = true;
1054a2e73f56SAlex Deucher 		wm_low.vsc = amdgpu_crtc->vsc;
1055a2e73f56SAlex Deucher 		wm_low.vtaps = 1;
1056a2e73f56SAlex Deucher 		if (amdgpu_crtc->rmx_type != RMX_OFF)
1057a2e73f56SAlex Deucher 			wm_low.vtaps = 2;
1058a2e73f56SAlex Deucher 		wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1059a2e73f56SAlex Deucher 		wm_low.lb_size = lb_size;
1060a2e73f56SAlex Deucher 		wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1061a2e73f56SAlex Deucher 		wm_low.num_heads = num_heads;
1062a2e73f56SAlex Deucher 
1063a2e73f56SAlex Deucher 		/* set for low clocks */
1064eb3b214cSSrinivasan Shanmugam 		latency_watermark_b = min_t(u32, dce_v8_0_latency_watermark(&wm_low), 65535);
1065a2e73f56SAlex Deucher 
1066a2e73f56SAlex Deucher 		/* possibly force display priority to high */
1067a2e73f56SAlex Deucher 		/* should really do this at mode validation time... */
1068a2e73f56SAlex Deucher 		if (!dce_v8_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1069a2e73f56SAlex Deucher 		    !dce_v8_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1070a2e73f56SAlex Deucher 		    !dce_v8_0_check_latency_hiding(&wm_low) ||
1071a2e73f56SAlex Deucher 		    (adev->mode_info.disp_priority == 2)) {
1072a2e73f56SAlex Deucher 			DRM_DEBUG_KMS("force priority to high\n");
1073a2e73f56SAlex Deucher 		}
10748e36f9d3SAlex Deucher 		lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
1075a2e73f56SAlex Deucher 	}
1076a2e73f56SAlex Deucher 
1077a2e73f56SAlex Deucher 	/* select wm A */
1078a2e73f56SAlex Deucher 	wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1079a2e73f56SAlex Deucher 	tmp = wm_mask;
1080a2e73f56SAlex Deucher 	tmp &= ~(3 << DPG_WATERMARK_MASK_CONTROL__URGENCY_WATERMARK_MASK__SHIFT);
1081a2e73f56SAlex Deucher 	tmp |= (1 << DPG_WATERMARK_MASK_CONTROL__URGENCY_WATERMARK_MASK__SHIFT);
1082a2e73f56SAlex Deucher 	WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1083a2e73f56SAlex Deucher 	WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset,
1084a2e73f56SAlex Deucher 	       ((latency_watermark_a << DPG_PIPE_URGENCY_CONTROL__URGENCY_LOW_WATERMARK__SHIFT) |
1085a2e73f56SAlex Deucher 		(line_time << DPG_PIPE_URGENCY_CONTROL__URGENCY_HIGH_WATERMARK__SHIFT)));
1086a2e73f56SAlex Deucher 	/* select wm B */
1087a2e73f56SAlex Deucher 	tmp = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1088a2e73f56SAlex Deucher 	tmp &= ~(3 << DPG_WATERMARK_MASK_CONTROL__URGENCY_WATERMARK_MASK__SHIFT);
1089a2e73f56SAlex Deucher 	tmp |= (2 << DPG_WATERMARK_MASK_CONTROL__URGENCY_WATERMARK_MASK__SHIFT);
1090a2e73f56SAlex Deucher 	WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1091a2e73f56SAlex Deucher 	WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset,
1092a2e73f56SAlex Deucher 	       ((latency_watermark_b << DPG_PIPE_URGENCY_CONTROL__URGENCY_LOW_WATERMARK__SHIFT) |
1093a2e73f56SAlex Deucher 		(line_time << DPG_PIPE_URGENCY_CONTROL__URGENCY_HIGH_WATERMARK__SHIFT)));
1094a2e73f56SAlex Deucher 	/* restore original selection */
1095a2e73f56SAlex Deucher 	WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1096a2e73f56SAlex Deucher 
1097a2e73f56SAlex Deucher 	/* save values for DPM */
1098a2e73f56SAlex Deucher 	amdgpu_crtc->line_time = line_time;
1099a2e73f56SAlex Deucher 	amdgpu_crtc->wm_high = latency_watermark_a;
1100a2e73f56SAlex Deucher 	amdgpu_crtc->wm_low = latency_watermark_b;
11018e36f9d3SAlex Deucher 	/* Save number of lines the linebuffer leads before the scanout */
11028e36f9d3SAlex Deucher 	amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
1103a2e73f56SAlex Deucher }
1104a2e73f56SAlex Deucher 
1105a2e73f56SAlex Deucher /**
1106a2e73f56SAlex Deucher  * dce_v8_0_bandwidth_update - program display watermarks
1107a2e73f56SAlex Deucher  *
1108a2e73f56SAlex Deucher  * @adev: amdgpu_device pointer
1109a2e73f56SAlex Deucher  *
1110a2e73f56SAlex Deucher  * Calculate and program the display watermarks and line
1111a2e73f56SAlex Deucher  * buffer allocation (CIK).
1112a2e73f56SAlex Deucher  */
dce_v8_0_bandwidth_update(struct amdgpu_device * adev)1113a2e73f56SAlex Deucher static void dce_v8_0_bandwidth_update(struct amdgpu_device *adev)
1114a2e73f56SAlex Deucher {
1115a2e73f56SAlex Deucher 	struct drm_display_mode *mode = NULL;
1116a2e73f56SAlex Deucher 	u32 num_heads = 0, lb_size;
1117a2e73f56SAlex Deucher 	int i;
1118a2e73f56SAlex Deucher 
1119166140fbSSamuel Li 	amdgpu_display_update_priority(adev);
1120a2e73f56SAlex Deucher 
1121a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
1122a2e73f56SAlex Deucher 		if (adev->mode_info.crtcs[i]->base.enabled)
1123a2e73f56SAlex Deucher 			num_heads++;
1124a2e73f56SAlex Deucher 	}
1125a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
1126a2e73f56SAlex Deucher 		mode = &adev->mode_info.crtcs[i]->base.mode;
1127a2e73f56SAlex Deucher 		lb_size = dce_v8_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1128a2e73f56SAlex Deucher 		dce_v8_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1129a2e73f56SAlex Deucher 					    lb_size, num_heads);
1130a2e73f56SAlex Deucher 	}
1131a2e73f56SAlex Deucher }
1132a2e73f56SAlex Deucher 
dce_v8_0_audio_get_connected_pins(struct amdgpu_device * adev)1133a2e73f56SAlex Deucher static void dce_v8_0_audio_get_connected_pins(struct amdgpu_device *adev)
1134a2e73f56SAlex Deucher {
1135a2e73f56SAlex Deucher 	int i;
1136a2e73f56SAlex Deucher 	u32 offset, tmp;
1137a2e73f56SAlex Deucher 
1138a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1139a2e73f56SAlex Deucher 		offset = adev->mode_info.audio.pin[i].offset;
1140a2e73f56SAlex Deucher 		tmp = RREG32_AUDIO_ENDPT(offset,
1141a2e73f56SAlex Deucher 					 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1142a2e73f56SAlex Deucher 		if (((tmp &
1143a2e73f56SAlex Deucher 		AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1144a2e73f56SAlex Deucher 		AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1145a2e73f56SAlex Deucher 			adev->mode_info.audio.pin[i].connected = false;
1146a2e73f56SAlex Deucher 		else
1147a2e73f56SAlex Deucher 			adev->mode_info.audio.pin[i].connected = true;
1148a2e73f56SAlex Deucher 	}
1149a2e73f56SAlex Deucher }
1150a2e73f56SAlex Deucher 
dce_v8_0_audio_get_pin(struct amdgpu_device * adev)1151a2e73f56SAlex Deucher static struct amdgpu_audio_pin *dce_v8_0_audio_get_pin(struct amdgpu_device *adev)
1152a2e73f56SAlex Deucher {
1153a2e73f56SAlex Deucher 	int i;
1154a2e73f56SAlex Deucher 
1155a2e73f56SAlex Deucher 	dce_v8_0_audio_get_connected_pins(adev);
1156a2e73f56SAlex Deucher 
1157a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1158a2e73f56SAlex Deucher 		if (adev->mode_info.audio.pin[i].connected)
1159a2e73f56SAlex Deucher 			return &adev->mode_info.audio.pin[i];
1160a2e73f56SAlex Deucher 	}
1161a2e73f56SAlex Deucher 	DRM_ERROR("No connected audio pins found!\n");
1162a2e73f56SAlex Deucher 	return NULL;
1163a2e73f56SAlex Deucher }
1164a2e73f56SAlex Deucher 
dce_v8_0_afmt_audio_select_pin(struct drm_encoder * encoder)1165a2e73f56SAlex Deucher static void dce_v8_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1166a2e73f56SAlex Deucher {
11671348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(encoder->dev);
1168a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1169a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1170a2e73f56SAlex Deucher 	u32 offset;
1171a2e73f56SAlex Deucher 
1172a2e73f56SAlex Deucher 	if (!dig || !dig->afmt || !dig->afmt->pin)
1173a2e73f56SAlex Deucher 		return;
1174a2e73f56SAlex Deucher 
1175a2e73f56SAlex Deucher 	offset = dig->afmt->offset;
1176a2e73f56SAlex Deucher 
1177a2e73f56SAlex Deucher 	WREG32(mmAFMT_AUDIO_SRC_CONTROL + offset,
1178a2e73f56SAlex Deucher 	       (dig->afmt->pin->id << AFMT_AUDIO_SRC_CONTROL__AFMT_AUDIO_SRC_SELECT__SHIFT));
1179a2e73f56SAlex Deucher }
1180a2e73f56SAlex Deucher 
dce_v8_0_audio_write_latency_fields(struct drm_encoder * encoder,struct drm_display_mode * mode)1181a2e73f56SAlex Deucher static void dce_v8_0_audio_write_latency_fields(struct drm_encoder *encoder,
1182a2e73f56SAlex Deucher 						struct drm_display_mode *mode)
1183a2e73f56SAlex Deucher {
1184f8d2d39eSLyude Paul 	struct drm_device *dev = encoder->dev;
11851348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1186a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1187a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1188a2e73f56SAlex Deucher 	struct drm_connector *connector;
1189f8d2d39eSLyude Paul 	struct drm_connector_list_iter iter;
1190a2e73f56SAlex Deucher 	struct amdgpu_connector *amdgpu_connector = NULL;
1191a2e73f56SAlex Deucher 	u32 tmp = 0, offset;
1192a2e73f56SAlex Deucher 
1193a2e73f56SAlex Deucher 	if (!dig || !dig->afmt || !dig->afmt->pin)
1194a2e73f56SAlex Deucher 		return;
1195a2e73f56SAlex Deucher 
1196a2e73f56SAlex Deucher 	offset = dig->afmt->pin->offset;
1197a2e73f56SAlex Deucher 
1198f8d2d39eSLyude Paul 	drm_connector_list_iter_begin(dev, &iter);
1199f8d2d39eSLyude Paul 	drm_for_each_connector_iter(connector, &iter) {
1200a2e73f56SAlex Deucher 		if (connector->encoder == encoder) {
1201a2e73f56SAlex Deucher 			amdgpu_connector = to_amdgpu_connector(connector);
1202a2e73f56SAlex Deucher 			break;
1203a2e73f56SAlex Deucher 		}
1204a2e73f56SAlex Deucher 	}
1205f8d2d39eSLyude Paul 	drm_connector_list_iter_end(&iter);
1206a2e73f56SAlex Deucher 
1207a2e73f56SAlex Deucher 	if (!amdgpu_connector) {
1208a2e73f56SAlex Deucher 		DRM_ERROR("Couldn't find encoder's connector\n");
1209a2e73f56SAlex Deucher 		return;
1210a2e73f56SAlex Deucher 	}
1211a2e73f56SAlex Deucher 
1212a2e73f56SAlex Deucher 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1213a2e73f56SAlex Deucher 		if (connector->latency_present[1])
1214a2e73f56SAlex Deucher 			tmp =
1215a2e73f56SAlex Deucher 			(connector->video_latency[1] <<
1216a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__VIDEO_LIPSYNC__SHIFT) |
1217a2e73f56SAlex Deucher 			(connector->audio_latency[1] <<
1218a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__AUDIO_LIPSYNC__SHIFT);
1219a2e73f56SAlex Deucher 		else
1220a2e73f56SAlex Deucher 			tmp =
1221a2e73f56SAlex Deucher 			(0 <<
1222a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__VIDEO_LIPSYNC__SHIFT) |
1223a2e73f56SAlex Deucher 			(0 <<
1224a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__AUDIO_LIPSYNC__SHIFT);
1225a2e73f56SAlex Deucher 	} else {
1226a2e73f56SAlex Deucher 		if (connector->latency_present[0])
1227a2e73f56SAlex Deucher 			tmp =
1228a2e73f56SAlex Deucher 			(connector->video_latency[0] <<
1229a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__VIDEO_LIPSYNC__SHIFT) |
1230a2e73f56SAlex Deucher 			(connector->audio_latency[0] <<
1231a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__AUDIO_LIPSYNC__SHIFT);
1232a2e73f56SAlex Deucher 		else
1233a2e73f56SAlex Deucher 			tmp =
1234a2e73f56SAlex Deucher 			(0 <<
1235a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__VIDEO_LIPSYNC__SHIFT) |
1236a2e73f56SAlex Deucher 			(0 <<
1237a2e73f56SAlex Deucher 			 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC__AUDIO_LIPSYNC__SHIFT);
1238a2e73f56SAlex Deucher 
1239a2e73f56SAlex Deucher 	}
1240a2e73f56SAlex Deucher 	WREG32_AUDIO_ENDPT(offset, ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1241a2e73f56SAlex Deucher }
1242a2e73f56SAlex Deucher 
dce_v8_0_audio_write_speaker_allocation(struct drm_encoder * encoder)1243a2e73f56SAlex Deucher static void dce_v8_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1244a2e73f56SAlex Deucher {
1245f8d2d39eSLyude Paul 	struct drm_device *dev = encoder->dev;
12461348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1247a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1248a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1249a2e73f56SAlex Deucher 	struct drm_connector *connector;
1250f8d2d39eSLyude Paul 	struct drm_connector_list_iter iter;
1251a2e73f56SAlex Deucher 	struct amdgpu_connector *amdgpu_connector = NULL;
1252a2e73f56SAlex Deucher 	u32 offset, tmp;
1253a2e73f56SAlex Deucher 	u8 *sadb = NULL;
1254a2e73f56SAlex Deucher 	int sad_count;
1255a2e73f56SAlex Deucher 
1256a2e73f56SAlex Deucher 	if (!dig || !dig->afmt || !dig->afmt->pin)
1257a2e73f56SAlex Deucher 		return;
1258a2e73f56SAlex Deucher 
1259a2e73f56SAlex Deucher 	offset = dig->afmt->pin->offset;
1260a2e73f56SAlex Deucher 
1261f8d2d39eSLyude Paul 	drm_connector_list_iter_begin(dev, &iter);
1262f8d2d39eSLyude Paul 	drm_for_each_connector_iter(connector, &iter) {
1263a2e73f56SAlex Deucher 		if (connector->encoder == encoder) {
1264a2e73f56SAlex Deucher 			amdgpu_connector = to_amdgpu_connector(connector);
1265a2e73f56SAlex Deucher 			break;
1266a2e73f56SAlex Deucher 		}
1267a2e73f56SAlex Deucher 	}
1268f8d2d39eSLyude Paul 	drm_connector_list_iter_end(&iter);
1269a2e73f56SAlex Deucher 
1270a2e73f56SAlex Deucher 	if (!amdgpu_connector) {
1271a2e73f56SAlex Deucher 		DRM_ERROR("Couldn't find encoder's connector\n");
1272a2e73f56SAlex Deucher 		return;
1273a2e73f56SAlex Deucher 	}
1274a2e73f56SAlex Deucher 
127542505ab1SJani Nikula 	sad_count = drm_edid_to_speaker_allocation(amdgpu_connector->edid, &sadb);
1276a2e73f56SAlex Deucher 	if (sad_count < 0) {
1277a2e73f56SAlex Deucher 		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1278a2e73f56SAlex Deucher 		sad_count = 0;
1279a2e73f56SAlex Deucher 	}
1280a2e73f56SAlex Deucher 
1281a2e73f56SAlex Deucher 	/* program the speaker allocation */
1282a2e73f56SAlex Deucher 	tmp = RREG32_AUDIO_ENDPT(offset, ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1283a2e73f56SAlex Deucher 	tmp &= ~(AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER__DP_CONNECTION_MASK |
1284a2e73f56SAlex Deucher 		AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER__SPEAKER_ALLOCATION_MASK);
1285a2e73f56SAlex Deucher 	/* set HDMI mode */
1286a2e73f56SAlex Deucher 	tmp |= AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER__HDMI_CONNECTION_MASK;
1287a2e73f56SAlex Deucher 	if (sad_count)
1288a2e73f56SAlex Deucher 		tmp |= (sadb[0] << AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER__SPEAKER_ALLOCATION__SHIFT);
1289a2e73f56SAlex Deucher 	else
1290a2e73f56SAlex Deucher 		tmp |= (5 << AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER__SPEAKER_ALLOCATION__SHIFT); /* stereo */
1291a2e73f56SAlex Deucher 	WREG32_AUDIO_ENDPT(offset, ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1292a2e73f56SAlex Deucher 
1293a2e73f56SAlex Deucher 	kfree(sadb);
1294a2e73f56SAlex Deucher }
1295a2e73f56SAlex Deucher 
dce_v8_0_audio_write_sad_regs(struct drm_encoder * encoder)1296a2e73f56SAlex Deucher static void dce_v8_0_audio_write_sad_regs(struct drm_encoder *encoder)
1297a2e73f56SAlex Deucher {
1298f8d2d39eSLyude Paul 	struct drm_device *dev = encoder->dev;
12991348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1300a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1301a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1302a2e73f56SAlex Deucher 	u32 offset;
1303a2e73f56SAlex Deucher 	struct drm_connector *connector;
1304f8d2d39eSLyude Paul 	struct drm_connector_list_iter iter;
1305a2e73f56SAlex Deucher 	struct amdgpu_connector *amdgpu_connector = NULL;
1306a2e73f56SAlex Deucher 	struct cea_sad *sads;
1307a2e73f56SAlex Deucher 	int i, sad_count;
1308a2e73f56SAlex Deucher 
1309a2e73f56SAlex Deucher 	static const u16 eld_reg_to_type[][2] = {
1310a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1311a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1312a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1313a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1314a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1315a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1316a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1317a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1318a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1319a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1320a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1321a2e73f56SAlex Deucher 		{ ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1322a2e73f56SAlex Deucher 	};
1323a2e73f56SAlex Deucher 
1324a2e73f56SAlex Deucher 	if (!dig || !dig->afmt || !dig->afmt->pin)
1325a2e73f56SAlex Deucher 		return;
1326a2e73f56SAlex Deucher 
1327a2e73f56SAlex Deucher 	offset = dig->afmt->pin->offset;
1328a2e73f56SAlex Deucher 
1329f8d2d39eSLyude Paul 	drm_connector_list_iter_begin(dev, &iter);
1330f8d2d39eSLyude Paul 	drm_for_each_connector_iter(connector, &iter) {
1331a2e73f56SAlex Deucher 		if (connector->encoder == encoder) {
1332a2e73f56SAlex Deucher 			amdgpu_connector = to_amdgpu_connector(connector);
1333a2e73f56SAlex Deucher 			break;
1334a2e73f56SAlex Deucher 		}
1335a2e73f56SAlex Deucher 	}
1336f8d2d39eSLyude Paul 	drm_connector_list_iter_end(&iter);
1337a2e73f56SAlex Deucher 
1338a2e73f56SAlex Deucher 	if (!amdgpu_connector) {
1339a2e73f56SAlex Deucher 		DRM_ERROR("Couldn't find encoder's connector\n");
1340a2e73f56SAlex Deucher 		return;
1341a2e73f56SAlex Deucher 	}
1342a2e73f56SAlex Deucher 
134342505ab1SJani Nikula 	sad_count = drm_edid_to_sad(amdgpu_connector->edid, &sads);
1344ae2a3495SJean Delvare 	if (sad_count < 0)
1345a2e73f56SAlex Deucher 		DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1346ae2a3495SJean Delvare 	if (sad_count <= 0)
1347a2e73f56SAlex Deucher 		return;
1348a2e73f56SAlex Deucher 	BUG_ON(!sads);
1349a2e73f56SAlex Deucher 
1350a2e73f56SAlex Deucher 	for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1351a2e73f56SAlex Deucher 		u32 value = 0;
1352a2e73f56SAlex Deucher 		u8 stereo_freqs = 0;
1353a2e73f56SAlex Deucher 		int max_channels = -1;
1354a2e73f56SAlex Deucher 		int j;
1355a2e73f56SAlex Deucher 
1356a2e73f56SAlex Deucher 		for (j = 0; j < sad_count; j++) {
1357a2e73f56SAlex Deucher 			struct cea_sad *sad = &sads[j];
1358a2e73f56SAlex Deucher 
1359a2e73f56SAlex Deucher 			if (sad->format == eld_reg_to_type[i][1]) {
1360a2e73f56SAlex Deucher 				if (sad->channels > max_channels) {
1361a2e73f56SAlex Deucher 					value = (sad->channels <<
1362a2e73f56SAlex Deucher 						 AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0__MAX_CHANNELS__SHIFT) |
1363a2e73f56SAlex Deucher 						(sad->byte2 <<
1364a2e73f56SAlex Deucher 						 AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0__DESCRIPTOR_BYTE_2__SHIFT) |
1365a2e73f56SAlex Deucher 						(sad->freq <<
1366a2e73f56SAlex Deucher 						 AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0__SUPPORTED_FREQUENCIES__SHIFT);
1367a2e73f56SAlex Deucher 					max_channels = sad->channels;
1368a2e73f56SAlex Deucher 				}
1369a2e73f56SAlex Deucher 
1370a2e73f56SAlex Deucher 				if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1371a2e73f56SAlex Deucher 					stereo_freqs |= sad->freq;
1372a2e73f56SAlex Deucher 				else
1373a2e73f56SAlex Deucher 					break;
1374a2e73f56SAlex Deucher 			}
1375a2e73f56SAlex Deucher 		}
1376a2e73f56SAlex Deucher 
1377a2e73f56SAlex Deucher 		value |= (stereo_freqs <<
1378a2e73f56SAlex Deucher 			AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0__SUPPORTED_FREQUENCIES_STEREO__SHIFT);
1379a2e73f56SAlex Deucher 
1380a2e73f56SAlex Deucher 		WREG32_AUDIO_ENDPT(offset, eld_reg_to_type[i][0], value);
1381a2e73f56SAlex Deucher 	}
1382a2e73f56SAlex Deucher 
1383a2e73f56SAlex Deucher 	kfree(sads);
1384a2e73f56SAlex Deucher }
1385a2e73f56SAlex Deucher 
dce_v8_0_audio_enable(struct amdgpu_device * adev,struct amdgpu_audio_pin * pin,bool enable)1386a2e73f56SAlex Deucher static void dce_v8_0_audio_enable(struct amdgpu_device *adev,
1387a2e73f56SAlex Deucher 				  struct amdgpu_audio_pin *pin,
1388a2e73f56SAlex Deucher 				  bool enable)
1389a2e73f56SAlex Deucher {
1390a2e73f56SAlex Deucher 	if (!pin)
1391a2e73f56SAlex Deucher 		return;
1392a2e73f56SAlex Deucher 
1393a2e73f56SAlex Deucher 	WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1394a2e73f56SAlex Deucher 		enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1395a2e73f56SAlex Deucher }
1396a2e73f56SAlex Deucher 
139718ef7544SRan Sun static const u32 pin_offsets[7] = {
1398a2e73f56SAlex Deucher 	(0x1780 - 0x1780),
1399a2e73f56SAlex Deucher 	(0x1786 - 0x1780),
1400a2e73f56SAlex Deucher 	(0x178c - 0x1780),
1401a2e73f56SAlex Deucher 	(0x1792 - 0x1780),
1402a2e73f56SAlex Deucher 	(0x1798 - 0x1780),
1403a2e73f56SAlex Deucher 	(0x179d - 0x1780),
1404a2e73f56SAlex Deucher 	(0x17a4 - 0x1780),
1405a2e73f56SAlex Deucher };
1406a2e73f56SAlex Deucher 
dce_v8_0_audio_init(struct amdgpu_device * adev)1407a2e73f56SAlex Deucher static int dce_v8_0_audio_init(struct amdgpu_device *adev)
1408a2e73f56SAlex Deucher {
1409a2e73f56SAlex Deucher 	int i;
1410a2e73f56SAlex Deucher 
1411a2e73f56SAlex Deucher 	if (!amdgpu_audio)
1412a2e73f56SAlex Deucher 		return 0;
1413a2e73f56SAlex Deucher 
1414a2e73f56SAlex Deucher 	adev->mode_info.audio.enabled = true;
1415a2e73f56SAlex Deucher 
1416a2e73f56SAlex Deucher 	if (adev->asic_type == CHIP_KAVERI) /* KV: 4 streams, 7 endpoints */
1417a2e73f56SAlex Deucher 		adev->mode_info.audio.num_pins = 7;
1418a2e73f56SAlex Deucher 	else if ((adev->asic_type == CHIP_KABINI) ||
1419a2e73f56SAlex Deucher 		 (adev->asic_type == CHIP_MULLINS)) /* KB/ML: 2 streams, 3 endpoints */
1420a2e73f56SAlex Deucher 		adev->mode_info.audio.num_pins = 3;
1421a2e73f56SAlex Deucher 	else if ((adev->asic_type == CHIP_BONAIRE) ||
1422a2e73f56SAlex Deucher 		 (adev->asic_type == CHIP_HAWAII))/* BN/HW: 6 streams, 7 endpoints */
1423a2e73f56SAlex Deucher 		adev->mode_info.audio.num_pins = 7;
1424a2e73f56SAlex Deucher 	else
1425a2e73f56SAlex Deucher 		adev->mode_info.audio.num_pins = 3;
1426a2e73f56SAlex Deucher 
1427a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1428a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].channels = -1;
1429a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].rate = -1;
1430a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].bits_per_sample = -1;
1431a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].status_bits = 0;
1432a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].category_code = 0;
1433a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].connected = false;
1434a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1435a2e73f56SAlex Deucher 		adev->mode_info.audio.pin[i].id = i;
1436a2e73f56SAlex Deucher 		/* disable audio.  it will be set up later */
1437a2e73f56SAlex Deucher 		/* XXX remove once we switch to ip funcs */
1438a2e73f56SAlex Deucher 		dce_v8_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1439a2e73f56SAlex Deucher 	}
1440a2e73f56SAlex Deucher 
1441a2e73f56SAlex Deucher 	return 0;
1442a2e73f56SAlex Deucher }
1443a2e73f56SAlex Deucher 
dce_v8_0_audio_fini(struct amdgpu_device * adev)1444a2e73f56SAlex Deucher static void dce_v8_0_audio_fini(struct amdgpu_device *adev)
1445a2e73f56SAlex Deucher {
1446a2e73f56SAlex Deucher 	int i;
1447a2e73f56SAlex Deucher 
144884cffef1STom St Denis 	if (!amdgpu_audio)
144984cffef1STom St Denis 		return;
145084cffef1STom St Denis 
1451a2e73f56SAlex Deucher 	if (!adev->mode_info.audio.enabled)
1452a2e73f56SAlex Deucher 		return;
1453a2e73f56SAlex Deucher 
1454a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1455a2e73f56SAlex Deucher 		dce_v8_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1456a2e73f56SAlex Deucher 
1457a2e73f56SAlex Deucher 	adev->mode_info.audio.enabled = false;
1458a2e73f56SAlex Deucher }
1459a2e73f56SAlex Deucher 
1460a2e73f56SAlex Deucher /*
1461a2e73f56SAlex Deucher  * update the N and CTS parameters for a given pixel clock rate
1462a2e73f56SAlex Deucher  */
dce_v8_0_afmt_update_ACR(struct drm_encoder * encoder,uint32_t clock)1463a2e73f56SAlex Deucher static void dce_v8_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1464a2e73f56SAlex Deucher {
1465a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
14661348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1467a2e73f56SAlex Deucher 	struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1468a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1469a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1470a2e73f56SAlex Deucher 	uint32_t offset = dig->afmt->offset;
1471a2e73f56SAlex Deucher 
147275cd45a4SAlexandre Demers 	WREG32(mmHDMI_ACR_32_0 + offset, (acr.cts_32khz << HDMI_ACR_32_0__HDMI_ACR_CTS_32__SHIFT));
1473a2e73f56SAlex Deucher 	WREG32(mmHDMI_ACR_32_1 + offset, acr.n_32khz);
1474a2e73f56SAlex Deucher 
1475a2e73f56SAlex Deucher 	WREG32(mmHDMI_ACR_44_0 + offset, (acr.cts_44_1khz << HDMI_ACR_44_0__HDMI_ACR_CTS_44__SHIFT));
1476a2e73f56SAlex Deucher 	WREG32(mmHDMI_ACR_44_1 + offset, acr.n_44_1khz);
1477a2e73f56SAlex Deucher 
1478a2e73f56SAlex Deucher 	WREG32(mmHDMI_ACR_48_0 + offset, (acr.cts_48khz << HDMI_ACR_48_0__HDMI_ACR_CTS_48__SHIFT));
1479a2e73f56SAlex Deucher 	WREG32(mmHDMI_ACR_48_1 + offset, acr.n_48khz);
1480a2e73f56SAlex Deucher }
1481a2e73f56SAlex Deucher 
1482a2e73f56SAlex Deucher /*
1483a2e73f56SAlex Deucher  * build a HDMI Video Info Frame
1484a2e73f56SAlex Deucher  */
dce_v8_0_afmt_update_avi_infoframe(struct drm_encoder * encoder,void * buffer,size_t size)1485a2e73f56SAlex Deucher static void dce_v8_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1486a2e73f56SAlex Deucher 					       void *buffer, size_t size)
1487a2e73f56SAlex Deucher {
1488a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
14891348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1490a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1491a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1492a2e73f56SAlex Deucher 	uint32_t offset = dig->afmt->offset;
1493a2e73f56SAlex Deucher 	uint8_t *frame = buffer + 3;
1494a2e73f56SAlex Deucher 	uint8_t *header = buffer;
1495a2e73f56SAlex Deucher 
1496a2e73f56SAlex Deucher 	WREG32(mmAFMT_AVI_INFO0 + offset,
1497a2e73f56SAlex Deucher 		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1498a2e73f56SAlex Deucher 	WREG32(mmAFMT_AVI_INFO1 + offset,
1499a2e73f56SAlex Deucher 		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1500a2e73f56SAlex Deucher 	WREG32(mmAFMT_AVI_INFO2 + offset,
1501a2e73f56SAlex Deucher 		frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1502a2e73f56SAlex Deucher 	WREG32(mmAFMT_AVI_INFO3 + offset,
1503a2e73f56SAlex Deucher 		frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1504a2e73f56SAlex Deucher }
1505a2e73f56SAlex Deucher 
dce_v8_0_audio_set_dto(struct drm_encoder * encoder,u32 clock)1506a2e73f56SAlex Deucher static void dce_v8_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1507a2e73f56SAlex Deucher {
1508a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
15091348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1510a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1511a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1512a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1513a2e73f56SAlex Deucher 	u32 dto_phase = 24 * 1000;
1514a2e73f56SAlex Deucher 	u32 dto_modulo = clock;
1515a2e73f56SAlex Deucher 
1516a2e73f56SAlex Deucher 	if (!dig || !dig->afmt)
1517a2e73f56SAlex Deucher 		return;
1518a2e73f56SAlex Deucher 
1519a2e73f56SAlex Deucher 	/* XXX two dtos; generally use dto0 for hdmi */
1520a2e73f56SAlex Deucher 	/* Express [24MHz / target pixel clock] as an exact rational
1521a2e73f56SAlex Deucher 	 * number (coefficient of two integer numbers.  DCCG_AUDIO_DTOx_PHASE
1522a2e73f56SAlex Deucher 	 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1523a2e73f56SAlex Deucher 	 */
1524a2e73f56SAlex Deucher 	WREG32(mmDCCG_AUDIO_DTO_SOURCE, (amdgpu_crtc->crtc_id << DCCG_AUDIO_DTO_SOURCE__DCCG_AUDIO_DTO0_SOURCE_SEL__SHIFT));
1525a2e73f56SAlex Deucher 	WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1526a2e73f56SAlex Deucher 	WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1527a2e73f56SAlex Deucher }
1528a2e73f56SAlex Deucher 
1529a2e73f56SAlex Deucher /*
1530a2e73f56SAlex Deucher  * update the info frames with the data from the current display mode
1531a2e73f56SAlex Deucher  */
dce_v8_0_afmt_setmode(struct drm_encoder * encoder,struct drm_display_mode * mode)1532a2e73f56SAlex Deucher static void dce_v8_0_afmt_setmode(struct drm_encoder *encoder,
1533a2e73f56SAlex Deucher 				  struct drm_display_mode *mode)
1534a2e73f56SAlex Deucher {
1535a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
15361348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1537a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1538a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1539a2e73f56SAlex Deucher 	struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1540a2e73f56SAlex Deucher 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1541a2e73f56SAlex Deucher 	struct hdmi_avi_infoframe frame;
1542a2e73f56SAlex Deucher 	uint32_t offset, val;
1543a2e73f56SAlex Deucher 	ssize_t err;
1544a2e73f56SAlex Deucher 	int bpc = 8;
1545a2e73f56SAlex Deucher 
1546a2e73f56SAlex Deucher 	if (!dig || !dig->afmt)
1547a2e73f56SAlex Deucher 		return;
1548a2e73f56SAlex Deucher 
1549a2e73f56SAlex Deucher 	/* Silent, r600_hdmi_enable will raise WARN for us */
1550a2e73f56SAlex Deucher 	if (!dig->afmt->enabled)
1551a2e73f56SAlex Deucher 		return;
1552dfaf2291SAlexandre Demers 
1553a2e73f56SAlex Deucher 	offset = dig->afmt->offset;
1554a2e73f56SAlex Deucher 
1555a2e73f56SAlex Deucher 	/* hdmi deep color mode general control packets setup, if bpc > 8 */
1556a2e73f56SAlex Deucher 	if (encoder->crtc) {
1557a2e73f56SAlex Deucher 		struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1558a2e73f56SAlex Deucher 		bpc = amdgpu_crtc->bpc;
1559a2e73f56SAlex Deucher 	}
1560a2e73f56SAlex Deucher 
1561a2e73f56SAlex Deucher 	/* disable audio prior to setting up hw */
1562a2e73f56SAlex Deucher 	dig->afmt->pin = dce_v8_0_audio_get_pin(adev);
1563a2e73f56SAlex Deucher 	dce_v8_0_audio_enable(adev, dig->afmt->pin, false);
1564a2e73f56SAlex Deucher 
1565a2e73f56SAlex Deucher 	dce_v8_0_audio_set_dto(encoder, mode->clock);
1566a2e73f56SAlex Deucher 
1567a2e73f56SAlex Deucher 	WREG32(mmHDMI_VBI_PACKET_CONTROL + offset,
1568a2e73f56SAlex Deucher 	       HDMI_VBI_PACKET_CONTROL__HDMI_NULL_SEND_MASK); /* send null packets when required */
1569a2e73f56SAlex Deucher 
1570a2e73f56SAlex Deucher 	WREG32(mmAFMT_AUDIO_CRC_CONTROL + offset, 0x1000);
1571a2e73f56SAlex Deucher 
1572a2e73f56SAlex Deucher 	val = RREG32(mmHDMI_CONTROL + offset);
1573a2e73f56SAlex Deucher 	val &= ~HDMI_CONTROL__HDMI_DEEP_COLOR_ENABLE_MASK;
1574a2e73f56SAlex Deucher 	val &= ~HDMI_CONTROL__HDMI_DEEP_COLOR_DEPTH_MASK;
1575a2e73f56SAlex Deucher 
1576a2e73f56SAlex Deucher 	switch (bpc) {
1577a2e73f56SAlex Deucher 	case 0:
1578a2e73f56SAlex Deucher 	case 6:
1579a2e73f56SAlex Deucher 	case 8:
1580a2e73f56SAlex Deucher 	case 16:
1581a2e73f56SAlex Deucher 	default:
1582a2e73f56SAlex Deucher 		DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1583a2e73f56SAlex Deucher 			  connector->name, bpc);
1584a2e73f56SAlex Deucher 		break;
1585a2e73f56SAlex Deucher 	case 10:
1586a2e73f56SAlex Deucher 		val |= HDMI_CONTROL__HDMI_DEEP_COLOR_ENABLE_MASK;
1587a2e73f56SAlex Deucher 		val |= 1 << HDMI_CONTROL__HDMI_DEEP_COLOR_DEPTH__SHIFT;
1588a2e73f56SAlex Deucher 		DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1589a2e73f56SAlex Deucher 			  connector->name);
1590a2e73f56SAlex Deucher 		break;
1591a2e73f56SAlex Deucher 	case 12:
1592a2e73f56SAlex Deucher 		val |= HDMI_CONTROL__HDMI_DEEP_COLOR_ENABLE_MASK;
1593a2e73f56SAlex Deucher 		val |= 2 << HDMI_CONTROL__HDMI_DEEP_COLOR_DEPTH__SHIFT;
1594a2e73f56SAlex Deucher 		DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1595a2e73f56SAlex Deucher 			  connector->name);
1596a2e73f56SAlex Deucher 		break;
1597a2e73f56SAlex Deucher 	}
1598a2e73f56SAlex Deucher 
1599a2e73f56SAlex Deucher 	WREG32(mmHDMI_CONTROL + offset, val);
1600a2e73f56SAlex Deucher 
1601a2e73f56SAlex Deucher 	WREG32(mmHDMI_VBI_PACKET_CONTROL + offset,
1602a2e73f56SAlex Deucher 	       HDMI_VBI_PACKET_CONTROL__HDMI_NULL_SEND_MASK | /* send null packets when required */
1603a2e73f56SAlex Deucher 	       HDMI_VBI_PACKET_CONTROL__HDMI_GC_SEND_MASK | /* send general control packets */
1604a2e73f56SAlex Deucher 	       HDMI_VBI_PACKET_CONTROL__HDMI_GC_CONT_MASK); /* send general control packets every frame */
1605a2e73f56SAlex Deucher 
1606a2e73f56SAlex Deucher 	WREG32(mmHDMI_INFOFRAME_CONTROL0 + offset,
1607a2e73f56SAlex Deucher 	       HDMI_INFOFRAME_CONTROL0__HDMI_AUDIO_INFO_SEND_MASK | /* enable audio info frames (frames won't be set until audio is enabled) */
1608a2e73f56SAlex Deucher 	       HDMI_INFOFRAME_CONTROL0__HDMI_AUDIO_INFO_CONT_MASK); /* required for audio info values to be updated */
1609a2e73f56SAlex Deucher 
1610a2e73f56SAlex Deucher 	WREG32(mmAFMT_INFOFRAME_CONTROL0 + offset,
1611a2e73f56SAlex Deucher 	       AFMT_INFOFRAME_CONTROL0__AFMT_AUDIO_INFO_UPDATE_MASK); /* required for audio info values to be updated */
1612a2e73f56SAlex Deucher 
1613a2e73f56SAlex Deucher 	WREG32(mmHDMI_INFOFRAME_CONTROL1 + offset,
1614a2e73f56SAlex Deucher 	       (2 << HDMI_INFOFRAME_CONTROL1__HDMI_AUDIO_INFO_LINE__SHIFT)); /* anything other than 0 */
1615a2e73f56SAlex Deucher 
1616a2e73f56SAlex Deucher 	WREG32(mmHDMI_GC + offset, 0); /* unset HDMI_GC_AVMUTE */
1617a2e73f56SAlex Deucher 
1618a2e73f56SAlex Deucher 	WREG32(mmHDMI_AUDIO_PACKET_CONTROL + offset,
1619a2e73f56SAlex Deucher 	       (1 << HDMI_AUDIO_PACKET_CONTROL__HDMI_AUDIO_DELAY_EN__SHIFT) | /* set the default audio delay */
1620a2e73f56SAlex Deucher 	       (3 << HDMI_AUDIO_PACKET_CONTROL__HDMI_AUDIO_PACKETS_PER_LINE__SHIFT)); /* should be suffient for all audio modes and small enough for all hblanks */
1621a2e73f56SAlex Deucher 
1622a2e73f56SAlex Deucher 	WREG32(mmAFMT_AUDIO_PACKET_CONTROL + offset,
1623a2e73f56SAlex Deucher 	       AFMT_AUDIO_PACKET_CONTROL__AFMT_60958_CS_UPDATE_MASK); /* allow 60958 channel status fields to be updated */
1624a2e73f56SAlex Deucher 
1625a2e73f56SAlex Deucher 	/* fglrx clears sth in AFMT_AUDIO_PACKET_CONTROL2 here */
1626a2e73f56SAlex Deucher 
1627a2e73f56SAlex Deucher 	if (bpc > 8)
1628a2e73f56SAlex Deucher 		WREG32(mmHDMI_ACR_PACKET_CONTROL + offset,
1629a2e73f56SAlex Deucher 		       HDMI_ACR_PACKET_CONTROL__HDMI_ACR_AUTO_SEND_MASK); /* allow hw to sent ACR packets when required */
1630a2e73f56SAlex Deucher 	else
1631a2e73f56SAlex Deucher 		WREG32(mmHDMI_ACR_PACKET_CONTROL + offset,
1632a2e73f56SAlex Deucher 		       HDMI_ACR_PACKET_CONTROL__HDMI_ACR_SOURCE_MASK | /* select SW CTS value */
1633a2e73f56SAlex Deucher 		       HDMI_ACR_PACKET_CONTROL__HDMI_ACR_AUTO_SEND_MASK); /* allow hw to sent ACR packets when required */
1634a2e73f56SAlex Deucher 
1635a2e73f56SAlex Deucher 	dce_v8_0_afmt_update_ACR(encoder, mode->clock);
1636a2e73f56SAlex Deucher 
1637a2e73f56SAlex Deucher 	WREG32(mmAFMT_60958_0 + offset,
1638a2e73f56SAlex Deucher 	       (1 << AFMT_60958_0__AFMT_60958_CS_CHANNEL_NUMBER_L__SHIFT));
1639a2e73f56SAlex Deucher 
1640a2e73f56SAlex Deucher 	WREG32(mmAFMT_60958_1 + offset,
1641a2e73f56SAlex Deucher 	       (2 << AFMT_60958_1__AFMT_60958_CS_CHANNEL_NUMBER_R__SHIFT));
1642a2e73f56SAlex Deucher 
1643a2e73f56SAlex Deucher 	WREG32(mmAFMT_60958_2 + offset,
1644a2e73f56SAlex Deucher 	       (3 << AFMT_60958_2__AFMT_60958_CS_CHANNEL_NUMBER_2__SHIFT) |
1645a2e73f56SAlex Deucher 	       (4 << AFMT_60958_2__AFMT_60958_CS_CHANNEL_NUMBER_3__SHIFT) |
1646a2e73f56SAlex Deucher 	       (5 << AFMT_60958_2__AFMT_60958_CS_CHANNEL_NUMBER_4__SHIFT) |
1647a2e73f56SAlex Deucher 	       (6 << AFMT_60958_2__AFMT_60958_CS_CHANNEL_NUMBER_5__SHIFT) |
1648a2e73f56SAlex Deucher 	       (7 << AFMT_60958_2__AFMT_60958_CS_CHANNEL_NUMBER_6__SHIFT) |
1649a2e73f56SAlex Deucher 	       (8 << AFMT_60958_2__AFMT_60958_CS_CHANNEL_NUMBER_7__SHIFT));
1650a2e73f56SAlex Deucher 
1651a2e73f56SAlex Deucher 	dce_v8_0_audio_write_speaker_allocation(encoder);
1652a2e73f56SAlex Deucher 
1653a2e73f56SAlex Deucher 
1654a2e73f56SAlex Deucher 	WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + offset,
1655a2e73f56SAlex Deucher 	       (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1656a2e73f56SAlex Deucher 
1657a2e73f56SAlex Deucher 	dce_v8_0_afmt_audio_select_pin(encoder);
1658a2e73f56SAlex Deucher 	dce_v8_0_audio_write_sad_regs(encoder);
1659a2e73f56SAlex Deucher 	dce_v8_0_audio_write_latency_fields(encoder, mode);
1660a2e73f56SAlex Deucher 
166113d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
1662a2e73f56SAlex Deucher 	if (err < 0) {
1663a2e73f56SAlex Deucher 		DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1664a2e73f56SAlex Deucher 		return;
1665a2e73f56SAlex Deucher 	}
1666a2e73f56SAlex Deucher 
1667a2e73f56SAlex Deucher 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1668a2e73f56SAlex Deucher 	if (err < 0) {
1669a2e73f56SAlex Deucher 		DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1670a2e73f56SAlex Deucher 		return;
1671a2e73f56SAlex Deucher 	}
1672a2e73f56SAlex Deucher 
1673a2e73f56SAlex Deucher 	dce_v8_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1674a2e73f56SAlex Deucher 
1675a2e73f56SAlex Deucher 	WREG32_OR(mmHDMI_INFOFRAME_CONTROL0 + offset,
1676a2e73f56SAlex Deucher 		  HDMI_INFOFRAME_CONTROL0__HDMI_AVI_INFO_SEND_MASK | /* enable AVI info frames */
1677dfaf2291SAlexandre Demers 		  HDMI_INFOFRAME_CONTROL0__HDMI_AVI_INFO_CONT_MASK); /* required for audio info values to be updated */
1678a2e73f56SAlex Deucher 
1679a2e73f56SAlex Deucher 	WREG32_P(mmHDMI_INFOFRAME_CONTROL1 + offset,
1680a2e73f56SAlex Deucher 		 (2 << HDMI_INFOFRAME_CONTROL1__HDMI_AVI_INFO_LINE__SHIFT), /* anything other than 0 */
1681a2e73f56SAlex Deucher 		 ~HDMI_INFOFRAME_CONTROL1__HDMI_AVI_INFO_LINE_MASK);
1682a2e73f56SAlex Deucher 
1683a2e73f56SAlex Deucher 	WREG32_OR(mmAFMT_AUDIO_PACKET_CONTROL + offset,
1684a2e73f56SAlex Deucher 		  AFMT_AUDIO_PACKET_CONTROL__AFMT_AUDIO_SAMPLE_SEND_MASK); /* send audio packets */
1685a2e73f56SAlex Deucher 
1686a2e73f56SAlex Deucher 	WREG32(mmAFMT_RAMP_CONTROL0 + offset, 0x00FFFFFF);
1687a2e73f56SAlex Deucher 	WREG32(mmAFMT_RAMP_CONTROL1 + offset, 0x007FFFFF);
1688a2e73f56SAlex Deucher 	WREG32(mmAFMT_RAMP_CONTROL2 + offset, 0x00000001);
1689a2e73f56SAlex Deucher 	WREG32(mmAFMT_RAMP_CONTROL3 + offset, 0x00000001);
1690a2e73f56SAlex Deucher 
1691dfaf2291SAlexandre Demers 	/* enable audio after setting up hw */
1692a2e73f56SAlex Deucher 	dce_v8_0_audio_enable(adev, dig->afmt->pin, true);
1693a2e73f56SAlex Deucher }
1694a2e73f56SAlex Deucher 
dce_v8_0_afmt_enable(struct drm_encoder * encoder,bool enable)1695a2e73f56SAlex Deucher static void dce_v8_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1696a2e73f56SAlex Deucher {
1697a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
16981348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1699a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1700a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1701a2e73f56SAlex Deucher 
1702a2e73f56SAlex Deucher 	if (!dig || !dig->afmt)
1703a2e73f56SAlex Deucher 		return;
1704a2e73f56SAlex Deucher 
1705a2e73f56SAlex Deucher 	/* Silent, r600_hdmi_enable will raise WARN for us */
1706a2e73f56SAlex Deucher 	if (enable && dig->afmt->enabled)
1707a2e73f56SAlex Deucher 		return;
1708a2e73f56SAlex Deucher 	if (!enable && !dig->afmt->enabled)
1709a2e73f56SAlex Deucher 		return;
1710a2e73f56SAlex Deucher 
1711a2e73f56SAlex Deucher 	if (!enable && dig->afmt->pin) {
1712a2e73f56SAlex Deucher 		dce_v8_0_audio_enable(adev, dig->afmt->pin, false);
1713a2e73f56SAlex Deucher 		dig->afmt->pin = NULL;
1714a2e73f56SAlex Deucher 	}
1715a2e73f56SAlex Deucher 
1716a2e73f56SAlex Deucher 	dig->afmt->enabled = enable;
1717a2e73f56SAlex Deucher 
1718a2e73f56SAlex Deucher 	DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1719a2e73f56SAlex Deucher 		  enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1720a2e73f56SAlex Deucher }
1721a2e73f56SAlex Deucher 
dce_v8_0_afmt_init(struct amdgpu_device * adev)1722ff923479STom St Denis static int dce_v8_0_afmt_init(struct amdgpu_device *adev)
1723a2e73f56SAlex Deucher {
1724a2e73f56SAlex Deucher 	int i;
1725a2e73f56SAlex Deucher 
1726a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_dig; i++)
1727a2e73f56SAlex Deucher 		adev->mode_info.afmt[i] = NULL;
1728a2e73f56SAlex Deucher 
1729a2e73f56SAlex Deucher 	/* DCE8 has audio blocks tied to DIG encoders */
1730a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_dig; i++) {
1731a2e73f56SAlex Deucher 		adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1732a2e73f56SAlex Deucher 		if (adev->mode_info.afmt[i]) {
1733a2e73f56SAlex Deucher 			adev->mode_info.afmt[i]->offset = dig_offsets[i];
1734a2e73f56SAlex Deucher 			adev->mode_info.afmt[i]->id = i;
1735ff923479STom St Denis 		} else {
1736ff923479STom St Denis 			int j;
1737ff923479STom St Denis 			for (j = 0; j < i; j++) {
1738ff923479STom St Denis 				kfree(adev->mode_info.afmt[j]);
1739ff923479STom St Denis 				adev->mode_info.afmt[j] = NULL;
1740ff923479STom St Denis 			}
1741ff923479STom St Denis 			return -ENOMEM;
1742a2e73f56SAlex Deucher 		}
1743a2e73f56SAlex Deucher 	}
1744ff923479STom St Denis 	return 0;
1745a2e73f56SAlex Deucher }
1746a2e73f56SAlex Deucher 
dce_v8_0_afmt_fini(struct amdgpu_device * adev)1747a2e73f56SAlex Deucher static void dce_v8_0_afmt_fini(struct amdgpu_device *adev)
1748a2e73f56SAlex Deucher {
1749a2e73f56SAlex Deucher 	int i;
1750a2e73f56SAlex Deucher 
1751a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_dig; i++) {
1752a2e73f56SAlex Deucher 		kfree(adev->mode_info.afmt[i]);
1753a2e73f56SAlex Deucher 		adev->mode_info.afmt[i] = NULL;
1754a2e73f56SAlex Deucher 	}
1755a2e73f56SAlex Deucher }
1756a2e73f56SAlex Deucher 
175718ef7544SRan Sun static const u32 vga_control_regs[6] = {
1758a2e73f56SAlex Deucher 	mmD1VGA_CONTROL,
1759a2e73f56SAlex Deucher 	mmD2VGA_CONTROL,
1760a2e73f56SAlex Deucher 	mmD3VGA_CONTROL,
1761a2e73f56SAlex Deucher 	mmD4VGA_CONTROL,
1762a2e73f56SAlex Deucher 	mmD5VGA_CONTROL,
1763a2e73f56SAlex Deucher 	mmD6VGA_CONTROL,
1764a2e73f56SAlex Deucher };
1765a2e73f56SAlex Deucher 
dce_v8_0_vga_enable(struct drm_crtc * crtc,bool enable)1766a2e73f56SAlex Deucher static void dce_v8_0_vga_enable(struct drm_crtc *crtc, bool enable)
1767a2e73f56SAlex Deucher {
1768a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1769a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
17701348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1771a2e73f56SAlex Deucher 	u32 vga_control;
1772a2e73f56SAlex Deucher 
1773a2e73f56SAlex Deucher 	vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
1774a2e73f56SAlex Deucher 	if (enable)
1775a2e73f56SAlex Deucher 		WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
1776a2e73f56SAlex Deucher 	else
1777a2e73f56SAlex Deucher 		WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
1778a2e73f56SAlex Deucher }
1779a2e73f56SAlex Deucher 
dce_v8_0_grph_enable(struct drm_crtc * crtc,bool enable)1780a2e73f56SAlex Deucher static void dce_v8_0_grph_enable(struct drm_crtc *crtc, bool enable)
1781a2e73f56SAlex Deucher {
1782a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1783a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
17841348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1785a2e73f56SAlex Deucher 
1786a2e73f56SAlex Deucher 	if (enable)
1787a2e73f56SAlex Deucher 		WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
1788a2e73f56SAlex Deucher 	else
1789a2e73f56SAlex Deucher 		WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
1790a2e73f56SAlex Deucher }
1791a2e73f56SAlex Deucher 
dce_v8_0_crtc_do_set_base(struct drm_crtc * crtc,struct drm_framebuffer * fb,int x,int y,int atomic)1792a2e73f56SAlex Deucher static int dce_v8_0_crtc_do_set_base(struct drm_crtc *crtc,
1793a2e73f56SAlex Deucher 				     struct drm_framebuffer *fb,
1794a2e73f56SAlex Deucher 				     int x, int y, int atomic)
1795a2e73f56SAlex Deucher {
1796a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1797a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
17981348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
1799a2e73f56SAlex Deucher 	struct drm_framebuffer *target_fb;
1800a2e73f56SAlex Deucher 	struct drm_gem_object *obj;
1801765e7fbfSChristian König 	struct amdgpu_bo *abo;
1802a2e73f56SAlex Deucher 	uint64_t fb_location, tiling_flags;
1803a2e73f56SAlex Deucher 	uint32_t fb_format, fb_pitch_pixels;
1804a2e73f56SAlex Deucher 	u32 fb_swap = (GRPH_ENDIAN_NONE << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1805fbd76d59SMarek Olšák 	u32 pipe_config;
1806cb9e59d7SAlex Deucher 	u32 viewport_w, viewport_h;
1807a2e73f56SAlex Deucher 	int r;
1808a2e73f56SAlex Deucher 	bool bypass_lut = false;
1809a2e73f56SAlex Deucher 
1810a2e73f56SAlex Deucher 	/* no fb bound */
1811a2e73f56SAlex Deucher 	if (!atomic && !crtc->primary->fb) {
1812a2e73f56SAlex Deucher 		DRM_DEBUG_KMS("No FB bound\n");
1813a2e73f56SAlex Deucher 		return 0;
1814a2e73f56SAlex Deucher 	}
1815a2e73f56SAlex Deucher 
1816e68d14ddSDaniel Stone 	if (atomic)
1817a2e73f56SAlex Deucher 		target_fb = fb;
1818e68d14ddSDaniel Stone 	else
1819a2e73f56SAlex Deucher 		target_fb = crtc->primary->fb;
1820a2e73f56SAlex Deucher 
1821a2e73f56SAlex Deucher 	/* If atomic, assume fb object is pinned & idle & fenced and
1822a2e73f56SAlex Deucher 	 * just update base pointers
1823a2e73f56SAlex Deucher 	 */
1824e68d14ddSDaniel Stone 	obj = target_fb->obj[0];
1825765e7fbfSChristian König 	abo = gem_to_amdgpu_bo(obj);
1826765e7fbfSChristian König 	r = amdgpu_bo_reserve(abo, false);
1827a2e73f56SAlex Deucher 	if (unlikely(r != 0))
1828a2e73f56SAlex Deucher 		return r;
1829a2e73f56SAlex Deucher 
18307b7c6c81SJunwei Zhang 	if (!atomic) {
1831*54b86443SChristian König 		abo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
18327b7c6c81SJunwei Zhang 		r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM);
1833a2e73f56SAlex Deucher 		if (unlikely(r != 0)) {
1834765e7fbfSChristian König 			amdgpu_bo_unreserve(abo);
1835a2e73f56SAlex Deucher 			return -EINVAL;
1836a2e73f56SAlex Deucher 		}
1837a2e73f56SAlex Deucher 	}
18387b7c6c81SJunwei Zhang 	fb_location = amdgpu_bo_gpu_offset(abo);
1839a2e73f56SAlex Deucher 
1840765e7fbfSChristian König 	amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
1841765e7fbfSChristian König 	amdgpu_bo_unreserve(abo);
1842a2e73f56SAlex Deucher 
1843fbd76d59SMarek Olšák 	pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1844fbd76d59SMarek Olšák 
1845438b74a5SVille Syrjälä 	switch (target_fb->format->format) {
1846a2e73f56SAlex Deucher 	case DRM_FORMAT_C8:
1847a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_8BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1848a2e73f56SAlex Deucher 			     (GRPH_FORMAT_INDEXED << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1849a2e73f56SAlex Deucher 		break;
1850a2e73f56SAlex Deucher 	case DRM_FORMAT_XRGB4444:
1851a2e73f56SAlex Deucher 	case DRM_FORMAT_ARGB4444:
1852a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_16BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
185375cd45a4SAlexandre Demers 			     (GRPH_FORMAT_ARGB4444 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1854a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1855a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN16 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1856a2e73f56SAlex Deucher #endif
1857a2e73f56SAlex Deucher 		break;
1858a2e73f56SAlex Deucher 	case DRM_FORMAT_XRGB1555:
1859a2e73f56SAlex Deucher 	case DRM_FORMAT_ARGB1555:
1860a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_16BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1861a2e73f56SAlex Deucher 			     (GRPH_FORMAT_ARGB1555 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1862a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1863a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN16 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1864a2e73f56SAlex Deucher #endif
1865a2e73f56SAlex Deucher 		break;
1866a2e73f56SAlex Deucher 	case DRM_FORMAT_BGRX5551:
1867a2e73f56SAlex Deucher 	case DRM_FORMAT_BGRA5551:
1868a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_16BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1869a2e73f56SAlex Deucher 			     (GRPH_FORMAT_BGRA5551 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1870a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1871a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN16 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1872a2e73f56SAlex Deucher #endif
1873a2e73f56SAlex Deucher 		break;
1874a2e73f56SAlex Deucher 	case DRM_FORMAT_RGB565:
1875a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_16BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1876a2e73f56SAlex Deucher 			     (GRPH_FORMAT_ARGB565 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1877a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1878a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN16 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1879a2e73f56SAlex Deucher #endif
1880a2e73f56SAlex Deucher 		break;
1881a2e73f56SAlex Deucher 	case DRM_FORMAT_XRGB8888:
1882a2e73f56SAlex Deucher 	case DRM_FORMAT_ARGB8888:
1883a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_32BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1884a2e73f56SAlex Deucher 			     (GRPH_FORMAT_ARGB8888 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1885a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1886a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN32 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1887a2e73f56SAlex Deucher #endif
1888a2e73f56SAlex Deucher 		break;
1889a2e73f56SAlex Deucher 	case DRM_FORMAT_XRGB2101010:
1890a2e73f56SAlex Deucher 	case DRM_FORMAT_ARGB2101010:
1891a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_32BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1892a2e73f56SAlex Deucher 			     (GRPH_FORMAT_ARGB2101010 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1893a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1894a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN32 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1895a2e73f56SAlex Deucher #endif
1896a2e73f56SAlex Deucher 		/* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1897a2e73f56SAlex Deucher 		bypass_lut = true;
1898a2e73f56SAlex Deucher 		break;
1899a2e73f56SAlex Deucher 	case DRM_FORMAT_BGRX1010102:
1900a2e73f56SAlex Deucher 	case DRM_FORMAT_BGRA1010102:
1901a2e73f56SAlex Deucher 		fb_format = ((GRPH_DEPTH_32BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
1902a2e73f56SAlex Deucher 			     (GRPH_FORMAT_BGRA1010102 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
1903a2e73f56SAlex Deucher #ifdef __BIG_ENDIAN
1904a2e73f56SAlex Deucher 		fb_swap = (GRPH_ENDIAN_8IN32 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
1905a2e73f56SAlex Deucher #endif
1906a2e73f56SAlex Deucher 		/* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1907a2e73f56SAlex Deucher 		bypass_lut = true;
1908a2e73f56SAlex Deucher 		break;
190900ecc6e6SMauro Rossi 	case DRM_FORMAT_XBGR8888:
191000ecc6e6SMauro Rossi 	case DRM_FORMAT_ABGR8888:
191100ecc6e6SMauro Rossi 		fb_format = ((GRPH_DEPTH_32BPP << GRPH_CONTROL__GRPH_DEPTH__SHIFT) |
191200ecc6e6SMauro Rossi 				(GRPH_FORMAT_ARGB8888 << GRPH_CONTROL__GRPH_FORMAT__SHIFT));
191300ecc6e6SMauro Rossi 		fb_swap = ((GRPH_RED_SEL_B << GRPH_SWAP_CNTL__GRPH_RED_CROSSBAR__SHIFT) |
191400ecc6e6SMauro Rossi 			(GRPH_BLUE_SEL_R << GRPH_SWAP_CNTL__GRPH_BLUE_CROSSBAR__SHIFT));
191500ecc6e6SMauro Rossi #ifdef __BIG_ENDIAN
191600ecc6e6SMauro Rossi 		fb_swap |= (GRPH_ENDIAN_8IN32 << GRPH_SWAP_CNTL__GRPH_ENDIAN_SWAP__SHIFT);
191700ecc6e6SMauro Rossi #endif
191800ecc6e6SMauro Rossi 		break;
1919a2e73f56SAlex Deucher 	default:
192092f1d09cSSakari Ailus 		DRM_ERROR("Unsupported screen format %p4cc\n",
192192f1d09cSSakari Ailus 			  &target_fb->format->format);
1922a2e73f56SAlex Deucher 		return -EINVAL;
1923a2e73f56SAlex Deucher 	}
1924a2e73f56SAlex Deucher 
1925fbd76d59SMarek Olšák 	if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
1926fbd76d59SMarek Olšák 		unsigned bankw, bankh, mtaspect, tile_split, num_banks;
1927a2e73f56SAlex Deucher 
1928fbd76d59SMarek Olšák 		bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1929fbd76d59SMarek Olšák 		bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1930fbd76d59SMarek Olšák 		mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1931fbd76d59SMarek Olšák 		tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
1932fbd76d59SMarek Olšák 		num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1933a2e73f56SAlex Deucher 
1934a2e73f56SAlex Deucher 		fb_format |= (num_banks << GRPH_CONTROL__GRPH_NUM_BANKS__SHIFT);
1935a2e73f56SAlex Deucher 		fb_format |= (GRPH_ARRAY_2D_TILED_THIN1 << GRPH_CONTROL__GRPH_ARRAY_MODE__SHIFT);
1936a2e73f56SAlex Deucher 		fb_format |= (tile_split << GRPH_CONTROL__GRPH_TILE_SPLIT__SHIFT);
1937a2e73f56SAlex Deucher 		fb_format |= (bankw << GRPH_CONTROL__GRPH_BANK_WIDTH__SHIFT);
1938a2e73f56SAlex Deucher 		fb_format |= (bankh << GRPH_CONTROL__GRPH_BANK_HEIGHT__SHIFT);
1939a2e73f56SAlex Deucher 		fb_format |= (mtaspect << GRPH_CONTROL__GRPH_MACRO_TILE_ASPECT__SHIFT);
1940a2e73f56SAlex Deucher 		fb_format |= (DISPLAY_MICRO_TILING << GRPH_CONTROL__GRPH_MICRO_TILE_MODE__SHIFT);
1941fbd76d59SMarek Olšák 	} else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
1942a2e73f56SAlex Deucher 		fb_format |= (GRPH_ARRAY_1D_TILED_THIN1 << GRPH_CONTROL__GRPH_ARRAY_MODE__SHIFT);
1943a2e73f56SAlex Deucher 	}
1944a2e73f56SAlex Deucher 
1945a2e73f56SAlex Deucher 	fb_format |= (pipe_config << GRPH_CONTROL__GRPH_PIPE_CONFIG__SHIFT);
1946a2e73f56SAlex Deucher 
1947a2e73f56SAlex Deucher 	dce_v8_0_vga_enable(crtc, false);
1948a2e73f56SAlex Deucher 
1949cb9e59d7SAlex Deucher 	/* Make sure surface address is updated at vertical blank rather than
1950cb9e59d7SAlex Deucher 	 * horizontal blank
1951cb9e59d7SAlex Deucher 	 */
1952cb9e59d7SAlex Deucher 	WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, 0);
1953cb9e59d7SAlex Deucher 
1954a2e73f56SAlex Deucher 	WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
1955a2e73f56SAlex Deucher 	       upper_32_bits(fb_location));
1956a2e73f56SAlex Deucher 	WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
1957a2e73f56SAlex Deucher 	       upper_32_bits(fb_location));
1958a2e73f56SAlex Deucher 	WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
1959a2e73f56SAlex Deucher 	       (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
1960a2e73f56SAlex Deucher 	WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
1961a2e73f56SAlex Deucher 	       (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
1962a2e73f56SAlex Deucher 	WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
1963a2e73f56SAlex Deucher 	WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
1964a2e73f56SAlex Deucher 
1965a2e73f56SAlex Deucher 	/*
1966a2e73f56SAlex Deucher 	 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
1967a2e73f56SAlex Deucher 	 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
1968a2e73f56SAlex Deucher 	 * retain the full precision throughout the pipeline.
1969a2e73f56SAlex Deucher 	 */
1970a2e73f56SAlex Deucher 	WREG32_P(mmGRPH_LUT_10BIT_BYPASS_CONTROL + amdgpu_crtc->crtc_offset,
1971a2e73f56SAlex Deucher 		 (bypass_lut ? LUT_10BIT_BYPASS_EN : 0),
1972a2e73f56SAlex Deucher 		 ~LUT_10BIT_BYPASS_EN);
1973a2e73f56SAlex Deucher 
1974a2e73f56SAlex Deucher 	if (bypass_lut)
1975a2e73f56SAlex Deucher 		DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1976a2e73f56SAlex Deucher 
1977a2e73f56SAlex Deucher 	WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
1978a2e73f56SAlex Deucher 	WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
1979a2e73f56SAlex Deucher 	WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
1980a2e73f56SAlex Deucher 	WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
1981a2e73f56SAlex Deucher 	WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
1982a2e73f56SAlex Deucher 	WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
1983a2e73f56SAlex Deucher 
1984272725c7SVille Syrjälä 	fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
1985a2e73f56SAlex Deucher 	WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
1986a2e73f56SAlex Deucher 
1987a2e73f56SAlex Deucher 	dce_v8_0_grph_enable(crtc, true);
1988a2e73f56SAlex Deucher 
1989a2e73f56SAlex Deucher 	WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
1990a2e73f56SAlex Deucher 	       target_fb->height);
1991a2e73f56SAlex Deucher 
1992a2e73f56SAlex Deucher 	x &= ~3;
1993a2e73f56SAlex Deucher 	y &= ~1;
1994a2e73f56SAlex Deucher 	WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
1995a2e73f56SAlex Deucher 	       (x << 16) | y);
1996a2e73f56SAlex Deucher 	viewport_w = crtc->mode.hdisplay;
1997a2e73f56SAlex Deucher 	viewport_h = (crtc->mode.vdisplay + 1) & ~1;
1998a2e73f56SAlex Deucher 	WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
1999a2e73f56SAlex Deucher 	       (viewport_w << 16) | viewport_h);
2000a2e73f56SAlex Deucher 
20013fd4b751SMichel Dänzer 	/* set pageflip to happen anywhere in vblank interval */
20023fd4b751SMichel Dänzer 	WREG32(mmMASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
2003a2e73f56SAlex Deucher 
2004a2e73f56SAlex Deucher 	if (!atomic && fb && fb != crtc->primary->fb) {
2005e68d14ddSDaniel Stone 		abo = gem_to_amdgpu_bo(fb->obj[0]);
2006c81a1a74SMichel Dänzer 		r = amdgpu_bo_reserve(abo, true);
2007a2e73f56SAlex Deucher 		if (unlikely(r != 0))
2008a2e73f56SAlex Deucher 			return r;
2009765e7fbfSChristian König 		amdgpu_bo_unpin(abo);
2010765e7fbfSChristian König 		amdgpu_bo_unreserve(abo);
2011a2e73f56SAlex Deucher 	}
2012a2e73f56SAlex Deucher 
2013a2e73f56SAlex Deucher 	/* Bytes per pixel may have changed */
2014a2e73f56SAlex Deucher 	dce_v8_0_bandwidth_update(adev);
2015a2e73f56SAlex Deucher 
2016a2e73f56SAlex Deucher 	return 0;
2017a2e73f56SAlex Deucher }
2018a2e73f56SAlex Deucher 
dce_v8_0_set_interleave(struct drm_crtc * crtc,struct drm_display_mode * mode)2019a2e73f56SAlex Deucher static void dce_v8_0_set_interleave(struct drm_crtc *crtc,
2020a2e73f56SAlex Deucher 				    struct drm_display_mode *mode)
2021a2e73f56SAlex Deucher {
2022a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
20231348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
2024a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2025a2e73f56SAlex Deucher 
2026a2e73f56SAlex Deucher 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2027a2e73f56SAlex Deucher 		WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset,
2028a2e73f56SAlex Deucher 		       LB_DATA_FORMAT__INTERLEAVE_EN__SHIFT);
2029a2e73f56SAlex Deucher 	else
2030a2e73f56SAlex Deucher 		WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, 0);
2031a2e73f56SAlex Deucher }
2032a2e73f56SAlex Deucher 
dce_v8_0_crtc_load_lut(struct drm_crtc * crtc)2033a2e73f56SAlex Deucher static void dce_v8_0_crtc_load_lut(struct drm_crtc *crtc)
2034a2e73f56SAlex Deucher {
2035a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2036a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
20371348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
203876dd3cd8SPeter Rosin 	u16 *r, *g, *b;
2039a2e73f56SAlex Deucher 	int i;
2040a2e73f56SAlex Deucher 
2041a2e73f56SAlex Deucher 	DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2042a2e73f56SAlex Deucher 
2043a2e73f56SAlex Deucher 	WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset,
2044a2e73f56SAlex Deucher 	       ((INPUT_CSC_BYPASS << INPUT_CSC_CONTROL__INPUT_CSC_GRPH_MODE__SHIFT) |
2045a2e73f56SAlex Deucher 		(INPUT_CSC_BYPASS << INPUT_CSC_CONTROL__INPUT_CSC_OVL_MODE__SHIFT)));
2046a2e73f56SAlex Deucher 	WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset,
2047a2e73f56SAlex Deucher 	       PRESCALE_GRPH_CONTROL__GRPH_PRESCALE_BYPASS_MASK);
2048a2e73f56SAlex Deucher 	WREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset,
2049a2e73f56SAlex Deucher 	       PRESCALE_OVL_CONTROL__OVL_PRESCALE_BYPASS_MASK);
2050a2e73f56SAlex Deucher 	WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset,
2051a2e73f56SAlex Deucher 	       ((INPUT_GAMMA_USE_LUT << INPUT_GAMMA_CONTROL__GRPH_INPUT_GAMMA_MODE__SHIFT) |
2052a2e73f56SAlex Deucher 		(INPUT_GAMMA_USE_LUT << INPUT_GAMMA_CONTROL__OVL_INPUT_GAMMA_MODE__SHIFT)));
2053a2e73f56SAlex Deucher 
2054a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2055a2e73f56SAlex Deucher 
2056a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2057a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2058a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2059a2e73f56SAlex Deucher 
2060a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2061a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2062a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2063a2e73f56SAlex Deucher 
2064a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2065a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2066a2e73f56SAlex Deucher 
2067a2e73f56SAlex Deucher 	WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
206876dd3cd8SPeter Rosin 	r = crtc->gamma_store;
206976dd3cd8SPeter Rosin 	g = r + crtc->gamma_size;
207076dd3cd8SPeter Rosin 	b = g + crtc->gamma_size;
2071a2e73f56SAlex Deucher 	for (i = 0; i < 256; i++) {
2072a2e73f56SAlex Deucher 		WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
207376dd3cd8SPeter Rosin 		       ((*r++ & 0xffc0) << 14) |
207476dd3cd8SPeter Rosin 		       ((*g++ & 0xffc0) << 4) |
207576dd3cd8SPeter Rosin 		       (*b++ >> 6));
2076a2e73f56SAlex Deucher 	}
2077a2e73f56SAlex Deucher 
2078a2e73f56SAlex Deucher 	WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset,
2079a2e73f56SAlex Deucher 	       ((DEGAMMA_BYPASS << DEGAMMA_CONTROL__GRPH_DEGAMMA_MODE__SHIFT) |
2080a2e73f56SAlex Deucher 		(DEGAMMA_BYPASS << DEGAMMA_CONTROL__OVL_DEGAMMA_MODE__SHIFT) |
2081a2e73f56SAlex Deucher 		(DEGAMMA_BYPASS << DEGAMMA_CONTROL__CURSOR_DEGAMMA_MODE__SHIFT)));
2082a2e73f56SAlex Deucher 	WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset,
2083a2e73f56SAlex Deucher 	       ((GAMUT_REMAP_BYPASS << GAMUT_REMAP_CONTROL__GRPH_GAMUT_REMAP_MODE__SHIFT) |
2084a2e73f56SAlex Deucher 		(GAMUT_REMAP_BYPASS << GAMUT_REMAP_CONTROL__OVL_GAMUT_REMAP_MODE__SHIFT)));
2085a2e73f56SAlex Deucher 	WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset,
2086a2e73f56SAlex Deucher 	       ((REGAMMA_BYPASS << REGAMMA_CONTROL__GRPH_REGAMMA_MODE__SHIFT) |
2087a2e73f56SAlex Deucher 		(REGAMMA_BYPASS << REGAMMA_CONTROL__OVL_REGAMMA_MODE__SHIFT)));
2088a2e73f56SAlex Deucher 	WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset,
2089a2e73f56SAlex Deucher 	       ((OUTPUT_CSC_BYPASS << OUTPUT_CSC_CONTROL__OUTPUT_CSC_GRPH_MODE__SHIFT) |
2090a2e73f56SAlex Deucher 		(OUTPUT_CSC_BYPASS << OUTPUT_CSC_CONTROL__OUTPUT_CSC_OVL_MODE__SHIFT)));
2091a2e73f56SAlex Deucher 	/* XXX match this to the depth of the crtc fmt block, move to modeset? */
2092a2e73f56SAlex Deucher 	WREG32(0x1a50 + amdgpu_crtc->crtc_offset, 0);
2093a2e73f56SAlex Deucher 	/* XXX this only needs to be programmed once per crtc at startup,
2094a2e73f56SAlex Deucher 	 * not sure where the best place for it is
2095a2e73f56SAlex Deucher 	 */
2096a2e73f56SAlex Deucher 	WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset,
2097a2e73f56SAlex Deucher 	       ALPHA_CONTROL__CURSOR_ALPHA_BLND_ENA_MASK);
2098a2e73f56SAlex Deucher }
2099a2e73f56SAlex Deucher 
dce_v8_0_pick_dig_encoder(struct drm_encoder * encoder)2100a2e73f56SAlex Deucher static int dce_v8_0_pick_dig_encoder(struct drm_encoder *encoder)
2101a2e73f56SAlex Deucher {
2102a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2103a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2104a2e73f56SAlex Deucher 
2105a2e73f56SAlex Deucher 	switch (amdgpu_encoder->encoder_id) {
2106a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2107a2e73f56SAlex Deucher 		if (dig->linkb)
2108a2e73f56SAlex Deucher 			return 1;
2109a2e73f56SAlex Deucher 		else
2110a2e73f56SAlex Deucher 			return 0;
2111a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2112a2e73f56SAlex Deucher 		if (dig->linkb)
2113a2e73f56SAlex Deucher 			return 3;
2114a2e73f56SAlex Deucher 		else
2115a2e73f56SAlex Deucher 			return 2;
2116a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2117a2e73f56SAlex Deucher 		if (dig->linkb)
2118a2e73f56SAlex Deucher 			return 5;
2119a2e73f56SAlex Deucher 		else
2120a2e73f56SAlex Deucher 			return 4;
2121a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2122a2e73f56SAlex Deucher 		return 6;
2123a2e73f56SAlex Deucher 	default:
2124a2e73f56SAlex Deucher 		DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2125a2e73f56SAlex Deucher 		return 0;
2126a2e73f56SAlex Deucher 	}
2127a2e73f56SAlex Deucher }
2128a2e73f56SAlex Deucher 
2129a2e73f56SAlex Deucher /**
2130a2e73f56SAlex Deucher  * dce_v8_0_pick_pll - Allocate a PPLL for use by the crtc.
2131a2e73f56SAlex Deucher  *
2132a2e73f56SAlex Deucher  * @crtc: drm crtc
2133a2e73f56SAlex Deucher  *
2134a2e73f56SAlex Deucher  * Returns the PPLL (Pixel PLL) to be used by the crtc.  For DP monitors
2135a2e73f56SAlex Deucher  * a single PPLL can be used for all DP crtcs/encoders.  For non-DP
2136a2e73f56SAlex Deucher  * monitors a dedicated PPLL must be used.  If a particular board has
2137a2e73f56SAlex Deucher  * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2138a2e73f56SAlex Deucher  * as there is no need to program the PLL itself.  If we are not able to
2139a2e73f56SAlex Deucher  * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2140a2e73f56SAlex Deucher  * avoid messing up an existing monitor.
2141a2e73f56SAlex Deucher  *
2142a2e73f56SAlex Deucher  * Asic specific PLL information
2143a2e73f56SAlex Deucher  *
2144a2e73f56SAlex Deucher  * DCE 8.x
2145a2e73f56SAlex Deucher  * KB/KV
2146a2e73f56SAlex Deucher  * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2147a2e73f56SAlex Deucher  * CI
2148a2e73f56SAlex Deucher  * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2149a2e73f56SAlex Deucher  *
2150a2e73f56SAlex Deucher  */
dce_v8_0_pick_pll(struct drm_crtc * crtc)2151a2e73f56SAlex Deucher static u32 dce_v8_0_pick_pll(struct drm_crtc *crtc)
2152a2e73f56SAlex Deucher {
2153a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2154a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
21551348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
2156a2e73f56SAlex Deucher 	u32 pll_in_use;
2157a2e73f56SAlex Deucher 	int pll;
2158a2e73f56SAlex Deucher 
2159a2e73f56SAlex Deucher 	if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2160a2e73f56SAlex Deucher 		if (adev->clock.dp_extclk)
2161a2e73f56SAlex Deucher 			/* skip PPLL programming if using ext clock */
2162a2e73f56SAlex Deucher 			return ATOM_PPLL_INVALID;
2163a2e73f56SAlex Deucher 		else {
2164a2e73f56SAlex Deucher 			/* use the same PPLL for all DP monitors */
2165a2e73f56SAlex Deucher 			pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2166a2e73f56SAlex Deucher 			if (pll != ATOM_PPLL_INVALID)
2167a2e73f56SAlex Deucher 				return pll;
2168a2e73f56SAlex Deucher 		}
2169a2e73f56SAlex Deucher 	} else {
2170a2e73f56SAlex Deucher 		/* use the same PPLL for all monitors with the same clock */
2171a2e73f56SAlex Deucher 		pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2172a2e73f56SAlex Deucher 		if (pll != ATOM_PPLL_INVALID)
2173a2e73f56SAlex Deucher 			return pll;
2174a2e73f56SAlex Deucher 	}
2175a2e73f56SAlex Deucher 	/* otherwise, pick one of the plls */
2176a2e73f56SAlex Deucher 	if ((adev->asic_type == CHIP_KABINI) ||
2177a2e73f56SAlex Deucher 	    (adev->asic_type == CHIP_MULLINS)) {
2178a2e73f56SAlex Deucher 		/* KB/ML has PPLL1 and PPLL2 */
2179a2e73f56SAlex Deucher 		pll_in_use = amdgpu_pll_get_use_mask(crtc);
2180a2e73f56SAlex Deucher 		if (!(pll_in_use & (1 << ATOM_PPLL2)))
2181a2e73f56SAlex Deucher 			return ATOM_PPLL2;
2182a2e73f56SAlex Deucher 		if (!(pll_in_use & (1 << ATOM_PPLL1)))
2183a2e73f56SAlex Deucher 			return ATOM_PPLL1;
2184a2e73f56SAlex Deucher 		DRM_ERROR("unable to allocate a PPLL\n");
2185a2e73f56SAlex Deucher 		return ATOM_PPLL_INVALID;
2186a2e73f56SAlex Deucher 	} else {
2187a2e73f56SAlex Deucher 		/* CI/KV has PPLL0, PPLL1, and PPLL2 */
2188a2e73f56SAlex Deucher 		pll_in_use = amdgpu_pll_get_use_mask(crtc);
2189a2e73f56SAlex Deucher 		if (!(pll_in_use & (1 << ATOM_PPLL2)))
2190a2e73f56SAlex Deucher 			return ATOM_PPLL2;
2191a2e73f56SAlex Deucher 		if (!(pll_in_use & (1 << ATOM_PPLL1)))
2192a2e73f56SAlex Deucher 			return ATOM_PPLL1;
2193a2e73f56SAlex Deucher 		if (!(pll_in_use & (1 << ATOM_PPLL0)))
2194a2e73f56SAlex Deucher 			return ATOM_PPLL0;
2195a2e73f56SAlex Deucher 		DRM_ERROR("unable to allocate a PPLL\n");
2196a2e73f56SAlex Deucher 		return ATOM_PPLL_INVALID;
2197a2e73f56SAlex Deucher 	}
2198a2e73f56SAlex Deucher 	return ATOM_PPLL_INVALID;
2199a2e73f56SAlex Deucher }
2200a2e73f56SAlex Deucher 
dce_v8_0_lock_cursor(struct drm_crtc * crtc,bool lock)2201a2e73f56SAlex Deucher static void dce_v8_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2202a2e73f56SAlex Deucher {
22031348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2204a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2205a2e73f56SAlex Deucher 	uint32_t cur_lock;
2206a2e73f56SAlex Deucher 
2207a2e73f56SAlex Deucher 	cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2208a2e73f56SAlex Deucher 	if (lock)
2209a2e73f56SAlex Deucher 		cur_lock |= CUR_UPDATE__CURSOR_UPDATE_LOCK_MASK;
2210a2e73f56SAlex Deucher 	else
2211a2e73f56SAlex Deucher 		cur_lock &= ~CUR_UPDATE__CURSOR_UPDATE_LOCK_MASK;
2212a2e73f56SAlex Deucher 	WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2213a2e73f56SAlex Deucher }
2214a2e73f56SAlex Deucher 
dce_v8_0_hide_cursor(struct drm_crtc * crtc)2215a2e73f56SAlex Deucher static void dce_v8_0_hide_cursor(struct drm_crtc *crtc)
2216a2e73f56SAlex Deucher {
2217a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
22181348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2219a2e73f56SAlex Deucher 
222046e840edSHawking Zhang 	WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset,
2221a2e73f56SAlex Deucher 	       (CURSOR_24_8_PRE_MULT << CUR_CONTROL__CURSOR_MODE__SHIFT) |
2222a2e73f56SAlex Deucher 	       (CURSOR_URGENT_1_2 << CUR_CONTROL__CURSOR_URGENT_CONTROL__SHIFT));
2223a2e73f56SAlex Deucher }
2224a2e73f56SAlex Deucher 
dce_v8_0_show_cursor(struct drm_crtc * crtc)2225a2e73f56SAlex Deucher static void dce_v8_0_show_cursor(struct drm_crtc *crtc)
2226a2e73f56SAlex Deucher {
2227a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
22281348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2229a2e73f56SAlex Deucher 
2230a2df42daSAlex Deucher 	WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2231a2df42daSAlex Deucher 	       upper_32_bits(amdgpu_crtc->cursor_addr));
2232a2df42daSAlex Deucher 	WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2233a2df42daSAlex Deucher 	       lower_32_bits(amdgpu_crtc->cursor_addr));
2234a2df42daSAlex Deucher 
223546e840edSHawking Zhang 	WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset,
2236a2e73f56SAlex Deucher 	       CUR_CONTROL__CURSOR_EN_MASK |
2237a2e73f56SAlex Deucher 	       (CURSOR_24_8_PRE_MULT << CUR_CONTROL__CURSOR_MODE__SHIFT) |
2238a2e73f56SAlex Deucher 	       (CURSOR_URGENT_1_2 << CUR_CONTROL__CURSOR_URGENT_CONTROL__SHIFT));
2239a2e73f56SAlex Deucher }
2240a2e73f56SAlex Deucher 
dce_v8_0_cursor_move_locked(struct drm_crtc * crtc,int x,int y)224177ed35b8SAlex Deucher static int dce_v8_0_cursor_move_locked(struct drm_crtc *crtc,
2242a2e73f56SAlex Deucher 				       int x, int y)
2243a2e73f56SAlex Deucher {
2244a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
22451348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2246a2e73f56SAlex Deucher 	int xorigin = 0, yorigin = 0;
2247a2e73f56SAlex Deucher 
22488e57ec61SMichel Dänzer 	amdgpu_crtc->cursor_x = x;
22498e57ec61SMichel Dänzer 	amdgpu_crtc->cursor_y = y;
22508e57ec61SMichel Dänzer 
2251a2e73f56SAlex Deucher 	/* avivo cursor are offset into the total surface */
2252a2e73f56SAlex Deucher 	x += crtc->x;
2253a2e73f56SAlex Deucher 	y += crtc->y;
2254a2e73f56SAlex Deucher 	DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2255a2e73f56SAlex Deucher 
2256a2e73f56SAlex Deucher 	if (x < 0) {
2257a2e73f56SAlex Deucher 		xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2258a2e73f56SAlex Deucher 		x = 0;
2259a2e73f56SAlex Deucher 	}
2260a2e73f56SAlex Deucher 	if (y < 0) {
2261a2e73f56SAlex Deucher 		yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2262a2e73f56SAlex Deucher 		y = 0;
2263a2e73f56SAlex Deucher 	}
2264a2e73f56SAlex Deucher 
2265a2e73f56SAlex Deucher 	WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2266a2e73f56SAlex Deucher 	WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
226769bcc0b7SMichel Dänzer 	WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
226869bcc0b7SMichel Dänzer 	       ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
226977ed35b8SAlex Deucher 
2270a2e73f56SAlex Deucher 	return 0;
2271a2e73f56SAlex Deucher }
2272a2e73f56SAlex Deucher 
dce_v8_0_crtc_cursor_move(struct drm_crtc * crtc,int x,int y)227377ed35b8SAlex Deucher static int dce_v8_0_crtc_cursor_move(struct drm_crtc *crtc,
227477ed35b8SAlex Deucher 				     int x, int y)
227577ed35b8SAlex Deucher {
227677ed35b8SAlex Deucher 	int ret;
227777ed35b8SAlex Deucher 
227877ed35b8SAlex Deucher 	dce_v8_0_lock_cursor(crtc, true);
227977ed35b8SAlex Deucher 	ret = dce_v8_0_cursor_move_locked(crtc, x, y);
228077ed35b8SAlex Deucher 	dce_v8_0_lock_cursor(crtc, false);
228177ed35b8SAlex Deucher 
228277ed35b8SAlex Deucher 	return ret;
228377ed35b8SAlex Deucher }
228477ed35b8SAlex Deucher 
dce_v8_0_crtc_cursor_set2(struct drm_crtc * crtc,struct drm_file * file_priv,uint32_t handle,uint32_t width,uint32_t height,int32_t hot_x,int32_t hot_y)228577ed35b8SAlex Deucher static int dce_v8_0_crtc_cursor_set2(struct drm_crtc *crtc,
2286a2e73f56SAlex Deucher 				     struct drm_file *file_priv,
2287a2e73f56SAlex Deucher 				     uint32_t handle,
2288a2e73f56SAlex Deucher 				     uint32_t width,
228977ed35b8SAlex Deucher 				     uint32_t height,
229077ed35b8SAlex Deucher 				     int32_t hot_x,
229177ed35b8SAlex Deucher 				     int32_t hot_y)
2292a2e73f56SAlex Deucher {
2293a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2294a2e73f56SAlex Deucher 	struct drm_gem_object *obj;
229572b40067SAlex Deucher 	struct amdgpu_bo *aobj;
2296a2e73f56SAlex Deucher 	int ret;
2297a2e73f56SAlex Deucher 
2298a2e73f56SAlex Deucher 	if (!handle) {
2299a2e73f56SAlex Deucher 		/* turn off cursor */
2300a2e73f56SAlex Deucher 		dce_v8_0_hide_cursor(crtc);
2301a2e73f56SAlex Deucher 		obj = NULL;
2302a2e73f56SAlex Deucher 		goto unpin;
2303a2e73f56SAlex Deucher 	}
2304a2e73f56SAlex Deucher 
2305a2e73f56SAlex Deucher 	if ((width > amdgpu_crtc->max_cursor_width) ||
2306a2e73f56SAlex Deucher 	    (height > amdgpu_crtc->max_cursor_height)) {
2307a2e73f56SAlex Deucher 		DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2308a2e73f56SAlex Deucher 		return -EINVAL;
2309a2e73f56SAlex Deucher 	}
2310a2e73f56SAlex Deucher 
2311a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, handle);
2312a2e73f56SAlex Deucher 	if (!obj) {
2313a2e73f56SAlex Deucher 		DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2314a2e73f56SAlex Deucher 		return -ENOENT;
2315a2e73f56SAlex Deucher 	}
2316a2e73f56SAlex Deucher 
231772b40067SAlex Deucher 	aobj = gem_to_amdgpu_bo(obj);
231872b40067SAlex Deucher 	ret = amdgpu_bo_reserve(aobj, false);
231972b40067SAlex Deucher 	if (ret != 0) {
2320e07ddb0cSEmil Velikov 		drm_gem_object_put(obj);
232172b40067SAlex Deucher 		return ret;
232272b40067SAlex Deucher 	}
232372b40067SAlex Deucher 
2324*54b86443SChristian König 	aobj->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
23257b7c6c81SJunwei Zhang 	ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
232672b40067SAlex Deucher 	amdgpu_bo_unreserve(aobj);
232772b40067SAlex Deucher 	if (ret) {
232872b40067SAlex Deucher 		DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2329e07ddb0cSEmil Velikov 		drm_gem_object_put(obj);
233072b40067SAlex Deucher 		return ret;
233172b40067SAlex Deucher 	}
23327b7c6c81SJunwei Zhang 	amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2333a2e73f56SAlex Deucher 
2334a2e73f56SAlex Deucher 	dce_v8_0_lock_cursor(crtc, true);
2335c4e0dfadSAlex Deucher 
233669bcc0b7SMichel Dänzer 	if (width != amdgpu_crtc->cursor_width ||
233769bcc0b7SMichel Dänzer 	    height != amdgpu_crtc->cursor_height ||
233869bcc0b7SMichel Dänzer 	    hot_x != amdgpu_crtc->cursor_hot_x ||
2339c4e0dfadSAlex Deucher 	    hot_y != amdgpu_crtc->cursor_hot_y) {
2340c4e0dfadSAlex Deucher 		int x, y;
2341c4e0dfadSAlex Deucher 
2342c4e0dfadSAlex Deucher 		x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
2343c4e0dfadSAlex Deucher 		y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
2344c4e0dfadSAlex Deucher 
2345c4e0dfadSAlex Deucher 		dce_v8_0_cursor_move_locked(crtc, x, y);
2346c4e0dfadSAlex Deucher 
23477c83d7abSMichel Dänzer 		amdgpu_crtc->cursor_width = width;
23487c83d7abSMichel Dänzer 		amdgpu_crtc->cursor_height = height;
234969bcc0b7SMichel Dänzer 		amdgpu_crtc->cursor_hot_x = hot_x;
235069bcc0b7SMichel Dänzer 		amdgpu_crtc->cursor_hot_y = hot_y;
23517c83d7abSMichel Dänzer 	}
23527c83d7abSMichel Dänzer 
2353a2e73f56SAlex Deucher 	dce_v8_0_show_cursor(crtc);
2354a2e73f56SAlex Deucher 	dce_v8_0_lock_cursor(crtc, false);
2355a2e73f56SAlex Deucher 
2356a2e73f56SAlex Deucher unpin:
2357a2e73f56SAlex Deucher 	if (amdgpu_crtc->cursor_bo) {
2358fd70cf63SAlex Deucher 		struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2359c81a1a74SMichel Dänzer 		ret = amdgpu_bo_reserve(aobj, true);
2360a2e73f56SAlex Deucher 		if (likely(ret == 0)) {
2361fd70cf63SAlex Deucher 			amdgpu_bo_unpin(aobj);
2362fd70cf63SAlex Deucher 			amdgpu_bo_unreserve(aobj);
2363a2e73f56SAlex Deucher 		}
2364e07ddb0cSEmil Velikov 		drm_gem_object_put(amdgpu_crtc->cursor_bo);
2365a2e73f56SAlex Deucher 	}
2366a2e73f56SAlex Deucher 
2367a2e73f56SAlex Deucher 	amdgpu_crtc->cursor_bo = obj;
2368a2e73f56SAlex Deucher 	return 0;
2369fd70cf63SAlex Deucher }
2370a2e73f56SAlex Deucher 
dce_v8_0_cursor_reset(struct drm_crtc * crtc)2371fd70cf63SAlex Deucher static void dce_v8_0_cursor_reset(struct drm_crtc *crtc)
2372fd70cf63SAlex Deucher {
2373fd70cf63SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2374fd70cf63SAlex Deucher 
2375fd70cf63SAlex Deucher 	if (amdgpu_crtc->cursor_bo) {
2376fd70cf63SAlex Deucher 		dce_v8_0_lock_cursor(crtc, true);
2377fd70cf63SAlex Deucher 
2378fd70cf63SAlex Deucher 		dce_v8_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
2379fd70cf63SAlex Deucher 					    amdgpu_crtc->cursor_y);
2380fd70cf63SAlex Deucher 
2381fd70cf63SAlex Deucher 		dce_v8_0_show_cursor(crtc);
2382fd70cf63SAlex Deucher 
2383fd70cf63SAlex Deucher 		dce_v8_0_lock_cursor(crtc, false);
2384fd70cf63SAlex Deucher 	}
2385a2e73f56SAlex Deucher }
2386a2e73f56SAlex Deucher 
dce_v8_0_crtc_gamma_set(struct drm_crtc * crtc,u16 * red,u16 * green,u16 * blue,uint32_t size,struct drm_modeset_acquire_ctx * ctx)23877ea77283SMaarten Lankhorst static int dce_v8_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
23886d124ff8SDaniel Vetter 				   u16 *blue, uint32_t size,
23896d124ff8SDaniel Vetter 				   struct drm_modeset_acquire_ctx *ctx)
2390a2e73f56SAlex Deucher {
2391a2e73f56SAlex Deucher 	dce_v8_0_crtc_load_lut(crtc);
23927ea77283SMaarten Lankhorst 
23937ea77283SMaarten Lankhorst 	return 0;
2394a2e73f56SAlex Deucher }
2395a2e73f56SAlex Deucher 
dce_v8_0_crtc_destroy(struct drm_crtc * crtc)2396a2e73f56SAlex Deucher static void dce_v8_0_crtc_destroy(struct drm_crtc *crtc)
2397a2e73f56SAlex Deucher {
2398a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2399a2e73f56SAlex Deucher 
2400a2e73f56SAlex Deucher 	drm_crtc_cleanup(crtc);
2401a2e73f56SAlex Deucher 	kfree(amdgpu_crtc);
2402a2e73f56SAlex Deucher }
2403a2e73f56SAlex Deucher 
2404a2e73f56SAlex Deucher static const struct drm_crtc_funcs dce_v8_0_crtc_funcs = {
240577ed35b8SAlex Deucher 	.cursor_set2 = dce_v8_0_crtc_cursor_set2,
2406a2e73f56SAlex Deucher 	.cursor_move = dce_v8_0_crtc_cursor_move,
2407a2e73f56SAlex Deucher 	.gamma_set = dce_v8_0_crtc_gamma_set,
2408775a8364SSamuel Li 	.set_config = amdgpu_display_crtc_set_config,
2409a2e73f56SAlex Deucher 	.destroy = dce_v8_0_crtc_destroy,
24100cd11932SSamuel Li 	.page_flip_target = amdgpu_display_crtc_page_flip_target,
2411e3eff4b5SThomas Zimmermann 	.get_vblank_counter = amdgpu_get_vblank_counter_kms,
2412e3eff4b5SThomas Zimmermann 	.enable_vblank = amdgpu_enable_vblank_kms,
2413e3eff4b5SThomas Zimmermann 	.disable_vblank = amdgpu_disable_vblank_kms,
2414e3eff4b5SThomas Zimmermann 	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
2415a2e73f56SAlex Deucher };
2416a2e73f56SAlex Deucher 
dce_v8_0_crtc_dpms(struct drm_crtc * crtc,int mode)2417a2e73f56SAlex Deucher static void dce_v8_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2418a2e73f56SAlex Deucher {
2419a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
24201348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
2421a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
24221002d718SMichel Dänzer 	unsigned type;
2423a2e73f56SAlex Deucher 
2424a2e73f56SAlex Deucher 	switch (mode) {
2425a2e73f56SAlex Deucher 	case DRM_MODE_DPMS_ON:
2426a2e73f56SAlex Deucher 		amdgpu_crtc->enabled = true;
2427a2e73f56SAlex Deucher 		amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2428a2e73f56SAlex Deucher 		dce_v8_0_vga_enable(crtc, true);
2429a2e73f56SAlex Deucher 		amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2430a2e73f56SAlex Deucher 		dce_v8_0_vga_enable(crtc, false);
2431f6c7aba4SMichel Dänzer 		/* Make sure VBLANK and PFLIP interrupts are still enabled */
2432734dd01dSSamuel Li 		type = amdgpu_display_crtc_idx_to_irq_type(adev,
2433734dd01dSSamuel Li 						amdgpu_crtc->crtc_id);
24341002d718SMichel Dänzer 		amdgpu_irq_update(adev, &adev->crtc_irq, type);
2435f6c7aba4SMichel Dänzer 		amdgpu_irq_update(adev, &adev->pageflip_irq, type);
24369a7841e9SGustavo Padovan 		drm_crtc_vblank_on(crtc);
2437a2e73f56SAlex Deucher 		dce_v8_0_crtc_load_lut(crtc);
2438a2e73f56SAlex Deucher 		break;
2439a2e73f56SAlex Deucher 	case DRM_MODE_DPMS_STANDBY:
2440a2e73f56SAlex Deucher 	case DRM_MODE_DPMS_SUSPEND:
2441a2e73f56SAlex Deucher 	case DRM_MODE_DPMS_OFF:
24429a7841e9SGustavo Padovan 		drm_crtc_vblank_off(crtc);
2443a2e73f56SAlex Deucher 		if (amdgpu_crtc->enabled) {
2444a2e73f56SAlex Deucher 			dce_v8_0_vga_enable(crtc, true);
2445a2e73f56SAlex Deucher 			amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2446a2e73f56SAlex Deucher 			dce_v8_0_vga_enable(crtc, false);
2447a2e73f56SAlex Deucher 		}
2448a2e73f56SAlex Deucher 		amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2449a2e73f56SAlex Deucher 		amdgpu_crtc->enabled = false;
2450a2e73f56SAlex Deucher 		break;
2451a2e73f56SAlex Deucher 	}
2452a2e73f56SAlex Deucher 	/* adjust pm to dpms */
245384176663SEvan Quan 	amdgpu_dpm_compute_clocks(adev);
2454a2e73f56SAlex Deucher }
2455a2e73f56SAlex Deucher 
dce_v8_0_crtc_prepare(struct drm_crtc * crtc)2456a2e73f56SAlex Deucher static void dce_v8_0_crtc_prepare(struct drm_crtc *crtc)
2457a2e73f56SAlex Deucher {
2458a2e73f56SAlex Deucher 	/* disable crtc pair power gating before programming */
2459a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2460a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2461a2e73f56SAlex Deucher 	dce_v8_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2462a2e73f56SAlex Deucher }
2463a2e73f56SAlex Deucher 
dce_v8_0_crtc_commit(struct drm_crtc * crtc)2464a2e73f56SAlex Deucher static void dce_v8_0_crtc_commit(struct drm_crtc *crtc)
2465a2e73f56SAlex Deucher {
2466a2e73f56SAlex Deucher 	dce_v8_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2467a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2468a2e73f56SAlex Deucher }
2469a2e73f56SAlex Deucher 
dce_v8_0_crtc_disable(struct drm_crtc * crtc)2470a2e73f56SAlex Deucher static void dce_v8_0_crtc_disable(struct drm_crtc *crtc)
2471a2e73f56SAlex Deucher {
2472a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2473a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
24741348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
2475a2e73f56SAlex Deucher 	struct amdgpu_atom_ss ss;
2476a2e73f56SAlex Deucher 	int i;
2477a2e73f56SAlex Deucher 
2478a2e73f56SAlex Deucher 	dce_v8_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2479a2e73f56SAlex Deucher 	if (crtc->primary->fb) {
2480a2e73f56SAlex Deucher 		int r;
2481765e7fbfSChristian König 		struct amdgpu_bo *abo;
2482a2e73f56SAlex Deucher 
2483e68d14ddSDaniel Stone 		abo = gem_to_amdgpu_bo(crtc->primary->fb->obj[0]);
2484c81a1a74SMichel Dänzer 		r = amdgpu_bo_reserve(abo, true);
2485a2e73f56SAlex Deucher 		if (unlikely(r))
2486765e7fbfSChristian König 			DRM_ERROR("failed to reserve abo before unpin\n");
2487a2e73f56SAlex Deucher 		else {
2488765e7fbfSChristian König 			amdgpu_bo_unpin(abo);
2489765e7fbfSChristian König 			amdgpu_bo_unreserve(abo);
2490a2e73f56SAlex Deucher 		}
2491a2e73f56SAlex Deucher 	}
2492a2e73f56SAlex Deucher 	/* disable the GRPH */
2493a2e73f56SAlex Deucher 	dce_v8_0_grph_enable(crtc, false);
2494a2e73f56SAlex Deucher 
2495a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2496a2e73f56SAlex Deucher 
2497a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
2498a2e73f56SAlex Deucher 		if (adev->mode_info.crtcs[i] &&
2499a2e73f56SAlex Deucher 		    adev->mode_info.crtcs[i]->enabled &&
2500a2e73f56SAlex Deucher 		    i != amdgpu_crtc->crtc_id &&
2501a2e73f56SAlex Deucher 		    amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2502a2e73f56SAlex Deucher 			/* one other crtc is using this pll don't turn
2503a2e73f56SAlex Deucher 			 * off the pll
2504a2e73f56SAlex Deucher 			 */
2505a2e73f56SAlex Deucher 			goto done;
2506a2e73f56SAlex Deucher 		}
2507a2e73f56SAlex Deucher 	}
2508a2e73f56SAlex Deucher 
2509a2e73f56SAlex Deucher 	switch (amdgpu_crtc->pll_id) {
2510a2e73f56SAlex Deucher 	case ATOM_PPLL1:
2511a2e73f56SAlex Deucher 	case ATOM_PPLL2:
2512a2e73f56SAlex Deucher 		/* disable the ppll */
2513a2e73f56SAlex Deucher 		amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2514a2e73f56SAlex Deucher 						 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2515a2e73f56SAlex Deucher 		break;
2516a2e73f56SAlex Deucher 	case ATOM_PPLL0:
2517a2e73f56SAlex Deucher 		/* disable the ppll */
2518a2e73f56SAlex Deucher 		if ((adev->asic_type == CHIP_KAVERI) ||
2519a2e73f56SAlex Deucher 		    (adev->asic_type == CHIP_BONAIRE) ||
2520a2e73f56SAlex Deucher 		    (adev->asic_type == CHIP_HAWAII))
2521a2e73f56SAlex Deucher 			amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2522a2e73f56SAlex Deucher 						  0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2523a2e73f56SAlex Deucher 		break;
2524a2e73f56SAlex Deucher 	default:
2525a2e73f56SAlex Deucher 		break;
2526a2e73f56SAlex Deucher 	}
2527a2e73f56SAlex Deucher done:
2528a2e73f56SAlex Deucher 	amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2529a2e73f56SAlex Deucher 	amdgpu_crtc->adjusted_clock = 0;
2530a2e73f56SAlex Deucher 	amdgpu_crtc->encoder = NULL;
2531a2e73f56SAlex Deucher 	amdgpu_crtc->connector = NULL;
2532a2e73f56SAlex Deucher }
2533a2e73f56SAlex Deucher 
dce_v8_0_crtc_mode_set(struct drm_crtc * crtc,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode,int x,int y,struct drm_framebuffer * old_fb)2534a2e73f56SAlex Deucher static int dce_v8_0_crtc_mode_set(struct drm_crtc *crtc,
2535a2e73f56SAlex Deucher 				  struct drm_display_mode *mode,
2536a2e73f56SAlex Deucher 				  struct drm_display_mode *adjusted_mode,
2537a2e73f56SAlex Deucher 				  int x, int y, struct drm_framebuffer *old_fb)
2538a2e73f56SAlex Deucher {
2539a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2540a2e73f56SAlex Deucher 
2541a2e73f56SAlex Deucher 	if (!amdgpu_crtc->adjusted_clock)
2542a2e73f56SAlex Deucher 		return -EINVAL;
2543a2e73f56SAlex Deucher 
2544a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
2545a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2546a2e73f56SAlex Deucher 	dce_v8_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2547a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2548a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_scaler_setup(crtc);
2549fd70cf63SAlex Deucher 	dce_v8_0_cursor_reset(crtc);
2550a2e73f56SAlex Deucher 	/* update the hw version fpr dpm */
2551a2e73f56SAlex Deucher 	amdgpu_crtc->hw_mode = *adjusted_mode;
2552a2e73f56SAlex Deucher 
2553a2e73f56SAlex Deucher 	return 0;
2554a2e73f56SAlex Deucher }
2555a2e73f56SAlex Deucher 
dce_v8_0_crtc_mode_fixup(struct drm_crtc * crtc,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)2556a2e73f56SAlex Deucher static bool dce_v8_0_crtc_mode_fixup(struct drm_crtc *crtc,
2557a2e73f56SAlex Deucher 				     const struct drm_display_mode *mode,
2558a2e73f56SAlex Deucher 				     struct drm_display_mode *adjusted_mode)
2559a2e73f56SAlex Deucher {
2560a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2561a2e73f56SAlex Deucher 	struct drm_device *dev = crtc->dev;
2562a2e73f56SAlex Deucher 	struct drm_encoder *encoder;
2563a2e73f56SAlex Deucher 
2564a2e73f56SAlex Deucher 	/* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2565a2e73f56SAlex Deucher 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2566a2e73f56SAlex Deucher 		if (encoder->crtc == crtc) {
2567a2e73f56SAlex Deucher 			amdgpu_crtc->encoder = encoder;
2568a2e73f56SAlex Deucher 			amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2569a2e73f56SAlex Deucher 			break;
2570a2e73f56SAlex Deucher 		}
2571a2e73f56SAlex Deucher 	}
2572a2e73f56SAlex Deucher 	if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2573a2e73f56SAlex Deucher 		amdgpu_crtc->encoder = NULL;
2574a2e73f56SAlex Deucher 		amdgpu_crtc->connector = NULL;
2575a2e73f56SAlex Deucher 		return false;
2576a2e73f56SAlex Deucher 	}
25770c16443aSSamuel Li 	if (!amdgpu_display_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2578a2e73f56SAlex Deucher 		return false;
2579a2e73f56SAlex Deucher 	if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2580a2e73f56SAlex Deucher 		return false;
2581a2e73f56SAlex Deucher 	/* pick pll */
2582a2e73f56SAlex Deucher 	amdgpu_crtc->pll_id = dce_v8_0_pick_pll(crtc);
2583a2e73f56SAlex Deucher 	/* if we can't get a PPLL for a non-DP encoder, fail */
2584a2e73f56SAlex Deucher 	if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2585a2e73f56SAlex Deucher 	    !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2586a2e73f56SAlex Deucher 		return false;
2587a2e73f56SAlex Deucher 
2588a2e73f56SAlex Deucher 	return true;
2589a2e73f56SAlex Deucher }
2590a2e73f56SAlex Deucher 
dce_v8_0_crtc_set_base(struct drm_crtc * crtc,int x,int y,struct drm_framebuffer * old_fb)2591a2e73f56SAlex Deucher static int dce_v8_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2592a2e73f56SAlex Deucher 				  struct drm_framebuffer *old_fb)
2593a2e73f56SAlex Deucher {
2594a2e73f56SAlex Deucher 	return dce_v8_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2595a2e73f56SAlex Deucher }
2596a2e73f56SAlex Deucher 
dce_v8_0_crtc_set_base_atomic(struct drm_crtc * crtc,struct drm_framebuffer * fb,int x,int y,enum mode_set_atomic state)2597a2e73f56SAlex Deucher static int dce_v8_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2598a2e73f56SAlex Deucher 					 struct drm_framebuffer *fb,
2599a2e73f56SAlex Deucher 					 int x, int y, enum mode_set_atomic state)
2600a2e73f56SAlex Deucher {
2601a2e73f56SAlex Deucher 	return dce_v8_0_crtc_do_set_base(crtc, fb, x, y, 1);
2602a2e73f56SAlex Deucher }
2603a2e73f56SAlex Deucher 
2604a2e73f56SAlex Deucher static const struct drm_crtc_helper_funcs dce_v8_0_crtc_helper_funcs = {
2605a2e73f56SAlex Deucher 	.dpms = dce_v8_0_crtc_dpms,
2606a2e73f56SAlex Deucher 	.mode_fixup = dce_v8_0_crtc_mode_fixup,
2607a2e73f56SAlex Deucher 	.mode_set = dce_v8_0_crtc_mode_set,
2608a2e73f56SAlex Deucher 	.mode_set_base = dce_v8_0_crtc_set_base,
2609a2e73f56SAlex Deucher 	.mode_set_base_atomic = dce_v8_0_crtc_set_base_atomic,
2610a2e73f56SAlex Deucher 	.prepare = dce_v8_0_crtc_prepare,
2611a2e73f56SAlex Deucher 	.commit = dce_v8_0_crtc_commit,
2612a2e73f56SAlex Deucher 	.disable = dce_v8_0_crtc_disable,
2613ea702333SThomas Zimmermann 	.get_scanout_position = amdgpu_crtc_get_scanout_position,
2614a2e73f56SAlex Deucher };
2615a2e73f56SAlex Deucher 
dce_v8_0_crtc_init(struct amdgpu_device * adev,int index)2616a2e73f56SAlex Deucher static int dce_v8_0_crtc_init(struct amdgpu_device *adev, int index)
2617a2e73f56SAlex Deucher {
2618a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc;
2619a2e73f56SAlex Deucher 
2620a2e73f56SAlex Deucher 	amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2621a2e73f56SAlex Deucher 			      (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2622a2e73f56SAlex Deucher 	if (amdgpu_crtc == NULL)
2623a2e73f56SAlex Deucher 		return -ENOMEM;
2624a2e73f56SAlex Deucher 
26254a580877SLuben Tuikov 	drm_crtc_init(adev_to_drm(adev), &amdgpu_crtc->base, &dce_v8_0_crtc_funcs);
2626a2e73f56SAlex Deucher 
2627a2e73f56SAlex Deucher 	drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2628a2e73f56SAlex Deucher 	amdgpu_crtc->crtc_id = index;
2629a2e73f56SAlex Deucher 	adev->mode_info.crtcs[index] = amdgpu_crtc;
2630a2e73f56SAlex Deucher 
2631a2e73f56SAlex Deucher 	amdgpu_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
2632a2e73f56SAlex Deucher 	amdgpu_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
26334a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
26344a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2635a2e73f56SAlex Deucher 
2636a2e73f56SAlex Deucher 	amdgpu_crtc->crtc_offset = crtc_offsets[amdgpu_crtc->crtc_id];
2637a2e73f56SAlex Deucher 
2638a2e73f56SAlex Deucher 	amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2639a2e73f56SAlex Deucher 	amdgpu_crtc->adjusted_clock = 0;
2640a2e73f56SAlex Deucher 	amdgpu_crtc->encoder = NULL;
2641a2e73f56SAlex Deucher 	amdgpu_crtc->connector = NULL;
2642a2e73f56SAlex Deucher 	drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v8_0_crtc_helper_funcs);
2643a2e73f56SAlex Deucher 
2644a2e73f56SAlex Deucher 	return 0;
2645a2e73f56SAlex Deucher }
2646a2e73f56SAlex Deucher 
dce_v8_0_early_init(void * handle)26475fc3aeebSyanyang1 static int dce_v8_0_early_init(void *handle)
2648a2e73f56SAlex Deucher {
26495fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
26505fc3aeebSyanyang1 
2651a2e73f56SAlex Deucher 	adev->audio_endpt_rreg = &dce_v8_0_audio_endpt_rreg;
2652a2e73f56SAlex Deucher 	adev->audio_endpt_wreg = &dce_v8_0_audio_endpt_wreg;
2653a2e73f56SAlex Deucher 
2654a2e73f56SAlex Deucher 	dce_v8_0_set_display_funcs(adev);
2655a2e73f56SAlex Deucher 
265683c9b025SEmily Deng 	adev->mode_info.num_crtc = dce_v8_0_get_num_crtc(adev);
265783c9b025SEmily Deng 
2658a2e73f56SAlex Deucher 	switch (adev->asic_type) {
2659a2e73f56SAlex Deucher 	case CHIP_BONAIRE:
2660a2e73f56SAlex Deucher 	case CHIP_HAWAII:
2661a2e73f56SAlex Deucher 		adev->mode_info.num_hpd = 6;
2662a2e73f56SAlex Deucher 		adev->mode_info.num_dig = 6;
2663a2e73f56SAlex Deucher 		break;
2664a2e73f56SAlex Deucher 	case CHIP_KAVERI:
2665a2e73f56SAlex Deucher 		adev->mode_info.num_hpd = 6;
2666a2e73f56SAlex Deucher 		adev->mode_info.num_dig = 7;
2667a2e73f56SAlex Deucher 		break;
2668a2e73f56SAlex Deucher 	case CHIP_KABINI:
2669a2e73f56SAlex Deucher 	case CHIP_MULLINS:
2670a2e73f56SAlex Deucher 		adev->mode_info.num_hpd = 6;
2671a2e73f56SAlex Deucher 		adev->mode_info.num_dig = 6; /* ? */
2672a2e73f56SAlex Deucher 		break;
2673a2e73f56SAlex Deucher 	default:
2674a2e73f56SAlex Deucher 		/* FIXME: not supported yet */
2675a2e73f56SAlex Deucher 		return -EINVAL;
2676a2e73f56SAlex Deucher 	}
2677a2e73f56SAlex Deucher 
2678d794b9f8SMichel Dänzer 	dce_v8_0_set_irq_funcs(adev);
2679d794b9f8SMichel Dänzer 
2680a2e73f56SAlex Deucher 	return 0;
2681a2e73f56SAlex Deucher }
2682a2e73f56SAlex Deucher 
dce_v8_0_sw_init(void * handle)26835fc3aeebSyanyang1 static int dce_v8_0_sw_init(void *handle)
2684a2e73f56SAlex Deucher {
2685a2e73f56SAlex Deucher 	int r, i;
26865fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2687a2e73f56SAlex Deucher 
2688a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
26891ffdeca6SChristian König 		r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
2690a2e73f56SAlex Deucher 		if (r)
2691a2e73f56SAlex Deucher 			return r;
2692a2e73f56SAlex Deucher 	}
2693a2e73f56SAlex Deucher 
2694a2e73f56SAlex Deucher 	for (i = 8; i < 20; i += 2) {
26951ffdeca6SChristian König 		r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i, &adev->pageflip_irq);
2696a2e73f56SAlex Deucher 		if (r)
2697a2e73f56SAlex Deucher 			return r;
2698a2e73f56SAlex Deucher 	}
2699a2e73f56SAlex Deucher 
2700a2e73f56SAlex Deucher 	/* HPD hotplug */
27011ffdeca6SChristian König 	r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 42, &adev->hpd_irq);
2702a2e73f56SAlex Deucher 	if (r)
2703a2e73f56SAlex Deucher 		return r;
2704a2e73f56SAlex Deucher 
27054a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.funcs = &amdgpu_mode_funcs;
2706a2e73f56SAlex Deucher 
27074a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.async_page_flip = true;
2708cb9e59d7SAlex Deucher 
27094a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.max_width = 16384;
27104a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.max_height = 16384;
2711a2e73f56SAlex Deucher 
27124a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.preferred_depth = 24;
2713a6250bdbSAlex Deucher 	if (adev->asic_type == CHIP_HAWAII)
2714fc25fd60SAlex Deucher 		/* disable prefer shadow for now due to hibernation issues */
2715fc25fd60SAlex Deucher 		adev_to_drm(adev)->mode_config.prefer_shadow = 0;
2716a6250bdbSAlex Deucher 	else
2717a6250bdbSAlex Deucher 		adev_to_drm(adev)->mode_config.prefer_shadow = 1;
2718a2e73f56SAlex Deucher 
27192af10429STomohito Esaki 	adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
27202af10429STomohito Esaki 
27213dc9b1ceSSamuel Li 	r = amdgpu_display_modeset_create_props(adev);
2722a2e73f56SAlex Deucher 	if (r)
2723a2e73f56SAlex Deucher 		return r;
2724a2e73f56SAlex Deucher 
27254a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.max_width = 16384;
27264a580877SLuben Tuikov 	adev_to_drm(adev)->mode_config.max_height = 16384;
2727a2e73f56SAlex Deucher 
2728a2e73f56SAlex Deucher 	/* allocate crtcs */
2729a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.num_crtc; i++) {
2730a2e73f56SAlex Deucher 		r = dce_v8_0_crtc_init(adev, i);
2731a2e73f56SAlex Deucher 		if (r)
2732a2e73f56SAlex Deucher 			return r;
2733a2e73f56SAlex Deucher 	}
2734a2e73f56SAlex Deucher 
2735a2e73f56SAlex Deucher 	if (amdgpu_atombios_get_connector_info_from_object_table(adev))
27364a580877SLuben Tuikov 		amdgpu_display_print_display_setup(adev_to_drm(adev));
2737a2e73f56SAlex Deucher 	else
2738a2e73f56SAlex Deucher 		return -EINVAL;
2739a2e73f56SAlex Deucher 
2740a2e73f56SAlex Deucher 	/* setup afmt */
2741ff923479STom St Denis 	r = dce_v8_0_afmt_init(adev);
2742ff923479STom St Denis 	if (r)
2743ff923479STom St Denis 		return r;
2744a2e73f56SAlex Deucher 
2745a2e73f56SAlex Deucher 	r = dce_v8_0_audio_init(adev);
2746a2e73f56SAlex Deucher 	if (r)
2747a2e73f56SAlex Deucher 		return r;
2748a2e73f56SAlex Deucher 
2749a347ca97SAlex Deucher 	/* Disable vblank IRQs aggressively for power-saving */
2750a347ca97SAlex Deucher 	/* XXX: can this be enabled for DC? */
2751a347ca97SAlex Deucher 	adev_to_drm(adev)->vblank_disable_immediate = true;
2752a347ca97SAlex Deucher 
2753a347ca97SAlex Deucher 	r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
2754a347ca97SAlex Deucher 	if (r)
2755a347ca97SAlex Deucher 		return r;
2756a347ca97SAlex Deucher 
2757a347ca97SAlex Deucher 	/* Pre-DCE11 */
275890f56611Sxurui 	INIT_DELAYED_WORK(&adev->hotplug_work,
2759a347ca97SAlex Deucher 		  amdgpu_display_hotplug_work_func);
2760a347ca97SAlex Deucher 
27614a580877SLuben Tuikov 	drm_kms_helper_poll_init(adev_to_drm(adev));
2762a2e73f56SAlex Deucher 
276374c1e842STom St Denis 	adev->mode_info.mode_config_initialized = true;
276474c1e842STom St Denis 	return 0;
2765a2e73f56SAlex Deucher }
2766a2e73f56SAlex Deucher 
dce_v8_0_sw_fini(void * handle)27675fc3aeebSyanyang1 static int dce_v8_0_sw_fini(void *handle)
2768a2e73f56SAlex Deucher {
27695fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
27705fc3aeebSyanyang1 
2771aeb81b62SThomas Weißschuh 	drm_edid_free(adev->mode_info.bios_hardcoded_edid);
2772a2e73f56SAlex Deucher 
27734a580877SLuben Tuikov 	drm_kms_helper_poll_fini(adev_to_drm(adev));
2774a2e73f56SAlex Deucher 
2775a2e73f56SAlex Deucher 	dce_v8_0_audio_fini(adev);
2776a2e73f56SAlex Deucher 
2777a2e73f56SAlex Deucher 	dce_v8_0_afmt_fini(adev);
2778a2e73f56SAlex Deucher 
27794a580877SLuben Tuikov 	drm_mode_config_cleanup(adev_to_drm(adev));
2780a2e73f56SAlex Deucher 	adev->mode_info.mode_config_initialized = false;
2781a2e73f56SAlex Deucher 
2782a2e73f56SAlex Deucher 	return 0;
2783a2e73f56SAlex Deucher }
2784a2e73f56SAlex Deucher 
dce_v8_0_hw_init(void * handle)27855fc3aeebSyanyang1 static int dce_v8_0_hw_init(void *handle)
2786a2e73f56SAlex Deucher {
2787a2e73f56SAlex Deucher 	int i;
27885fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2789a2e73f56SAlex Deucher 
279084b5d3d1SAlex Deucher 	/* disable vga render */
279184b5d3d1SAlex Deucher 	dce_v8_0_set_vga_render_state(adev, false);
2792a2e73f56SAlex Deucher 	/* init dig PHYs, disp eng pll */
2793a2e73f56SAlex Deucher 	amdgpu_atombios_encoder_init_dig(adev);
2794a2e73f56SAlex Deucher 	amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
2795a2e73f56SAlex Deucher 
2796a2e73f56SAlex Deucher 	/* initialize hpd */
2797a2e73f56SAlex Deucher 	dce_v8_0_hpd_init(adev);
2798a2e73f56SAlex Deucher 
2799a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
2800a2e73f56SAlex Deucher 		dce_v8_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
2801a2e73f56SAlex Deucher 	}
2802a2e73f56SAlex Deucher 
2803f6c7aba4SMichel Dänzer 	dce_v8_0_pageflip_interrupt_init(adev);
2804f6c7aba4SMichel Dänzer 
2805a2e73f56SAlex Deucher 	return 0;
2806a2e73f56SAlex Deucher }
2807a2e73f56SAlex Deucher 
dce_v8_0_hw_fini(void * handle)28085fc3aeebSyanyang1 static int dce_v8_0_hw_fini(void *handle)
2809a2e73f56SAlex Deucher {
2810a2e73f56SAlex Deucher 	int i;
28115fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2812a2e73f56SAlex Deucher 
2813a2e73f56SAlex Deucher 	dce_v8_0_hpd_fini(adev);
2814a2e73f56SAlex Deucher 
2815a2e73f56SAlex Deucher 	for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
2816a2e73f56SAlex Deucher 		dce_v8_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
2817a2e73f56SAlex Deucher 	}
2818a2e73f56SAlex Deucher 
2819f6c7aba4SMichel Dänzer 	dce_v8_0_pageflip_interrupt_fini(adev);
2820f6c7aba4SMichel Dänzer 
282190f56611Sxurui 	flush_delayed_work(&adev->hotplug_work);
2822a347ca97SAlex Deucher 
2823a2e73f56SAlex Deucher 	return 0;
2824a2e73f56SAlex Deucher }
2825a2e73f56SAlex Deucher 
dce_v8_0_suspend(void * handle)28265fc3aeebSyanyang1 static int dce_v8_0_suspend(void *handle)
2827a2e73f56SAlex Deucher {
2828a59b3c80SAlex Deucher 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2829a2e15b0eSAlex Deucher 	int r;
2830a2e15b0eSAlex Deucher 
2831a2e15b0eSAlex Deucher 	r = amdgpu_display_suspend_helper(adev);
2832a2e15b0eSAlex Deucher 	if (r)
2833a2e15b0eSAlex Deucher 		return r;
2834a59b3c80SAlex Deucher 
2835a59b3c80SAlex Deucher 	adev->mode_info.bl_level =
2836a59b3c80SAlex Deucher 		amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
2837a59b3c80SAlex Deucher 
2838f9fff064SAlex Deucher 	return dce_v8_0_hw_fini(handle);
2839a2e73f56SAlex Deucher }
2840a2e73f56SAlex Deucher 
dce_v8_0_resume(void * handle)28415fc3aeebSyanyang1 static int dce_v8_0_resume(void *handle)
2842a2e73f56SAlex Deucher {
28435fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2844f9fff064SAlex Deucher 	int ret;
2845f9fff064SAlex Deucher 
2846a59b3c80SAlex Deucher 	amdgpu_atombios_encoder_set_backlight_level_to_reg(adev,
2847a59b3c80SAlex Deucher 							   adev->mode_info.bl_level);
2848a59b3c80SAlex Deucher 
2849f9fff064SAlex Deucher 	ret = dce_v8_0_hw_init(handle);
2850a2e73f56SAlex Deucher 
2851a2e73f56SAlex Deucher 	/* turn on the BL */
2852a2e73f56SAlex Deucher 	if (adev->mode_info.bl_encoder) {
2853a2e73f56SAlex Deucher 		u8 bl_level = amdgpu_display_backlight_get_level(adev,
2854a2e73f56SAlex Deucher 								  adev->mode_info.bl_encoder);
2855a2e73f56SAlex Deucher 		amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
2856a2e73f56SAlex Deucher 						    bl_level);
2857a2e73f56SAlex Deucher 	}
2858a2e15b0eSAlex Deucher 	if (ret)
2859f9fff064SAlex Deucher 		return ret;
2860a2e15b0eSAlex Deucher 
2861a2e15b0eSAlex Deucher 	return amdgpu_display_resume_helper(adev);
2862a2e73f56SAlex Deucher }
2863a2e73f56SAlex Deucher 
dce_v8_0_is_idle(void * handle)28645fc3aeebSyanyang1 static bool dce_v8_0_is_idle(void *handle)
2865a2e73f56SAlex Deucher {
2866a2e73f56SAlex Deucher 	return true;
2867a2e73f56SAlex Deucher }
2868a2e73f56SAlex Deucher 
dce_v8_0_wait_for_idle(void * handle)28695fc3aeebSyanyang1 static int dce_v8_0_wait_for_idle(void *handle)
2870a2e73f56SAlex Deucher {
2871a2e73f56SAlex Deucher 	return 0;
2872a2e73f56SAlex Deucher }
2873a2e73f56SAlex Deucher 
dce_v8_0_soft_reset(void * handle)28745fc3aeebSyanyang1 static int dce_v8_0_soft_reset(void *handle)
2875a2e73f56SAlex Deucher {
2876a2e73f56SAlex Deucher 	u32 srbm_soft_reset = 0, tmp;
28775fc3aeebSyanyang1 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2878a2e73f56SAlex Deucher 
2879a2e73f56SAlex Deucher 	if (dce_v8_0_is_display_hung(adev))
2880a2e73f56SAlex Deucher 		srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
2881a2e73f56SAlex Deucher 
2882a2e73f56SAlex Deucher 	if (srbm_soft_reset) {
2883a2e73f56SAlex Deucher 		tmp = RREG32(mmSRBM_SOFT_RESET);
2884a2e73f56SAlex Deucher 		tmp |= srbm_soft_reset;
2885a2e73f56SAlex Deucher 		dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
2886a2e73f56SAlex Deucher 		WREG32(mmSRBM_SOFT_RESET, tmp);
2887a2e73f56SAlex Deucher 		tmp = RREG32(mmSRBM_SOFT_RESET);
2888a2e73f56SAlex Deucher 
2889a2e73f56SAlex Deucher 		udelay(50);
2890a2e73f56SAlex Deucher 
2891a2e73f56SAlex Deucher 		tmp &= ~srbm_soft_reset;
2892a2e73f56SAlex Deucher 		WREG32(mmSRBM_SOFT_RESET, tmp);
2893a2e73f56SAlex Deucher 		tmp = RREG32(mmSRBM_SOFT_RESET);
2894a2e73f56SAlex Deucher 
2895a2e73f56SAlex Deucher 		/* Wait a little for things to settle down */
2896a2e73f56SAlex Deucher 		udelay(50);
2897a2e73f56SAlex Deucher 	}
2898a2e73f56SAlex Deucher 	return 0;
2899a2e73f56SAlex Deucher }
2900a2e73f56SAlex Deucher 
dce_v8_0_set_crtc_vblank_interrupt_state(struct amdgpu_device * adev,int crtc,enum amdgpu_interrupt_state state)2901a2e73f56SAlex Deucher static void dce_v8_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
2902a2e73f56SAlex Deucher 						     int crtc,
2903a2e73f56SAlex Deucher 						     enum amdgpu_interrupt_state state)
2904a2e73f56SAlex Deucher {
2905a2e73f56SAlex Deucher 	u32 reg_block, lb_interrupt_mask;
2906a2e73f56SAlex Deucher 
2907a2e73f56SAlex Deucher 	if (crtc >= adev->mode_info.num_crtc) {
2908a2e73f56SAlex Deucher 		DRM_DEBUG("invalid crtc %d\n", crtc);
2909a2e73f56SAlex Deucher 		return;
2910a2e73f56SAlex Deucher 	}
2911a2e73f56SAlex Deucher 
2912a2e73f56SAlex Deucher 	switch (crtc) {
2913a2e73f56SAlex Deucher 	case 0:
2914a2e73f56SAlex Deucher 		reg_block = CRTC0_REGISTER_OFFSET;
2915a2e73f56SAlex Deucher 		break;
2916a2e73f56SAlex Deucher 	case 1:
2917a2e73f56SAlex Deucher 		reg_block = CRTC1_REGISTER_OFFSET;
2918a2e73f56SAlex Deucher 		break;
2919a2e73f56SAlex Deucher 	case 2:
2920a2e73f56SAlex Deucher 		reg_block = CRTC2_REGISTER_OFFSET;
2921a2e73f56SAlex Deucher 		break;
2922a2e73f56SAlex Deucher 	case 3:
2923a2e73f56SAlex Deucher 		reg_block = CRTC3_REGISTER_OFFSET;
2924a2e73f56SAlex Deucher 		break;
2925a2e73f56SAlex Deucher 	case 4:
2926a2e73f56SAlex Deucher 		reg_block = CRTC4_REGISTER_OFFSET;
2927a2e73f56SAlex Deucher 		break;
2928a2e73f56SAlex Deucher 	case 5:
2929a2e73f56SAlex Deucher 		reg_block = CRTC5_REGISTER_OFFSET;
2930a2e73f56SAlex Deucher 		break;
2931a2e73f56SAlex Deucher 	default:
2932a2e73f56SAlex Deucher 		DRM_DEBUG("invalid crtc %d\n", crtc);
2933a2e73f56SAlex Deucher 		return;
2934a2e73f56SAlex Deucher 	}
2935a2e73f56SAlex Deucher 
2936a2e73f56SAlex Deucher 	switch (state) {
2937a2e73f56SAlex Deucher 	case AMDGPU_IRQ_STATE_DISABLE:
2938a2e73f56SAlex Deucher 		lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + reg_block);
2939a2e73f56SAlex Deucher 		lb_interrupt_mask &= ~LB_INTERRUPT_MASK__VBLANK_INTERRUPT_MASK_MASK;
2940a2e73f56SAlex Deucher 		WREG32(mmLB_INTERRUPT_MASK + reg_block, lb_interrupt_mask);
2941a2e73f56SAlex Deucher 		break;
2942a2e73f56SAlex Deucher 	case AMDGPU_IRQ_STATE_ENABLE:
2943a2e73f56SAlex Deucher 		lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + reg_block);
2944a2e73f56SAlex Deucher 		lb_interrupt_mask |= LB_INTERRUPT_MASK__VBLANK_INTERRUPT_MASK_MASK;
2945a2e73f56SAlex Deucher 		WREG32(mmLB_INTERRUPT_MASK + reg_block, lb_interrupt_mask);
2946a2e73f56SAlex Deucher 		break;
2947a2e73f56SAlex Deucher 	default:
2948a2e73f56SAlex Deucher 		break;
2949a2e73f56SAlex Deucher 	}
2950a2e73f56SAlex Deucher }
2951a2e73f56SAlex Deucher 
dce_v8_0_set_crtc_vline_interrupt_state(struct amdgpu_device * adev,int crtc,enum amdgpu_interrupt_state state)2952a2e73f56SAlex Deucher static void dce_v8_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
2953a2e73f56SAlex Deucher 						    int crtc,
2954a2e73f56SAlex Deucher 						    enum amdgpu_interrupt_state state)
2955a2e73f56SAlex Deucher {
2956a2e73f56SAlex Deucher 	u32 reg_block, lb_interrupt_mask;
2957a2e73f56SAlex Deucher 
2958a2e73f56SAlex Deucher 	if (crtc >= adev->mode_info.num_crtc) {
2959a2e73f56SAlex Deucher 		DRM_DEBUG("invalid crtc %d\n", crtc);
2960a2e73f56SAlex Deucher 		return;
2961a2e73f56SAlex Deucher 	}
2962a2e73f56SAlex Deucher 
2963a2e73f56SAlex Deucher 	switch (crtc) {
2964a2e73f56SAlex Deucher 	case 0:
2965a2e73f56SAlex Deucher 		reg_block = CRTC0_REGISTER_OFFSET;
2966a2e73f56SAlex Deucher 		break;
2967a2e73f56SAlex Deucher 	case 1:
2968a2e73f56SAlex Deucher 		reg_block = CRTC1_REGISTER_OFFSET;
2969a2e73f56SAlex Deucher 		break;
2970a2e73f56SAlex Deucher 	case 2:
2971a2e73f56SAlex Deucher 		reg_block = CRTC2_REGISTER_OFFSET;
2972a2e73f56SAlex Deucher 		break;
2973a2e73f56SAlex Deucher 	case 3:
2974a2e73f56SAlex Deucher 		reg_block = CRTC3_REGISTER_OFFSET;
2975a2e73f56SAlex Deucher 		break;
2976a2e73f56SAlex Deucher 	case 4:
2977a2e73f56SAlex Deucher 		reg_block = CRTC4_REGISTER_OFFSET;
2978a2e73f56SAlex Deucher 		break;
2979a2e73f56SAlex Deucher 	case 5:
2980a2e73f56SAlex Deucher 		reg_block = CRTC5_REGISTER_OFFSET;
2981a2e73f56SAlex Deucher 		break;
2982a2e73f56SAlex Deucher 	default:
2983a2e73f56SAlex Deucher 		DRM_DEBUG("invalid crtc %d\n", crtc);
2984a2e73f56SAlex Deucher 		return;
2985a2e73f56SAlex Deucher 	}
2986a2e73f56SAlex Deucher 
2987a2e73f56SAlex Deucher 	switch (state) {
2988a2e73f56SAlex Deucher 	case AMDGPU_IRQ_STATE_DISABLE:
2989a2e73f56SAlex Deucher 		lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + reg_block);
2990a2e73f56SAlex Deucher 		lb_interrupt_mask &= ~LB_INTERRUPT_MASK__VLINE_INTERRUPT_MASK_MASK;
2991a2e73f56SAlex Deucher 		WREG32(mmLB_INTERRUPT_MASK + reg_block, lb_interrupt_mask);
2992a2e73f56SAlex Deucher 		break;
2993a2e73f56SAlex Deucher 	case AMDGPU_IRQ_STATE_ENABLE:
2994a2e73f56SAlex Deucher 		lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + reg_block);
2995a2e73f56SAlex Deucher 		lb_interrupt_mask |= LB_INTERRUPT_MASK__VLINE_INTERRUPT_MASK_MASK;
2996a2e73f56SAlex Deucher 		WREG32(mmLB_INTERRUPT_MASK + reg_block, lb_interrupt_mask);
2997a2e73f56SAlex Deucher 		break;
2998a2e73f56SAlex Deucher 	default:
2999a2e73f56SAlex Deucher 		break;
3000a2e73f56SAlex Deucher 	}
3001a2e73f56SAlex Deucher }
3002a2e73f56SAlex Deucher 
dce_v8_0_set_hpd_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)3003a2e73f56SAlex Deucher static int dce_v8_0_set_hpd_interrupt_state(struct amdgpu_device *adev,
3004a2e73f56SAlex Deucher 					    struct amdgpu_irq_src *src,
3005a2e73f56SAlex Deucher 					    unsigned type,
3006a2e73f56SAlex Deucher 					    enum amdgpu_interrupt_state state)
3007a2e73f56SAlex Deucher {
30082285b91cSAlex Deucher 	u32 dc_hpd_int_cntl;
3009a2e73f56SAlex Deucher 
30102285b91cSAlex Deucher 	if (type >= adev->mode_info.num_hpd) {
3011a2e73f56SAlex Deucher 		DRM_DEBUG("invalid hdp %d\n", type);
3012a2e73f56SAlex Deucher 		return 0;
3013a2e73f56SAlex Deucher 	}
3014a2e73f56SAlex Deucher 
3015a2e73f56SAlex Deucher 	switch (state) {
3016a2e73f56SAlex Deucher 	case AMDGPU_IRQ_STATE_DISABLE:
30172285b91cSAlex Deucher 		dc_hpd_int_cntl = RREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[type]);
3018a2e73f56SAlex Deucher 		dc_hpd_int_cntl &= ~DC_HPD1_INT_CONTROL__DC_HPD1_INT_EN_MASK;
30192285b91cSAlex Deucher 		WREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[type], dc_hpd_int_cntl);
3020a2e73f56SAlex Deucher 		break;
3021a2e73f56SAlex Deucher 	case AMDGPU_IRQ_STATE_ENABLE:
30222285b91cSAlex Deucher 		dc_hpd_int_cntl = RREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[type]);
3023a2e73f56SAlex Deucher 		dc_hpd_int_cntl |= DC_HPD1_INT_CONTROL__DC_HPD1_INT_EN_MASK;
30242285b91cSAlex Deucher 		WREG32(mmDC_HPD1_INT_CONTROL + hpd_offsets[type], dc_hpd_int_cntl);
3025a2e73f56SAlex Deucher 		break;
3026a2e73f56SAlex Deucher 	default:
3027a2e73f56SAlex Deucher 		break;
3028a2e73f56SAlex Deucher 	}
3029a2e73f56SAlex Deucher 
3030a2e73f56SAlex Deucher 	return 0;
3031a2e73f56SAlex Deucher }
3032a2e73f56SAlex Deucher 
dce_v8_0_set_crtc_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)3033a2e73f56SAlex Deucher static int dce_v8_0_set_crtc_interrupt_state(struct amdgpu_device *adev,
3034a2e73f56SAlex Deucher 					     struct amdgpu_irq_src *src,
3035a2e73f56SAlex Deucher 					     unsigned type,
3036a2e73f56SAlex Deucher 					     enum amdgpu_interrupt_state state)
3037a2e73f56SAlex Deucher {
3038a2e73f56SAlex Deucher 	switch (type) {
3039a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VBLANK1:
3040a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3041a2e73f56SAlex Deucher 		break;
3042a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VBLANK2:
3043a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3044a2e73f56SAlex Deucher 		break;
3045a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VBLANK3:
3046a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3047a2e73f56SAlex Deucher 		break;
3048a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VBLANK4:
3049a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3050a2e73f56SAlex Deucher 		break;
3051a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VBLANK5:
3052a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3053a2e73f56SAlex Deucher 		break;
3054a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VBLANK6:
3055a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3056a2e73f56SAlex Deucher 		break;
3057a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VLINE1:
3058a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vline_interrupt_state(adev, 0, state);
3059a2e73f56SAlex Deucher 		break;
3060a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VLINE2:
3061a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vline_interrupt_state(adev, 1, state);
3062a2e73f56SAlex Deucher 		break;
3063a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VLINE3:
3064a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vline_interrupt_state(adev, 2, state);
3065a2e73f56SAlex Deucher 		break;
3066a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VLINE4:
3067a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vline_interrupt_state(adev, 3, state);
3068a2e73f56SAlex Deucher 		break;
3069a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VLINE5:
3070a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vline_interrupt_state(adev, 4, state);
3071a2e73f56SAlex Deucher 		break;
3072a2e73f56SAlex Deucher 	case AMDGPU_CRTC_IRQ_VLINE6:
3073a2e73f56SAlex Deucher 		dce_v8_0_set_crtc_vline_interrupt_state(adev, 5, state);
3074a2e73f56SAlex Deucher 		break;
3075a2e73f56SAlex Deucher 	default:
3076a2e73f56SAlex Deucher 		break;
3077a2e73f56SAlex Deucher 	}
3078a2e73f56SAlex Deucher 	return 0;
3079a2e73f56SAlex Deucher }
3080a2e73f56SAlex Deucher 
dce_v8_0_crtc_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3081a2e73f56SAlex Deucher static int dce_v8_0_crtc_irq(struct amdgpu_device *adev,
3082a2e73f56SAlex Deucher 			     struct amdgpu_irq_src *source,
3083a2e73f56SAlex Deucher 			     struct amdgpu_iv_entry *entry)
3084a2e73f56SAlex Deucher {
3085a2e73f56SAlex Deucher 	unsigned crtc = entry->src_id - 1;
3086a2e73f56SAlex Deucher 	uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3087734dd01dSSamuel Li 	unsigned int irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
3088734dd01dSSamuel Li 								    crtc);
3089a2e73f56SAlex Deucher 
30907ccf5aa8SAlex Deucher 	switch (entry->src_data[0]) {
3091a2e73f56SAlex Deucher 	case 0: /* vblank */
3092bd833144SMario Kleiner 		if (disp_int & interrupt_status_offsets[crtc].vblank)
3093a2e73f56SAlex Deucher 			WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], LB_VBLANK_STATUS__VBLANK_ACK_MASK);
3094bd833144SMario Kleiner 		else
3095bd833144SMario Kleiner 			DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3096bd833144SMario Kleiner 
3097a2e73f56SAlex Deucher 		if (amdgpu_irq_enabled(adev, source, irq_type)) {
30984a580877SLuben Tuikov 			drm_handle_vblank(adev_to_drm(adev), crtc);
3099a2e73f56SAlex Deucher 		}
3100a2e73f56SAlex Deucher 		DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3101a2e73f56SAlex Deucher 		break;
3102a2e73f56SAlex Deucher 	case 1: /* vline */
3103bd833144SMario Kleiner 		if (disp_int & interrupt_status_offsets[crtc].vline)
3104a2e73f56SAlex Deucher 			WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], LB_VLINE_STATUS__VLINE_ACK_MASK);
3105bd833144SMario Kleiner 		else
3106bd833144SMario Kleiner 			DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3107bd833144SMario Kleiner 
3108a2e73f56SAlex Deucher 		DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3109a2e73f56SAlex Deucher 		break;
3110a2e73f56SAlex Deucher 	default:
31117ccf5aa8SAlex Deucher 		DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3112a2e73f56SAlex Deucher 		break;
3113a2e73f56SAlex Deucher 	}
3114a2e73f56SAlex Deucher 
3115a2e73f56SAlex Deucher 	return 0;
3116a2e73f56SAlex Deucher }
3117a2e73f56SAlex Deucher 
dce_v8_0_set_pageflip_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)3118a2e73f56SAlex Deucher static int dce_v8_0_set_pageflip_interrupt_state(struct amdgpu_device *adev,
3119a2e73f56SAlex Deucher 						 struct amdgpu_irq_src *src,
3120a2e73f56SAlex Deucher 						 unsigned type,
3121a2e73f56SAlex Deucher 						 enum amdgpu_interrupt_state state)
3122a2e73f56SAlex Deucher {
31237dfac896SAlex Deucher 	u32 reg;
31247dfac896SAlex Deucher 
31257dfac896SAlex Deucher 	if (type >= adev->mode_info.num_crtc) {
3126a2e73f56SAlex Deucher 		DRM_ERROR("invalid pageflip crtc %d\n", type);
3127a2e73f56SAlex Deucher 		return -EINVAL;
3128a2e73f56SAlex Deucher 	}
3129a2e73f56SAlex Deucher 
31307dfac896SAlex Deucher 	reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3131a2e73f56SAlex Deucher 	if (state == AMDGPU_IRQ_STATE_DISABLE)
31327dfac896SAlex Deucher 		WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
31337dfac896SAlex Deucher 		       reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3134a2e73f56SAlex Deucher 	else
31357dfac896SAlex Deucher 		WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
31367dfac896SAlex Deucher 		       reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3137a2e73f56SAlex Deucher 
3138a2e73f56SAlex Deucher 	return 0;
3139a2e73f56SAlex Deucher }
3140a2e73f56SAlex Deucher 
dce_v8_0_pageflip_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3141a2e73f56SAlex Deucher static int dce_v8_0_pageflip_irq(struct amdgpu_device *adev,
3142a2e73f56SAlex Deucher 				struct amdgpu_irq_src *source,
3143a2e73f56SAlex Deucher 				struct amdgpu_iv_entry *entry)
3144a2e73f56SAlex Deucher {
3145a2e73f56SAlex Deucher 	unsigned long flags;
3146a2e73f56SAlex Deucher 	unsigned crtc_id;
3147a2e73f56SAlex Deucher 	struct amdgpu_crtc *amdgpu_crtc;
3148a2e73f56SAlex Deucher 	struct amdgpu_flip_work *works;
3149a2e73f56SAlex Deucher 
3150a2e73f56SAlex Deucher 	crtc_id = (entry->src_id - 8) >> 1;
3151a2e73f56SAlex Deucher 	amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3152a2e73f56SAlex Deucher 
31537dfac896SAlex Deucher 	if (crtc_id >= adev->mode_info.num_crtc) {
3154a2e73f56SAlex Deucher 		DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3155a2e73f56SAlex Deucher 		return -EINVAL;
3156a2e73f56SAlex Deucher 	}
3157a2e73f56SAlex Deucher 
31587dfac896SAlex Deucher 	if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
31597dfac896SAlex Deucher 	    GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
31607dfac896SAlex Deucher 		WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
31617dfac896SAlex Deucher 		       GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3162a2e73f56SAlex Deucher 
3163a2e73f56SAlex Deucher 	/* IRQ could occur when in initial stage */
3164a2e73f56SAlex Deucher 	if (amdgpu_crtc == NULL)
3165a2e73f56SAlex Deucher 		return 0;
3166a2e73f56SAlex Deucher 
31674a580877SLuben Tuikov 	spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
3168a2e73f56SAlex Deucher 	works = amdgpu_crtc->pflip_works;
3169a2e73f56SAlex Deucher 	if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
3170a2e73f56SAlex Deucher 		DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3171a2e73f56SAlex Deucher 						"AMDGPU_FLIP_SUBMITTED(%d)\n",
3172a2e73f56SAlex Deucher 						amdgpu_crtc->pflip_status,
3173a2e73f56SAlex Deucher 						AMDGPU_FLIP_SUBMITTED);
31744a580877SLuben Tuikov 		spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3175a2e73f56SAlex Deucher 		return 0;
3176a2e73f56SAlex Deucher 	}
3177a2e73f56SAlex Deucher 
3178a2e73f56SAlex Deucher 	/* page flip completed. clean up */
3179a2e73f56SAlex Deucher 	amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3180a2e73f56SAlex Deucher 	amdgpu_crtc->pflip_works = NULL;
3181a2e73f56SAlex Deucher 
3182a2e73f56SAlex Deucher 	/* wakeup usersapce */
3183a2e73f56SAlex Deucher 	if (works->event)
318456286769SGustavo Padovan 		drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
3185a2e73f56SAlex Deucher 
31864a580877SLuben Tuikov 	spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3187a2e73f56SAlex Deucher 
318860629c4dSGustavo Padovan 	drm_crtc_vblank_put(&amdgpu_crtc->base);
318987d58c11SChristian König 	schedule_work(&works->unpin_work);
3190a2e73f56SAlex Deucher 
3191a2e73f56SAlex Deucher 	return 0;
3192a2e73f56SAlex Deucher }
3193a2e73f56SAlex Deucher 
dce_v8_0_hpd_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3194a2e73f56SAlex Deucher static int dce_v8_0_hpd_irq(struct amdgpu_device *adev,
3195a2e73f56SAlex Deucher 			    struct amdgpu_irq_src *source,
3196a2e73f56SAlex Deucher 			    struct amdgpu_iv_entry *entry)
3197a2e73f56SAlex Deucher {
3198aeaf3e6cSQiang Ma 	uint32_t disp_int, mask;
3199a2e73f56SAlex Deucher 	unsigned hpd;
3200a2e73f56SAlex Deucher 
32017ccf5aa8SAlex Deucher 	if (entry->src_data[0] >= adev->mode_info.num_hpd) {
32027ccf5aa8SAlex Deucher 		DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3203a2e73f56SAlex Deucher 		return 0;
3204a2e73f56SAlex Deucher 	}
3205a2e73f56SAlex Deucher 
32067ccf5aa8SAlex Deucher 	hpd = entry->src_data[0];
3207a2e73f56SAlex Deucher 	disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3208a2e73f56SAlex Deucher 	mask = interrupt_status_offsets[hpd].hpd;
3209a2e73f56SAlex Deucher 
3210a2e73f56SAlex Deucher 	if (disp_int & mask) {
3211aeaf3e6cSQiang Ma 		dce_v8_0_hpd_int_ack(adev, hpd);
321290f56611Sxurui 		schedule_delayed_work(&adev->hotplug_work, 0);
3213a2e73f56SAlex Deucher 		DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3214a2e73f56SAlex Deucher 	}
3215a2e73f56SAlex Deucher 
3216a2e73f56SAlex Deucher 	return 0;
3217a2e73f56SAlex Deucher 
3218a2e73f56SAlex Deucher }
3219a2e73f56SAlex Deucher 
dce_v8_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)32205fc3aeebSyanyang1 static int dce_v8_0_set_clockgating_state(void *handle,
32215fc3aeebSyanyang1 					  enum amd_clockgating_state state)
3222a2e73f56SAlex Deucher {
3223a2e73f56SAlex Deucher 	return 0;
3224a2e73f56SAlex Deucher }
3225a2e73f56SAlex Deucher 
dce_v8_0_set_powergating_state(void * handle,enum amd_powergating_state state)32265fc3aeebSyanyang1 static int dce_v8_0_set_powergating_state(void *handle,
32275fc3aeebSyanyang1 					  enum amd_powergating_state state)
3228a2e73f56SAlex Deucher {
3229a2e73f56SAlex Deucher 	return 0;
3230a2e73f56SAlex Deucher }
3231a2e73f56SAlex Deucher 
3232a1255107SAlex Deucher static const struct amd_ip_funcs dce_v8_0_ip_funcs = {
323388a907d6STom St Denis 	.name = "dce_v8_0",
3234a2e73f56SAlex Deucher 	.early_init = dce_v8_0_early_init,
3235a2e73f56SAlex Deucher 	.late_init = NULL,
3236a2e73f56SAlex Deucher 	.sw_init = dce_v8_0_sw_init,
3237a2e73f56SAlex Deucher 	.sw_fini = dce_v8_0_sw_fini,
3238a2e73f56SAlex Deucher 	.hw_init = dce_v8_0_hw_init,
3239a2e73f56SAlex Deucher 	.hw_fini = dce_v8_0_hw_fini,
3240a2e73f56SAlex Deucher 	.suspend = dce_v8_0_suspend,
3241a2e73f56SAlex Deucher 	.resume = dce_v8_0_resume,
3242a2e73f56SAlex Deucher 	.is_idle = dce_v8_0_is_idle,
3243a2e73f56SAlex Deucher 	.wait_for_idle = dce_v8_0_wait_for_idle,
3244a2e73f56SAlex Deucher 	.soft_reset = dce_v8_0_soft_reset,
3245a2e73f56SAlex Deucher 	.set_clockgating_state = dce_v8_0_set_clockgating_state,
3246a2e73f56SAlex Deucher 	.set_powergating_state = dce_v8_0_set_powergating_state,
3247e21d253bSSunil Khatri 	.dump_ip_state = NULL,
324840356542SSunil Khatri 	.print_ip_state = NULL,
3249a2e73f56SAlex Deucher };
3250a2e73f56SAlex Deucher 
3251a2e73f56SAlex Deucher static void
dce_v8_0_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)3252a2e73f56SAlex Deucher dce_v8_0_encoder_mode_set(struct drm_encoder *encoder,
3253a2e73f56SAlex Deucher 			  struct drm_display_mode *mode,
3254a2e73f56SAlex Deucher 			  struct drm_display_mode *adjusted_mode)
3255a2e73f56SAlex Deucher {
3256a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3257a2e73f56SAlex Deucher 
3258a2e73f56SAlex Deucher 	amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3259a2e73f56SAlex Deucher 
3260a2e73f56SAlex Deucher 	/* need to call this here rather than in prepare() since we need some crtc info */
3261a2e73f56SAlex Deucher 	amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3262a2e73f56SAlex Deucher 
3263a2e73f56SAlex Deucher 	/* set scaler clears this on some chips */
3264a2e73f56SAlex Deucher 	dce_v8_0_set_interleave(encoder->crtc, mode);
3265a2e73f56SAlex Deucher 
3266a2e73f56SAlex Deucher 	if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3267a2e73f56SAlex Deucher 		dce_v8_0_afmt_enable(encoder, true);
3268a2e73f56SAlex Deucher 		dce_v8_0_afmt_setmode(encoder, adjusted_mode);
3269a2e73f56SAlex Deucher 	}
3270a2e73f56SAlex Deucher }
3271a2e73f56SAlex Deucher 
dce_v8_0_encoder_prepare(struct drm_encoder * encoder)3272a2e73f56SAlex Deucher static void dce_v8_0_encoder_prepare(struct drm_encoder *encoder)
3273a2e73f56SAlex Deucher {
32741348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(encoder->dev);
3275a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3276a2e73f56SAlex Deucher 	struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3277a2e73f56SAlex Deucher 
3278a2e73f56SAlex Deucher 	if ((amdgpu_encoder->active_device &
3279a2e73f56SAlex Deucher 	     (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3280a2e73f56SAlex Deucher 	    (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3281a2e73f56SAlex Deucher 	     ENCODER_OBJECT_ID_NONE)) {
3282a2e73f56SAlex Deucher 		struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3283a2e73f56SAlex Deucher 		if (dig) {
3284a2e73f56SAlex Deucher 			dig->dig_encoder = dce_v8_0_pick_dig_encoder(encoder);
3285a2e73f56SAlex Deucher 			if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3286a2e73f56SAlex Deucher 				dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3287a2e73f56SAlex Deucher 		}
3288a2e73f56SAlex Deucher 	}
3289a2e73f56SAlex Deucher 
3290a2e73f56SAlex Deucher 	amdgpu_atombios_scratch_regs_lock(adev, true);
3291a2e73f56SAlex Deucher 
3292a2e73f56SAlex Deucher 	if (connector) {
3293a2e73f56SAlex Deucher 		struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3294a2e73f56SAlex Deucher 
3295a2e73f56SAlex Deucher 		/* select the clock/data port if it uses a router */
3296a2e73f56SAlex Deucher 		if (amdgpu_connector->router.cd_valid)
3297a2e73f56SAlex Deucher 			amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3298a2e73f56SAlex Deucher 
3299a2e73f56SAlex Deucher 		/* turn eDP panel on for mode set */
3300a2e73f56SAlex Deucher 		if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3301a2e73f56SAlex Deucher 			amdgpu_atombios_encoder_set_edp_panel_power(connector,
3302a2e73f56SAlex Deucher 							     ATOM_TRANSMITTER_ACTION_POWER_ON);
3303a2e73f56SAlex Deucher 	}
3304a2e73f56SAlex Deucher 
3305a2e73f56SAlex Deucher 	/* this is needed for the pll/ss setup to work correctly in some cases */
3306a2e73f56SAlex Deucher 	amdgpu_atombios_encoder_set_crtc_source(encoder);
3307a2e73f56SAlex Deucher 	/* set up the FMT blocks */
3308a2e73f56SAlex Deucher 	dce_v8_0_program_fmt(encoder);
3309a2e73f56SAlex Deucher }
3310a2e73f56SAlex Deucher 
dce_v8_0_encoder_commit(struct drm_encoder * encoder)3311a2e73f56SAlex Deucher static void dce_v8_0_encoder_commit(struct drm_encoder *encoder)
3312a2e73f56SAlex Deucher {
3313a2e73f56SAlex Deucher 	struct drm_device *dev = encoder->dev;
33141348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
3315a2e73f56SAlex Deucher 
3316a2e73f56SAlex Deucher 	/* need to call this here as we need the crtc set up */
3317a2e73f56SAlex Deucher 	amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3318a2e73f56SAlex Deucher 	amdgpu_atombios_scratch_regs_lock(adev, false);
3319a2e73f56SAlex Deucher }
3320a2e73f56SAlex Deucher 
dce_v8_0_encoder_disable(struct drm_encoder * encoder)3321a2e73f56SAlex Deucher static void dce_v8_0_encoder_disable(struct drm_encoder *encoder)
3322a2e73f56SAlex Deucher {
3323a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3324a2e73f56SAlex Deucher 	struct amdgpu_encoder_atom_dig *dig;
3325a2e73f56SAlex Deucher 
3326a2e73f56SAlex Deucher 	amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3327a2e73f56SAlex Deucher 
3328a2e73f56SAlex Deucher 	if (amdgpu_atombios_encoder_is_digital(encoder)) {
3329a2e73f56SAlex Deucher 		if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3330a2e73f56SAlex Deucher 			dce_v8_0_afmt_enable(encoder, false);
3331a2e73f56SAlex Deucher 		dig = amdgpu_encoder->enc_priv;
3332a2e73f56SAlex Deucher 		dig->dig_encoder = -1;
3333a2e73f56SAlex Deucher 	}
3334a2e73f56SAlex Deucher 	amdgpu_encoder->active_device = 0;
3335a2e73f56SAlex Deucher }
3336a2e73f56SAlex Deucher 
3337a2e73f56SAlex Deucher /* these are handled by the primary encoders */
dce_v8_0_ext_prepare(struct drm_encoder * encoder)3338a2e73f56SAlex Deucher static void dce_v8_0_ext_prepare(struct drm_encoder *encoder)
3339a2e73f56SAlex Deucher {
3340a2e73f56SAlex Deucher 
3341a2e73f56SAlex Deucher }
3342a2e73f56SAlex Deucher 
dce_v8_0_ext_commit(struct drm_encoder * encoder)3343a2e73f56SAlex Deucher static void dce_v8_0_ext_commit(struct drm_encoder *encoder)
3344a2e73f56SAlex Deucher {
3345a2e73f56SAlex Deucher 
3346a2e73f56SAlex Deucher }
3347a2e73f56SAlex Deucher 
3348a2e73f56SAlex Deucher static void
dce_v8_0_ext_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)3349a2e73f56SAlex Deucher dce_v8_0_ext_mode_set(struct drm_encoder *encoder,
3350a2e73f56SAlex Deucher 		      struct drm_display_mode *mode,
3351a2e73f56SAlex Deucher 		      struct drm_display_mode *adjusted_mode)
3352a2e73f56SAlex Deucher {
3353a2e73f56SAlex Deucher 
3354a2e73f56SAlex Deucher }
3355a2e73f56SAlex Deucher 
dce_v8_0_ext_disable(struct drm_encoder * encoder)3356a2e73f56SAlex Deucher static void dce_v8_0_ext_disable(struct drm_encoder *encoder)
3357a2e73f56SAlex Deucher {
3358a2e73f56SAlex Deucher 
3359a2e73f56SAlex Deucher }
3360a2e73f56SAlex Deucher 
3361a2e73f56SAlex Deucher static void
dce_v8_0_ext_dpms(struct drm_encoder * encoder,int mode)3362a2e73f56SAlex Deucher dce_v8_0_ext_dpms(struct drm_encoder *encoder, int mode)
3363a2e73f56SAlex Deucher {
3364a2e73f56SAlex Deucher 
3365a2e73f56SAlex Deucher }
3366a2e73f56SAlex Deucher 
3367a2e73f56SAlex Deucher static const struct drm_encoder_helper_funcs dce_v8_0_ext_helper_funcs = {
3368a2e73f56SAlex Deucher 	.dpms = dce_v8_0_ext_dpms,
3369a2e73f56SAlex Deucher 	.prepare = dce_v8_0_ext_prepare,
3370a2e73f56SAlex Deucher 	.mode_set = dce_v8_0_ext_mode_set,
3371a2e73f56SAlex Deucher 	.commit = dce_v8_0_ext_commit,
3372a2e73f56SAlex Deucher 	.disable = dce_v8_0_ext_disable,
3373a2e73f56SAlex Deucher 	/* no detect for TMDS/LVDS yet */
3374a2e73f56SAlex Deucher };
3375a2e73f56SAlex Deucher 
3376a2e73f56SAlex Deucher static const struct drm_encoder_helper_funcs dce_v8_0_dig_helper_funcs = {
3377a2e73f56SAlex Deucher 	.dpms = amdgpu_atombios_encoder_dpms,
3378a2e73f56SAlex Deucher 	.mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3379a2e73f56SAlex Deucher 	.prepare = dce_v8_0_encoder_prepare,
3380a2e73f56SAlex Deucher 	.mode_set = dce_v8_0_encoder_mode_set,
3381a2e73f56SAlex Deucher 	.commit = dce_v8_0_encoder_commit,
3382a2e73f56SAlex Deucher 	.disable = dce_v8_0_encoder_disable,
3383a2e73f56SAlex Deucher 	.detect = amdgpu_atombios_encoder_dig_detect,
3384a2e73f56SAlex Deucher };
3385a2e73f56SAlex Deucher 
3386a2e73f56SAlex Deucher static const struct drm_encoder_helper_funcs dce_v8_0_dac_helper_funcs = {
3387a2e73f56SAlex Deucher 	.dpms = amdgpu_atombios_encoder_dpms,
3388a2e73f56SAlex Deucher 	.mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3389a2e73f56SAlex Deucher 	.prepare = dce_v8_0_encoder_prepare,
3390a2e73f56SAlex Deucher 	.mode_set = dce_v8_0_encoder_mode_set,
3391a2e73f56SAlex Deucher 	.commit = dce_v8_0_encoder_commit,
3392a2e73f56SAlex Deucher 	.detect = amdgpu_atombios_encoder_dac_detect,
3393a2e73f56SAlex Deucher };
3394a2e73f56SAlex Deucher 
dce_v8_0_encoder_destroy(struct drm_encoder * encoder)3395a2e73f56SAlex Deucher static void dce_v8_0_encoder_destroy(struct drm_encoder *encoder)
3396a2e73f56SAlex Deucher {
3397a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3398a2e73f56SAlex Deucher 	if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3399a2e73f56SAlex Deucher 		amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3400a2e73f56SAlex Deucher 	kfree(amdgpu_encoder->enc_priv);
3401a2e73f56SAlex Deucher 	drm_encoder_cleanup(encoder);
3402a2e73f56SAlex Deucher 	kfree(amdgpu_encoder);
3403a2e73f56SAlex Deucher }
3404a2e73f56SAlex Deucher 
3405a2e73f56SAlex Deucher static const struct drm_encoder_funcs dce_v8_0_encoder_funcs = {
3406a2e73f56SAlex Deucher 	.destroy = dce_v8_0_encoder_destroy,
3407a2e73f56SAlex Deucher };
3408a2e73f56SAlex Deucher 
dce_v8_0_encoder_add(struct amdgpu_device * adev,uint32_t encoder_enum,uint32_t supported_device,u16 caps)3409a2e73f56SAlex Deucher static void dce_v8_0_encoder_add(struct amdgpu_device *adev,
3410a2e73f56SAlex Deucher 				 uint32_t encoder_enum,
3411a2e73f56SAlex Deucher 				 uint32_t supported_device,
3412a2e73f56SAlex Deucher 				 u16 caps)
3413a2e73f56SAlex Deucher {
34144a580877SLuben Tuikov 	struct drm_device *dev = adev_to_drm(adev);
3415a2e73f56SAlex Deucher 	struct drm_encoder *encoder;
3416a2e73f56SAlex Deucher 	struct amdgpu_encoder *amdgpu_encoder;
3417a2e73f56SAlex Deucher 
3418a2e73f56SAlex Deucher 	/* see if we already added it */
3419a2e73f56SAlex Deucher 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3420a2e73f56SAlex Deucher 		amdgpu_encoder = to_amdgpu_encoder(encoder);
3421a2e73f56SAlex Deucher 		if (amdgpu_encoder->encoder_enum == encoder_enum) {
3422a2e73f56SAlex Deucher 			amdgpu_encoder->devices |= supported_device;
3423a2e73f56SAlex Deucher 			return;
3424a2e73f56SAlex Deucher 		}
3425a2e73f56SAlex Deucher 
3426a2e73f56SAlex Deucher 	}
3427a2e73f56SAlex Deucher 
3428a2e73f56SAlex Deucher 	/* add a new one */
3429a2e73f56SAlex Deucher 	amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3430a2e73f56SAlex Deucher 	if (!amdgpu_encoder)
3431a2e73f56SAlex Deucher 		return;
3432a2e73f56SAlex Deucher 
3433a2e73f56SAlex Deucher 	encoder = &amdgpu_encoder->base;
3434a2e73f56SAlex Deucher 	switch (adev->mode_info.num_crtc) {
3435a2e73f56SAlex Deucher 	case 1:
3436a2e73f56SAlex Deucher 		encoder->possible_crtcs = 0x1;
3437a2e73f56SAlex Deucher 		break;
3438a2e73f56SAlex Deucher 	case 2:
3439a2e73f56SAlex Deucher 	default:
3440a2e73f56SAlex Deucher 		encoder->possible_crtcs = 0x3;
3441a2e73f56SAlex Deucher 		break;
3442a2e73f56SAlex Deucher 	case 4:
3443a2e73f56SAlex Deucher 		encoder->possible_crtcs = 0xf;
3444a2e73f56SAlex Deucher 		break;
3445a2e73f56SAlex Deucher 	case 6:
3446a2e73f56SAlex Deucher 		encoder->possible_crtcs = 0x3f;
3447a2e73f56SAlex Deucher 		break;
3448a2e73f56SAlex Deucher 	}
3449a2e73f56SAlex Deucher 
3450a2e73f56SAlex Deucher 	amdgpu_encoder->enc_priv = NULL;
3451a2e73f56SAlex Deucher 
3452a2e73f56SAlex Deucher 	amdgpu_encoder->encoder_enum = encoder_enum;
3453a2e73f56SAlex Deucher 	amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3454a2e73f56SAlex Deucher 	amdgpu_encoder->devices = supported_device;
3455a2e73f56SAlex Deucher 	amdgpu_encoder->rmx_type = RMX_OFF;
3456a2e73f56SAlex Deucher 	amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3457a2e73f56SAlex Deucher 	amdgpu_encoder->is_ext_encoder = false;
3458a2e73f56SAlex Deucher 	amdgpu_encoder->caps = caps;
3459a2e73f56SAlex Deucher 
3460a2e73f56SAlex Deucher 	switch (amdgpu_encoder->encoder_id) {
3461a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3462a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3463a2e73f56SAlex Deucher 		drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
346413a3d91fSVille Syrjälä 				 DRM_MODE_ENCODER_DAC, NULL);
3465a2e73f56SAlex Deucher 		drm_encoder_helper_add(encoder, &dce_v8_0_dac_helper_funcs);
3466a2e73f56SAlex Deucher 		break;
3467a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3468a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3469a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3470a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3471a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3472a2e73f56SAlex Deucher 		if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3473a2e73f56SAlex Deucher 			amdgpu_encoder->rmx_type = RMX_FULL;
3474a2e73f56SAlex Deucher 			drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
347513a3d91fSVille Syrjälä 					 DRM_MODE_ENCODER_LVDS, NULL);
3476a2e73f56SAlex Deucher 			amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3477a2e73f56SAlex Deucher 		} else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3478a2e73f56SAlex Deucher 			drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
347913a3d91fSVille Syrjälä 					 DRM_MODE_ENCODER_DAC, NULL);
3480a2e73f56SAlex Deucher 			amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3481a2e73f56SAlex Deucher 		} else {
3482a2e73f56SAlex Deucher 			drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
348313a3d91fSVille Syrjälä 					 DRM_MODE_ENCODER_TMDS, NULL);
3484a2e73f56SAlex Deucher 			amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3485a2e73f56SAlex Deucher 		}
3486a2e73f56SAlex Deucher 		drm_encoder_helper_add(encoder, &dce_v8_0_dig_helper_funcs);
3487a2e73f56SAlex Deucher 		break;
3488a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_SI170B:
3489a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_CH7303:
3490a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3491a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3492a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_TITFP513:
3493a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_VT1623:
3494a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_HDMI_SI1930:
3495a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_TRAVIS:
3496a2e73f56SAlex Deucher 	case ENCODER_OBJECT_ID_NUTMEG:
3497a2e73f56SAlex Deucher 		/* these are handled by the primary encoders */
3498a2e73f56SAlex Deucher 		amdgpu_encoder->is_ext_encoder = true;
3499a2e73f56SAlex Deucher 		if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3500a2e73f56SAlex Deucher 			drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
350113a3d91fSVille Syrjälä 					 DRM_MODE_ENCODER_LVDS, NULL);
3502a2e73f56SAlex Deucher 		else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3503a2e73f56SAlex Deucher 			drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
350413a3d91fSVille Syrjälä 					 DRM_MODE_ENCODER_DAC, NULL);
3505a2e73f56SAlex Deucher 		else
3506a2e73f56SAlex Deucher 			drm_encoder_init(dev, encoder, &dce_v8_0_encoder_funcs,
350713a3d91fSVille Syrjälä 					 DRM_MODE_ENCODER_TMDS, NULL);
3508a2e73f56SAlex Deucher 		drm_encoder_helper_add(encoder, &dce_v8_0_ext_helper_funcs);
3509a2e73f56SAlex Deucher 		break;
3510a2e73f56SAlex Deucher 	}
3511a2e73f56SAlex Deucher }
3512a2e73f56SAlex Deucher 
3513a2e73f56SAlex Deucher static const struct amdgpu_display_funcs dce_v8_0_display_funcs = {
3514a2e73f56SAlex Deucher 	.bandwidth_update = &dce_v8_0_bandwidth_update,
3515a2e73f56SAlex Deucher 	.vblank_get_counter = &dce_v8_0_vblank_get_counter,
3516a2e73f56SAlex Deucher 	.backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3517a2e73f56SAlex Deucher 	.backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3518a2e73f56SAlex Deucher 	.hpd_sense = &dce_v8_0_hpd_sense,
3519a2e73f56SAlex Deucher 	.hpd_set_polarity = &dce_v8_0_hpd_set_polarity,
3520a2e73f56SAlex Deucher 	.hpd_get_gpio_reg = &dce_v8_0_hpd_get_gpio_reg,
3521a2e73f56SAlex Deucher 	.page_flip = &dce_v8_0_page_flip,
3522a2e73f56SAlex Deucher 	.page_flip_get_scanoutpos = &dce_v8_0_crtc_get_scanoutpos,
3523a2e73f56SAlex Deucher 	.add_encoder = &dce_v8_0_encoder_add,
3524a2e73f56SAlex Deucher 	.add_connector = &amdgpu_connector_add,
3525a2e73f56SAlex Deucher };
3526a2e73f56SAlex Deucher 
dce_v8_0_set_display_funcs(struct amdgpu_device * adev)3527a2e73f56SAlex Deucher static void dce_v8_0_set_display_funcs(struct amdgpu_device *adev)
3528a2e73f56SAlex Deucher {
3529a2e73f56SAlex Deucher 	adev->mode_info.funcs = &dce_v8_0_display_funcs;
3530a2e73f56SAlex Deucher }
3531a2e73f56SAlex Deucher 
3532a2e73f56SAlex Deucher static const struct amdgpu_irq_src_funcs dce_v8_0_crtc_irq_funcs = {
3533a2e73f56SAlex Deucher 	.set = dce_v8_0_set_crtc_interrupt_state,
3534a2e73f56SAlex Deucher 	.process = dce_v8_0_crtc_irq,
3535a2e73f56SAlex Deucher };
3536a2e73f56SAlex Deucher 
3537a2e73f56SAlex Deucher static const struct amdgpu_irq_src_funcs dce_v8_0_pageflip_irq_funcs = {
3538a2e73f56SAlex Deucher 	.set = dce_v8_0_set_pageflip_interrupt_state,
3539a2e73f56SAlex Deucher 	.process = dce_v8_0_pageflip_irq,
3540a2e73f56SAlex Deucher };
3541a2e73f56SAlex Deucher 
3542a2e73f56SAlex Deucher static const struct amdgpu_irq_src_funcs dce_v8_0_hpd_irq_funcs = {
3543a2e73f56SAlex Deucher 	.set = dce_v8_0_set_hpd_interrupt_state,
3544a2e73f56SAlex Deucher 	.process = dce_v8_0_hpd_irq,
3545a2e73f56SAlex Deucher };
3546a2e73f56SAlex Deucher 
dce_v8_0_set_irq_funcs(struct amdgpu_device * adev)3547a2e73f56SAlex Deucher static void dce_v8_0_set_irq_funcs(struct amdgpu_device *adev)
3548a2e73f56SAlex Deucher {
3549d794b9f8SMichel Dänzer 	if (adev->mode_info.num_crtc > 0)
3550d794b9f8SMichel Dänzer 		adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VLINE1 + adev->mode_info.num_crtc;
3551d794b9f8SMichel Dänzer 	else
3552d794b9f8SMichel Dänzer 		adev->crtc_irq.num_types = 0;
3553a2e73f56SAlex Deucher 	adev->crtc_irq.funcs = &dce_v8_0_crtc_irq_funcs;
3554a2e73f56SAlex Deucher 
3555d794b9f8SMichel Dänzer 	adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
3556a2e73f56SAlex Deucher 	adev->pageflip_irq.funcs = &dce_v8_0_pageflip_irq_funcs;
3557a2e73f56SAlex Deucher 
3558d794b9f8SMichel Dänzer 	adev->hpd_irq.num_types = adev->mode_info.num_hpd;
3559a2e73f56SAlex Deucher 	adev->hpd_irq.funcs = &dce_v8_0_hpd_irq_funcs;
3560a2e73f56SAlex Deucher }
3561a1255107SAlex Deucher 
356218ef7544SRan Sun const struct amdgpu_ip_block_version dce_v8_0_ip_block = {
3563a1255107SAlex Deucher 	.type = AMD_IP_BLOCK_TYPE_DCE,
3564a1255107SAlex Deucher 	.major = 8,
3565a1255107SAlex Deucher 	.minor = 0,
3566a1255107SAlex Deucher 	.rev = 0,
3567a1255107SAlex Deucher 	.funcs = &dce_v8_0_ip_funcs,
3568a1255107SAlex Deucher };
3569a1255107SAlex Deucher 
357018ef7544SRan Sun const struct amdgpu_ip_block_version dce_v8_1_ip_block = {
3571a1255107SAlex Deucher 	.type = AMD_IP_BLOCK_TYPE_DCE,
3572a1255107SAlex Deucher 	.major = 8,
3573a1255107SAlex Deucher 	.minor = 1,
3574a1255107SAlex Deucher 	.rev = 0,
3575a1255107SAlex Deucher 	.funcs = &dce_v8_0_ip_funcs,
3576a1255107SAlex Deucher };
3577a1255107SAlex Deucher 
357818ef7544SRan Sun const struct amdgpu_ip_block_version dce_v8_2_ip_block = {
3579a1255107SAlex Deucher 	.type = AMD_IP_BLOCK_TYPE_DCE,
3580a1255107SAlex Deucher 	.major = 8,
3581a1255107SAlex Deucher 	.minor = 2,
3582a1255107SAlex Deucher 	.rev = 0,
3583a1255107SAlex Deucher 	.funcs = &dce_v8_0_ip_funcs,
3584a1255107SAlex Deucher };
3585a1255107SAlex Deucher 
358618ef7544SRan Sun const struct amdgpu_ip_block_version dce_v8_3_ip_block = {
3587a1255107SAlex Deucher 	.type = AMD_IP_BLOCK_TYPE_DCE,
3588a1255107SAlex Deucher 	.major = 8,
3589a1255107SAlex Deucher 	.minor = 3,
3590a1255107SAlex Deucher 	.rev = 0,
3591a1255107SAlex Deucher 	.funcs = &dce_v8_0_ip_funcs,
3592a1255107SAlex Deucher };
3593a1255107SAlex Deucher 
359418ef7544SRan Sun const struct amdgpu_ip_block_version dce_v8_5_ip_block = {
3595a1255107SAlex Deucher 	.type = AMD_IP_BLOCK_TYPE_DCE,
3596a1255107SAlex Deucher 	.major = 8,
3597a1255107SAlex Deucher 	.minor = 5,
3598a1255107SAlex Deucher 	.rev = 0,
3599a1255107SAlex Deucher 	.funcs = &dce_v8_0_ip_funcs,
3600a1255107SAlex Deucher };
3601