1aaa36a97SAlex Deucher /*
2aaa36a97SAlex Deucher * Copyright 2014 Advanced Micro Devices, Inc.
3aaa36a97SAlex Deucher *
4aaa36a97SAlex Deucher * Permission is hereby granted, free of charge, to any person obtaining a
5aaa36a97SAlex Deucher * copy of this software and associated documentation files (the "Software"),
6aaa36a97SAlex Deucher * to deal in the Software without restriction, including without limitation
7aaa36a97SAlex Deucher * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8aaa36a97SAlex Deucher * and/or sell copies of the Software, and to permit persons to whom the
9aaa36a97SAlex Deucher * Software is furnished to do so, subject to the following conditions:
10aaa36a97SAlex Deucher *
11aaa36a97SAlex Deucher * The above copyright notice and this permission notice shall be included in
12aaa36a97SAlex Deucher * all copies or substantial portions of the Software.
13aaa36a97SAlex Deucher *
14aaa36a97SAlex Deucher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15aaa36a97SAlex Deucher * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16aaa36a97SAlex Deucher * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17aaa36a97SAlex Deucher * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18aaa36a97SAlex Deucher * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19aaa36a97SAlex Deucher * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20aaa36a97SAlex Deucher * OTHER DEALINGS IN THE SOFTWARE.
21aaa36a97SAlex Deucher *
22aaa36a97SAlex Deucher */
2347b757fbSSam Ravnborg
2447b757fbSSam Ravnborg #include <drm/drm_fourcc.h>
25*973ad627SThomas Zimmermann #include <drm/drm_modeset_helper.h>
26*973ad627SThomas Zimmermann #include <drm/drm_modeset_helper_vtables.h>
2747b757fbSSam Ravnborg #include <drm/drm_vblank.h>
2847b757fbSSam Ravnborg
29aaa36a97SAlex Deucher #include "amdgpu.h"
30aaa36a97SAlex Deucher #include "amdgpu_pm.h"
31aaa36a97SAlex Deucher #include "amdgpu_i2c.h"
32aaa36a97SAlex Deucher #include "vid.h"
33aaa36a97SAlex Deucher #include "atom.h"
34aaa36a97SAlex Deucher #include "amdgpu_atombios.h"
35aaa36a97SAlex Deucher #include "atombios_crtc.h"
36aaa36a97SAlex Deucher #include "atombios_encoders.h"
37aaa36a97SAlex Deucher #include "amdgpu_pll.h"
38aaa36a97SAlex Deucher #include "amdgpu_connectors.h"
395df58525SHuang Rui #include "amdgpu_display.h"
40356aee30SBaoyou Xie #include "dce_v11_0.h"
41aaa36a97SAlex Deucher
42aaa36a97SAlex Deucher #include "dce/dce_11_0_d.h"
43aaa36a97SAlex Deucher #include "dce/dce_11_0_sh_mask.h"
44aaa36a97SAlex Deucher #include "dce/dce_11_0_enum.h"
45aaa36a97SAlex Deucher #include "oss/oss_3_0_d.h"
46aaa36a97SAlex Deucher #include "oss/oss_3_0_sh_mask.h"
47aaa36a97SAlex Deucher #include "gmc/gmc_8_1_d.h"
48aaa36a97SAlex Deucher #include "gmc/gmc_8_1_sh_mask.h"
49aaa36a97SAlex Deucher
50091aec0bSAndrey Grodzovsky #include "ivsrcid/ivsrcid_vislands30.h"
51091aec0bSAndrey Grodzovsky
52aaa36a97SAlex Deucher static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev);
53aaa36a97SAlex Deucher static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev);
54aaa36a97SAlex Deucher
55aaa36a97SAlex Deucher static const u32 crtc_offsets[] =
56aaa36a97SAlex Deucher {
57aaa36a97SAlex Deucher CRTC0_REGISTER_OFFSET,
58aaa36a97SAlex Deucher CRTC1_REGISTER_OFFSET,
59aaa36a97SAlex Deucher CRTC2_REGISTER_OFFSET,
60aaa36a97SAlex Deucher CRTC3_REGISTER_OFFSET,
61aaa36a97SAlex Deucher CRTC4_REGISTER_OFFSET,
62aaa36a97SAlex Deucher CRTC5_REGISTER_OFFSET,
63aaa36a97SAlex Deucher CRTC6_REGISTER_OFFSET
64aaa36a97SAlex Deucher };
65aaa36a97SAlex Deucher
66aaa36a97SAlex Deucher static const u32 hpd_offsets[] =
67aaa36a97SAlex Deucher {
68aaa36a97SAlex Deucher HPD0_REGISTER_OFFSET,
69aaa36a97SAlex Deucher HPD1_REGISTER_OFFSET,
70aaa36a97SAlex Deucher HPD2_REGISTER_OFFSET,
71aaa36a97SAlex Deucher HPD3_REGISTER_OFFSET,
72aaa36a97SAlex Deucher HPD4_REGISTER_OFFSET,
73aaa36a97SAlex Deucher HPD5_REGISTER_OFFSET
74aaa36a97SAlex Deucher };
75aaa36a97SAlex Deucher
76aaa36a97SAlex Deucher static const uint32_t dig_offsets[] = {
77aaa36a97SAlex Deucher DIG0_REGISTER_OFFSET,
78aaa36a97SAlex Deucher DIG1_REGISTER_OFFSET,
79aaa36a97SAlex Deucher DIG2_REGISTER_OFFSET,
80aaa36a97SAlex Deucher DIG3_REGISTER_OFFSET,
81aaa36a97SAlex Deucher DIG4_REGISTER_OFFSET,
82aaa36a97SAlex Deucher DIG5_REGISTER_OFFSET,
83aaa36a97SAlex Deucher DIG6_REGISTER_OFFSET,
84aaa36a97SAlex Deucher DIG7_REGISTER_OFFSET,
85aaa36a97SAlex Deucher DIG8_REGISTER_OFFSET
86aaa36a97SAlex Deucher };
87aaa36a97SAlex Deucher
88aaa36a97SAlex Deucher static const struct {
89aaa36a97SAlex Deucher uint32_t reg;
90aaa36a97SAlex Deucher uint32_t vblank;
91aaa36a97SAlex Deucher uint32_t vline;
92aaa36a97SAlex Deucher uint32_t hpd;
93aaa36a97SAlex Deucher
94aaa36a97SAlex Deucher } interrupt_status_offsets[] = { {
95aaa36a97SAlex Deucher .reg = mmDISP_INTERRUPT_STATUS,
96aaa36a97SAlex Deucher .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
97aaa36a97SAlex Deucher .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
98aaa36a97SAlex Deucher .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
99aaa36a97SAlex Deucher }, {
100aaa36a97SAlex Deucher .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
101aaa36a97SAlex Deucher .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
102aaa36a97SAlex Deucher .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
103aaa36a97SAlex Deucher .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
104aaa36a97SAlex Deucher }, {
105aaa36a97SAlex Deucher .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
106aaa36a97SAlex Deucher .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
107aaa36a97SAlex Deucher .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
108aaa36a97SAlex Deucher .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
109aaa36a97SAlex Deucher }, {
110aaa36a97SAlex Deucher .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
111aaa36a97SAlex Deucher .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
112aaa36a97SAlex Deucher .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
113aaa36a97SAlex Deucher .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
114aaa36a97SAlex Deucher }, {
115aaa36a97SAlex Deucher .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
116aaa36a97SAlex Deucher .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
117aaa36a97SAlex Deucher .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
118aaa36a97SAlex Deucher .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
119aaa36a97SAlex Deucher }, {
120aaa36a97SAlex Deucher .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
121aaa36a97SAlex Deucher .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
122aaa36a97SAlex Deucher .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
123aaa36a97SAlex Deucher .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
124aaa36a97SAlex Deucher } };
125aaa36a97SAlex Deucher
126aaa36a97SAlex Deucher static const u32 cz_golden_settings_a11[] =
127aaa36a97SAlex Deucher {
128aaa36a97SAlex Deucher mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
129aaa36a97SAlex Deucher mmFBC_MISC, 0x1f311fff, 0x14300000,
130aaa36a97SAlex Deucher };
131aaa36a97SAlex Deucher
1325732a94fSAlex Deucher static const u32 cz_mgcg_cgcg_init[] =
1335732a94fSAlex Deucher {
1345732a94fSAlex Deucher mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
1355732a94fSAlex Deucher mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
1365732a94fSAlex Deucher };
1375732a94fSAlex Deucher
138fa2f9befSSamuel Li static const u32 stoney_golden_settings_a11[] =
139fa2f9befSSamuel Li {
140fa2f9befSSamuel Li mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
141fa2f9befSSamuel Li mmFBC_MISC, 0x1f311fff, 0x14302000,
142fa2f9befSSamuel Li };
143fa2f9befSSamuel Li
1442cc0c0b5SFlora Cui static const u32 polaris11_golden_settings_a11[] =
14560909285SFlora Cui {
14660909285SFlora Cui mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
14760909285SFlora Cui mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
14860909285SFlora Cui mmFBC_DEBUG1, 0xffffffff, 0x00000008,
149b9934878SFlora Cui mmFBC_MISC, 0x9f313fff, 0x14302008,
15060909285SFlora Cui mmHDMI_CONTROL, 0x313f031f, 0x00000011,
15160909285SFlora Cui };
15260909285SFlora Cui
1532cc0c0b5SFlora Cui static const u32 polaris10_golden_settings_a11[] =
15460909285SFlora Cui {
15560909285SFlora Cui mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
15660909285SFlora Cui mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
157d4ab989fSFlora Cui mmFBC_MISC, 0x9f313fff, 0x14302008,
15860909285SFlora Cui mmHDMI_CONTROL, 0x313f031f, 0x00000011,
15960909285SFlora Cui };
160fa2f9befSSamuel Li
dce_v11_0_init_golden_registers(struct amdgpu_device * adev)161aaa36a97SAlex Deucher static void dce_v11_0_init_golden_registers(struct amdgpu_device *adev)
162aaa36a97SAlex Deucher {
163aaa36a97SAlex Deucher switch (adev->asic_type) {
164aaa36a97SAlex Deucher case CHIP_CARRIZO:
1659c3f2b54SAlex Deucher amdgpu_device_program_register_sequence(adev,
1665732a94fSAlex Deucher cz_mgcg_cgcg_init,
167c47b41a7SChristian König ARRAY_SIZE(cz_mgcg_cgcg_init));
1689c3f2b54SAlex Deucher amdgpu_device_program_register_sequence(adev,
169aaa36a97SAlex Deucher cz_golden_settings_a11,
170c47b41a7SChristian König ARRAY_SIZE(cz_golden_settings_a11));
171aaa36a97SAlex Deucher break;
172fa2f9befSSamuel Li case CHIP_STONEY:
1739c3f2b54SAlex Deucher amdgpu_device_program_register_sequence(adev,
174fa2f9befSSamuel Li stoney_golden_settings_a11,
175c47b41a7SChristian König ARRAY_SIZE(stoney_golden_settings_a11));
176fa2f9befSSamuel Li break;
1772cc0c0b5SFlora Cui case CHIP_POLARIS11:
178c4642a47SJunwei Zhang case CHIP_POLARIS12:
1799c3f2b54SAlex Deucher amdgpu_device_program_register_sequence(adev,
1802cc0c0b5SFlora Cui polaris11_golden_settings_a11,
181c47b41a7SChristian König ARRAY_SIZE(polaris11_golden_settings_a11));
18260909285SFlora Cui break;
1832cc0c0b5SFlora Cui case CHIP_POLARIS10:
184221adb21SAlex Deucher case CHIP_VEGAM:
1859c3f2b54SAlex Deucher amdgpu_device_program_register_sequence(adev,
1862cc0c0b5SFlora Cui polaris10_golden_settings_a11,
187c47b41a7SChristian König ARRAY_SIZE(polaris10_golden_settings_a11));
18860909285SFlora Cui break;
189aaa36a97SAlex Deucher default:
190aaa36a97SAlex Deucher break;
191aaa36a97SAlex Deucher }
192aaa36a97SAlex Deucher }
193aaa36a97SAlex Deucher
dce_v11_0_audio_endpt_rreg(struct amdgpu_device * adev,u32 block_offset,u32 reg)194aaa36a97SAlex Deucher static u32 dce_v11_0_audio_endpt_rreg(struct amdgpu_device *adev,
195aaa36a97SAlex Deucher u32 block_offset, u32 reg)
196aaa36a97SAlex Deucher {
197aaa36a97SAlex Deucher unsigned long flags;
198aaa36a97SAlex Deucher u32 r;
199aaa36a97SAlex Deucher
200aaa36a97SAlex Deucher spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
201aaa36a97SAlex Deucher WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
202aaa36a97SAlex Deucher r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
203aaa36a97SAlex Deucher spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
204aaa36a97SAlex Deucher
205aaa36a97SAlex Deucher return r;
206aaa36a97SAlex Deucher }
207aaa36a97SAlex Deucher
dce_v11_0_audio_endpt_wreg(struct amdgpu_device * adev,u32 block_offset,u32 reg,u32 v)208aaa36a97SAlex Deucher static void dce_v11_0_audio_endpt_wreg(struct amdgpu_device *adev,
209aaa36a97SAlex Deucher u32 block_offset, u32 reg, u32 v)
210aaa36a97SAlex Deucher {
211aaa36a97SAlex Deucher unsigned long flags;
212aaa36a97SAlex Deucher
213aaa36a97SAlex Deucher spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
214aaa36a97SAlex Deucher WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
215aaa36a97SAlex Deucher WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
216aaa36a97SAlex Deucher spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
217aaa36a97SAlex Deucher }
218aaa36a97SAlex Deucher
dce_v11_0_vblank_get_counter(struct amdgpu_device * adev,int crtc)219aaa36a97SAlex Deucher static u32 dce_v11_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
220aaa36a97SAlex Deucher {
22115c3277fSTom St Denis if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
222aaa36a97SAlex Deucher return 0;
223aaa36a97SAlex Deucher else
224aaa36a97SAlex Deucher return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
225aaa36a97SAlex Deucher }
226aaa36a97SAlex Deucher
dce_v11_0_pageflip_interrupt_init(struct amdgpu_device * adev)227f6c7aba4SMichel Dänzer static void dce_v11_0_pageflip_interrupt_init(struct amdgpu_device *adev)
228f6c7aba4SMichel Dänzer {
229f6c7aba4SMichel Dänzer unsigned i;
230f6c7aba4SMichel Dänzer
231f6c7aba4SMichel Dänzer /* Enable pflip interrupts */
232f6c7aba4SMichel Dänzer for (i = 0; i < adev->mode_info.num_crtc; i++)
233f6c7aba4SMichel Dänzer amdgpu_irq_get(adev, &adev->pageflip_irq, i);
234f6c7aba4SMichel Dänzer }
235f6c7aba4SMichel Dänzer
dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device * adev)236f6c7aba4SMichel Dänzer static void dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
237f6c7aba4SMichel Dänzer {
238f6c7aba4SMichel Dänzer unsigned i;
239f6c7aba4SMichel Dänzer
240f6c7aba4SMichel Dänzer /* Disable pflip interrupts */
241f6c7aba4SMichel Dänzer for (i = 0; i < adev->mode_info.num_crtc; i++)
242f6c7aba4SMichel Dänzer amdgpu_irq_put(adev, &adev->pageflip_irq, i);
243f6c7aba4SMichel Dänzer }
244f6c7aba4SMichel Dänzer
245aaa36a97SAlex Deucher /**
246aaa36a97SAlex Deucher * dce_v11_0_page_flip - pageflip callback.
247aaa36a97SAlex Deucher *
248aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
249aaa36a97SAlex Deucher * @crtc_id: crtc to cleanup pageflip on
250aaa36a97SAlex Deucher * @crtc_base: new address of the crtc (GPU MC address)
251c4403754SLee Jones * @async: asynchronous flip
252aaa36a97SAlex Deucher *
253ce055fe3SAlex Deucher * Triggers the actual pageflip by updating the primary
254ce055fe3SAlex Deucher * surface base address.
255aaa36a97SAlex Deucher */
dce_v11_0_page_flip(struct amdgpu_device * adev,int crtc_id,u64 crtc_base,bool async)256aaa36a97SAlex Deucher static void dce_v11_0_page_flip(struct amdgpu_device *adev,
257cb9e59d7SAlex Deucher int crtc_id, u64 crtc_base, bool async)
258aaa36a97SAlex Deucher {
259aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
260965ebe3dSMichel Dänzer struct drm_framebuffer *fb = amdgpu_crtc->base.primary->fb;
261cb9e59d7SAlex Deucher u32 tmp;
262aaa36a97SAlex Deucher
2637359ee63SAlex Deucher /* flip immediate for async, default is vsync */
264cb9e59d7SAlex Deucher tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
265cb9e59d7SAlex Deucher tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
2667359ee63SAlex Deucher GRPH_SURFACE_UPDATE_IMMEDIATE_EN, async ? 1 : 0);
267cb9e59d7SAlex Deucher WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
268965ebe3dSMichel Dänzer /* update pitch */
269965ebe3dSMichel Dänzer WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset,
270965ebe3dSMichel Dänzer fb->pitches[0] / fb->format->cpp[0]);
271aaa36a97SAlex Deucher /* update the scanout addresses */
272aaa36a97SAlex Deucher WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
273aaa36a97SAlex Deucher upper_32_bits(crtc_base));
274ce055fe3SAlex Deucher /* writing to the low address triggers the update */
275aaa36a97SAlex Deucher WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
276aaa36a97SAlex Deucher lower_32_bits(crtc_base));
277ce055fe3SAlex Deucher /* post the write */
278ce055fe3SAlex Deucher RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
279aaa36a97SAlex Deucher }
280aaa36a97SAlex Deucher
dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device * adev,int crtc,u32 * vbl,u32 * position)281aaa36a97SAlex Deucher static int dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
282aaa36a97SAlex Deucher u32 *vbl, u32 *position)
283aaa36a97SAlex Deucher {
284aaa36a97SAlex Deucher if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
285aaa36a97SAlex Deucher return -EINVAL;
286aaa36a97SAlex Deucher
287aaa36a97SAlex Deucher *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
288aaa36a97SAlex Deucher *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
289aaa36a97SAlex Deucher
290aaa36a97SAlex Deucher return 0;
291aaa36a97SAlex Deucher }
292aaa36a97SAlex Deucher
293aaa36a97SAlex Deucher /**
294aaa36a97SAlex Deucher * dce_v11_0_hpd_sense - hpd sense callback.
295aaa36a97SAlex Deucher *
296aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
297aaa36a97SAlex Deucher * @hpd: hpd (hotplug detect) pin
298aaa36a97SAlex Deucher *
299aaa36a97SAlex Deucher * Checks if a digital monitor is connected (evergreen+).
300aaa36a97SAlex Deucher * Returns true if connected, false if not connected.
301aaa36a97SAlex Deucher */
dce_v11_0_hpd_sense(struct amdgpu_device * adev,enum amdgpu_hpd_id hpd)302aaa36a97SAlex Deucher static bool dce_v11_0_hpd_sense(struct amdgpu_device *adev,
303aaa36a97SAlex Deucher enum amdgpu_hpd_id hpd)
304aaa36a97SAlex Deucher {
305aaa36a97SAlex Deucher bool connected = false;
306aaa36a97SAlex Deucher
307d2486d25SAlex Deucher if (hpd >= adev->mode_info.num_hpd)
308aaa36a97SAlex Deucher return connected;
309aaa36a97SAlex Deucher
310d2486d25SAlex Deucher if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[hpd]) &
311aaa36a97SAlex Deucher DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
312aaa36a97SAlex Deucher connected = true;
313aaa36a97SAlex Deucher
314aaa36a97SAlex Deucher return connected;
315aaa36a97SAlex Deucher }
316aaa36a97SAlex Deucher
317aaa36a97SAlex Deucher /**
318aaa36a97SAlex Deucher * dce_v11_0_hpd_set_polarity - hpd set polarity callback.
319aaa36a97SAlex Deucher *
320aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
321aaa36a97SAlex Deucher * @hpd: hpd (hotplug detect) pin
322aaa36a97SAlex Deucher *
323aaa36a97SAlex Deucher * Set the polarity of the hpd pin (evergreen+).
324aaa36a97SAlex Deucher */
dce_v11_0_hpd_set_polarity(struct amdgpu_device * adev,enum amdgpu_hpd_id hpd)325aaa36a97SAlex Deucher static void dce_v11_0_hpd_set_polarity(struct amdgpu_device *adev,
326aaa36a97SAlex Deucher enum amdgpu_hpd_id hpd)
327aaa36a97SAlex Deucher {
328aaa36a97SAlex Deucher u32 tmp;
329aaa36a97SAlex Deucher bool connected = dce_v11_0_hpd_sense(adev, hpd);
330aaa36a97SAlex Deucher
331d2486d25SAlex Deucher if (hpd >= adev->mode_info.num_hpd)
332aaa36a97SAlex Deucher return;
333aaa36a97SAlex Deucher
334d2486d25SAlex Deucher tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
335aaa36a97SAlex Deucher if (connected)
336aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
337aaa36a97SAlex Deucher else
338aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
339d2486d25SAlex Deucher WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
340aaa36a97SAlex Deucher }
341aaa36a97SAlex Deucher
342aaa36a97SAlex Deucher /**
343aaa36a97SAlex Deucher * dce_v11_0_hpd_init - hpd setup callback.
344aaa36a97SAlex Deucher *
345aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
346aaa36a97SAlex Deucher *
347aaa36a97SAlex Deucher * Setup the hpd pins used by the card (evergreen+).
348aaa36a97SAlex Deucher * Enable the pin, set the polarity, and enable the hpd interrupts.
349aaa36a97SAlex Deucher */
dce_v11_0_hpd_init(struct amdgpu_device * adev)350aaa36a97SAlex Deucher static void dce_v11_0_hpd_init(struct amdgpu_device *adev)
351aaa36a97SAlex Deucher {
3524a580877SLuben Tuikov struct drm_device *dev = adev_to_drm(adev);
353aaa36a97SAlex Deucher struct drm_connector *connector;
354f8d2d39eSLyude Paul struct drm_connector_list_iter iter;
355aaa36a97SAlex Deucher u32 tmp;
356aaa36a97SAlex Deucher
357f8d2d39eSLyude Paul drm_connector_list_iter_begin(dev, &iter);
358f8d2d39eSLyude Paul drm_for_each_connector_iter(connector, &iter) {
359aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
360aaa36a97SAlex Deucher
361d2486d25SAlex Deucher if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
362aaa36a97SAlex Deucher continue;
363aaa36a97SAlex Deucher
3643a9d993eSAlex Deucher if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
3653a9d993eSAlex Deucher connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
3663a9d993eSAlex Deucher /* don't try to enable hpd on eDP or LVDS avoid breaking the
3673a9d993eSAlex Deucher * aux dp channel on imac and help (but not completely fix)
3683a9d993eSAlex Deucher * https://bugzilla.redhat.com/show_bug.cgi?id=726143
3693a9d993eSAlex Deucher * also avoid interrupt storms during dpms.
3703a9d993eSAlex Deucher */
371d2486d25SAlex Deucher tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
3723a9d993eSAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
373d2486d25SAlex Deucher WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
3743a9d993eSAlex Deucher continue;
3753a9d993eSAlex Deucher }
3763a9d993eSAlex Deucher
377d2486d25SAlex Deucher tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
378aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
379d2486d25SAlex Deucher WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
380aaa36a97SAlex Deucher
381d2486d25SAlex Deucher tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd]);
382aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
383aaa36a97SAlex Deucher DC_HPD_CONNECT_INT_DELAY,
384aaa36a97SAlex Deucher AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
385aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
386aaa36a97SAlex Deucher DC_HPD_DISCONNECT_INT_DELAY,
387aaa36a97SAlex Deucher AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
388d2486d25SAlex Deucher WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
389aaa36a97SAlex Deucher
390aaa36a97SAlex Deucher dce_v11_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
391aaa36a97SAlex Deucher amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
392aaa36a97SAlex Deucher }
393f8d2d39eSLyude Paul drm_connector_list_iter_end(&iter);
394aaa36a97SAlex Deucher }
395aaa36a97SAlex Deucher
396aaa36a97SAlex Deucher /**
397aaa36a97SAlex Deucher * dce_v11_0_hpd_fini - hpd tear down callback.
398aaa36a97SAlex Deucher *
399aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
400aaa36a97SAlex Deucher *
401aaa36a97SAlex Deucher * Tear down the hpd pins used by the card (evergreen+).
402aaa36a97SAlex Deucher * Disable the hpd interrupts.
403aaa36a97SAlex Deucher */
dce_v11_0_hpd_fini(struct amdgpu_device * adev)404aaa36a97SAlex Deucher static void dce_v11_0_hpd_fini(struct amdgpu_device *adev)
405aaa36a97SAlex Deucher {
4064a580877SLuben Tuikov struct drm_device *dev = adev_to_drm(adev);
407aaa36a97SAlex Deucher struct drm_connector *connector;
408f8d2d39eSLyude Paul struct drm_connector_list_iter iter;
409aaa36a97SAlex Deucher u32 tmp;
410aaa36a97SAlex Deucher
411f8d2d39eSLyude Paul drm_connector_list_iter_begin(dev, &iter);
412f8d2d39eSLyude Paul drm_for_each_connector_iter(connector, &iter) {
413aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
414aaa36a97SAlex Deucher
415d2486d25SAlex Deucher if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
416aaa36a97SAlex Deucher continue;
417aaa36a97SAlex Deucher
418d2486d25SAlex Deucher tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
419aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
420d2486d25SAlex Deucher WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
421aaa36a97SAlex Deucher
422aaa36a97SAlex Deucher amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
423aaa36a97SAlex Deucher }
424f8d2d39eSLyude Paul drm_connector_list_iter_end(&iter);
425aaa36a97SAlex Deucher }
426aaa36a97SAlex Deucher
dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device * adev)427aaa36a97SAlex Deucher static u32 dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
428aaa36a97SAlex Deucher {
429aaa36a97SAlex Deucher return mmDC_GPIO_HPD_A;
430aaa36a97SAlex Deucher }
431aaa36a97SAlex Deucher
dce_v11_0_is_display_hung(struct amdgpu_device * adev)432aaa36a97SAlex Deucher static bool dce_v11_0_is_display_hung(struct amdgpu_device *adev)
433aaa36a97SAlex Deucher {
434aaa36a97SAlex Deucher u32 crtc_hung = 0;
435aaa36a97SAlex Deucher u32 crtc_status[6];
436aaa36a97SAlex Deucher u32 i, j, tmp;
437aaa36a97SAlex Deucher
438aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
439aaa36a97SAlex Deucher tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
440aaa36a97SAlex Deucher if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
441aaa36a97SAlex Deucher crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
442aaa36a97SAlex Deucher crtc_hung |= (1 << i);
443aaa36a97SAlex Deucher }
444aaa36a97SAlex Deucher }
445aaa36a97SAlex Deucher
446aaa36a97SAlex Deucher for (j = 0; j < 10; j++) {
447aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
448aaa36a97SAlex Deucher if (crtc_hung & (1 << i)) {
449aaa36a97SAlex Deucher tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
450aaa36a97SAlex Deucher if (tmp != crtc_status[i])
451aaa36a97SAlex Deucher crtc_hung &= ~(1 << i);
452aaa36a97SAlex Deucher }
453aaa36a97SAlex Deucher }
454aaa36a97SAlex Deucher if (crtc_hung == 0)
455aaa36a97SAlex Deucher return false;
456aaa36a97SAlex Deucher udelay(100);
457aaa36a97SAlex Deucher }
458aaa36a97SAlex Deucher
459aaa36a97SAlex Deucher return true;
460aaa36a97SAlex Deucher }
461aaa36a97SAlex Deucher
dce_v11_0_set_vga_render_state(struct amdgpu_device * adev,bool render)462aaa36a97SAlex Deucher static void dce_v11_0_set_vga_render_state(struct amdgpu_device *adev,
463aaa36a97SAlex Deucher bool render)
464aaa36a97SAlex Deucher {
465aaa36a97SAlex Deucher u32 tmp;
466aaa36a97SAlex Deucher
467aaa36a97SAlex Deucher /* Lockout access through VGA aperture*/
468aaa36a97SAlex Deucher tmp = RREG32(mmVGA_HDP_CONTROL);
469aaa36a97SAlex Deucher if (render)
470aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
471aaa36a97SAlex Deucher else
472aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
473aaa36a97SAlex Deucher WREG32(mmVGA_HDP_CONTROL, tmp);
474aaa36a97SAlex Deucher
475aaa36a97SAlex Deucher /* disable VGA render */
476aaa36a97SAlex Deucher tmp = RREG32(mmVGA_RENDER_CONTROL);
477aaa36a97SAlex Deucher if (render)
478aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
479aaa36a97SAlex Deucher else
480aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
481aaa36a97SAlex Deucher WREG32(mmVGA_RENDER_CONTROL, tmp);
482aaa36a97SAlex Deucher }
483aaa36a97SAlex Deucher
dce_v11_0_get_num_crtc(struct amdgpu_device * adev)48483c9b025SEmily Deng static int dce_v11_0_get_num_crtc (struct amdgpu_device *adev)
48583c9b025SEmily Deng {
48683c9b025SEmily Deng int num_crtc = 0;
48783c9b025SEmily Deng
48883c9b025SEmily Deng switch (adev->asic_type) {
48983c9b025SEmily Deng case CHIP_CARRIZO:
49083c9b025SEmily Deng num_crtc = 3;
49183c9b025SEmily Deng break;
49283c9b025SEmily Deng case CHIP_STONEY:
49383c9b025SEmily Deng num_crtc = 2;
49483c9b025SEmily Deng break;
49583c9b025SEmily Deng case CHIP_POLARIS10:
496221adb21SAlex Deucher case CHIP_VEGAM:
49783c9b025SEmily Deng num_crtc = 6;
49883c9b025SEmily Deng break;
49983c9b025SEmily Deng case CHIP_POLARIS11:
500c4642a47SJunwei Zhang case CHIP_POLARIS12:
50183c9b025SEmily Deng num_crtc = 5;
50283c9b025SEmily Deng break;
50383c9b025SEmily Deng default:
50483c9b025SEmily Deng num_crtc = 0;
50583c9b025SEmily Deng }
50683c9b025SEmily Deng return num_crtc;
50783c9b025SEmily Deng }
50883c9b025SEmily Deng
dce_v11_0_disable_dce(struct amdgpu_device * adev)50983c9b025SEmily Deng void dce_v11_0_disable_dce(struct amdgpu_device *adev)
51083c9b025SEmily Deng {
51183c9b025SEmily Deng /*Disable VGA render and enabled crtc, if has DCE engine*/
51283c9b025SEmily Deng if (amdgpu_atombios_has_dce_engine_info(adev)) {
51383c9b025SEmily Deng u32 tmp;
51483c9b025SEmily Deng int crtc_enabled, i;
51583c9b025SEmily Deng
51683c9b025SEmily Deng dce_v11_0_set_vga_render_state(adev, false);
51783c9b025SEmily Deng
51883c9b025SEmily Deng /*Disable crtc*/
51983c9b025SEmily Deng for (i = 0; i < dce_v11_0_get_num_crtc(adev); i++) {
52083c9b025SEmily Deng crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
52183c9b025SEmily Deng CRTC_CONTROL, CRTC_MASTER_EN);
52283c9b025SEmily Deng if (crtc_enabled) {
52383c9b025SEmily Deng WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
52483c9b025SEmily Deng tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
52583c9b025SEmily Deng tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
52683c9b025SEmily Deng WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
52783c9b025SEmily Deng WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
52883c9b025SEmily Deng }
52983c9b025SEmily Deng }
53083c9b025SEmily Deng }
53183c9b025SEmily Deng }
53283c9b025SEmily Deng
dce_v11_0_program_fmt(struct drm_encoder * encoder)533aaa36a97SAlex Deucher static void dce_v11_0_program_fmt(struct drm_encoder *encoder)
534aaa36a97SAlex Deucher {
535aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
5361348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
537aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
538aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
539aaa36a97SAlex Deucher struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
540aaa36a97SAlex Deucher int bpc = 0;
541aaa36a97SAlex Deucher u32 tmp = 0;
542aaa36a97SAlex Deucher enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
543aaa36a97SAlex Deucher
544aaa36a97SAlex Deucher if (connector) {
545aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
546aaa36a97SAlex Deucher bpc = amdgpu_connector_get_monitor_bpc(connector);
547aaa36a97SAlex Deucher dither = amdgpu_connector->dither;
548aaa36a97SAlex Deucher }
549aaa36a97SAlex Deucher
550aaa36a97SAlex Deucher /* LVDS/eDP FMT is set up by atom */
551aaa36a97SAlex Deucher if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
552aaa36a97SAlex Deucher return;
553aaa36a97SAlex Deucher
554aaa36a97SAlex Deucher /* not needed for analog */
555aaa36a97SAlex Deucher if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
556aaa36a97SAlex Deucher (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
557aaa36a97SAlex Deucher return;
558aaa36a97SAlex Deucher
559aaa36a97SAlex Deucher if (bpc == 0)
560aaa36a97SAlex Deucher return;
561aaa36a97SAlex Deucher
562aaa36a97SAlex Deucher switch (bpc) {
563aaa36a97SAlex Deucher case 6:
564aaa36a97SAlex Deucher if (dither == AMDGPU_FMT_DITHER_ENABLE) {
565aaa36a97SAlex Deucher /* XXX sort out optimal dither settings */
566aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
567aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
568aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
569aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
570aaa36a97SAlex Deucher } else {
571aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
572aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
573aaa36a97SAlex Deucher }
574aaa36a97SAlex Deucher break;
575aaa36a97SAlex Deucher case 8:
576aaa36a97SAlex Deucher if (dither == AMDGPU_FMT_DITHER_ENABLE) {
577aaa36a97SAlex Deucher /* XXX sort out optimal dither settings */
578aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
579aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
580aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
581aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
582aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
583aaa36a97SAlex Deucher } else {
584aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
585aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
586aaa36a97SAlex Deucher }
587aaa36a97SAlex Deucher break;
588aaa36a97SAlex Deucher case 10:
589aaa36a97SAlex Deucher if (dither == AMDGPU_FMT_DITHER_ENABLE) {
590aaa36a97SAlex Deucher /* XXX sort out optimal dither settings */
591aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
592aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
593aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
594aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
595aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
596aaa36a97SAlex Deucher } else {
597aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
598aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
599aaa36a97SAlex Deucher }
600aaa36a97SAlex Deucher break;
601aaa36a97SAlex Deucher default:
602aaa36a97SAlex Deucher /* not needed */
603aaa36a97SAlex Deucher break;
604aaa36a97SAlex Deucher }
605aaa36a97SAlex Deucher
606aaa36a97SAlex Deucher WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
607aaa36a97SAlex Deucher }
608aaa36a97SAlex Deucher
609aaa36a97SAlex Deucher
610aaa36a97SAlex Deucher /* display watermark setup */
611aaa36a97SAlex Deucher /**
612aaa36a97SAlex Deucher * dce_v11_0_line_buffer_adjust - Set up the line buffer
613aaa36a97SAlex Deucher *
614aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
615aaa36a97SAlex Deucher * @amdgpu_crtc: the selected display controller
616aaa36a97SAlex Deucher * @mode: the current display mode on the selected display
617aaa36a97SAlex Deucher * controller
618aaa36a97SAlex Deucher *
619aaa36a97SAlex Deucher * Setup up the line buffer allocation for
620aaa36a97SAlex Deucher * the selected display controller (CIK).
621aaa36a97SAlex Deucher * Returns the line buffer size in pixels.
622aaa36a97SAlex Deucher */
dce_v11_0_line_buffer_adjust(struct amdgpu_device * adev,struct amdgpu_crtc * amdgpu_crtc,struct drm_display_mode * mode)623aaa36a97SAlex Deucher static u32 dce_v11_0_line_buffer_adjust(struct amdgpu_device *adev,
624aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc,
625aaa36a97SAlex Deucher struct drm_display_mode *mode)
626aaa36a97SAlex Deucher {
627aaa36a97SAlex Deucher u32 tmp, buffer_alloc, i, mem_cfg;
628aaa36a97SAlex Deucher u32 pipe_offset = amdgpu_crtc->crtc_id;
629aaa36a97SAlex Deucher /*
630aaa36a97SAlex Deucher * Line Buffer Setup
631aaa36a97SAlex Deucher * There are 6 line buffers, one for each display controllers.
632aaa36a97SAlex Deucher * There are 3 partitions per LB. Select the number of partitions
633aaa36a97SAlex Deucher * to enable based on the display width. For display widths larger
634aaa36a97SAlex Deucher * than 4096, you need use to use 2 display controllers and combine
635aaa36a97SAlex Deucher * them using the stereo blender.
636aaa36a97SAlex Deucher */
637aaa36a97SAlex Deucher if (amdgpu_crtc->base.enabled && mode) {
638aaa36a97SAlex Deucher if (mode->crtc_hdisplay < 1920) {
639aaa36a97SAlex Deucher mem_cfg = 1;
640aaa36a97SAlex Deucher buffer_alloc = 2;
641aaa36a97SAlex Deucher } else if (mode->crtc_hdisplay < 2560) {
642aaa36a97SAlex Deucher mem_cfg = 2;
643aaa36a97SAlex Deucher buffer_alloc = 2;
644aaa36a97SAlex Deucher } else if (mode->crtc_hdisplay < 4096) {
645aaa36a97SAlex Deucher mem_cfg = 0;
6462f7d10b3SJammy Zhou buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
647aaa36a97SAlex Deucher } else {
648aaa36a97SAlex Deucher DRM_DEBUG_KMS("Mode too big for LB!\n");
649aaa36a97SAlex Deucher mem_cfg = 0;
6502f7d10b3SJammy Zhou buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
651aaa36a97SAlex Deucher }
652aaa36a97SAlex Deucher } else {
653aaa36a97SAlex Deucher mem_cfg = 1;
654aaa36a97SAlex Deucher buffer_alloc = 0;
655aaa36a97SAlex Deucher }
656aaa36a97SAlex Deucher
657aaa36a97SAlex Deucher tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
658aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
659aaa36a97SAlex Deucher WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
660aaa36a97SAlex Deucher
661aaa36a97SAlex Deucher tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
662aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
663aaa36a97SAlex Deucher WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
664aaa36a97SAlex Deucher
665aaa36a97SAlex Deucher for (i = 0; i < adev->usec_timeout; i++) {
666aaa36a97SAlex Deucher tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
667aaa36a97SAlex Deucher if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
668aaa36a97SAlex Deucher break;
669aaa36a97SAlex Deucher udelay(1);
670aaa36a97SAlex Deucher }
671aaa36a97SAlex Deucher
672aaa36a97SAlex Deucher if (amdgpu_crtc->base.enabled && mode) {
673aaa36a97SAlex Deucher switch (mem_cfg) {
674aaa36a97SAlex Deucher case 0:
675aaa36a97SAlex Deucher default:
676aaa36a97SAlex Deucher return 4096 * 2;
677aaa36a97SAlex Deucher case 1:
678aaa36a97SAlex Deucher return 1920 * 2;
679aaa36a97SAlex Deucher case 2:
680aaa36a97SAlex Deucher return 2560 * 2;
681aaa36a97SAlex Deucher }
682aaa36a97SAlex Deucher }
683aaa36a97SAlex Deucher
684aaa36a97SAlex Deucher /* controller not enabled, so no lb used */
685aaa36a97SAlex Deucher return 0;
686aaa36a97SAlex Deucher }
687aaa36a97SAlex Deucher
688aaa36a97SAlex Deucher /**
689aaa36a97SAlex Deucher * cik_get_number_of_dram_channels - get the number of dram channels
690aaa36a97SAlex Deucher *
691aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
692aaa36a97SAlex Deucher *
693aaa36a97SAlex Deucher * Look up the number of video ram channels (CIK).
694aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
695aaa36a97SAlex Deucher * Returns the number of dram channels
696aaa36a97SAlex Deucher */
cik_get_number_of_dram_channels(struct amdgpu_device * adev)697aaa36a97SAlex Deucher static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
698aaa36a97SAlex Deucher {
699aaa36a97SAlex Deucher u32 tmp = RREG32(mmMC_SHARED_CHMAP);
700aaa36a97SAlex Deucher
701aaa36a97SAlex Deucher switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
702aaa36a97SAlex Deucher case 0:
703aaa36a97SAlex Deucher default:
704aaa36a97SAlex Deucher return 1;
705aaa36a97SAlex Deucher case 1:
706aaa36a97SAlex Deucher return 2;
707aaa36a97SAlex Deucher case 2:
708aaa36a97SAlex Deucher return 4;
709aaa36a97SAlex Deucher case 3:
710aaa36a97SAlex Deucher return 8;
711aaa36a97SAlex Deucher case 4:
712aaa36a97SAlex Deucher return 3;
713aaa36a97SAlex Deucher case 5:
714aaa36a97SAlex Deucher return 6;
715aaa36a97SAlex Deucher case 6:
716aaa36a97SAlex Deucher return 10;
717aaa36a97SAlex Deucher case 7:
718aaa36a97SAlex Deucher return 12;
719aaa36a97SAlex Deucher case 8:
720aaa36a97SAlex Deucher return 16;
721aaa36a97SAlex Deucher }
722aaa36a97SAlex Deucher }
723aaa36a97SAlex Deucher
724aaa36a97SAlex Deucher struct dce10_wm_params {
725aaa36a97SAlex Deucher u32 dram_channels; /* number of dram channels */
726aaa36a97SAlex Deucher u32 yclk; /* bandwidth per dram data pin in kHz */
727aaa36a97SAlex Deucher u32 sclk; /* engine clock in kHz */
728aaa36a97SAlex Deucher u32 disp_clk; /* display clock in kHz */
729aaa36a97SAlex Deucher u32 src_width; /* viewport width */
730aaa36a97SAlex Deucher u32 active_time; /* active display time in ns */
731aaa36a97SAlex Deucher u32 blank_time; /* blank time in ns */
732aaa36a97SAlex Deucher bool interlaced; /* mode is interlaced */
733aaa36a97SAlex Deucher fixed20_12 vsc; /* vertical scale ratio */
734aaa36a97SAlex Deucher u32 num_heads; /* number of active crtcs */
735aaa36a97SAlex Deucher u32 bytes_per_pixel; /* bytes per pixel display + overlay */
736aaa36a97SAlex Deucher u32 lb_size; /* line buffer allocated to pipe */
737aaa36a97SAlex Deucher u32 vtaps; /* vertical scaler taps */
738aaa36a97SAlex Deucher };
739aaa36a97SAlex Deucher
740aaa36a97SAlex Deucher /**
741aaa36a97SAlex Deucher * dce_v11_0_dram_bandwidth - get the dram bandwidth
742aaa36a97SAlex Deucher *
743aaa36a97SAlex Deucher * @wm: watermark calculation data
744aaa36a97SAlex Deucher *
745aaa36a97SAlex Deucher * Calculate the raw dram bandwidth (CIK).
746aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
747aaa36a97SAlex Deucher * Returns the dram bandwidth in MBytes/s
748aaa36a97SAlex Deucher */
dce_v11_0_dram_bandwidth(struct dce10_wm_params * wm)749aaa36a97SAlex Deucher static u32 dce_v11_0_dram_bandwidth(struct dce10_wm_params *wm)
750aaa36a97SAlex Deucher {
751aaa36a97SAlex Deucher /* Calculate raw DRAM Bandwidth */
752aaa36a97SAlex Deucher fixed20_12 dram_efficiency; /* 0.7 */
753aaa36a97SAlex Deucher fixed20_12 yclk, dram_channels, bandwidth;
754aaa36a97SAlex Deucher fixed20_12 a;
755aaa36a97SAlex Deucher
756aaa36a97SAlex Deucher a.full = dfixed_const(1000);
757aaa36a97SAlex Deucher yclk.full = dfixed_const(wm->yclk);
758aaa36a97SAlex Deucher yclk.full = dfixed_div(yclk, a);
759aaa36a97SAlex Deucher dram_channels.full = dfixed_const(wm->dram_channels * 4);
760aaa36a97SAlex Deucher a.full = dfixed_const(10);
761aaa36a97SAlex Deucher dram_efficiency.full = dfixed_const(7);
762aaa36a97SAlex Deucher dram_efficiency.full = dfixed_div(dram_efficiency, a);
763aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(dram_channels, yclk);
764aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
765aaa36a97SAlex Deucher
766aaa36a97SAlex Deucher return dfixed_trunc(bandwidth);
767aaa36a97SAlex Deucher }
768aaa36a97SAlex Deucher
769aaa36a97SAlex Deucher /**
770aaa36a97SAlex Deucher * dce_v11_0_dram_bandwidth_for_display - get the dram bandwidth for display
771aaa36a97SAlex Deucher *
772aaa36a97SAlex Deucher * @wm: watermark calculation data
773aaa36a97SAlex Deucher *
774aaa36a97SAlex Deucher * Calculate the dram bandwidth used for display (CIK).
775aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
776aaa36a97SAlex Deucher * Returns the dram bandwidth for display in MBytes/s
777aaa36a97SAlex Deucher */
dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params * wm)778aaa36a97SAlex Deucher static u32 dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
779aaa36a97SAlex Deucher {
780aaa36a97SAlex Deucher /* Calculate DRAM Bandwidth and the part allocated to display. */
781aaa36a97SAlex Deucher fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
782aaa36a97SAlex Deucher fixed20_12 yclk, dram_channels, bandwidth;
783aaa36a97SAlex Deucher fixed20_12 a;
784aaa36a97SAlex Deucher
785aaa36a97SAlex Deucher a.full = dfixed_const(1000);
786aaa36a97SAlex Deucher yclk.full = dfixed_const(wm->yclk);
787aaa36a97SAlex Deucher yclk.full = dfixed_div(yclk, a);
788aaa36a97SAlex Deucher dram_channels.full = dfixed_const(wm->dram_channels * 4);
789aaa36a97SAlex Deucher a.full = dfixed_const(10);
790aaa36a97SAlex Deucher disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
791aaa36a97SAlex Deucher disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
792aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(dram_channels, yclk);
793aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
794aaa36a97SAlex Deucher
795aaa36a97SAlex Deucher return dfixed_trunc(bandwidth);
796aaa36a97SAlex Deucher }
797aaa36a97SAlex Deucher
798aaa36a97SAlex Deucher /**
799aaa36a97SAlex Deucher * dce_v11_0_data_return_bandwidth - get the data return bandwidth
800aaa36a97SAlex Deucher *
801aaa36a97SAlex Deucher * @wm: watermark calculation data
802aaa36a97SAlex Deucher *
803aaa36a97SAlex Deucher * Calculate the data return bandwidth used for display (CIK).
804aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
805aaa36a97SAlex Deucher * Returns the data return bandwidth in MBytes/s
806aaa36a97SAlex Deucher */
dce_v11_0_data_return_bandwidth(struct dce10_wm_params * wm)807aaa36a97SAlex Deucher static u32 dce_v11_0_data_return_bandwidth(struct dce10_wm_params *wm)
808aaa36a97SAlex Deucher {
809aaa36a97SAlex Deucher /* Calculate the display Data return Bandwidth */
810aaa36a97SAlex Deucher fixed20_12 return_efficiency; /* 0.8 */
811aaa36a97SAlex Deucher fixed20_12 sclk, bandwidth;
812aaa36a97SAlex Deucher fixed20_12 a;
813aaa36a97SAlex Deucher
814aaa36a97SAlex Deucher a.full = dfixed_const(1000);
815aaa36a97SAlex Deucher sclk.full = dfixed_const(wm->sclk);
816aaa36a97SAlex Deucher sclk.full = dfixed_div(sclk, a);
817aaa36a97SAlex Deucher a.full = dfixed_const(10);
818aaa36a97SAlex Deucher return_efficiency.full = dfixed_const(8);
819aaa36a97SAlex Deucher return_efficiency.full = dfixed_div(return_efficiency, a);
820aaa36a97SAlex Deucher a.full = dfixed_const(32);
821aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(a, sclk);
822aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
823aaa36a97SAlex Deucher
824aaa36a97SAlex Deucher return dfixed_trunc(bandwidth);
825aaa36a97SAlex Deucher }
826aaa36a97SAlex Deucher
827aaa36a97SAlex Deucher /**
828aaa36a97SAlex Deucher * dce_v11_0_dmif_request_bandwidth - get the dmif bandwidth
829aaa36a97SAlex Deucher *
830aaa36a97SAlex Deucher * @wm: watermark calculation data
831aaa36a97SAlex Deucher *
832aaa36a97SAlex Deucher * Calculate the dmif bandwidth used for display (CIK).
833aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
834aaa36a97SAlex Deucher * Returns the dmif bandwidth in MBytes/s
835aaa36a97SAlex Deucher */
dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params * wm)836aaa36a97SAlex Deucher static u32 dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
837aaa36a97SAlex Deucher {
838aaa36a97SAlex Deucher /* Calculate the DMIF Request Bandwidth */
839aaa36a97SAlex Deucher fixed20_12 disp_clk_request_efficiency; /* 0.8 */
840aaa36a97SAlex Deucher fixed20_12 disp_clk, bandwidth;
841aaa36a97SAlex Deucher fixed20_12 a, b;
842aaa36a97SAlex Deucher
843aaa36a97SAlex Deucher a.full = dfixed_const(1000);
844aaa36a97SAlex Deucher disp_clk.full = dfixed_const(wm->disp_clk);
845aaa36a97SAlex Deucher disp_clk.full = dfixed_div(disp_clk, a);
846aaa36a97SAlex Deucher a.full = dfixed_const(32);
847aaa36a97SAlex Deucher b.full = dfixed_mul(a, disp_clk);
848aaa36a97SAlex Deucher
849aaa36a97SAlex Deucher a.full = dfixed_const(10);
850aaa36a97SAlex Deucher disp_clk_request_efficiency.full = dfixed_const(8);
851aaa36a97SAlex Deucher disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
852aaa36a97SAlex Deucher
853aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
854aaa36a97SAlex Deucher
855aaa36a97SAlex Deucher return dfixed_trunc(bandwidth);
856aaa36a97SAlex Deucher }
857aaa36a97SAlex Deucher
858aaa36a97SAlex Deucher /**
859aaa36a97SAlex Deucher * dce_v11_0_available_bandwidth - get the min available bandwidth
860aaa36a97SAlex Deucher *
861aaa36a97SAlex Deucher * @wm: watermark calculation data
862aaa36a97SAlex Deucher *
863aaa36a97SAlex Deucher * Calculate the min available bandwidth used for display (CIK).
864aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
865aaa36a97SAlex Deucher * Returns the min available bandwidth in MBytes/s
866aaa36a97SAlex Deucher */
dce_v11_0_available_bandwidth(struct dce10_wm_params * wm)867aaa36a97SAlex Deucher static u32 dce_v11_0_available_bandwidth(struct dce10_wm_params *wm)
868aaa36a97SAlex Deucher {
869aaa36a97SAlex Deucher /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
870aaa36a97SAlex Deucher u32 dram_bandwidth = dce_v11_0_dram_bandwidth(wm);
871aaa36a97SAlex Deucher u32 data_return_bandwidth = dce_v11_0_data_return_bandwidth(wm);
872aaa36a97SAlex Deucher u32 dmif_req_bandwidth = dce_v11_0_dmif_request_bandwidth(wm);
873aaa36a97SAlex Deucher
874aaa36a97SAlex Deucher return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
875aaa36a97SAlex Deucher }
876aaa36a97SAlex Deucher
877aaa36a97SAlex Deucher /**
878aaa36a97SAlex Deucher * dce_v11_0_average_bandwidth - get the average available bandwidth
879aaa36a97SAlex Deucher *
880aaa36a97SAlex Deucher * @wm: watermark calculation data
881aaa36a97SAlex Deucher *
882aaa36a97SAlex Deucher * Calculate the average available bandwidth used for display (CIK).
883aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
884aaa36a97SAlex Deucher * Returns the average available bandwidth in MBytes/s
885aaa36a97SAlex Deucher */
dce_v11_0_average_bandwidth(struct dce10_wm_params * wm)886aaa36a97SAlex Deucher static u32 dce_v11_0_average_bandwidth(struct dce10_wm_params *wm)
887aaa36a97SAlex Deucher {
888aaa36a97SAlex Deucher /* Calculate the display mode Average Bandwidth
889aaa36a97SAlex Deucher * DisplayMode should contain the source and destination dimensions,
890aaa36a97SAlex Deucher * timing, etc.
891aaa36a97SAlex Deucher */
892aaa36a97SAlex Deucher fixed20_12 bpp;
893aaa36a97SAlex Deucher fixed20_12 line_time;
894aaa36a97SAlex Deucher fixed20_12 src_width;
895aaa36a97SAlex Deucher fixed20_12 bandwidth;
896aaa36a97SAlex Deucher fixed20_12 a;
897aaa36a97SAlex Deucher
898aaa36a97SAlex Deucher a.full = dfixed_const(1000);
899aaa36a97SAlex Deucher line_time.full = dfixed_const(wm->active_time + wm->blank_time);
900aaa36a97SAlex Deucher line_time.full = dfixed_div(line_time, a);
901aaa36a97SAlex Deucher bpp.full = dfixed_const(wm->bytes_per_pixel);
902aaa36a97SAlex Deucher src_width.full = dfixed_const(wm->src_width);
903aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(src_width, bpp);
904aaa36a97SAlex Deucher bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
905aaa36a97SAlex Deucher bandwidth.full = dfixed_div(bandwidth, line_time);
906aaa36a97SAlex Deucher
907aaa36a97SAlex Deucher return dfixed_trunc(bandwidth);
908aaa36a97SAlex Deucher }
909aaa36a97SAlex Deucher
910aaa36a97SAlex Deucher /**
911aaa36a97SAlex Deucher * dce_v11_0_latency_watermark - get the latency watermark
912aaa36a97SAlex Deucher *
913aaa36a97SAlex Deucher * @wm: watermark calculation data
914aaa36a97SAlex Deucher *
915aaa36a97SAlex Deucher * Calculate the latency watermark (CIK).
916aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
917aaa36a97SAlex Deucher * Returns the latency watermark in ns
918aaa36a97SAlex Deucher */
dce_v11_0_latency_watermark(struct dce10_wm_params * wm)919aaa36a97SAlex Deucher static u32 dce_v11_0_latency_watermark(struct dce10_wm_params *wm)
920aaa36a97SAlex Deucher {
921aaa36a97SAlex Deucher /* First calculate the latency in ns */
922aaa36a97SAlex Deucher u32 mc_latency = 2000; /* 2000 ns. */
923aaa36a97SAlex Deucher u32 available_bandwidth = dce_v11_0_available_bandwidth(wm);
924aaa36a97SAlex Deucher u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
925aaa36a97SAlex Deucher u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
926aaa36a97SAlex Deucher u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
927aaa36a97SAlex Deucher u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
928aaa36a97SAlex Deucher (wm->num_heads * cursor_line_pair_return_time);
929aaa36a97SAlex Deucher u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
930aaa36a97SAlex Deucher u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
931aaa36a97SAlex Deucher u32 tmp, dmif_size = 12288;
932aaa36a97SAlex Deucher fixed20_12 a, b, c;
933aaa36a97SAlex Deucher
934aaa36a97SAlex Deucher if (wm->num_heads == 0)
935aaa36a97SAlex Deucher return 0;
936aaa36a97SAlex Deucher
937aaa36a97SAlex Deucher a.full = dfixed_const(2);
938aaa36a97SAlex Deucher b.full = dfixed_const(1);
939aaa36a97SAlex Deucher if ((wm->vsc.full > a.full) ||
940aaa36a97SAlex Deucher ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
941aaa36a97SAlex Deucher (wm->vtaps >= 5) ||
942aaa36a97SAlex Deucher ((wm->vsc.full >= a.full) && wm->interlaced))
943aaa36a97SAlex Deucher max_src_lines_per_dst_line = 4;
944aaa36a97SAlex Deucher else
945aaa36a97SAlex Deucher max_src_lines_per_dst_line = 2;
946aaa36a97SAlex Deucher
947aaa36a97SAlex Deucher a.full = dfixed_const(available_bandwidth);
948aaa36a97SAlex Deucher b.full = dfixed_const(wm->num_heads);
949aaa36a97SAlex Deucher a.full = dfixed_div(a, b);
950e190ed1eSMario Kleiner tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
951e190ed1eSMario Kleiner tmp = min(dfixed_trunc(a), tmp);
952aaa36a97SAlex Deucher
953e190ed1eSMario Kleiner lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
954aaa36a97SAlex Deucher
955aaa36a97SAlex Deucher a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
956aaa36a97SAlex Deucher b.full = dfixed_const(1000);
957aaa36a97SAlex Deucher c.full = dfixed_const(lb_fill_bw);
958aaa36a97SAlex Deucher b.full = dfixed_div(c, b);
959aaa36a97SAlex Deucher a.full = dfixed_div(a, b);
960aaa36a97SAlex Deucher line_fill_time = dfixed_trunc(a);
961aaa36a97SAlex Deucher
962aaa36a97SAlex Deucher if (line_fill_time < wm->active_time)
963aaa36a97SAlex Deucher return latency;
964aaa36a97SAlex Deucher else
965aaa36a97SAlex Deucher return latency + (line_fill_time - wm->active_time);
966aaa36a97SAlex Deucher
967aaa36a97SAlex Deucher }
968aaa36a97SAlex Deucher
969aaa36a97SAlex Deucher /**
970aaa36a97SAlex Deucher * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
971aaa36a97SAlex Deucher * average and available dram bandwidth
972aaa36a97SAlex Deucher *
973aaa36a97SAlex Deucher * @wm: watermark calculation data
974aaa36a97SAlex Deucher *
975aaa36a97SAlex Deucher * Check if the display average bandwidth fits in the display
976aaa36a97SAlex Deucher * dram bandwidth (CIK).
977aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
978aaa36a97SAlex Deucher * Returns true if the display fits, false if not.
979aaa36a97SAlex Deucher */
dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params * wm)980aaa36a97SAlex Deucher static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
981aaa36a97SAlex Deucher {
982aaa36a97SAlex Deucher if (dce_v11_0_average_bandwidth(wm) <=
983aaa36a97SAlex Deucher (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
984aaa36a97SAlex Deucher return true;
985aaa36a97SAlex Deucher else
986aaa36a97SAlex Deucher return false;
987aaa36a97SAlex Deucher }
988aaa36a97SAlex Deucher
989aaa36a97SAlex Deucher /**
990aaa36a97SAlex Deucher * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
991aaa36a97SAlex Deucher * average and available bandwidth
992aaa36a97SAlex Deucher *
993aaa36a97SAlex Deucher * @wm: watermark calculation data
994aaa36a97SAlex Deucher *
995aaa36a97SAlex Deucher * Check if the display average bandwidth fits in the display
996aaa36a97SAlex Deucher * available bandwidth (CIK).
997aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
998aaa36a97SAlex Deucher * Returns true if the display fits, false if not.
999aaa36a97SAlex Deucher */
dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params * wm)1000aaa36a97SAlex Deucher static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
1001aaa36a97SAlex Deucher {
1002aaa36a97SAlex Deucher if (dce_v11_0_average_bandwidth(wm) <=
1003aaa36a97SAlex Deucher (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
1004aaa36a97SAlex Deucher return true;
1005aaa36a97SAlex Deucher else
1006aaa36a97SAlex Deucher return false;
1007aaa36a97SAlex Deucher }
1008aaa36a97SAlex Deucher
1009aaa36a97SAlex Deucher /**
1010aaa36a97SAlex Deucher * dce_v11_0_check_latency_hiding - check latency hiding
1011aaa36a97SAlex Deucher *
1012aaa36a97SAlex Deucher * @wm: watermark calculation data
1013aaa36a97SAlex Deucher *
1014aaa36a97SAlex Deucher * Check latency hiding (CIK).
1015aaa36a97SAlex Deucher * Used for display watermark bandwidth calculations
1016aaa36a97SAlex Deucher * Returns true if the display fits, false if not.
1017aaa36a97SAlex Deucher */
dce_v11_0_check_latency_hiding(struct dce10_wm_params * wm)1018aaa36a97SAlex Deucher static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
1019aaa36a97SAlex Deucher {
1020aaa36a97SAlex Deucher u32 lb_partitions = wm->lb_size / wm->src_width;
1021aaa36a97SAlex Deucher u32 line_time = wm->active_time + wm->blank_time;
1022aaa36a97SAlex Deucher u32 latency_tolerant_lines;
1023aaa36a97SAlex Deucher u32 latency_hiding;
1024aaa36a97SAlex Deucher fixed20_12 a;
1025aaa36a97SAlex Deucher
1026aaa36a97SAlex Deucher a.full = dfixed_const(1);
1027aaa36a97SAlex Deucher if (wm->vsc.full > a.full)
1028aaa36a97SAlex Deucher latency_tolerant_lines = 1;
1029aaa36a97SAlex Deucher else {
1030aaa36a97SAlex Deucher if (lb_partitions <= (wm->vtaps + 1))
1031aaa36a97SAlex Deucher latency_tolerant_lines = 1;
1032aaa36a97SAlex Deucher else
1033aaa36a97SAlex Deucher latency_tolerant_lines = 2;
1034aaa36a97SAlex Deucher }
1035aaa36a97SAlex Deucher
1036aaa36a97SAlex Deucher latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
1037aaa36a97SAlex Deucher
1038aaa36a97SAlex Deucher if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
1039aaa36a97SAlex Deucher return true;
1040aaa36a97SAlex Deucher else
1041aaa36a97SAlex Deucher return false;
1042aaa36a97SAlex Deucher }
1043aaa36a97SAlex Deucher
1044aaa36a97SAlex Deucher /**
1045aaa36a97SAlex Deucher * dce_v11_0_program_watermarks - program display watermarks
1046aaa36a97SAlex Deucher *
1047aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
1048aaa36a97SAlex Deucher * @amdgpu_crtc: the selected display controller
1049aaa36a97SAlex Deucher * @lb_size: line buffer size
1050aaa36a97SAlex Deucher * @num_heads: number of display controllers in use
1051aaa36a97SAlex Deucher *
1052aaa36a97SAlex Deucher * Calculate and program the display watermarks for the
1053aaa36a97SAlex Deucher * selected display controller (CIK).
1054aaa36a97SAlex Deucher */
dce_v11_0_program_watermarks(struct amdgpu_device * adev,struct amdgpu_crtc * amdgpu_crtc,u32 lb_size,u32 num_heads)1055aaa36a97SAlex Deucher static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
1056aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc,
1057aaa36a97SAlex Deucher u32 lb_size, u32 num_heads)
1058aaa36a97SAlex Deucher {
1059aaa36a97SAlex Deucher struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
1060aaa36a97SAlex Deucher struct dce10_wm_params wm_low, wm_high;
1061d63c277dSMario Kleiner u32 active_time;
1062aaa36a97SAlex Deucher u32 line_time = 0;
1063aaa36a97SAlex Deucher u32 latency_watermark_a = 0, latency_watermark_b = 0;
10648e36f9d3SAlex Deucher u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
1065aaa36a97SAlex Deucher
1066aaa36a97SAlex Deucher if (amdgpu_crtc->base.enabled && num_heads && mode) {
1067bea10413SMario Kleiner active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
1068bea10413SMario Kleiner (u32)mode->clock);
1069bea10413SMario Kleiner line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
1070bea10413SMario Kleiner (u32)mode->clock);
1071bea10413SMario Kleiner line_time = min(line_time, (u32)65535);
1072aaa36a97SAlex Deucher
1073aaa36a97SAlex Deucher /* watermark for high clocks */
1074aaa36a97SAlex Deucher if (adev->pm.dpm_enabled) {
1075aaa36a97SAlex Deucher wm_high.yclk =
1076aaa36a97SAlex Deucher amdgpu_dpm_get_mclk(adev, false) * 10;
1077aaa36a97SAlex Deucher wm_high.sclk =
1078aaa36a97SAlex Deucher amdgpu_dpm_get_sclk(adev, false) * 10;
1079aaa36a97SAlex Deucher } else {
1080aaa36a97SAlex Deucher wm_high.yclk = adev->pm.current_mclk * 10;
1081aaa36a97SAlex Deucher wm_high.sclk = adev->pm.current_sclk * 10;
1082aaa36a97SAlex Deucher }
1083aaa36a97SAlex Deucher
1084aaa36a97SAlex Deucher wm_high.disp_clk = mode->clock;
1085aaa36a97SAlex Deucher wm_high.src_width = mode->crtc_hdisplay;
1086d63c277dSMario Kleiner wm_high.active_time = active_time;
1087aaa36a97SAlex Deucher wm_high.blank_time = line_time - wm_high.active_time;
1088aaa36a97SAlex Deucher wm_high.interlaced = false;
1089aaa36a97SAlex Deucher if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1090aaa36a97SAlex Deucher wm_high.interlaced = true;
1091aaa36a97SAlex Deucher wm_high.vsc = amdgpu_crtc->vsc;
1092aaa36a97SAlex Deucher wm_high.vtaps = 1;
1093aaa36a97SAlex Deucher if (amdgpu_crtc->rmx_type != RMX_OFF)
1094aaa36a97SAlex Deucher wm_high.vtaps = 2;
1095aaa36a97SAlex Deucher wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1096aaa36a97SAlex Deucher wm_high.lb_size = lb_size;
1097aaa36a97SAlex Deucher wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1098aaa36a97SAlex Deucher wm_high.num_heads = num_heads;
1099aaa36a97SAlex Deucher
1100aaa36a97SAlex Deucher /* set for high clocks */
1101aaa36a97SAlex Deucher latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
1102aaa36a97SAlex Deucher
1103aaa36a97SAlex Deucher /* possibly force display priority to high */
1104aaa36a97SAlex Deucher /* should really do this at mode validation time... */
1105aaa36a97SAlex Deucher if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1106aaa36a97SAlex Deucher !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1107aaa36a97SAlex Deucher !dce_v11_0_check_latency_hiding(&wm_high) ||
1108aaa36a97SAlex Deucher (adev->mode_info.disp_priority == 2)) {
1109aaa36a97SAlex Deucher DRM_DEBUG_KMS("force priority to high\n");
1110aaa36a97SAlex Deucher }
1111aaa36a97SAlex Deucher
1112aaa36a97SAlex Deucher /* watermark for low clocks */
1113aaa36a97SAlex Deucher if (adev->pm.dpm_enabled) {
1114aaa36a97SAlex Deucher wm_low.yclk =
1115aaa36a97SAlex Deucher amdgpu_dpm_get_mclk(adev, true) * 10;
1116aaa36a97SAlex Deucher wm_low.sclk =
1117aaa36a97SAlex Deucher amdgpu_dpm_get_sclk(adev, true) * 10;
1118aaa36a97SAlex Deucher } else {
1119aaa36a97SAlex Deucher wm_low.yclk = adev->pm.current_mclk * 10;
1120aaa36a97SAlex Deucher wm_low.sclk = adev->pm.current_sclk * 10;
1121aaa36a97SAlex Deucher }
1122aaa36a97SAlex Deucher
1123aaa36a97SAlex Deucher wm_low.disp_clk = mode->clock;
1124aaa36a97SAlex Deucher wm_low.src_width = mode->crtc_hdisplay;
1125d63c277dSMario Kleiner wm_low.active_time = active_time;
1126aaa36a97SAlex Deucher wm_low.blank_time = line_time - wm_low.active_time;
1127aaa36a97SAlex Deucher wm_low.interlaced = false;
1128aaa36a97SAlex Deucher if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1129aaa36a97SAlex Deucher wm_low.interlaced = true;
1130aaa36a97SAlex Deucher wm_low.vsc = amdgpu_crtc->vsc;
1131aaa36a97SAlex Deucher wm_low.vtaps = 1;
1132aaa36a97SAlex Deucher if (amdgpu_crtc->rmx_type != RMX_OFF)
1133aaa36a97SAlex Deucher wm_low.vtaps = 2;
1134aaa36a97SAlex Deucher wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1135aaa36a97SAlex Deucher wm_low.lb_size = lb_size;
1136aaa36a97SAlex Deucher wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1137aaa36a97SAlex Deucher wm_low.num_heads = num_heads;
1138aaa36a97SAlex Deucher
1139aaa36a97SAlex Deucher /* set for low clocks */
1140aaa36a97SAlex Deucher latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
1141aaa36a97SAlex Deucher
1142aaa36a97SAlex Deucher /* possibly force display priority to high */
1143aaa36a97SAlex Deucher /* should really do this at mode validation time... */
1144aaa36a97SAlex Deucher if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1145aaa36a97SAlex Deucher !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1146aaa36a97SAlex Deucher !dce_v11_0_check_latency_hiding(&wm_low) ||
1147aaa36a97SAlex Deucher (adev->mode_info.disp_priority == 2)) {
1148aaa36a97SAlex Deucher DRM_DEBUG_KMS("force priority to high\n");
1149aaa36a97SAlex Deucher }
11508e36f9d3SAlex Deucher lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
1151aaa36a97SAlex Deucher }
1152aaa36a97SAlex Deucher
1153aaa36a97SAlex Deucher /* select wm A */
1154aaa36a97SAlex Deucher wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1155aaa36a97SAlex Deucher tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
1156aaa36a97SAlex Deucher WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1157aaa36a97SAlex Deucher tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1158aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
1159aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1160aaa36a97SAlex Deucher WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1161aaa36a97SAlex Deucher /* select wm B */
1162aaa36a97SAlex Deucher tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
1163aaa36a97SAlex Deucher WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1164aaa36a97SAlex Deucher tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
116599a09238SAlex Deucher tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
1166aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1167aaa36a97SAlex Deucher WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1168aaa36a97SAlex Deucher /* restore original selection */
1169aaa36a97SAlex Deucher WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1170aaa36a97SAlex Deucher
1171aaa36a97SAlex Deucher /* save values for DPM */
1172aaa36a97SAlex Deucher amdgpu_crtc->line_time = line_time;
1173aaa36a97SAlex Deucher amdgpu_crtc->wm_high = latency_watermark_a;
1174aaa36a97SAlex Deucher amdgpu_crtc->wm_low = latency_watermark_b;
11758e36f9d3SAlex Deucher /* Save number of lines the linebuffer leads before the scanout */
11768e36f9d3SAlex Deucher amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
1177aaa36a97SAlex Deucher }
1178aaa36a97SAlex Deucher
1179aaa36a97SAlex Deucher /**
1180aaa36a97SAlex Deucher * dce_v11_0_bandwidth_update - program display watermarks
1181aaa36a97SAlex Deucher *
1182aaa36a97SAlex Deucher * @adev: amdgpu_device pointer
1183aaa36a97SAlex Deucher *
1184aaa36a97SAlex Deucher * Calculate and program the display watermarks and line
1185aaa36a97SAlex Deucher * buffer allocation (CIK).
1186aaa36a97SAlex Deucher */
dce_v11_0_bandwidth_update(struct amdgpu_device * adev)1187aaa36a97SAlex Deucher static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
1188aaa36a97SAlex Deucher {
1189aaa36a97SAlex Deucher struct drm_display_mode *mode = NULL;
1190aaa36a97SAlex Deucher u32 num_heads = 0, lb_size;
1191aaa36a97SAlex Deucher int i;
1192aaa36a97SAlex Deucher
1193166140fbSSamuel Li amdgpu_display_update_priority(adev);
1194aaa36a97SAlex Deucher
1195aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
1196aaa36a97SAlex Deucher if (adev->mode_info.crtcs[i]->base.enabled)
1197aaa36a97SAlex Deucher num_heads++;
1198aaa36a97SAlex Deucher }
1199aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
1200aaa36a97SAlex Deucher mode = &adev->mode_info.crtcs[i]->base.mode;
1201aaa36a97SAlex Deucher lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1202aaa36a97SAlex Deucher dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1203aaa36a97SAlex Deucher lb_size, num_heads);
1204aaa36a97SAlex Deucher }
1205aaa36a97SAlex Deucher }
1206aaa36a97SAlex Deucher
dce_v11_0_audio_get_connected_pins(struct amdgpu_device * adev)1207aaa36a97SAlex Deucher static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
1208aaa36a97SAlex Deucher {
1209aaa36a97SAlex Deucher int i;
1210aaa36a97SAlex Deucher u32 offset, tmp;
1211aaa36a97SAlex Deucher
1212aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1213aaa36a97SAlex Deucher offset = adev->mode_info.audio.pin[i].offset;
1214aaa36a97SAlex Deucher tmp = RREG32_AUDIO_ENDPT(offset,
1215aaa36a97SAlex Deucher ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1216aaa36a97SAlex Deucher if (((tmp &
1217aaa36a97SAlex Deucher AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1218aaa36a97SAlex Deucher AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1219aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].connected = false;
1220aaa36a97SAlex Deucher else
1221aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].connected = true;
1222aaa36a97SAlex Deucher }
1223aaa36a97SAlex Deucher }
1224aaa36a97SAlex Deucher
dce_v11_0_audio_get_pin(struct amdgpu_device * adev)1225aaa36a97SAlex Deucher static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
1226aaa36a97SAlex Deucher {
1227aaa36a97SAlex Deucher int i;
1228aaa36a97SAlex Deucher
1229aaa36a97SAlex Deucher dce_v11_0_audio_get_connected_pins(adev);
1230aaa36a97SAlex Deucher
1231aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1232aaa36a97SAlex Deucher if (adev->mode_info.audio.pin[i].connected)
1233aaa36a97SAlex Deucher return &adev->mode_info.audio.pin[i];
1234aaa36a97SAlex Deucher }
1235aaa36a97SAlex Deucher DRM_ERROR("No connected audio pins found!\n");
1236aaa36a97SAlex Deucher return NULL;
1237aaa36a97SAlex Deucher }
1238aaa36a97SAlex Deucher
dce_v11_0_afmt_audio_select_pin(struct drm_encoder * encoder)1239aaa36a97SAlex Deucher static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1240aaa36a97SAlex Deucher {
12411348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(encoder->dev);
1242aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1243aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1244aaa36a97SAlex Deucher u32 tmp;
1245aaa36a97SAlex Deucher
1246aaa36a97SAlex Deucher if (!dig || !dig->afmt || !dig->afmt->pin)
1247aaa36a97SAlex Deucher return;
1248aaa36a97SAlex Deucher
1249aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
1250aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
1251aaa36a97SAlex Deucher WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
1252aaa36a97SAlex Deucher }
1253aaa36a97SAlex Deucher
dce_v11_0_audio_write_latency_fields(struct drm_encoder * encoder,struct drm_display_mode * mode)1254aaa36a97SAlex Deucher static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
1255aaa36a97SAlex Deucher struct drm_display_mode *mode)
1256aaa36a97SAlex Deucher {
1257f8d2d39eSLyude Paul struct drm_device *dev = encoder->dev;
12581348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1259aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1260aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1261aaa36a97SAlex Deucher struct drm_connector *connector;
1262f8d2d39eSLyude Paul struct drm_connector_list_iter iter;
1263aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = NULL;
1264aaa36a97SAlex Deucher u32 tmp;
1265aaa36a97SAlex Deucher int interlace = 0;
1266aaa36a97SAlex Deucher
1267aaa36a97SAlex Deucher if (!dig || !dig->afmt || !dig->afmt->pin)
1268aaa36a97SAlex Deucher return;
1269aaa36a97SAlex Deucher
1270f8d2d39eSLyude Paul drm_connector_list_iter_begin(dev, &iter);
1271f8d2d39eSLyude Paul drm_for_each_connector_iter(connector, &iter) {
1272aaa36a97SAlex Deucher if (connector->encoder == encoder) {
1273aaa36a97SAlex Deucher amdgpu_connector = to_amdgpu_connector(connector);
1274aaa36a97SAlex Deucher break;
1275aaa36a97SAlex Deucher }
1276aaa36a97SAlex Deucher }
1277f8d2d39eSLyude Paul drm_connector_list_iter_end(&iter);
1278aaa36a97SAlex Deucher
1279aaa36a97SAlex Deucher if (!amdgpu_connector) {
1280aaa36a97SAlex Deucher DRM_ERROR("Couldn't find encoder's connector\n");
1281aaa36a97SAlex Deucher return;
1282aaa36a97SAlex Deucher }
1283aaa36a97SAlex Deucher
1284aaa36a97SAlex Deucher if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1285aaa36a97SAlex Deucher interlace = 1;
1286aaa36a97SAlex Deucher if (connector->latency_present[interlace]) {
1287aaa36a97SAlex Deucher tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1288aaa36a97SAlex Deucher VIDEO_LIPSYNC, connector->video_latency[interlace]);
1289aaa36a97SAlex Deucher tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1290aaa36a97SAlex Deucher AUDIO_LIPSYNC, connector->audio_latency[interlace]);
1291aaa36a97SAlex Deucher } else {
1292aaa36a97SAlex Deucher tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1293aaa36a97SAlex Deucher VIDEO_LIPSYNC, 0);
1294aaa36a97SAlex Deucher tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1295aaa36a97SAlex Deucher AUDIO_LIPSYNC, 0);
1296aaa36a97SAlex Deucher }
1297aaa36a97SAlex Deucher WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1298aaa36a97SAlex Deucher ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1299aaa36a97SAlex Deucher }
1300aaa36a97SAlex Deucher
dce_v11_0_audio_write_speaker_allocation(struct drm_encoder * encoder)1301aaa36a97SAlex Deucher static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1302aaa36a97SAlex Deucher {
1303f8d2d39eSLyude Paul struct drm_device *dev = encoder->dev;
13041348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1305aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1306aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1307aaa36a97SAlex Deucher struct drm_connector *connector;
1308f8d2d39eSLyude Paul struct drm_connector_list_iter iter;
1309aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = NULL;
1310aaa36a97SAlex Deucher u32 tmp;
1311aaa36a97SAlex Deucher u8 *sadb = NULL;
1312aaa36a97SAlex Deucher int sad_count;
1313aaa36a97SAlex Deucher
1314aaa36a97SAlex Deucher if (!dig || !dig->afmt || !dig->afmt->pin)
1315aaa36a97SAlex Deucher return;
1316aaa36a97SAlex Deucher
1317f8d2d39eSLyude Paul drm_connector_list_iter_begin(dev, &iter);
1318f8d2d39eSLyude Paul drm_for_each_connector_iter(connector, &iter) {
1319aaa36a97SAlex Deucher if (connector->encoder == encoder) {
1320aaa36a97SAlex Deucher amdgpu_connector = to_amdgpu_connector(connector);
1321aaa36a97SAlex Deucher break;
1322aaa36a97SAlex Deucher }
1323aaa36a97SAlex Deucher }
1324f8d2d39eSLyude Paul drm_connector_list_iter_end(&iter);
1325aaa36a97SAlex Deucher
1326aaa36a97SAlex Deucher if (!amdgpu_connector) {
1327aaa36a97SAlex Deucher DRM_ERROR("Couldn't find encoder's connector\n");
1328aaa36a97SAlex Deucher return;
1329aaa36a97SAlex Deucher }
1330aaa36a97SAlex Deucher
1331aaa36a97SAlex Deucher sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
1332aaa36a97SAlex Deucher if (sad_count < 0) {
1333aaa36a97SAlex Deucher DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1334aaa36a97SAlex Deucher sad_count = 0;
1335aaa36a97SAlex Deucher }
1336aaa36a97SAlex Deucher
1337aaa36a97SAlex Deucher /* program the speaker allocation */
1338aaa36a97SAlex Deucher tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1339aaa36a97SAlex Deucher ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1340aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1341aaa36a97SAlex Deucher DP_CONNECTION, 0);
1342aaa36a97SAlex Deucher /* set HDMI mode */
1343aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1344aaa36a97SAlex Deucher HDMI_CONNECTION, 1);
1345aaa36a97SAlex Deucher if (sad_count)
1346aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1347aaa36a97SAlex Deucher SPEAKER_ALLOCATION, sadb[0]);
1348aaa36a97SAlex Deucher else
1349aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1350aaa36a97SAlex Deucher SPEAKER_ALLOCATION, 5); /* stereo */
1351aaa36a97SAlex Deucher WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1352aaa36a97SAlex Deucher ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1353aaa36a97SAlex Deucher
1354aaa36a97SAlex Deucher kfree(sadb);
1355aaa36a97SAlex Deucher }
1356aaa36a97SAlex Deucher
dce_v11_0_audio_write_sad_regs(struct drm_encoder * encoder)1357aaa36a97SAlex Deucher static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
1358aaa36a97SAlex Deucher {
1359f8d2d39eSLyude Paul struct drm_device *dev = encoder->dev;
13601348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1361aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1362aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1363aaa36a97SAlex Deucher struct drm_connector *connector;
1364f8d2d39eSLyude Paul struct drm_connector_list_iter iter;
1365aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = NULL;
1366aaa36a97SAlex Deucher struct cea_sad *sads;
1367aaa36a97SAlex Deucher int i, sad_count;
1368aaa36a97SAlex Deucher
1369aaa36a97SAlex Deucher static const u16 eld_reg_to_type[][2] = {
1370aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1371aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1372aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1373aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1374aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1375aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1376aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1377aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1378aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1379aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1380aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1381aaa36a97SAlex Deucher { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1382aaa36a97SAlex Deucher };
1383aaa36a97SAlex Deucher
1384aaa36a97SAlex Deucher if (!dig || !dig->afmt || !dig->afmt->pin)
1385aaa36a97SAlex Deucher return;
1386aaa36a97SAlex Deucher
1387f8d2d39eSLyude Paul drm_connector_list_iter_begin(dev, &iter);
1388f8d2d39eSLyude Paul drm_for_each_connector_iter(connector, &iter) {
1389aaa36a97SAlex Deucher if (connector->encoder == encoder) {
1390aaa36a97SAlex Deucher amdgpu_connector = to_amdgpu_connector(connector);
1391aaa36a97SAlex Deucher break;
1392aaa36a97SAlex Deucher }
1393aaa36a97SAlex Deucher }
1394f8d2d39eSLyude Paul drm_connector_list_iter_end(&iter);
1395aaa36a97SAlex Deucher
1396aaa36a97SAlex Deucher if (!amdgpu_connector) {
1397aaa36a97SAlex Deucher DRM_ERROR("Couldn't find encoder's connector\n");
1398aaa36a97SAlex Deucher return;
1399aaa36a97SAlex Deucher }
1400aaa36a97SAlex Deucher
1401aaa36a97SAlex Deucher sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
1402ae2a3495SJean Delvare if (sad_count < 0)
1403aaa36a97SAlex Deucher DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1404ae2a3495SJean Delvare if (sad_count <= 0)
1405aaa36a97SAlex Deucher return;
1406aaa36a97SAlex Deucher BUG_ON(!sads);
1407aaa36a97SAlex Deucher
1408aaa36a97SAlex Deucher for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1409aaa36a97SAlex Deucher u32 tmp = 0;
1410aaa36a97SAlex Deucher u8 stereo_freqs = 0;
1411aaa36a97SAlex Deucher int max_channels = -1;
1412aaa36a97SAlex Deucher int j;
1413aaa36a97SAlex Deucher
1414aaa36a97SAlex Deucher for (j = 0; j < sad_count; j++) {
1415aaa36a97SAlex Deucher struct cea_sad *sad = &sads[j];
1416aaa36a97SAlex Deucher
1417aaa36a97SAlex Deucher if (sad->format == eld_reg_to_type[i][1]) {
1418aaa36a97SAlex Deucher if (sad->channels > max_channels) {
1419aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1420aaa36a97SAlex Deucher MAX_CHANNELS, sad->channels);
1421aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1422aaa36a97SAlex Deucher DESCRIPTOR_BYTE_2, sad->byte2);
1423aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1424aaa36a97SAlex Deucher SUPPORTED_FREQUENCIES, sad->freq);
1425aaa36a97SAlex Deucher max_channels = sad->channels;
1426aaa36a97SAlex Deucher }
1427aaa36a97SAlex Deucher
1428aaa36a97SAlex Deucher if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1429aaa36a97SAlex Deucher stereo_freqs |= sad->freq;
1430aaa36a97SAlex Deucher else
1431aaa36a97SAlex Deucher break;
1432aaa36a97SAlex Deucher }
1433aaa36a97SAlex Deucher }
1434aaa36a97SAlex Deucher
1435aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1436aaa36a97SAlex Deucher SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
1437aaa36a97SAlex Deucher WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
1438aaa36a97SAlex Deucher }
1439aaa36a97SAlex Deucher
1440aaa36a97SAlex Deucher kfree(sads);
1441aaa36a97SAlex Deucher }
1442aaa36a97SAlex Deucher
dce_v11_0_audio_enable(struct amdgpu_device * adev,struct amdgpu_audio_pin * pin,bool enable)1443aaa36a97SAlex Deucher static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
1444aaa36a97SAlex Deucher struct amdgpu_audio_pin *pin,
1445aaa36a97SAlex Deucher bool enable)
1446aaa36a97SAlex Deucher {
1447aaa36a97SAlex Deucher if (!pin)
1448aaa36a97SAlex Deucher return;
1449aaa36a97SAlex Deucher
1450aaa36a97SAlex Deucher WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1451aaa36a97SAlex Deucher enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1452aaa36a97SAlex Deucher }
1453aaa36a97SAlex Deucher
1454aaa36a97SAlex Deucher static const u32 pin_offsets[] =
1455aaa36a97SAlex Deucher {
1456aaa36a97SAlex Deucher AUD0_REGISTER_OFFSET,
1457aaa36a97SAlex Deucher AUD1_REGISTER_OFFSET,
1458aaa36a97SAlex Deucher AUD2_REGISTER_OFFSET,
1459aaa36a97SAlex Deucher AUD3_REGISTER_OFFSET,
1460aaa36a97SAlex Deucher AUD4_REGISTER_OFFSET,
1461aaa36a97SAlex Deucher AUD5_REGISTER_OFFSET,
1462aaa36a97SAlex Deucher AUD6_REGISTER_OFFSET,
146367b1fcc9SAlex Deucher AUD7_REGISTER_OFFSET,
1464aaa36a97SAlex Deucher };
1465aaa36a97SAlex Deucher
dce_v11_0_audio_init(struct amdgpu_device * adev)1466aaa36a97SAlex Deucher static int dce_v11_0_audio_init(struct amdgpu_device *adev)
1467aaa36a97SAlex Deucher {
1468aaa36a97SAlex Deucher int i;
1469aaa36a97SAlex Deucher
1470aaa36a97SAlex Deucher if (!amdgpu_audio)
1471aaa36a97SAlex Deucher return 0;
1472aaa36a97SAlex Deucher
1473aaa36a97SAlex Deucher adev->mode_info.audio.enabled = true;
1474aaa36a97SAlex Deucher
1475d525eb8dSAlex Deucher switch (adev->asic_type) {
1476d525eb8dSAlex Deucher case CHIP_CARRIZO:
1477d525eb8dSAlex Deucher case CHIP_STONEY:
1478aaa36a97SAlex Deucher adev->mode_info.audio.num_pins = 7;
1479d525eb8dSAlex Deucher break;
14802cc0c0b5SFlora Cui case CHIP_POLARIS10:
1481221adb21SAlex Deucher case CHIP_VEGAM:
1482d525eb8dSAlex Deucher adev->mode_info.audio.num_pins = 8;
1483d525eb8dSAlex Deucher break;
14842cc0c0b5SFlora Cui case CHIP_POLARIS11:
1485c4642a47SJunwei Zhang case CHIP_POLARIS12:
1486d525eb8dSAlex Deucher adev->mode_info.audio.num_pins = 6;
1487d525eb8dSAlex Deucher break;
1488d525eb8dSAlex Deucher default:
1489d525eb8dSAlex Deucher return -EINVAL;
1490d525eb8dSAlex Deucher }
1491aaa36a97SAlex Deucher
1492aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1493aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].channels = -1;
1494aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].rate = -1;
1495aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].bits_per_sample = -1;
1496aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].status_bits = 0;
1497aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].category_code = 0;
1498aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].connected = false;
1499aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1500aaa36a97SAlex Deucher adev->mode_info.audio.pin[i].id = i;
1501aaa36a97SAlex Deucher /* disable audio. it will be set up later */
1502aaa36a97SAlex Deucher /* XXX remove once we switch to ip funcs */
1503aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1504aaa36a97SAlex Deucher }
1505aaa36a97SAlex Deucher
1506aaa36a97SAlex Deucher return 0;
1507aaa36a97SAlex Deucher }
1508aaa36a97SAlex Deucher
dce_v11_0_audio_fini(struct amdgpu_device * adev)1509aaa36a97SAlex Deucher static void dce_v11_0_audio_fini(struct amdgpu_device *adev)
1510aaa36a97SAlex Deucher {
1511aaa36a97SAlex Deucher int i;
1512aaa36a97SAlex Deucher
151329f646dfSTom St Denis if (!amdgpu_audio)
151429f646dfSTom St Denis return;
151529f646dfSTom St Denis
1516aaa36a97SAlex Deucher if (!adev->mode_info.audio.enabled)
1517aaa36a97SAlex Deucher return;
1518aaa36a97SAlex Deucher
1519aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1520aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1521aaa36a97SAlex Deucher
1522aaa36a97SAlex Deucher adev->mode_info.audio.enabled = false;
1523aaa36a97SAlex Deucher }
1524aaa36a97SAlex Deucher
1525aaa36a97SAlex Deucher /*
1526aaa36a97SAlex Deucher * update the N and CTS parameters for a given pixel clock rate
1527aaa36a97SAlex Deucher */
dce_v11_0_afmt_update_ACR(struct drm_encoder * encoder,uint32_t clock)1528aaa36a97SAlex Deucher static void dce_v11_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1529aaa36a97SAlex Deucher {
1530aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
15311348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1532aaa36a97SAlex Deucher struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1533aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1534aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1535aaa36a97SAlex Deucher u32 tmp;
1536aaa36a97SAlex Deucher
1537aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
1538aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
1539aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
1540aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
1541aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
1542aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
1543aaa36a97SAlex Deucher
1544aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
1545aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
1546aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
1547aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
1548aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
1549aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
1550aaa36a97SAlex Deucher
1551aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
1552aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
1553aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
1554aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
1555aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
1556aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
1557aaa36a97SAlex Deucher
1558aaa36a97SAlex Deucher }
1559aaa36a97SAlex Deucher
1560aaa36a97SAlex Deucher /*
1561aaa36a97SAlex Deucher * build a HDMI Video Info Frame
1562aaa36a97SAlex Deucher */
dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder * encoder,void * buffer,size_t size)1563aaa36a97SAlex Deucher static void dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1564aaa36a97SAlex Deucher void *buffer, size_t size)
1565aaa36a97SAlex Deucher {
1566aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
15671348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1568aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1569aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1570aaa36a97SAlex Deucher uint8_t *frame = buffer + 3;
1571aaa36a97SAlex Deucher uint8_t *header = buffer;
1572aaa36a97SAlex Deucher
1573aaa36a97SAlex Deucher WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
1574aaa36a97SAlex Deucher frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1575aaa36a97SAlex Deucher WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
1576aaa36a97SAlex Deucher frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1577aaa36a97SAlex Deucher WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
1578aaa36a97SAlex Deucher frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1579aaa36a97SAlex Deucher WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
1580aaa36a97SAlex Deucher frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1581aaa36a97SAlex Deucher }
1582aaa36a97SAlex Deucher
dce_v11_0_audio_set_dto(struct drm_encoder * encoder,u32 clock)1583aaa36a97SAlex Deucher static void dce_v11_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1584aaa36a97SAlex Deucher {
1585aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
15861348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1587aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1588aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1589aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1590aaa36a97SAlex Deucher u32 dto_phase = 24 * 1000;
1591aaa36a97SAlex Deucher u32 dto_modulo = clock;
1592aaa36a97SAlex Deucher u32 tmp;
1593aaa36a97SAlex Deucher
1594aaa36a97SAlex Deucher if (!dig || !dig->afmt)
1595aaa36a97SAlex Deucher return;
1596aaa36a97SAlex Deucher
1597aaa36a97SAlex Deucher /* XXX two dtos; generally use dto0 for hdmi */
1598aaa36a97SAlex Deucher /* Express [24MHz / target pixel clock] as an exact rational
1599aaa36a97SAlex Deucher * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
1600aaa36a97SAlex Deucher * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1601aaa36a97SAlex Deucher */
1602aaa36a97SAlex Deucher tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
1603aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
1604aaa36a97SAlex Deucher amdgpu_crtc->crtc_id);
1605aaa36a97SAlex Deucher WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
1606aaa36a97SAlex Deucher WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1607aaa36a97SAlex Deucher WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1608aaa36a97SAlex Deucher }
1609aaa36a97SAlex Deucher
1610aaa36a97SAlex Deucher /*
1611aaa36a97SAlex Deucher * update the info frames with the data from the current display mode
1612aaa36a97SAlex Deucher */
dce_v11_0_afmt_setmode(struct drm_encoder * encoder,struct drm_display_mode * mode)1613aaa36a97SAlex Deucher static void dce_v11_0_afmt_setmode(struct drm_encoder *encoder,
1614aaa36a97SAlex Deucher struct drm_display_mode *mode)
1615aaa36a97SAlex Deucher {
1616aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
16171348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1618aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1619aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1620aaa36a97SAlex Deucher struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1621aaa36a97SAlex Deucher u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1622aaa36a97SAlex Deucher struct hdmi_avi_infoframe frame;
1623aaa36a97SAlex Deucher ssize_t err;
1624aaa36a97SAlex Deucher u32 tmp;
1625aaa36a97SAlex Deucher int bpc = 8;
1626aaa36a97SAlex Deucher
1627aaa36a97SAlex Deucher if (!dig || !dig->afmt)
1628aaa36a97SAlex Deucher return;
1629aaa36a97SAlex Deucher
1630aaa36a97SAlex Deucher /* Silent, r600_hdmi_enable will raise WARN for us */
1631aaa36a97SAlex Deucher if (!dig->afmt->enabled)
1632aaa36a97SAlex Deucher return;
1633aaa36a97SAlex Deucher
1634aaa36a97SAlex Deucher /* hdmi deep color mode general control packets setup, if bpc > 8 */
1635aaa36a97SAlex Deucher if (encoder->crtc) {
1636aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1637aaa36a97SAlex Deucher bpc = amdgpu_crtc->bpc;
1638aaa36a97SAlex Deucher }
1639aaa36a97SAlex Deucher
1640aaa36a97SAlex Deucher /* disable audio prior to setting up hw */
1641aaa36a97SAlex Deucher dig->afmt->pin = dce_v11_0_audio_get_pin(adev);
1642aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
1643aaa36a97SAlex Deucher
1644aaa36a97SAlex Deucher dce_v11_0_audio_set_dto(encoder, mode->clock);
1645aaa36a97SAlex Deucher
1646aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1647aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
1648aaa36a97SAlex Deucher WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
1649aaa36a97SAlex Deucher
1650aaa36a97SAlex Deucher WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
1651aaa36a97SAlex Deucher
1652aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
1653aaa36a97SAlex Deucher switch (bpc) {
1654aaa36a97SAlex Deucher case 0:
1655aaa36a97SAlex Deucher case 6:
1656aaa36a97SAlex Deucher case 8:
1657aaa36a97SAlex Deucher case 16:
1658aaa36a97SAlex Deucher default:
1659aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
1660aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
1661aaa36a97SAlex Deucher DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1662aaa36a97SAlex Deucher connector->name, bpc);
1663aaa36a97SAlex Deucher break;
1664aaa36a97SAlex Deucher case 10:
1665aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1666aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
1667aaa36a97SAlex Deucher DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1668aaa36a97SAlex Deucher connector->name);
1669aaa36a97SAlex Deucher break;
1670aaa36a97SAlex Deucher case 12:
1671aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1672aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
1673aaa36a97SAlex Deucher DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1674aaa36a97SAlex Deucher connector->name);
1675aaa36a97SAlex Deucher break;
1676aaa36a97SAlex Deucher }
1677aaa36a97SAlex Deucher WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
1678aaa36a97SAlex Deucher
1679aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1680aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
1681aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
1682aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
1683aaa36a97SAlex Deucher WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
1684aaa36a97SAlex Deucher
1685aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1686aaa36a97SAlex Deucher /* enable audio info frames (frames won't be set until audio is enabled) */
1687aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
1688aaa36a97SAlex Deucher /* required for audio info values to be updated */
1689aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
1690aaa36a97SAlex Deucher WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1691aaa36a97SAlex Deucher
1692aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
1693aaa36a97SAlex Deucher /* required for audio info values to be updated */
1694aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
1695aaa36a97SAlex Deucher WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1696aaa36a97SAlex Deucher
1697aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1698aaa36a97SAlex Deucher /* anything other than 0 */
1699aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
1700aaa36a97SAlex Deucher WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1701aaa36a97SAlex Deucher
1702aaa36a97SAlex Deucher WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
1703aaa36a97SAlex Deucher
1704aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1705aaa36a97SAlex Deucher /* set the default audio delay */
1706aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
1707aaa36a97SAlex Deucher /* should be suffient for all audio modes and small enough for all hblanks */
1708aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
1709aaa36a97SAlex Deucher WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1710aaa36a97SAlex Deucher
1711aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1712aaa36a97SAlex Deucher /* allow 60958 channel status fields to be updated */
1713aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
1714aaa36a97SAlex Deucher WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1715aaa36a97SAlex Deucher
1716aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
1717aaa36a97SAlex Deucher if (bpc > 8)
1718aaa36a97SAlex Deucher /* clear SW CTS value */
1719aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
1720aaa36a97SAlex Deucher else
1721aaa36a97SAlex Deucher /* select SW CTS value */
1722aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
1723aaa36a97SAlex Deucher /* allow hw to sent ACR packets when required */
1724aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
1725aaa36a97SAlex Deucher WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
1726aaa36a97SAlex Deucher
1727aaa36a97SAlex Deucher dce_v11_0_afmt_update_ACR(encoder, mode->clock);
1728aaa36a97SAlex Deucher
1729aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
1730aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
1731aaa36a97SAlex Deucher WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
1732aaa36a97SAlex Deucher
1733aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
1734aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
1735aaa36a97SAlex Deucher WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
1736aaa36a97SAlex Deucher
1737aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
1738aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
1739aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
1740aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
1741aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
1742aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
1743aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
1744aaa36a97SAlex Deucher WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
1745aaa36a97SAlex Deucher
1746aaa36a97SAlex Deucher dce_v11_0_audio_write_speaker_allocation(encoder);
1747aaa36a97SAlex Deucher
1748aaa36a97SAlex Deucher WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
1749aaa36a97SAlex Deucher (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1750aaa36a97SAlex Deucher
1751aaa36a97SAlex Deucher dce_v11_0_afmt_audio_select_pin(encoder);
1752aaa36a97SAlex Deucher dce_v11_0_audio_write_sad_regs(encoder);
1753aaa36a97SAlex Deucher dce_v11_0_audio_write_latency_fields(encoder, mode);
1754aaa36a97SAlex Deucher
175513d0add3SVille Syrjälä err = drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
1756aaa36a97SAlex Deucher if (err < 0) {
1757aaa36a97SAlex Deucher DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1758aaa36a97SAlex Deucher return;
1759aaa36a97SAlex Deucher }
1760aaa36a97SAlex Deucher
1761aaa36a97SAlex Deucher err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1762aaa36a97SAlex Deucher if (err < 0) {
1763aaa36a97SAlex Deucher DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1764aaa36a97SAlex Deucher return;
1765aaa36a97SAlex Deucher }
1766aaa36a97SAlex Deucher
1767aaa36a97SAlex Deucher dce_v11_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1768aaa36a97SAlex Deucher
1769aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1770aaa36a97SAlex Deucher /* enable AVI info frames */
1771aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
1772aaa36a97SAlex Deucher /* required for audio info values to be updated */
1773aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
1774aaa36a97SAlex Deucher WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1775aaa36a97SAlex Deucher
1776aaa36a97SAlex Deucher tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1777aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
1778aaa36a97SAlex Deucher WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1779aaa36a97SAlex Deucher
1780aaa36a97SAlex Deucher tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1781aaa36a97SAlex Deucher /* send audio packets */
1782aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
1783aaa36a97SAlex Deucher WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1784aaa36a97SAlex Deucher
1785aaa36a97SAlex Deucher WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
1786aaa36a97SAlex Deucher WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
1787aaa36a97SAlex Deucher WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
1788aaa36a97SAlex Deucher WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
1789aaa36a97SAlex Deucher
1790aaa36a97SAlex Deucher /* enable audio after to setting up hw */
1791aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, dig->afmt->pin, true);
1792aaa36a97SAlex Deucher }
1793aaa36a97SAlex Deucher
dce_v11_0_afmt_enable(struct drm_encoder * encoder,bool enable)1794aaa36a97SAlex Deucher static void dce_v11_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1795aaa36a97SAlex Deucher {
1796aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
17971348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1798aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1799aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1800aaa36a97SAlex Deucher
1801aaa36a97SAlex Deucher if (!dig || !dig->afmt)
1802aaa36a97SAlex Deucher return;
1803aaa36a97SAlex Deucher
1804aaa36a97SAlex Deucher /* Silent, r600_hdmi_enable will raise WARN for us */
1805aaa36a97SAlex Deucher if (enable && dig->afmt->enabled)
1806aaa36a97SAlex Deucher return;
1807aaa36a97SAlex Deucher if (!enable && !dig->afmt->enabled)
1808aaa36a97SAlex Deucher return;
1809aaa36a97SAlex Deucher
1810aaa36a97SAlex Deucher if (!enable && dig->afmt->pin) {
1811aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
1812aaa36a97SAlex Deucher dig->afmt->pin = NULL;
1813aaa36a97SAlex Deucher }
1814aaa36a97SAlex Deucher
1815aaa36a97SAlex Deucher dig->afmt->enabled = enable;
1816aaa36a97SAlex Deucher
1817aaa36a97SAlex Deucher DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1818aaa36a97SAlex Deucher enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1819aaa36a97SAlex Deucher }
1820aaa36a97SAlex Deucher
dce_v11_0_afmt_init(struct amdgpu_device * adev)1821041ab0a4STom St Denis static int dce_v11_0_afmt_init(struct amdgpu_device *adev)
1822aaa36a97SAlex Deucher {
1823aaa36a97SAlex Deucher int i;
1824aaa36a97SAlex Deucher
1825aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_dig; i++)
1826aaa36a97SAlex Deucher adev->mode_info.afmt[i] = NULL;
1827aaa36a97SAlex Deucher
1828aaa36a97SAlex Deucher /* DCE11 has audio blocks tied to DIG encoders */
1829aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_dig; i++) {
1830aaa36a97SAlex Deucher adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1831aaa36a97SAlex Deucher if (adev->mode_info.afmt[i]) {
1832aaa36a97SAlex Deucher adev->mode_info.afmt[i]->offset = dig_offsets[i];
1833aaa36a97SAlex Deucher adev->mode_info.afmt[i]->id = i;
1834041ab0a4STom St Denis } else {
1835041ab0a4STom St Denis int j;
1836041ab0a4STom St Denis for (j = 0; j < i; j++) {
1837041ab0a4STom St Denis kfree(adev->mode_info.afmt[j]);
1838041ab0a4STom St Denis adev->mode_info.afmt[j] = NULL;
1839041ab0a4STom St Denis }
1840041ab0a4STom St Denis return -ENOMEM;
1841aaa36a97SAlex Deucher }
1842aaa36a97SAlex Deucher }
1843041ab0a4STom St Denis return 0;
1844aaa36a97SAlex Deucher }
1845aaa36a97SAlex Deucher
dce_v11_0_afmt_fini(struct amdgpu_device * adev)1846aaa36a97SAlex Deucher static void dce_v11_0_afmt_fini(struct amdgpu_device *adev)
1847aaa36a97SAlex Deucher {
1848aaa36a97SAlex Deucher int i;
1849aaa36a97SAlex Deucher
1850aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_dig; i++) {
1851aaa36a97SAlex Deucher kfree(adev->mode_info.afmt[i]);
1852aaa36a97SAlex Deucher adev->mode_info.afmt[i] = NULL;
1853aaa36a97SAlex Deucher }
1854aaa36a97SAlex Deucher }
1855aaa36a97SAlex Deucher
1856aaa36a97SAlex Deucher static const u32 vga_control_regs[6] =
1857aaa36a97SAlex Deucher {
1858aaa36a97SAlex Deucher mmD1VGA_CONTROL,
1859aaa36a97SAlex Deucher mmD2VGA_CONTROL,
1860aaa36a97SAlex Deucher mmD3VGA_CONTROL,
1861aaa36a97SAlex Deucher mmD4VGA_CONTROL,
1862aaa36a97SAlex Deucher mmD5VGA_CONTROL,
1863aaa36a97SAlex Deucher mmD6VGA_CONTROL,
1864aaa36a97SAlex Deucher };
1865aaa36a97SAlex Deucher
dce_v11_0_vga_enable(struct drm_crtc * crtc,bool enable)1866aaa36a97SAlex Deucher static void dce_v11_0_vga_enable(struct drm_crtc *crtc, bool enable)
1867aaa36a97SAlex Deucher {
1868aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1869aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
18701348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1871aaa36a97SAlex Deucher u32 vga_control;
1872aaa36a97SAlex Deucher
1873aaa36a97SAlex Deucher vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
1874aaa36a97SAlex Deucher if (enable)
1875aaa36a97SAlex Deucher WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
1876aaa36a97SAlex Deucher else
1877aaa36a97SAlex Deucher WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
1878aaa36a97SAlex Deucher }
1879aaa36a97SAlex Deucher
dce_v11_0_grph_enable(struct drm_crtc * crtc,bool enable)1880aaa36a97SAlex Deucher static void dce_v11_0_grph_enable(struct drm_crtc *crtc, bool enable)
1881aaa36a97SAlex Deucher {
1882aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1883aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
18841348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1885aaa36a97SAlex Deucher
1886aaa36a97SAlex Deucher if (enable)
1887aaa36a97SAlex Deucher WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
1888aaa36a97SAlex Deucher else
1889aaa36a97SAlex Deucher WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
1890aaa36a97SAlex Deucher }
1891aaa36a97SAlex Deucher
dce_v11_0_crtc_do_set_base(struct drm_crtc * crtc,struct drm_framebuffer * fb,int x,int y,int atomic)1892aaa36a97SAlex Deucher static int dce_v11_0_crtc_do_set_base(struct drm_crtc *crtc,
1893aaa36a97SAlex Deucher struct drm_framebuffer *fb,
1894aaa36a97SAlex Deucher int x, int y, int atomic)
1895aaa36a97SAlex Deucher {
1896aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1897aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
18981348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
1899aaa36a97SAlex Deucher struct drm_framebuffer *target_fb;
1900aaa36a97SAlex Deucher struct drm_gem_object *obj;
1901765e7fbfSChristian König struct amdgpu_bo *abo;
1902aaa36a97SAlex Deucher uint64_t fb_location, tiling_flags;
1903aaa36a97SAlex Deucher uint32_t fb_format, fb_pitch_pixels;
1904aaa36a97SAlex Deucher u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
1905fbd76d59SMarek Olšák u32 pipe_config;
1906aaa36a97SAlex Deucher u32 tmp, viewport_w, viewport_h;
1907aaa36a97SAlex Deucher int r;
1908aaa36a97SAlex Deucher bool bypass_lut = false;
1909aaa36a97SAlex Deucher
1910aaa36a97SAlex Deucher /* no fb bound */
1911aaa36a97SAlex Deucher if (!atomic && !crtc->primary->fb) {
1912aaa36a97SAlex Deucher DRM_DEBUG_KMS("No FB bound\n");
1913aaa36a97SAlex Deucher return 0;
1914aaa36a97SAlex Deucher }
1915aaa36a97SAlex Deucher
1916e68d14ddSDaniel Stone if (atomic)
1917aaa36a97SAlex Deucher target_fb = fb;
1918e68d14ddSDaniel Stone else
1919aaa36a97SAlex Deucher target_fb = crtc->primary->fb;
1920aaa36a97SAlex Deucher
1921aaa36a97SAlex Deucher /* If atomic, assume fb object is pinned & idle & fenced and
1922aaa36a97SAlex Deucher * just update base pointers
1923aaa36a97SAlex Deucher */
1924e68d14ddSDaniel Stone obj = target_fb->obj[0];
1925765e7fbfSChristian König abo = gem_to_amdgpu_bo(obj);
1926765e7fbfSChristian König r = amdgpu_bo_reserve(abo, false);
1927aaa36a97SAlex Deucher if (unlikely(r != 0))
1928aaa36a97SAlex Deucher return r;
1929aaa36a97SAlex Deucher
19307b7c6c81SJunwei Zhang if (!atomic) {
19317b7c6c81SJunwei Zhang r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM);
1932aaa36a97SAlex Deucher if (unlikely(r != 0)) {
1933765e7fbfSChristian König amdgpu_bo_unreserve(abo);
1934aaa36a97SAlex Deucher return -EINVAL;
1935aaa36a97SAlex Deucher }
1936aaa36a97SAlex Deucher }
19377b7c6c81SJunwei Zhang fb_location = amdgpu_bo_gpu_offset(abo);
1938aaa36a97SAlex Deucher
1939765e7fbfSChristian König amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
1940765e7fbfSChristian König amdgpu_bo_unreserve(abo);
1941aaa36a97SAlex Deucher
1942fbd76d59SMarek Olšák pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1943fbd76d59SMarek Olšák
1944438b74a5SVille Syrjälä switch (target_fb->format->format) {
1945aaa36a97SAlex Deucher case DRM_FORMAT_C8:
1946aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
1947aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1948aaa36a97SAlex Deucher break;
1949aaa36a97SAlex Deucher case DRM_FORMAT_XRGB4444:
1950aaa36a97SAlex Deucher case DRM_FORMAT_ARGB4444:
1951aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1952aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
1953aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
1954aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1955aaa36a97SAlex Deucher ENDIAN_8IN16);
1956aaa36a97SAlex Deucher #endif
1957aaa36a97SAlex Deucher break;
1958aaa36a97SAlex Deucher case DRM_FORMAT_XRGB1555:
1959aaa36a97SAlex Deucher case DRM_FORMAT_ARGB1555:
1960aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1961aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1962aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
1963aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1964aaa36a97SAlex Deucher ENDIAN_8IN16);
1965aaa36a97SAlex Deucher #endif
1966aaa36a97SAlex Deucher break;
1967aaa36a97SAlex Deucher case DRM_FORMAT_BGRX5551:
1968aaa36a97SAlex Deucher case DRM_FORMAT_BGRA5551:
1969aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1970aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
1971aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
1972aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1973aaa36a97SAlex Deucher ENDIAN_8IN16);
1974aaa36a97SAlex Deucher #endif
1975aaa36a97SAlex Deucher break;
1976aaa36a97SAlex Deucher case DRM_FORMAT_RGB565:
1977aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1978aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1979aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
1980aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1981aaa36a97SAlex Deucher ENDIAN_8IN16);
1982aaa36a97SAlex Deucher #endif
1983aaa36a97SAlex Deucher break;
1984aaa36a97SAlex Deucher case DRM_FORMAT_XRGB8888:
1985aaa36a97SAlex Deucher case DRM_FORMAT_ARGB8888:
1986aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1987aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1988aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
1989aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1990aaa36a97SAlex Deucher ENDIAN_8IN32);
1991aaa36a97SAlex Deucher #endif
1992aaa36a97SAlex Deucher break;
1993aaa36a97SAlex Deucher case DRM_FORMAT_XRGB2101010:
1994aaa36a97SAlex Deucher case DRM_FORMAT_ARGB2101010:
1995aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1996aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1997aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
1998aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1999aaa36a97SAlex Deucher ENDIAN_8IN32);
2000aaa36a97SAlex Deucher #endif
2001aaa36a97SAlex Deucher /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2002aaa36a97SAlex Deucher bypass_lut = true;
2003aaa36a97SAlex Deucher break;
2004aaa36a97SAlex Deucher case DRM_FORMAT_BGRX1010102:
2005aaa36a97SAlex Deucher case DRM_FORMAT_BGRA1010102:
2006aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2007aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
2008aaa36a97SAlex Deucher #ifdef __BIG_ENDIAN
2009aaa36a97SAlex Deucher fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2010aaa36a97SAlex Deucher ENDIAN_8IN32);
2011aaa36a97SAlex Deucher #endif
2012aaa36a97SAlex Deucher /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2013aaa36a97SAlex Deucher bypass_lut = true;
2014aaa36a97SAlex Deucher break;
201500ecc6e6SMauro Rossi case DRM_FORMAT_XBGR8888:
201600ecc6e6SMauro Rossi case DRM_FORMAT_ABGR8888:
201700ecc6e6SMauro Rossi fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
201800ecc6e6SMauro Rossi fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
201900ecc6e6SMauro Rossi fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_RED_CROSSBAR, 2);
202000ecc6e6SMauro Rossi fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_BLUE_CROSSBAR, 2);
202100ecc6e6SMauro Rossi #ifdef __BIG_ENDIAN
202200ecc6e6SMauro Rossi fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
202300ecc6e6SMauro Rossi ENDIAN_8IN32);
202400ecc6e6SMauro Rossi #endif
202500ecc6e6SMauro Rossi break;
2026aaa36a97SAlex Deucher default:
202792f1d09cSSakari Ailus DRM_ERROR("Unsupported screen format %p4cc\n",
202892f1d09cSSakari Ailus &target_fb->format->format);
2029aaa36a97SAlex Deucher return -EINVAL;
2030aaa36a97SAlex Deucher }
2031aaa36a97SAlex Deucher
2032fbd76d59SMarek Olšák if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
2033fbd76d59SMarek Olšák unsigned bankw, bankh, mtaspect, tile_split, num_banks;
2034aaa36a97SAlex Deucher
2035fbd76d59SMarek Olšák bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2036fbd76d59SMarek Olšák bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2037fbd76d59SMarek Olšák mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2038fbd76d59SMarek Olšák tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2039fbd76d59SMarek Olšák num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2040aaa36a97SAlex Deucher
2041aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
2042aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2043aaa36a97SAlex Deucher ARRAY_2D_TILED_THIN1);
2044aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
2045aaa36a97SAlex Deucher tile_split);
2046aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
2047aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
2048aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
2049aaa36a97SAlex Deucher mtaspect);
2050aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
2051aaa36a97SAlex Deucher ADDR_SURF_MICRO_TILING_DISPLAY);
2052fbd76d59SMarek Olšák } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
2053aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2054aaa36a97SAlex Deucher ARRAY_1D_TILED_THIN1);
2055aaa36a97SAlex Deucher }
2056aaa36a97SAlex Deucher
2057aaa36a97SAlex Deucher fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
2058aaa36a97SAlex Deucher pipe_config);
2059aaa36a97SAlex Deucher
2060aaa36a97SAlex Deucher dce_v11_0_vga_enable(crtc, false);
2061aaa36a97SAlex Deucher
2062cb9e59d7SAlex Deucher /* Make sure surface address is updated at vertical blank rather than
2063cb9e59d7SAlex Deucher * horizontal blank
2064cb9e59d7SAlex Deucher */
2065cb9e59d7SAlex Deucher tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
2066cb9e59d7SAlex Deucher tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
2067cb9e59d7SAlex Deucher GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
2068cb9e59d7SAlex Deucher WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2069cb9e59d7SAlex Deucher
2070aaa36a97SAlex Deucher WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2071aaa36a97SAlex Deucher upper_32_bits(fb_location));
2072aaa36a97SAlex Deucher WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2073aaa36a97SAlex Deucher upper_32_bits(fb_location));
2074aaa36a97SAlex Deucher WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2075aaa36a97SAlex Deucher (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
2076aaa36a97SAlex Deucher WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2077aaa36a97SAlex Deucher (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
2078aaa36a97SAlex Deucher WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
2079aaa36a97SAlex Deucher WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
2080aaa36a97SAlex Deucher
2081aaa36a97SAlex Deucher /*
2082aaa36a97SAlex Deucher * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
2083aaa36a97SAlex Deucher * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
2084aaa36a97SAlex Deucher * retain the full precision throughout the pipeline.
2085aaa36a97SAlex Deucher */
2086aaa36a97SAlex Deucher tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
2087aaa36a97SAlex Deucher if (bypass_lut)
2088aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
2089aaa36a97SAlex Deucher else
2090aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
2091aaa36a97SAlex Deucher WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
2092aaa36a97SAlex Deucher
2093aaa36a97SAlex Deucher if (bypass_lut)
2094aaa36a97SAlex Deucher DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
2095aaa36a97SAlex Deucher
2096aaa36a97SAlex Deucher WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
2097aaa36a97SAlex Deucher WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
2098aaa36a97SAlex Deucher WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
2099aaa36a97SAlex Deucher WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
2100aaa36a97SAlex Deucher WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
2101aaa36a97SAlex Deucher WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
2102aaa36a97SAlex Deucher
2103272725c7SVille Syrjälä fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
2104aaa36a97SAlex Deucher WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
2105aaa36a97SAlex Deucher
2106aaa36a97SAlex Deucher dce_v11_0_grph_enable(crtc, true);
2107aaa36a97SAlex Deucher
2108aaa36a97SAlex Deucher WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
2109aaa36a97SAlex Deucher target_fb->height);
2110aaa36a97SAlex Deucher
2111aaa36a97SAlex Deucher x &= ~3;
2112aaa36a97SAlex Deucher y &= ~1;
2113aaa36a97SAlex Deucher WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
2114aaa36a97SAlex Deucher (x << 16) | y);
2115aaa36a97SAlex Deucher viewport_w = crtc->mode.hdisplay;
2116aaa36a97SAlex Deucher viewport_h = (crtc->mode.vdisplay + 1) & ~1;
2117aaa36a97SAlex Deucher WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
2118aaa36a97SAlex Deucher (viewport_w << 16) | viewport_h);
2119aaa36a97SAlex Deucher
21203fd4b751SMichel Dänzer /* set pageflip to happen anywhere in vblank interval */
21213fd4b751SMichel Dänzer WREG32(mmCRTC_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
2122aaa36a97SAlex Deucher
2123aaa36a97SAlex Deucher if (!atomic && fb && fb != crtc->primary->fb) {
2124e68d14ddSDaniel Stone abo = gem_to_amdgpu_bo(fb->obj[0]);
2125c81a1a74SMichel Dänzer r = amdgpu_bo_reserve(abo, true);
2126aaa36a97SAlex Deucher if (unlikely(r != 0))
2127aaa36a97SAlex Deucher return r;
2128765e7fbfSChristian König amdgpu_bo_unpin(abo);
2129765e7fbfSChristian König amdgpu_bo_unreserve(abo);
2130aaa36a97SAlex Deucher }
2131aaa36a97SAlex Deucher
2132aaa36a97SAlex Deucher /* Bytes per pixel may have changed */
2133aaa36a97SAlex Deucher dce_v11_0_bandwidth_update(adev);
2134aaa36a97SAlex Deucher
2135aaa36a97SAlex Deucher return 0;
2136aaa36a97SAlex Deucher }
2137aaa36a97SAlex Deucher
dce_v11_0_set_interleave(struct drm_crtc * crtc,struct drm_display_mode * mode)2138aaa36a97SAlex Deucher static void dce_v11_0_set_interleave(struct drm_crtc *crtc,
2139aaa36a97SAlex Deucher struct drm_display_mode *mode)
2140aaa36a97SAlex Deucher {
2141aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
21421348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
2143aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2144aaa36a97SAlex Deucher u32 tmp;
2145aaa36a97SAlex Deucher
2146aaa36a97SAlex Deucher tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
2147aaa36a97SAlex Deucher if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2148aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
2149aaa36a97SAlex Deucher else
2150aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
2151aaa36a97SAlex Deucher WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
2152aaa36a97SAlex Deucher }
2153aaa36a97SAlex Deucher
dce_v11_0_crtc_load_lut(struct drm_crtc * crtc)2154aaa36a97SAlex Deucher static void dce_v11_0_crtc_load_lut(struct drm_crtc *crtc)
2155aaa36a97SAlex Deucher {
2156aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2157aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
21581348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
215976dd3cd8SPeter Rosin u16 *r, *g, *b;
2160aaa36a97SAlex Deucher int i;
2161aaa36a97SAlex Deucher u32 tmp;
2162aaa36a97SAlex Deucher
2163aaa36a97SAlex Deucher DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2164aaa36a97SAlex Deucher
2165aaa36a97SAlex Deucher tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2166aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
2167aaa36a97SAlex Deucher WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2168aaa36a97SAlex Deucher
2169aaa36a97SAlex Deucher tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
2170aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
2171aaa36a97SAlex Deucher WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2172aaa36a97SAlex Deucher
2173aaa36a97SAlex Deucher tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2174aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
2175aaa36a97SAlex Deucher WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2176aaa36a97SAlex Deucher
2177aaa36a97SAlex Deucher WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2178aaa36a97SAlex Deucher
2179aaa36a97SAlex Deucher WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2180aaa36a97SAlex Deucher WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2181aaa36a97SAlex Deucher WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2182aaa36a97SAlex Deucher
2183aaa36a97SAlex Deucher WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2184aaa36a97SAlex Deucher WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2185aaa36a97SAlex Deucher WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2186aaa36a97SAlex Deucher
2187aaa36a97SAlex Deucher WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2188aaa36a97SAlex Deucher WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2189aaa36a97SAlex Deucher
2190aaa36a97SAlex Deucher WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
219176dd3cd8SPeter Rosin r = crtc->gamma_store;
219276dd3cd8SPeter Rosin g = r + crtc->gamma_size;
219376dd3cd8SPeter Rosin b = g + crtc->gamma_size;
2194aaa36a97SAlex Deucher for (i = 0; i < 256; i++) {
2195aaa36a97SAlex Deucher WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
219676dd3cd8SPeter Rosin ((*r++ & 0xffc0) << 14) |
219776dd3cd8SPeter Rosin ((*g++ & 0xffc0) << 4) |
219876dd3cd8SPeter Rosin (*b++ >> 6));
2199aaa36a97SAlex Deucher }
2200aaa36a97SAlex Deucher
2201aaa36a97SAlex Deucher tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2202aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
2203aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
2204aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR2_DEGAMMA_MODE, 0);
2205aaa36a97SAlex Deucher WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2206aaa36a97SAlex Deucher
2207aaa36a97SAlex Deucher tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
2208aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
2209aaa36a97SAlex Deucher WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2210aaa36a97SAlex Deucher
2211aaa36a97SAlex Deucher tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2212aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
2213aaa36a97SAlex Deucher WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2214aaa36a97SAlex Deucher
2215aaa36a97SAlex Deucher tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2216aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
2217aaa36a97SAlex Deucher WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2218aaa36a97SAlex Deucher
2219aaa36a97SAlex Deucher /* XXX match this to the depth of the crtc fmt block, move to modeset? */
2220aaa36a97SAlex Deucher WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
2221aaa36a97SAlex Deucher /* XXX this only needs to be programmed once per crtc at startup,
2222aaa36a97SAlex Deucher * not sure where the best place for it is
2223aaa36a97SAlex Deucher */
2224aaa36a97SAlex Deucher tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
2225aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
2226aaa36a97SAlex Deucher WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2227aaa36a97SAlex Deucher }
2228aaa36a97SAlex Deucher
dce_v11_0_pick_dig_encoder(struct drm_encoder * encoder)2229aaa36a97SAlex Deucher static int dce_v11_0_pick_dig_encoder(struct drm_encoder *encoder)
2230aaa36a97SAlex Deucher {
2231aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2232aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2233aaa36a97SAlex Deucher
2234aaa36a97SAlex Deucher switch (amdgpu_encoder->encoder_id) {
2235aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2236aaa36a97SAlex Deucher if (dig->linkb)
2237aaa36a97SAlex Deucher return 1;
2238aaa36a97SAlex Deucher else
2239aaa36a97SAlex Deucher return 0;
2240aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2241aaa36a97SAlex Deucher if (dig->linkb)
2242aaa36a97SAlex Deucher return 3;
2243aaa36a97SAlex Deucher else
2244aaa36a97SAlex Deucher return 2;
2245aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2246aaa36a97SAlex Deucher if (dig->linkb)
2247aaa36a97SAlex Deucher return 5;
2248aaa36a97SAlex Deucher else
2249aaa36a97SAlex Deucher return 4;
2250aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2251aaa36a97SAlex Deucher return 6;
2252aaa36a97SAlex Deucher default:
2253aaa36a97SAlex Deucher DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2254aaa36a97SAlex Deucher return 0;
2255aaa36a97SAlex Deucher }
2256aaa36a97SAlex Deucher }
2257aaa36a97SAlex Deucher
2258aaa36a97SAlex Deucher /**
2259aaa36a97SAlex Deucher * dce_v11_0_pick_pll - Allocate a PPLL for use by the crtc.
2260aaa36a97SAlex Deucher *
2261aaa36a97SAlex Deucher * @crtc: drm crtc
2262aaa36a97SAlex Deucher *
2263aaa36a97SAlex Deucher * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
2264aaa36a97SAlex Deucher * a single PPLL can be used for all DP crtcs/encoders. For non-DP
2265aaa36a97SAlex Deucher * monitors a dedicated PPLL must be used. If a particular board has
2266aaa36a97SAlex Deucher * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2267aaa36a97SAlex Deucher * as there is no need to program the PLL itself. If we are not able to
2268aaa36a97SAlex Deucher * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2269aaa36a97SAlex Deucher * avoid messing up an existing monitor.
2270aaa36a97SAlex Deucher *
2271aaa36a97SAlex Deucher * Asic specific PLL information
2272aaa36a97SAlex Deucher *
2273aaa36a97SAlex Deucher * DCE 10.x
2274aaa36a97SAlex Deucher * Tonga
2275aaa36a97SAlex Deucher * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2276aaa36a97SAlex Deucher * CI
2277aaa36a97SAlex Deucher * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2278aaa36a97SAlex Deucher *
2279aaa36a97SAlex Deucher */
dce_v11_0_pick_pll(struct drm_crtc * crtc)2280aaa36a97SAlex Deucher static u32 dce_v11_0_pick_pll(struct drm_crtc *crtc)
2281aaa36a97SAlex Deucher {
2282aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2283aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
22841348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
2285aaa36a97SAlex Deucher u32 pll_in_use;
2286aaa36a97SAlex Deucher int pll;
2287aaa36a97SAlex Deucher
22882cc0c0b5SFlora Cui if ((adev->asic_type == CHIP_POLARIS10) ||
2289c4642a47SJunwei Zhang (adev->asic_type == CHIP_POLARIS11) ||
2290221adb21SAlex Deucher (adev->asic_type == CHIP_POLARIS12) ||
2291221adb21SAlex Deucher (adev->asic_type == CHIP_VEGAM)) {
2292927a81c9SAlex Deucher struct amdgpu_encoder *amdgpu_encoder =
2293927a81c9SAlex Deucher to_amdgpu_encoder(amdgpu_crtc->encoder);
2294927a81c9SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2295927a81c9SAlex Deucher
2296927a81c9SAlex Deucher if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2297927a81c9SAlex Deucher return ATOM_DP_DTO;
2298927a81c9SAlex Deucher
2299927a81c9SAlex Deucher switch (amdgpu_encoder->encoder_id) {
2300927a81c9SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2301927a81c9SAlex Deucher if (dig->linkb)
2302927a81c9SAlex Deucher return ATOM_COMBOPHY_PLL1;
2303927a81c9SAlex Deucher else
2304927a81c9SAlex Deucher return ATOM_COMBOPHY_PLL0;
2305927a81c9SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2306927a81c9SAlex Deucher if (dig->linkb)
2307927a81c9SAlex Deucher return ATOM_COMBOPHY_PLL3;
2308927a81c9SAlex Deucher else
2309927a81c9SAlex Deucher return ATOM_COMBOPHY_PLL2;
2310927a81c9SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2311927a81c9SAlex Deucher if (dig->linkb)
2312927a81c9SAlex Deucher return ATOM_COMBOPHY_PLL5;
2313927a81c9SAlex Deucher else
2314927a81c9SAlex Deucher return ATOM_COMBOPHY_PLL4;
2315927a81c9SAlex Deucher default:
2316927a81c9SAlex Deucher DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2317927a81c9SAlex Deucher return ATOM_PPLL_INVALID;
2318927a81c9SAlex Deucher }
2319927a81c9SAlex Deucher }
2320927a81c9SAlex Deucher
2321aaa36a97SAlex Deucher if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2322aaa36a97SAlex Deucher if (adev->clock.dp_extclk)
2323aaa36a97SAlex Deucher /* skip PPLL programming if using ext clock */
2324aaa36a97SAlex Deucher return ATOM_PPLL_INVALID;
2325aaa36a97SAlex Deucher else {
2326aaa36a97SAlex Deucher /* use the same PPLL for all DP monitors */
2327aaa36a97SAlex Deucher pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2328aaa36a97SAlex Deucher if (pll != ATOM_PPLL_INVALID)
2329aaa36a97SAlex Deucher return pll;
2330aaa36a97SAlex Deucher }
2331aaa36a97SAlex Deucher } else {
2332aaa36a97SAlex Deucher /* use the same PPLL for all monitors with the same clock */
2333aaa36a97SAlex Deucher pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2334aaa36a97SAlex Deucher if (pll != ATOM_PPLL_INVALID)
2335aaa36a97SAlex Deucher return pll;
2336aaa36a97SAlex Deucher }
2337aaa36a97SAlex Deucher
2338aaa36a97SAlex Deucher /* XXX need to determine what plls are available on each DCE11 part */
2339aaa36a97SAlex Deucher pll_in_use = amdgpu_pll_get_use_mask(crtc);
2340e1ad2d53SAlex Deucher if (adev->flags & AMD_IS_APU) {
2341aaa36a97SAlex Deucher if (!(pll_in_use & (1 << ATOM_PPLL1)))
2342aaa36a97SAlex Deucher return ATOM_PPLL1;
2343aaa36a97SAlex Deucher if (!(pll_in_use & (1 << ATOM_PPLL0)))
2344aaa36a97SAlex Deucher return ATOM_PPLL0;
2345aaa36a97SAlex Deucher DRM_ERROR("unable to allocate a PPLL\n");
2346aaa36a97SAlex Deucher return ATOM_PPLL_INVALID;
2347aaa36a97SAlex Deucher } else {
2348aaa36a97SAlex Deucher if (!(pll_in_use & (1 << ATOM_PPLL2)))
2349aaa36a97SAlex Deucher return ATOM_PPLL2;
2350aaa36a97SAlex Deucher if (!(pll_in_use & (1 << ATOM_PPLL1)))
2351aaa36a97SAlex Deucher return ATOM_PPLL1;
2352aaa36a97SAlex Deucher if (!(pll_in_use & (1 << ATOM_PPLL0)))
2353aaa36a97SAlex Deucher return ATOM_PPLL0;
2354aaa36a97SAlex Deucher DRM_ERROR("unable to allocate a PPLL\n");
2355aaa36a97SAlex Deucher return ATOM_PPLL_INVALID;
2356aaa36a97SAlex Deucher }
2357aaa36a97SAlex Deucher return ATOM_PPLL_INVALID;
2358aaa36a97SAlex Deucher }
2359aaa36a97SAlex Deucher
dce_v11_0_lock_cursor(struct drm_crtc * crtc,bool lock)2360aaa36a97SAlex Deucher static void dce_v11_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2361aaa36a97SAlex Deucher {
23621348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2363aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2364aaa36a97SAlex Deucher uint32_t cur_lock;
2365aaa36a97SAlex Deucher
2366aaa36a97SAlex Deucher cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2367aaa36a97SAlex Deucher if (lock)
2368aaa36a97SAlex Deucher cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
2369aaa36a97SAlex Deucher else
2370aaa36a97SAlex Deucher cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
2371aaa36a97SAlex Deucher WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2372aaa36a97SAlex Deucher }
2373aaa36a97SAlex Deucher
dce_v11_0_hide_cursor(struct drm_crtc * crtc)2374aaa36a97SAlex Deucher static void dce_v11_0_hide_cursor(struct drm_crtc *crtc)
2375aaa36a97SAlex Deucher {
2376aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
23771348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2378aaa36a97SAlex Deucher u32 tmp;
2379aaa36a97SAlex Deucher
238046e840edSHawking Zhang tmp = RREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2381aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
238246e840edSHawking Zhang WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2383aaa36a97SAlex Deucher }
2384aaa36a97SAlex Deucher
dce_v11_0_show_cursor(struct drm_crtc * crtc)2385aaa36a97SAlex Deucher static void dce_v11_0_show_cursor(struct drm_crtc *crtc)
2386aaa36a97SAlex Deucher {
2387aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
23881348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2389aaa36a97SAlex Deucher u32 tmp;
2390aaa36a97SAlex Deucher
2391ec9353dcSAlex Deucher WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2392ec9353dcSAlex Deucher upper_32_bits(amdgpu_crtc->cursor_addr));
2393ec9353dcSAlex Deucher WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2394ec9353dcSAlex Deucher lower_32_bits(amdgpu_crtc->cursor_addr));
2395ec9353dcSAlex Deucher
239646e840edSHawking Zhang tmp = RREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2397aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
2398aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
239946e840edSHawking Zhang WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2400aaa36a97SAlex Deucher }
2401aaa36a97SAlex Deucher
dce_v11_0_cursor_move_locked(struct drm_crtc * crtc,int x,int y)2402d8ee89c6SAlex Deucher static int dce_v11_0_cursor_move_locked(struct drm_crtc *crtc,
2403aaa36a97SAlex Deucher int x, int y)
2404aaa36a97SAlex Deucher {
2405aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
24061348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2407aaa36a97SAlex Deucher int xorigin = 0, yorigin = 0;
2408aaa36a97SAlex Deucher
24098e57ec61SMichel Dänzer amdgpu_crtc->cursor_x = x;
24108e57ec61SMichel Dänzer amdgpu_crtc->cursor_y = y;
24118e57ec61SMichel Dänzer
2412aaa36a97SAlex Deucher /* avivo cursor are offset into the total surface */
2413aaa36a97SAlex Deucher x += crtc->x;
2414aaa36a97SAlex Deucher y += crtc->y;
2415aaa36a97SAlex Deucher DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2416aaa36a97SAlex Deucher
2417aaa36a97SAlex Deucher if (x < 0) {
2418aaa36a97SAlex Deucher xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2419aaa36a97SAlex Deucher x = 0;
2420aaa36a97SAlex Deucher }
2421aaa36a97SAlex Deucher if (y < 0) {
2422aaa36a97SAlex Deucher yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2423aaa36a97SAlex Deucher y = 0;
2424aaa36a97SAlex Deucher }
2425aaa36a97SAlex Deucher
2426aaa36a97SAlex Deucher WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2427aaa36a97SAlex Deucher WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
242869bcc0b7SMichel Dänzer WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
242969bcc0b7SMichel Dänzer ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
2430d8ee89c6SAlex Deucher
2431aaa36a97SAlex Deucher return 0;
2432aaa36a97SAlex Deucher }
2433aaa36a97SAlex Deucher
dce_v11_0_crtc_cursor_move(struct drm_crtc * crtc,int x,int y)2434d8ee89c6SAlex Deucher static int dce_v11_0_crtc_cursor_move(struct drm_crtc *crtc,
2435d8ee89c6SAlex Deucher int x, int y)
2436d8ee89c6SAlex Deucher {
2437d8ee89c6SAlex Deucher int ret;
2438d8ee89c6SAlex Deucher
2439d8ee89c6SAlex Deucher dce_v11_0_lock_cursor(crtc, true);
2440d8ee89c6SAlex Deucher ret = dce_v11_0_cursor_move_locked(crtc, x, y);
2441d8ee89c6SAlex Deucher dce_v11_0_lock_cursor(crtc, false);
2442d8ee89c6SAlex Deucher
2443d8ee89c6SAlex Deucher return ret;
2444d8ee89c6SAlex Deucher }
2445d8ee89c6SAlex Deucher
dce_v11_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)2446d8ee89c6SAlex Deucher static int dce_v11_0_crtc_cursor_set2(struct drm_crtc *crtc,
2447aaa36a97SAlex Deucher struct drm_file *file_priv,
2448aaa36a97SAlex Deucher uint32_t handle,
2449aaa36a97SAlex Deucher uint32_t width,
2450d8ee89c6SAlex Deucher uint32_t height,
2451d8ee89c6SAlex Deucher int32_t hot_x,
2452d8ee89c6SAlex Deucher int32_t hot_y)
2453aaa36a97SAlex Deucher {
2454aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2455aaa36a97SAlex Deucher struct drm_gem_object *obj;
2456232cc652SAlex Deucher struct amdgpu_bo *aobj;
2457aaa36a97SAlex Deucher int ret;
2458aaa36a97SAlex Deucher
2459aaa36a97SAlex Deucher if (!handle) {
2460aaa36a97SAlex Deucher /* turn off cursor */
2461aaa36a97SAlex Deucher dce_v11_0_hide_cursor(crtc);
2462aaa36a97SAlex Deucher obj = NULL;
2463aaa36a97SAlex Deucher goto unpin;
2464aaa36a97SAlex Deucher }
2465aaa36a97SAlex Deucher
2466aaa36a97SAlex Deucher if ((width > amdgpu_crtc->max_cursor_width) ||
2467aaa36a97SAlex Deucher (height > amdgpu_crtc->max_cursor_height)) {
2468aaa36a97SAlex Deucher DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2469aaa36a97SAlex Deucher return -EINVAL;
2470aaa36a97SAlex Deucher }
2471aaa36a97SAlex Deucher
2472a8ad0bd8SChris Wilson obj = drm_gem_object_lookup(file_priv, handle);
2473aaa36a97SAlex Deucher if (!obj) {
2474aaa36a97SAlex Deucher DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2475aaa36a97SAlex Deucher return -ENOENT;
2476aaa36a97SAlex Deucher }
2477aaa36a97SAlex Deucher
2478232cc652SAlex Deucher aobj = gem_to_amdgpu_bo(obj);
2479232cc652SAlex Deucher ret = amdgpu_bo_reserve(aobj, false);
2480232cc652SAlex Deucher if (ret != 0) {
2481e07ddb0cSEmil Velikov drm_gem_object_put(obj);
2482232cc652SAlex Deucher return ret;
2483232cc652SAlex Deucher }
2484232cc652SAlex Deucher
24857b7c6c81SJunwei Zhang ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
2486232cc652SAlex Deucher amdgpu_bo_unreserve(aobj);
2487232cc652SAlex Deucher if (ret) {
2488232cc652SAlex Deucher DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2489e07ddb0cSEmil Velikov drm_gem_object_put(obj);
2490232cc652SAlex Deucher return ret;
2491232cc652SAlex Deucher }
24927b7c6c81SJunwei Zhang amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2493aaa36a97SAlex Deucher
2494aaa36a97SAlex Deucher dce_v11_0_lock_cursor(crtc, true);
24951996ea09SAlex Deucher
249669bcc0b7SMichel Dänzer if (width != amdgpu_crtc->cursor_width ||
249769bcc0b7SMichel Dänzer height != amdgpu_crtc->cursor_height ||
249869bcc0b7SMichel Dänzer hot_x != amdgpu_crtc->cursor_hot_x ||
24991996ea09SAlex Deucher hot_y != amdgpu_crtc->cursor_hot_y) {
25001996ea09SAlex Deucher int x, y;
25011996ea09SAlex Deucher
25021996ea09SAlex Deucher x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
25031996ea09SAlex Deucher y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
25041996ea09SAlex Deucher
25051996ea09SAlex Deucher dce_v11_0_cursor_move_locked(crtc, x, y);
25061996ea09SAlex Deucher
25077c83d7abSMichel Dänzer amdgpu_crtc->cursor_width = width;
25087c83d7abSMichel Dänzer amdgpu_crtc->cursor_height = height;
250969bcc0b7SMichel Dänzer amdgpu_crtc->cursor_hot_x = hot_x;
251069bcc0b7SMichel Dänzer amdgpu_crtc->cursor_hot_y = hot_y;
25117c83d7abSMichel Dänzer }
25127c83d7abSMichel Dänzer
2513aaa36a97SAlex Deucher dce_v11_0_show_cursor(crtc);
2514aaa36a97SAlex Deucher dce_v11_0_lock_cursor(crtc, false);
2515aaa36a97SAlex Deucher
2516aaa36a97SAlex Deucher unpin:
2517aaa36a97SAlex Deucher if (amdgpu_crtc->cursor_bo) {
25188ddef5a5SAlex Deucher struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2519c81a1a74SMichel Dänzer ret = amdgpu_bo_reserve(aobj, true);
2520aaa36a97SAlex Deucher if (likely(ret == 0)) {
25218ddef5a5SAlex Deucher amdgpu_bo_unpin(aobj);
25228ddef5a5SAlex Deucher amdgpu_bo_unreserve(aobj);
2523aaa36a97SAlex Deucher }
2524e07ddb0cSEmil Velikov drm_gem_object_put(amdgpu_crtc->cursor_bo);
2525aaa36a97SAlex Deucher }
2526aaa36a97SAlex Deucher
2527aaa36a97SAlex Deucher amdgpu_crtc->cursor_bo = obj;
2528aaa36a97SAlex Deucher return 0;
25298ddef5a5SAlex Deucher }
2530aaa36a97SAlex Deucher
dce_v11_0_cursor_reset(struct drm_crtc * crtc)25318ddef5a5SAlex Deucher static void dce_v11_0_cursor_reset(struct drm_crtc *crtc)
25328ddef5a5SAlex Deucher {
25338ddef5a5SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
25348ddef5a5SAlex Deucher
25358ddef5a5SAlex Deucher if (amdgpu_crtc->cursor_bo) {
25368ddef5a5SAlex Deucher dce_v11_0_lock_cursor(crtc, true);
25378ddef5a5SAlex Deucher
25388ddef5a5SAlex Deucher dce_v11_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
25398ddef5a5SAlex Deucher amdgpu_crtc->cursor_y);
25408ddef5a5SAlex Deucher
25418ddef5a5SAlex Deucher dce_v11_0_show_cursor(crtc);
25428ddef5a5SAlex Deucher
25438ddef5a5SAlex Deucher dce_v11_0_lock_cursor(crtc, false);
25448ddef5a5SAlex Deucher }
2545aaa36a97SAlex Deucher }
2546aaa36a97SAlex Deucher
dce_v11_0_crtc_gamma_set(struct drm_crtc * crtc,u16 * red,u16 * green,u16 * blue,uint32_t size,struct drm_modeset_acquire_ctx * ctx)25477ea77283SMaarten Lankhorst static int dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
25486d124ff8SDaniel Vetter u16 *blue, uint32_t size,
25496d124ff8SDaniel Vetter struct drm_modeset_acquire_ctx *ctx)
2550aaa36a97SAlex Deucher {
2551aaa36a97SAlex Deucher dce_v11_0_crtc_load_lut(crtc);
25527ea77283SMaarten Lankhorst
25537ea77283SMaarten Lankhorst return 0;
2554aaa36a97SAlex Deucher }
2555aaa36a97SAlex Deucher
dce_v11_0_crtc_destroy(struct drm_crtc * crtc)2556aaa36a97SAlex Deucher static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
2557aaa36a97SAlex Deucher {
2558aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2559aaa36a97SAlex Deucher
2560aaa36a97SAlex Deucher drm_crtc_cleanup(crtc);
2561aaa36a97SAlex Deucher kfree(amdgpu_crtc);
2562aaa36a97SAlex Deucher }
2563aaa36a97SAlex Deucher
2564aaa36a97SAlex Deucher static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
2565d8ee89c6SAlex Deucher .cursor_set2 = dce_v11_0_crtc_cursor_set2,
2566aaa36a97SAlex Deucher .cursor_move = dce_v11_0_crtc_cursor_move,
2567aaa36a97SAlex Deucher .gamma_set = dce_v11_0_crtc_gamma_set,
2568775a8364SSamuel Li .set_config = amdgpu_display_crtc_set_config,
2569aaa36a97SAlex Deucher .destroy = dce_v11_0_crtc_destroy,
25700cd11932SSamuel Li .page_flip_target = amdgpu_display_crtc_page_flip_target,
2571e3eff4b5SThomas Zimmermann .get_vblank_counter = amdgpu_get_vblank_counter_kms,
2572e3eff4b5SThomas Zimmermann .enable_vblank = amdgpu_enable_vblank_kms,
2573e3eff4b5SThomas Zimmermann .disable_vblank = amdgpu_disable_vblank_kms,
2574e3eff4b5SThomas Zimmermann .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
2575aaa36a97SAlex Deucher };
2576aaa36a97SAlex Deucher
dce_v11_0_crtc_dpms(struct drm_crtc * crtc,int mode)2577aaa36a97SAlex Deucher static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2578aaa36a97SAlex Deucher {
2579aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
25801348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
2581aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2582d84b272aSMichel Dänzer unsigned type;
2583aaa36a97SAlex Deucher
2584aaa36a97SAlex Deucher switch (mode) {
2585aaa36a97SAlex Deucher case DRM_MODE_DPMS_ON:
2586aaa36a97SAlex Deucher amdgpu_crtc->enabled = true;
2587aaa36a97SAlex Deucher amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2588aaa36a97SAlex Deucher dce_v11_0_vga_enable(crtc, true);
2589aaa36a97SAlex Deucher amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2590aaa36a97SAlex Deucher dce_v11_0_vga_enable(crtc, false);
2591f6c7aba4SMichel Dänzer /* Make sure VBLANK and PFLIP interrupts are still enabled */
2592734dd01dSSamuel Li type = amdgpu_display_crtc_idx_to_irq_type(adev,
2593734dd01dSSamuel Li amdgpu_crtc->crtc_id);
2594d84b272aSMichel Dänzer amdgpu_irq_update(adev, &adev->crtc_irq, type);
2595f6c7aba4SMichel Dänzer amdgpu_irq_update(adev, &adev->pageflip_irq, type);
25969a7841e9SGustavo Padovan drm_crtc_vblank_on(crtc);
2597aaa36a97SAlex Deucher dce_v11_0_crtc_load_lut(crtc);
2598aaa36a97SAlex Deucher break;
2599aaa36a97SAlex Deucher case DRM_MODE_DPMS_STANDBY:
2600aaa36a97SAlex Deucher case DRM_MODE_DPMS_SUSPEND:
2601aaa36a97SAlex Deucher case DRM_MODE_DPMS_OFF:
26029a7841e9SGustavo Padovan drm_crtc_vblank_off(crtc);
2603aaa36a97SAlex Deucher if (amdgpu_crtc->enabled) {
2604aaa36a97SAlex Deucher dce_v11_0_vga_enable(crtc, true);
2605aaa36a97SAlex Deucher amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2606aaa36a97SAlex Deucher dce_v11_0_vga_enable(crtc, false);
2607aaa36a97SAlex Deucher }
2608aaa36a97SAlex Deucher amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2609aaa36a97SAlex Deucher amdgpu_crtc->enabled = false;
2610aaa36a97SAlex Deucher break;
2611aaa36a97SAlex Deucher }
2612aaa36a97SAlex Deucher /* adjust pm to dpms */
261384176663SEvan Quan amdgpu_dpm_compute_clocks(adev);
2614aaa36a97SAlex Deucher }
2615aaa36a97SAlex Deucher
dce_v11_0_crtc_prepare(struct drm_crtc * crtc)2616aaa36a97SAlex Deucher static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
2617aaa36a97SAlex Deucher {
2618aaa36a97SAlex Deucher /* disable crtc pair power gating before programming */
2619aaa36a97SAlex Deucher amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2620aaa36a97SAlex Deucher amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2621aaa36a97SAlex Deucher dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2622aaa36a97SAlex Deucher }
2623aaa36a97SAlex Deucher
dce_v11_0_crtc_commit(struct drm_crtc * crtc)2624aaa36a97SAlex Deucher static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
2625aaa36a97SAlex Deucher {
2626aaa36a97SAlex Deucher dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2627aaa36a97SAlex Deucher amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2628aaa36a97SAlex Deucher }
2629aaa36a97SAlex Deucher
dce_v11_0_crtc_disable(struct drm_crtc * crtc)2630aaa36a97SAlex Deucher static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
2631aaa36a97SAlex Deucher {
2632aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2633aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
26341348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
2635aaa36a97SAlex Deucher struct amdgpu_atom_ss ss;
2636aaa36a97SAlex Deucher int i;
2637aaa36a97SAlex Deucher
2638aaa36a97SAlex Deucher dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2639aaa36a97SAlex Deucher if (crtc->primary->fb) {
2640aaa36a97SAlex Deucher int r;
2641765e7fbfSChristian König struct amdgpu_bo *abo;
2642aaa36a97SAlex Deucher
2643e68d14ddSDaniel Stone abo = gem_to_amdgpu_bo(crtc->primary->fb->obj[0]);
2644c81a1a74SMichel Dänzer r = amdgpu_bo_reserve(abo, true);
2645aaa36a97SAlex Deucher if (unlikely(r))
2646765e7fbfSChristian König DRM_ERROR("failed to reserve abo before unpin\n");
2647aaa36a97SAlex Deucher else {
2648765e7fbfSChristian König amdgpu_bo_unpin(abo);
2649765e7fbfSChristian König amdgpu_bo_unreserve(abo);
2650aaa36a97SAlex Deucher }
2651aaa36a97SAlex Deucher }
2652aaa36a97SAlex Deucher /* disable the GRPH */
2653aaa36a97SAlex Deucher dce_v11_0_grph_enable(crtc, false);
2654aaa36a97SAlex Deucher
2655aaa36a97SAlex Deucher amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2656aaa36a97SAlex Deucher
2657aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
2658aaa36a97SAlex Deucher if (adev->mode_info.crtcs[i] &&
2659aaa36a97SAlex Deucher adev->mode_info.crtcs[i]->enabled &&
2660aaa36a97SAlex Deucher i != amdgpu_crtc->crtc_id &&
2661aaa36a97SAlex Deucher amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2662aaa36a97SAlex Deucher /* one other crtc is using this pll don't turn
2663aaa36a97SAlex Deucher * off the pll
2664aaa36a97SAlex Deucher */
2665aaa36a97SAlex Deucher goto done;
2666aaa36a97SAlex Deucher }
2667aaa36a97SAlex Deucher }
2668aaa36a97SAlex Deucher
2669aaa36a97SAlex Deucher switch (amdgpu_crtc->pll_id) {
2670aaa36a97SAlex Deucher case ATOM_PPLL0:
2671aaa36a97SAlex Deucher case ATOM_PPLL1:
2672aaa36a97SAlex Deucher case ATOM_PPLL2:
2673aaa36a97SAlex Deucher /* disable the ppll */
2674aaa36a97SAlex Deucher amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2675aaa36a97SAlex Deucher 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2676aaa36a97SAlex Deucher break;
267722f0c5bdSAlex Deucher case ATOM_COMBOPHY_PLL0:
267822f0c5bdSAlex Deucher case ATOM_COMBOPHY_PLL1:
267922f0c5bdSAlex Deucher case ATOM_COMBOPHY_PLL2:
268022f0c5bdSAlex Deucher case ATOM_COMBOPHY_PLL3:
268122f0c5bdSAlex Deucher case ATOM_COMBOPHY_PLL4:
268222f0c5bdSAlex Deucher case ATOM_COMBOPHY_PLL5:
268322f0c5bdSAlex Deucher /* disable the ppll */
268422f0c5bdSAlex Deucher amdgpu_atombios_crtc_program_pll(crtc, ATOM_CRTC_INVALID, amdgpu_crtc->pll_id,
268522f0c5bdSAlex Deucher 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
268622f0c5bdSAlex Deucher break;
2687aaa36a97SAlex Deucher default:
2688aaa36a97SAlex Deucher break;
2689aaa36a97SAlex Deucher }
2690aaa36a97SAlex Deucher done:
2691aaa36a97SAlex Deucher amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2692aaa36a97SAlex Deucher amdgpu_crtc->adjusted_clock = 0;
2693aaa36a97SAlex Deucher amdgpu_crtc->encoder = NULL;
2694aaa36a97SAlex Deucher amdgpu_crtc->connector = NULL;
2695aaa36a97SAlex Deucher }
2696aaa36a97SAlex Deucher
dce_v11_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)2697aaa36a97SAlex Deucher static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
2698aaa36a97SAlex Deucher struct drm_display_mode *mode,
2699aaa36a97SAlex Deucher struct drm_display_mode *adjusted_mode,
2700aaa36a97SAlex Deucher int x, int y, struct drm_framebuffer *old_fb)
2701aaa36a97SAlex Deucher {
2702aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
270322f0c5bdSAlex Deucher struct drm_device *dev = crtc->dev;
27041348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
2705aaa36a97SAlex Deucher
2706aaa36a97SAlex Deucher if (!amdgpu_crtc->adjusted_clock)
2707aaa36a97SAlex Deucher return -EINVAL;
2708aaa36a97SAlex Deucher
27092cc0c0b5SFlora Cui if ((adev->asic_type == CHIP_POLARIS10) ||
2710c4642a47SJunwei Zhang (adev->asic_type == CHIP_POLARIS11) ||
2711221adb21SAlex Deucher (adev->asic_type == CHIP_POLARIS12) ||
2712221adb21SAlex Deucher (adev->asic_type == CHIP_VEGAM)) {
271322f0c5bdSAlex Deucher struct amdgpu_encoder *amdgpu_encoder =
271422f0c5bdSAlex Deucher to_amdgpu_encoder(amdgpu_crtc->encoder);
271522f0c5bdSAlex Deucher int encoder_mode =
271622f0c5bdSAlex Deucher amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
271722f0c5bdSAlex Deucher
271822f0c5bdSAlex Deucher /* SetPixelClock calculates the plls and ss values now */
271922f0c5bdSAlex Deucher amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id,
272022f0c5bdSAlex Deucher amdgpu_crtc->pll_id,
272122f0c5bdSAlex Deucher encoder_mode, amdgpu_encoder->encoder_id,
272222f0c5bdSAlex Deucher adjusted_mode->clock, 0, 0, 0, 0,
272322f0c5bdSAlex Deucher amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
272422f0c5bdSAlex Deucher } else {
2725aaa36a97SAlex Deucher amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
272622f0c5bdSAlex Deucher }
2727aaa36a97SAlex Deucher amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2728aaa36a97SAlex Deucher dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2729aaa36a97SAlex Deucher amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2730aaa36a97SAlex Deucher amdgpu_atombios_crtc_scaler_setup(crtc);
27318ddef5a5SAlex Deucher dce_v11_0_cursor_reset(crtc);
2732aaa36a97SAlex Deucher /* update the hw version fpr dpm */
2733aaa36a97SAlex Deucher amdgpu_crtc->hw_mode = *adjusted_mode;
2734aaa36a97SAlex Deucher
2735aaa36a97SAlex Deucher return 0;
2736aaa36a97SAlex Deucher }
2737aaa36a97SAlex Deucher
dce_v11_0_crtc_mode_fixup(struct drm_crtc * crtc,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)2738aaa36a97SAlex Deucher static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
2739aaa36a97SAlex Deucher const struct drm_display_mode *mode,
2740aaa36a97SAlex Deucher struct drm_display_mode *adjusted_mode)
2741aaa36a97SAlex Deucher {
2742aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2743aaa36a97SAlex Deucher struct drm_device *dev = crtc->dev;
2744aaa36a97SAlex Deucher struct drm_encoder *encoder;
2745aaa36a97SAlex Deucher
2746aaa36a97SAlex Deucher /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2747aaa36a97SAlex Deucher list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2748aaa36a97SAlex Deucher if (encoder->crtc == crtc) {
2749aaa36a97SAlex Deucher amdgpu_crtc->encoder = encoder;
2750aaa36a97SAlex Deucher amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2751aaa36a97SAlex Deucher break;
2752aaa36a97SAlex Deucher }
2753aaa36a97SAlex Deucher }
2754aaa36a97SAlex Deucher if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2755aaa36a97SAlex Deucher amdgpu_crtc->encoder = NULL;
2756aaa36a97SAlex Deucher amdgpu_crtc->connector = NULL;
2757aaa36a97SAlex Deucher return false;
2758aaa36a97SAlex Deucher }
27590c16443aSSamuel Li if (!amdgpu_display_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2760aaa36a97SAlex Deucher return false;
2761aaa36a97SAlex Deucher if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2762aaa36a97SAlex Deucher return false;
2763aaa36a97SAlex Deucher /* pick pll */
2764aaa36a97SAlex Deucher amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
2765aaa36a97SAlex Deucher /* if we can't get a PPLL for a non-DP encoder, fail */
2766aaa36a97SAlex Deucher if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2767aaa36a97SAlex Deucher !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2768aaa36a97SAlex Deucher return false;
2769aaa36a97SAlex Deucher
2770aaa36a97SAlex Deucher return true;
2771aaa36a97SAlex Deucher }
2772aaa36a97SAlex Deucher
dce_v11_0_crtc_set_base(struct drm_crtc * crtc,int x,int y,struct drm_framebuffer * old_fb)2773aaa36a97SAlex Deucher static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2774aaa36a97SAlex Deucher struct drm_framebuffer *old_fb)
2775aaa36a97SAlex Deucher {
2776aaa36a97SAlex Deucher return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2777aaa36a97SAlex Deucher }
2778aaa36a97SAlex Deucher
dce_v11_0_crtc_set_base_atomic(struct drm_crtc * crtc,struct drm_framebuffer * fb,int x,int y,enum mode_set_atomic state)2779aaa36a97SAlex Deucher static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2780aaa36a97SAlex Deucher struct drm_framebuffer *fb,
2781aaa36a97SAlex Deucher int x, int y, enum mode_set_atomic state)
2782aaa36a97SAlex Deucher {
2783aaa36a97SAlex Deucher return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
2784aaa36a97SAlex Deucher }
2785aaa36a97SAlex Deucher
2786aaa36a97SAlex Deucher static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
2787aaa36a97SAlex Deucher .dpms = dce_v11_0_crtc_dpms,
2788aaa36a97SAlex Deucher .mode_fixup = dce_v11_0_crtc_mode_fixup,
2789aaa36a97SAlex Deucher .mode_set = dce_v11_0_crtc_mode_set,
2790aaa36a97SAlex Deucher .mode_set_base = dce_v11_0_crtc_set_base,
2791aaa36a97SAlex Deucher .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
2792aaa36a97SAlex Deucher .prepare = dce_v11_0_crtc_prepare,
2793aaa36a97SAlex Deucher .commit = dce_v11_0_crtc_commit,
2794aaa36a97SAlex Deucher .disable = dce_v11_0_crtc_disable,
2795ea702333SThomas Zimmermann .get_scanout_position = amdgpu_crtc_get_scanout_position,
2796aaa36a97SAlex Deucher };
2797aaa36a97SAlex Deucher
dce_v11_0_crtc_init(struct amdgpu_device * adev,int index)2798aaa36a97SAlex Deucher static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
2799aaa36a97SAlex Deucher {
2800aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc;
2801aaa36a97SAlex Deucher
2802aaa36a97SAlex Deucher amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2803aaa36a97SAlex Deucher (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2804aaa36a97SAlex Deucher if (amdgpu_crtc == NULL)
2805aaa36a97SAlex Deucher return -ENOMEM;
2806aaa36a97SAlex Deucher
28074a580877SLuben Tuikov drm_crtc_init(adev_to_drm(adev), &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
2808aaa36a97SAlex Deucher
2809aaa36a97SAlex Deucher drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2810aaa36a97SAlex Deucher amdgpu_crtc->crtc_id = index;
2811aaa36a97SAlex Deucher adev->mode_info.crtcs[index] = amdgpu_crtc;
2812aaa36a97SAlex Deucher
2813aaa36a97SAlex Deucher amdgpu_crtc->max_cursor_width = 128;
2814aaa36a97SAlex Deucher amdgpu_crtc->max_cursor_height = 128;
28154a580877SLuben Tuikov adev_to_drm(adev)->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
28164a580877SLuben Tuikov adev_to_drm(adev)->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2817aaa36a97SAlex Deucher
2818aaa36a97SAlex Deucher switch (amdgpu_crtc->crtc_id) {
2819aaa36a97SAlex Deucher case 0:
2820aaa36a97SAlex Deucher default:
2821aaa36a97SAlex Deucher amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
2822aaa36a97SAlex Deucher break;
2823aaa36a97SAlex Deucher case 1:
2824aaa36a97SAlex Deucher amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
2825aaa36a97SAlex Deucher break;
2826aaa36a97SAlex Deucher case 2:
2827aaa36a97SAlex Deucher amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
2828aaa36a97SAlex Deucher break;
2829aaa36a97SAlex Deucher case 3:
2830aaa36a97SAlex Deucher amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
2831aaa36a97SAlex Deucher break;
2832aaa36a97SAlex Deucher case 4:
2833aaa36a97SAlex Deucher amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
2834aaa36a97SAlex Deucher break;
2835aaa36a97SAlex Deucher case 5:
2836aaa36a97SAlex Deucher amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
2837aaa36a97SAlex Deucher break;
2838aaa36a97SAlex Deucher }
2839aaa36a97SAlex Deucher
2840aaa36a97SAlex Deucher amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2841aaa36a97SAlex Deucher amdgpu_crtc->adjusted_clock = 0;
2842aaa36a97SAlex Deucher amdgpu_crtc->encoder = NULL;
2843aaa36a97SAlex Deucher amdgpu_crtc->connector = NULL;
2844aaa36a97SAlex Deucher drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
2845aaa36a97SAlex Deucher
2846aaa36a97SAlex Deucher return 0;
2847aaa36a97SAlex Deucher }
2848aaa36a97SAlex Deucher
dce_v11_0_early_init(void * handle)28495fc3aeebSyanyang1 static int dce_v11_0_early_init(void *handle)
2850aaa36a97SAlex Deucher {
28515fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
28525fc3aeebSyanyang1
2853aaa36a97SAlex Deucher adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
2854aaa36a97SAlex Deucher adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
2855aaa36a97SAlex Deucher
2856aaa36a97SAlex Deucher dce_v11_0_set_display_funcs(adev);
2857aaa36a97SAlex Deucher
285883c9b025SEmily Deng adev->mode_info.num_crtc = dce_v11_0_get_num_crtc(adev);
285983c9b025SEmily Deng
2860aaa36a97SAlex Deucher switch (adev->asic_type) {
2861aaa36a97SAlex Deucher case CHIP_CARRIZO:
2862aaa36a97SAlex Deucher adev->mode_info.num_hpd = 6;
2863aaa36a97SAlex Deucher adev->mode_info.num_dig = 9;
2864aaa36a97SAlex Deucher break;
2865fa2f9befSSamuel Li case CHIP_STONEY:
2866fa2f9befSSamuel Li adev->mode_info.num_hpd = 6;
2867fa2f9befSSamuel Li adev->mode_info.num_dig = 9;
2868fa2f9befSSamuel Li break;
28692cc0c0b5SFlora Cui case CHIP_POLARIS10:
2870221adb21SAlex Deucher case CHIP_VEGAM:
2871d525eb8dSAlex Deucher adev->mode_info.num_hpd = 6;
2872d525eb8dSAlex Deucher adev->mode_info.num_dig = 6;
2873d525eb8dSAlex Deucher break;
28742cc0c0b5SFlora Cui case CHIP_POLARIS11:
2875c4642a47SJunwei Zhang case CHIP_POLARIS12:
2876d525eb8dSAlex Deucher adev->mode_info.num_hpd = 5;
2877d525eb8dSAlex Deucher adev->mode_info.num_dig = 5;
2878d525eb8dSAlex Deucher break;
2879aaa36a97SAlex Deucher default:
2880aaa36a97SAlex Deucher /* FIXME: not supported yet */
2881aaa36a97SAlex Deucher return -EINVAL;
2882aaa36a97SAlex Deucher }
2883aaa36a97SAlex Deucher
2884d794b9f8SMichel Dänzer dce_v11_0_set_irq_funcs(adev);
2885d794b9f8SMichel Dänzer
2886aaa36a97SAlex Deucher return 0;
2887aaa36a97SAlex Deucher }
2888aaa36a97SAlex Deucher
dce_v11_0_sw_init(void * handle)28895fc3aeebSyanyang1 static int dce_v11_0_sw_init(void *handle)
2890aaa36a97SAlex Deucher {
2891aaa36a97SAlex Deucher int r, i;
28925fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2893aaa36a97SAlex Deucher
2894aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
28951ffdeca6SChristian König r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
2896aaa36a97SAlex Deucher if (r)
2897aaa36a97SAlex Deucher return r;
2898aaa36a97SAlex Deucher }
2899aaa36a97SAlex Deucher
2900091aec0bSAndrey Grodzovsky for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; i < 20; i += 2) {
29011ffdeca6SChristian König r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i, &adev->pageflip_irq);
2902aaa36a97SAlex Deucher if (r)
2903aaa36a97SAlex Deucher return r;
2904aaa36a97SAlex Deucher }
2905aaa36a97SAlex Deucher
2906aaa36a97SAlex Deucher /* HPD hotplug */
29071ffdeca6SChristian König r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
2908aaa36a97SAlex Deucher if (r)
2909aaa36a97SAlex Deucher return r;
2910aaa36a97SAlex Deucher
29114a580877SLuben Tuikov adev_to_drm(adev)->mode_config.funcs = &amdgpu_mode_funcs;
2912aaa36a97SAlex Deucher
29134a580877SLuben Tuikov adev_to_drm(adev)->mode_config.async_page_flip = true;
2914cb9e59d7SAlex Deucher
29154a580877SLuben Tuikov adev_to_drm(adev)->mode_config.max_width = 16384;
29164a580877SLuben Tuikov adev_to_drm(adev)->mode_config.max_height = 16384;
2917aaa36a97SAlex Deucher
29184a580877SLuben Tuikov adev_to_drm(adev)->mode_config.preferred_depth = 24;
2919a6250bdbSAlex Deucher adev_to_drm(adev)->mode_config.prefer_shadow = 1;
2920aaa36a97SAlex Deucher
29212af10429STomohito Esaki adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
29222af10429STomohito Esaki
29233dc9b1ceSSamuel Li r = amdgpu_display_modeset_create_props(adev);
2924aaa36a97SAlex Deucher if (r)
2925aaa36a97SAlex Deucher return r;
2926aaa36a97SAlex Deucher
29274a580877SLuben Tuikov adev_to_drm(adev)->mode_config.max_width = 16384;
29284a580877SLuben Tuikov adev_to_drm(adev)->mode_config.max_height = 16384;
2929aaa36a97SAlex Deucher
2930c437b9d6STom St Denis
2931aaa36a97SAlex Deucher /* allocate crtcs */
2932aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.num_crtc; i++) {
2933aaa36a97SAlex Deucher r = dce_v11_0_crtc_init(adev, i);
2934aaa36a97SAlex Deucher if (r)
2935aaa36a97SAlex Deucher return r;
2936aaa36a97SAlex Deucher }
2937aaa36a97SAlex Deucher
2938aaa36a97SAlex Deucher if (amdgpu_atombios_get_connector_info_from_object_table(adev))
29394a580877SLuben Tuikov amdgpu_display_print_display_setup(adev_to_drm(adev));
2940aaa36a97SAlex Deucher else
2941aaa36a97SAlex Deucher return -EINVAL;
2942aaa36a97SAlex Deucher
2943aaa36a97SAlex Deucher /* setup afmt */
2944041ab0a4STom St Denis r = dce_v11_0_afmt_init(adev);
2945041ab0a4STom St Denis if (r)
2946041ab0a4STom St Denis return r;
2947aaa36a97SAlex Deucher
2948aaa36a97SAlex Deucher r = dce_v11_0_audio_init(adev);
2949aaa36a97SAlex Deucher if (r)
2950aaa36a97SAlex Deucher return r;
2951aaa36a97SAlex Deucher
2952a347ca97SAlex Deucher /* Disable vblank IRQs aggressively for power-saving */
2953a347ca97SAlex Deucher /* XXX: can this be enabled for DC? */
2954a347ca97SAlex Deucher adev_to_drm(adev)->vblank_disable_immediate = true;
2955a347ca97SAlex Deucher
2956a347ca97SAlex Deucher r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
2957a347ca97SAlex Deucher if (r)
2958a347ca97SAlex Deucher return r;
2959a347ca97SAlex Deucher
296090f56611Sxurui INIT_DELAYED_WORK(&adev->hotplug_work,
2961a347ca97SAlex Deucher amdgpu_display_hotplug_work_func);
2962a347ca97SAlex Deucher
29634a580877SLuben Tuikov drm_kms_helper_poll_init(adev_to_drm(adev));
2964aaa36a97SAlex Deucher
2965c437b9d6STom St Denis adev->mode_info.mode_config_initialized = true;
2966c437b9d6STom St Denis return 0;
2967aaa36a97SAlex Deucher }
2968aaa36a97SAlex Deucher
dce_v11_0_sw_fini(void * handle)29695fc3aeebSyanyang1 static int dce_v11_0_sw_fini(void *handle)
2970aaa36a97SAlex Deucher {
29715fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
29725fc3aeebSyanyang1
2973aaa36a97SAlex Deucher kfree(adev->mode_info.bios_hardcoded_edid);
2974aaa36a97SAlex Deucher
29754a580877SLuben Tuikov drm_kms_helper_poll_fini(adev_to_drm(adev));
2976aaa36a97SAlex Deucher
2977aaa36a97SAlex Deucher dce_v11_0_audio_fini(adev);
2978aaa36a97SAlex Deucher
2979aaa36a97SAlex Deucher dce_v11_0_afmt_fini(adev);
2980aaa36a97SAlex Deucher
29814a580877SLuben Tuikov drm_mode_config_cleanup(adev_to_drm(adev));
2982aaa36a97SAlex Deucher adev->mode_info.mode_config_initialized = false;
2983aaa36a97SAlex Deucher
2984aaa36a97SAlex Deucher return 0;
2985aaa36a97SAlex Deucher }
2986aaa36a97SAlex Deucher
dce_v11_0_hw_init(void * handle)29875fc3aeebSyanyang1 static int dce_v11_0_hw_init(void *handle)
2988aaa36a97SAlex Deucher {
2989aaa36a97SAlex Deucher int i;
29905fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2991aaa36a97SAlex Deucher
2992aaa36a97SAlex Deucher dce_v11_0_init_golden_registers(adev);
2993aaa36a97SAlex Deucher
299484b5d3d1SAlex Deucher /* disable vga render */
299584b5d3d1SAlex Deucher dce_v11_0_set_vga_render_state(adev, false);
2996aaa36a97SAlex Deucher /* init dig PHYs, disp eng pll */
2997f9fff064SAlex Deucher amdgpu_atombios_crtc_powergate_init(adev);
2998aaa36a97SAlex Deucher amdgpu_atombios_encoder_init_dig(adev);
29992cc0c0b5SFlora Cui if ((adev->asic_type == CHIP_POLARIS10) ||
3000c4642a47SJunwei Zhang (adev->asic_type == CHIP_POLARIS11) ||
3001221adb21SAlex Deucher (adev->asic_type == CHIP_POLARIS12) ||
3002221adb21SAlex Deucher (adev->asic_type == CHIP_VEGAM)) {
3003b18e6ad7SAlex Deucher amdgpu_atombios_crtc_set_dce_clock(adev, adev->clock.default_dispclk,
3004b18e6ad7SAlex Deucher DCE_CLOCK_TYPE_DISPCLK, ATOM_GCK_DFS);
3005b18e6ad7SAlex Deucher amdgpu_atombios_crtc_set_dce_clock(adev, 0,
3006b18e6ad7SAlex Deucher DCE_CLOCK_TYPE_DPREFCLK, ATOM_GCK_DFS);
3007b18e6ad7SAlex Deucher } else {
3008aaa36a97SAlex Deucher amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
3009b18e6ad7SAlex Deucher }
3010aaa36a97SAlex Deucher
3011aaa36a97SAlex Deucher /* initialize hpd */
3012aaa36a97SAlex Deucher dce_v11_0_hpd_init(adev);
3013aaa36a97SAlex Deucher
3014aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3015aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3016aaa36a97SAlex Deucher }
3017aaa36a97SAlex Deucher
3018f6c7aba4SMichel Dänzer dce_v11_0_pageflip_interrupt_init(adev);
3019f6c7aba4SMichel Dänzer
3020aaa36a97SAlex Deucher return 0;
3021aaa36a97SAlex Deucher }
3022aaa36a97SAlex Deucher
dce_v11_0_hw_fini(void * handle)30235fc3aeebSyanyang1 static int dce_v11_0_hw_fini(void *handle)
3024aaa36a97SAlex Deucher {
3025aaa36a97SAlex Deucher int i;
30265fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3027aaa36a97SAlex Deucher
3028aaa36a97SAlex Deucher dce_v11_0_hpd_fini(adev);
3029aaa36a97SAlex Deucher
3030aaa36a97SAlex Deucher for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3031aaa36a97SAlex Deucher dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3032aaa36a97SAlex Deucher }
3033aaa36a97SAlex Deucher
3034f6c7aba4SMichel Dänzer dce_v11_0_pageflip_interrupt_fini(adev);
3035f6c7aba4SMichel Dänzer
303690f56611Sxurui flush_delayed_work(&adev->hotplug_work);
3037a347ca97SAlex Deucher
3038aaa36a97SAlex Deucher return 0;
3039aaa36a97SAlex Deucher }
3040aaa36a97SAlex Deucher
dce_v11_0_suspend(void * handle)30415fc3aeebSyanyang1 static int dce_v11_0_suspend(void *handle)
3042aaa36a97SAlex Deucher {
3043a59b3c80SAlex Deucher struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3044a2e15b0eSAlex Deucher int r;
3045a2e15b0eSAlex Deucher
3046a2e15b0eSAlex Deucher r = amdgpu_display_suspend_helper(adev);
3047a2e15b0eSAlex Deucher if (r)
3048a2e15b0eSAlex Deucher return r;
3049a59b3c80SAlex Deucher
3050a59b3c80SAlex Deucher adev->mode_info.bl_level =
3051a59b3c80SAlex Deucher amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
3052a59b3c80SAlex Deucher
3053f9fff064SAlex Deucher return dce_v11_0_hw_fini(handle);
3054aaa36a97SAlex Deucher }
3055aaa36a97SAlex Deucher
dce_v11_0_resume(void * handle)30565fc3aeebSyanyang1 static int dce_v11_0_resume(void *handle)
3057aaa36a97SAlex Deucher {
30585fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3059f9fff064SAlex Deucher int ret;
3060aaa36a97SAlex Deucher
3061a59b3c80SAlex Deucher amdgpu_atombios_encoder_set_backlight_level_to_reg(adev,
3062a59b3c80SAlex Deucher adev->mode_info.bl_level);
3063a59b3c80SAlex Deucher
3064f9fff064SAlex Deucher ret = dce_v11_0_hw_init(handle);
3065aaa36a97SAlex Deucher
3066aaa36a97SAlex Deucher /* turn on the BL */
3067aaa36a97SAlex Deucher if (adev->mode_info.bl_encoder) {
3068aaa36a97SAlex Deucher u8 bl_level = amdgpu_display_backlight_get_level(adev,
3069aaa36a97SAlex Deucher adev->mode_info.bl_encoder);
3070aaa36a97SAlex Deucher amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
3071aaa36a97SAlex Deucher bl_level);
3072aaa36a97SAlex Deucher }
3073a2e15b0eSAlex Deucher if (ret)
3074f9fff064SAlex Deucher return ret;
3075a2e15b0eSAlex Deucher
3076a2e15b0eSAlex Deucher return amdgpu_display_resume_helper(adev);
3077aaa36a97SAlex Deucher }
3078aaa36a97SAlex Deucher
dce_v11_0_is_idle(void * handle)30795fc3aeebSyanyang1 static bool dce_v11_0_is_idle(void *handle)
3080aaa36a97SAlex Deucher {
3081aaa36a97SAlex Deucher return true;
3082aaa36a97SAlex Deucher }
3083aaa36a97SAlex Deucher
dce_v11_0_wait_for_idle(void * handle)30845fc3aeebSyanyang1 static int dce_v11_0_wait_for_idle(void *handle)
3085aaa36a97SAlex Deucher {
3086aaa36a97SAlex Deucher return 0;
3087aaa36a97SAlex Deucher }
3088aaa36a97SAlex Deucher
dce_v11_0_soft_reset(void * handle)30895fc3aeebSyanyang1 static int dce_v11_0_soft_reset(void *handle)
3090aaa36a97SAlex Deucher {
3091aaa36a97SAlex Deucher u32 srbm_soft_reset = 0, tmp;
30925fc3aeebSyanyang1 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3093aaa36a97SAlex Deucher
3094aaa36a97SAlex Deucher if (dce_v11_0_is_display_hung(adev))
3095aaa36a97SAlex Deucher srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
3096aaa36a97SAlex Deucher
3097aaa36a97SAlex Deucher if (srbm_soft_reset) {
3098aaa36a97SAlex Deucher tmp = RREG32(mmSRBM_SOFT_RESET);
3099aaa36a97SAlex Deucher tmp |= srbm_soft_reset;
3100aaa36a97SAlex Deucher dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
3101aaa36a97SAlex Deucher WREG32(mmSRBM_SOFT_RESET, tmp);
3102aaa36a97SAlex Deucher tmp = RREG32(mmSRBM_SOFT_RESET);
3103aaa36a97SAlex Deucher
3104aaa36a97SAlex Deucher udelay(50);
3105aaa36a97SAlex Deucher
3106aaa36a97SAlex Deucher tmp &= ~srbm_soft_reset;
3107aaa36a97SAlex Deucher WREG32(mmSRBM_SOFT_RESET, tmp);
3108aaa36a97SAlex Deucher tmp = RREG32(mmSRBM_SOFT_RESET);
3109aaa36a97SAlex Deucher
3110aaa36a97SAlex Deucher /* Wait a little for things to settle down */
3111aaa36a97SAlex Deucher udelay(50);
3112aaa36a97SAlex Deucher }
3113aaa36a97SAlex Deucher return 0;
3114aaa36a97SAlex Deucher }
3115aaa36a97SAlex Deucher
dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device * adev,int crtc,enum amdgpu_interrupt_state state)3116aaa36a97SAlex Deucher static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
3117aaa36a97SAlex Deucher int crtc,
3118aaa36a97SAlex Deucher enum amdgpu_interrupt_state state)
3119aaa36a97SAlex Deucher {
3120aaa36a97SAlex Deucher u32 lb_interrupt_mask;
3121aaa36a97SAlex Deucher
3122aaa36a97SAlex Deucher if (crtc >= adev->mode_info.num_crtc) {
3123aaa36a97SAlex Deucher DRM_DEBUG("invalid crtc %d\n", crtc);
3124aaa36a97SAlex Deucher return;
3125aaa36a97SAlex Deucher }
3126aaa36a97SAlex Deucher
3127aaa36a97SAlex Deucher switch (state) {
3128aaa36a97SAlex Deucher case AMDGPU_IRQ_STATE_DISABLE:
3129aaa36a97SAlex Deucher lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3130aaa36a97SAlex Deucher lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3131aaa36a97SAlex Deucher VBLANK_INTERRUPT_MASK, 0);
3132aaa36a97SAlex Deucher WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3133aaa36a97SAlex Deucher break;
3134aaa36a97SAlex Deucher case AMDGPU_IRQ_STATE_ENABLE:
3135aaa36a97SAlex Deucher lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3136aaa36a97SAlex Deucher lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3137aaa36a97SAlex Deucher VBLANK_INTERRUPT_MASK, 1);
3138aaa36a97SAlex Deucher WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3139aaa36a97SAlex Deucher break;
3140aaa36a97SAlex Deucher default:
3141aaa36a97SAlex Deucher break;
3142aaa36a97SAlex Deucher }
3143aaa36a97SAlex Deucher }
3144aaa36a97SAlex Deucher
dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device * adev,int crtc,enum amdgpu_interrupt_state state)3145aaa36a97SAlex Deucher static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
3146aaa36a97SAlex Deucher int crtc,
3147aaa36a97SAlex Deucher enum amdgpu_interrupt_state state)
3148aaa36a97SAlex Deucher {
3149aaa36a97SAlex Deucher u32 lb_interrupt_mask;
3150aaa36a97SAlex Deucher
3151aaa36a97SAlex Deucher if (crtc >= adev->mode_info.num_crtc) {
3152aaa36a97SAlex Deucher DRM_DEBUG("invalid crtc %d\n", crtc);
3153aaa36a97SAlex Deucher return;
3154aaa36a97SAlex Deucher }
3155aaa36a97SAlex Deucher
3156aaa36a97SAlex Deucher switch (state) {
3157aaa36a97SAlex Deucher case AMDGPU_IRQ_STATE_DISABLE:
3158aaa36a97SAlex Deucher lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3159aaa36a97SAlex Deucher lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3160aaa36a97SAlex Deucher VLINE_INTERRUPT_MASK, 0);
3161aaa36a97SAlex Deucher WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3162aaa36a97SAlex Deucher break;
3163aaa36a97SAlex Deucher case AMDGPU_IRQ_STATE_ENABLE:
3164aaa36a97SAlex Deucher lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3165aaa36a97SAlex Deucher lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3166aaa36a97SAlex Deucher VLINE_INTERRUPT_MASK, 1);
3167aaa36a97SAlex Deucher WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3168aaa36a97SAlex Deucher break;
3169aaa36a97SAlex Deucher default:
3170aaa36a97SAlex Deucher break;
3171aaa36a97SAlex Deucher }
3172aaa36a97SAlex Deucher }
3173aaa36a97SAlex Deucher
dce_v11_0_set_hpd_irq_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned hpd,enum amdgpu_interrupt_state state)3174aaa36a97SAlex Deucher static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
3175aaa36a97SAlex Deucher struct amdgpu_irq_src *source,
3176aaa36a97SAlex Deucher unsigned hpd,
3177aaa36a97SAlex Deucher enum amdgpu_interrupt_state state)
3178aaa36a97SAlex Deucher {
3179aaa36a97SAlex Deucher u32 tmp;
3180aaa36a97SAlex Deucher
3181aaa36a97SAlex Deucher if (hpd >= adev->mode_info.num_hpd) {
3182aaa36a97SAlex Deucher DRM_DEBUG("invalid hdp %d\n", hpd);
3183aaa36a97SAlex Deucher return 0;
3184aaa36a97SAlex Deucher }
3185aaa36a97SAlex Deucher
3186aaa36a97SAlex Deucher switch (state) {
3187aaa36a97SAlex Deucher case AMDGPU_IRQ_STATE_DISABLE:
3188aaa36a97SAlex Deucher tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3189aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
3190aaa36a97SAlex Deucher WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3191aaa36a97SAlex Deucher break;
3192aaa36a97SAlex Deucher case AMDGPU_IRQ_STATE_ENABLE:
3193aaa36a97SAlex Deucher tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3194aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
3195aaa36a97SAlex Deucher WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3196aaa36a97SAlex Deucher break;
3197aaa36a97SAlex Deucher default:
3198aaa36a97SAlex Deucher break;
3199aaa36a97SAlex Deucher }
3200aaa36a97SAlex Deucher
3201aaa36a97SAlex Deucher return 0;
3202aaa36a97SAlex Deucher }
3203aaa36a97SAlex Deucher
dce_v11_0_set_crtc_irq_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned type,enum amdgpu_interrupt_state state)3204aaa36a97SAlex Deucher static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
3205aaa36a97SAlex Deucher struct amdgpu_irq_src *source,
3206aaa36a97SAlex Deucher unsigned type,
3207aaa36a97SAlex Deucher enum amdgpu_interrupt_state state)
3208aaa36a97SAlex Deucher {
3209aaa36a97SAlex Deucher switch (type) {
3210aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VBLANK1:
3211aaa36a97SAlex Deucher dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3212aaa36a97SAlex Deucher break;
3213aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VBLANK2:
3214aaa36a97SAlex Deucher dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3215aaa36a97SAlex Deucher break;
3216aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VBLANK3:
3217aaa36a97SAlex Deucher dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3218aaa36a97SAlex Deucher break;
3219aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VBLANK4:
3220aaa36a97SAlex Deucher dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3221aaa36a97SAlex Deucher break;
3222aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VBLANK5:
3223aaa36a97SAlex Deucher dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3224aaa36a97SAlex Deucher break;
3225aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VBLANK6:
3226aaa36a97SAlex Deucher dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3227aaa36a97SAlex Deucher break;
3228aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VLINE1:
3229aaa36a97SAlex Deucher dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
3230aaa36a97SAlex Deucher break;
3231aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VLINE2:
3232aaa36a97SAlex Deucher dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
3233aaa36a97SAlex Deucher break;
3234aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VLINE3:
3235aaa36a97SAlex Deucher dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
3236aaa36a97SAlex Deucher break;
3237aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VLINE4:
3238aaa36a97SAlex Deucher dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
3239aaa36a97SAlex Deucher break;
3240aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VLINE5:
3241aaa36a97SAlex Deucher dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
3242aaa36a97SAlex Deucher break;
3243aaa36a97SAlex Deucher case AMDGPU_CRTC_IRQ_VLINE6:
3244aaa36a97SAlex Deucher dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
3245aaa36a97SAlex Deucher break;
3246aaa36a97SAlex Deucher default:
3247aaa36a97SAlex Deucher break;
3248aaa36a97SAlex Deucher }
3249aaa36a97SAlex Deucher return 0;
3250aaa36a97SAlex Deucher }
3251aaa36a97SAlex Deucher
dce_v11_0_set_pageflip_irq_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)3252aaa36a97SAlex Deucher static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
3253aaa36a97SAlex Deucher struct amdgpu_irq_src *src,
3254aaa36a97SAlex Deucher unsigned type,
3255aaa36a97SAlex Deucher enum amdgpu_interrupt_state state)
3256aaa36a97SAlex Deucher {
32577dfac896SAlex Deucher u32 reg;
32587dfac896SAlex Deucher
32597dfac896SAlex Deucher if (type >= adev->mode_info.num_crtc) {
3260aaa36a97SAlex Deucher DRM_ERROR("invalid pageflip crtc %d\n", type);
3261aaa36a97SAlex Deucher return -EINVAL;
3262aaa36a97SAlex Deucher }
3263aaa36a97SAlex Deucher
32647dfac896SAlex Deucher reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3265aaa36a97SAlex Deucher if (state == AMDGPU_IRQ_STATE_DISABLE)
32667dfac896SAlex Deucher WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
32677dfac896SAlex Deucher reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3268aaa36a97SAlex Deucher else
32697dfac896SAlex Deucher WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
32707dfac896SAlex Deucher reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3271aaa36a97SAlex Deucher
3272aaa36a97SAlex Deucher return 0;
3273aaa36a97SAlex Deucher }
3274aaa36a97SAlex Deucher
dce_v11_0_pageflip_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3275aaa36a97SAlex Deucher static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
3276aaa36a97SAlex Deucher struct amdgpu_irq_src *source,
3277aaa36a97SAlex Deucher struct amdgpu_iv_entry *entry)
3278aaa36a97SAlex Deucher {
3279aaa36a97SAlex Deucher unsigned long flags;
3280aaa36a97SAlex Deucher unsigned crtc_id;
3281aaa36a97SAlex Deucher struct amdgpu_crtc *amdgpu_crtc;
3282aaa36a97SAlex Deucher struct amdgpu_flip_work *works;
3283aaa36a97SAlex Deucher
3284aaa36a97SAlex Deucher crtc_id = (entry->src_id - 8) >> 1;
3285aaa36a97SAlex Deucher amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3286aaa36a97SAlex Deucher
32877dfac896SAlex Deucher if (crtc_id >= adev->mode_info.num_crtc) {
3288aaa36a97SAlex Deucher DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3289aaa36a97SAlex Deucher return -EINVAL;
3290aaa36a97SAlex Deucher }
3291aaa36a97SAlex Deucher
32927dfac896SAlex Deucher if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
32937dfac896SAlex Deucher GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
32947dfac896SAlex Deucher WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
32957dfac896SAlex Deucher GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3296aaa36a97SAlex Deucher
3297aaa36a97SAlex Deucher /* IRQ could occur when in initial stage */
3298aaa36a97SAlex Deucher if(amdgpu_crtc == NULL)
3299aaa36a97SAlex Deucher return 0;
3300aaa36a97SAlex Deucher
33014a580877SLuben Tuikov spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
3302aaa36a97SAlex Deucher works = amdgpu_crtc->pflip_works;
3303aaa36a97SAlex Deucher if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
3304aaa36a97SAlex Deucher DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3305aaa36a97SAlex Deucher "AMDGPU_FLIP_SUBMITTED(%d)\n",
3306aaa36a97SAlex Deucher amdgpu_crtc->pflip_status,
3307aaa36a97SAlex Deucher AMDGPU_FLIP_SUBMITTED);
33084a580877SLuben Tuikov spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3309aaa36a97SAlex Deucher return 0;
3310aaa36a97SAlex Deucher }
3311aaa36a97SAlex Deucher
3312aaa36a97SAlex Deucher /* page flip completed. clean up */
3313aaa36a97SAlex Deucher amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3314aaa36a97SAlex Deucher amdgpu_crtc->pflip_works = NULL;
3315aaa36a97SAlex Deucher
3316aaa36a97SAlex Deucher /* wakeup usersapce */
3317aaa36a97SAlex Deucher if(works->event)
331856286769SGustavo Padovan drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
3319aaa36a97SAlex Deucher
33204a580877SLuben Tuikov spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3321aaa36a97SAlex Deucher
332260629c4dSGustavo Padovan drm_crtc_vblank_put(&amdgpu_crtc->base);
332387d58c11SChristian König schedule_work(&works->unpin_work);
3324aaa36a97SAlex Deucher
3325aaa36a97SAlex Deucher return 0;
3326aaa36a97SAlex Deucher }
3327aaa36a97SAlex Deucher
dce_v11_0_hpd_int_ack(struct amdgpu_device * adev,int hpd)3328aaa36a97SAlex Deucher static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
3329aaa36a97SAlex Deucher int hpd)
3330aaa36a97SAlex Deucher {
3331aaa36a97SAlex Deucher u32 tmp;
3332aaa36a97SAlex Deucher
3333aaa36a97SAlex Deucher if (hpd >= adev->mode_info.num_hpd) {
3334aaa36a97SAlex Deucher DRM_DEBUG("invalid hdp %d\n", hpd);
3335aaa36a97SAlex Deucher return;
3336aaa36a97SAlex Deucher }
3337aaa36a97SAlex Deucher
3338aaa36a97SAlex Deucher tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3339aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
3340aaa36a97SAlex Deucher WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3341aaa36a97SAlex Deucher }
3342aaa36a97SAlex Deucher
dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device * adev,int crtc)3343aaa36a97SAlex Deucher static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
3344aaa36a97SAlex Deucher int crtc)
3345aaa36a97SAlex Deucher {
3346aaa36a97SAlex Deucher u32 tmp;
3347aaa36a97SAlex Deucher
334815c3277fSTom St Denis if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
3349aaa36a97SAlex Deucher DRM_DEBUG("invalid crtc %d\n", crtc);
3350aaa36a97SAlex Deucher return;
3351aaa36a97SAlex Deucher }
3352aaa36a97SAlex Deucher
3353aaa36a97SAlex Deucher tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
3354aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
3355aaa36a97SAlex Deucher WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
3356aaa36a97SAlex Deucher }
3357aaa36a97SAlex Deucher
dce_v11_0_crtc_vline_int_ack(struct amdgpu_device * adev,int crtc)3358aaa36a97SAlex Deucher static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
3359aaa36a97SAlex Deucher int crtc)
3360aaa36a97SAlex Deucher {
3361aaa36a97SAlex Deucher u32 tmp;
3362aaa36a97SAlex Deucher
336315c3277fSTom St Denis if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
3364aaa36a97SAlex Deucher DRM_DEBUG("invalid crtc %d\n", crtc);
3365aaa36a97SAlex Deucher return;
3366aaa36a97SAlex Deucher }
3367aaa36a97SAlex Deucher
3368aaa36a97SAlex Deucher tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
3369aaa36a97SAlex Deucher tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
3370aaa36a97SAlex Deucher WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
3371aaa36a97SAlex Deucher }
3372aaa36a97SAlex Deucher
dce_v11_0_crtc_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3373aaa36a97SAlex Deucher static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
3374aaa36a97SAlex Deucher struct amdgpu_irq_src *source,
3375aaa36a97SAlex Deucher struct amdgpu_iv_entry *entry)
3376aaa36a97SAlex Deucher {
3377aaa36a97SAlex Deucher unsigned crtc = entry->src_id - 1;
3378aaa36a97SAlex Deucher uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3379734dd01dSSamuel Li unsigned int irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
3380734dd01dSSamuel Li crtc);
3381aaa36a97SAlex Deucher
33827ccf5aa8SAlex Deucher switch (entry->src_data[0]) {
3383aaa36a97SAlex Deucher case 0: /* vblank */
3384bd833144SMario Kleiner if (disp_int & interrupt_status_offsets[crtc].vblank)
3385aaa36a97SAlex Deucher dce_v11_0_crtc_vblank_int_ack(adev, crtc);
3386bd833144SMario Kleiner else
3387bd833144SMario Kleiner DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3388bd833144SMario Kleiner
3389aaa36a97SAlex Deucher if (amdgpu_irq_enabled(adev, source, irq_type)) {
33904a580877SLuben Tuikov drm_handle_vblank(adev_to_drm(adev), crtc);
3391aaa36a97SAlex Deucher }
3392aaa36a97SAlex Deucher DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3393bd833144SMario Kleiner
3394aaa36a97SAlex Deucher break;
3395aaa36a97SAlex Deucher case 1: /* vline */
3396bd833144SMario Kleiner if (disp_int & interrupt_status_offsets[crtc].vline)
3397aaa36a97SAlex Deucher dce_v11_0_crtc_vline_int_ack(adev, crtc);
3398bd833144SMario Kleiner else
3399bd833144SMario Kleiner DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3400bd833144SMario Kleiner
3401aaa36a97SAlex Deucher DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3402bd833144SMario Kleiner
3403aaa36a97SAlex Deucher break;
3404aaa36a97SAlex Deucher default:
34057ccf5aa8SAlex Deucher DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3406aaa36a97SAlex Deucher break;
3407aaa36a97SAlex Deucher }
3408aaa36a97SAlex Deucher
3409aaa36a97SAlex Deucher return 0;
3410aaa36a97SAlex Deucher }
3411aaa36a97SAlex Deucher
dce_v11_0_hpd_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3412aaa36a97SAlex Deucher static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
3413aaa36a97SAlex Deucher struct amdgpu_irq_src *source,
3414aaa36a97SAlex Deucher struct amdgpu_iv_entry *entry)
3415aaa36a97SAlex Deucher {
3416aaa36a97SAlex Deucher uint32_t disp_int, mask;
3417aaa36a97SAlex Deucher unsigned hpd;
3418aaa36a97SAlex Deucher
34197ccf5aa8SAlex Deucher if (entry->src_data[0] >= adev->mode_info.num_hpd) {
34207ccf5aa8SAlex Deucher DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3421aaa36a97SAlex Deucher return 0;
3422aaa36a97SAlex Deucher }
3423aaa36a97SAlex Deucher
34247ccf5aa8SAlex Deucher hpd = entry->src_data[0];
3425aaa36a97SAlex Deucher disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3426aaa36a97SAlex Deucher mask = interrupt_status_offsets[hpd].hpd;
3427aaa36a97SAlex Deucher
3428aaa36a97SAlex Deucher if (disp_int & mask) {
3429aaa36a97SAlex Deucher dce_v11_0_hpd_int_ack(adev, hpd);
343090f56611Sxurui schedule_delayed_work(&adev->hotplug_work, 0);
3431aaa36a97SAlex Deucher DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3432aaa36a97SAlex Deucher }
3433aaa36a97SAlex Deucher
3434aaa36a97SAlex Deucher return 0;
3435aaa36a97SAlex Deucher }
3436aaa36a97SAlex Deucher
dce_v11_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)34375fc3aeebSyanyang1 static int dce_v11_0_set_clockgating_state(void *handle,
34385fc3aeebSyanyang1 enum amd_clockgating_state state)
3439aaa36a97SAlex Deucher {
3440aaa36a97SAlex Deucher return 0;
3441aaa36a97SAlex Deucher }
3442aaa36a97SAlex Deucher
dce_v11_0_set_powergating_state(void * handle,enum amd_powergating_state state)34435fc3aeebSyanyang1 static int dce_v11_0_set_powergating_state(void *handle,
34445fc3aeebSyanyang1 enum amd_powergating_state state)
3445aaa36a97SAlex Deucher {
3446aaa36a97SAlex Deucher return 0;
3447aaa36a97SAlex Deucher }
3448aaa36a97SAlex Deucher
3449a1255107SAlex Deucher static const struct amd_ip_funcs dce_v11_0_ip_funcs = {
345088a907d6STom St Denis .name = "dce_v11_0",
3451aaa36a97SAlex Deucher .early_init = dce_v11_0_early_init,
3452aaa36a97SAlex Deucher .late_init = NULL,
3453aaa36a97SAlex Deucher .sw_init = dce_v11_0_sw_init,
3454aaa36a97SAlex Deucher .sw_fini = dce_v11_0_sw_fini,
3455aaa36a97SAlex Deucher .hw_init = dce_v11_0_hw_init,
3456aaa36a97SAlex Deucher .hw_fini = dce_v11_0_hw_fini,
3457aaa36a97SAlex Deucher .suspend = dce_v11_0_suspend,
3458aaa36a97SAlex Deucher .resume = dce_v11_0_resume,
3459aaa36a97SAlex Deucher .is_idle = dce_v11_0_is_idle,
3460aaa36a97SAlex Deucher .wait_for_idle = dce_v11_0_wait_for_idle,
3461aaa36a97SAlex Deucher .soft_reset = dce_v11_0_soft_reset,
3462aaa36a97SAlex Deucher .set_clockgating_state = dce_v11_0_set_clockgating_state,
3463aaa36a97SAlex Deucher .set_powergating_state = dce_v11_0_set_powergating_state,
3464aaa36a97SAlex Deucher };
3465aaa36a97SAlex Deucher
3466aaa36a97SAlex Deucher static void
dce_v11_0_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)3467aaa36a97SAlex Deucher dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
3468aaa36a97SAlex Deucher struct drm_display_mode *mode,
3469aaa36a97SAlex Deucher struct drm_display_mode *adjusted_mode)
3470aaa36a97SAlex Deucher {
3471aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3472aaa36a97SAlex Deucher
3473aaa36a97SAlex Deucher amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3474aaa36a97SAlex Deucher
3475aaa36a97SAlex Deucher /* need to call this here rather than in prepare() since we need some crtc info */
3476aaa36a97SAlex Deucher amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3477aaa36a97SAlex Deucher
3478aaa36a97SAlex Deucher /* set scaler clears this on some chips */
3479aaa36a97SAlex Deucher dce_v11_0_set_interleave(encoder->crtc, mode);
3480aaa36a97SAlex Deucher
3481aaa36a97SAlex Deucher if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3482aaa36a97SAlex Deucher dce_v11_0_afmt_enable(encoder, true);
3483aaa36a97SAlex Deucher dce_v11_0_afmt_setmode(encoder, adjusted_mode);
3484aaa36a97SAlex Deucher }
3485aaa36a97SAlex Deucher }
3486aaa36a97SAlex Deucher
dce_v11_0_encoder_prepare(struct drm_encoder * encoder)3487aaa36a97SAlex Deucher static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
3488aaa36a97SAlex Deucher {
34891348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(encoder->dev);
3490aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3491aaa36a97SAlex Deucher struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3492aaa36a97SAlex Deucher
3493aaa36a97SAlex Deucher if ((amdgpu_encoder->active_device &
3494aaa36a97SAlex Deucher (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3495aaa36a97SAlex Deucher (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3496aaa36a97SAlex Deucher ENCODER_OBJECT_ID_NONE)) {
3497aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3498aaa36a97SAlex Deucher if (dig) {
3499aaa36a97SAlex Deucher dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
3500aaa36a97SAlex Deucher if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3501aaa36a97SAlex Deucher dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3502aaa36a97SAlex Deucher }
3503aaa36a97SAlex Deucher }
3504aaa36a97SAlex Deucher
3505aaa36a97SAlex Deucher amdgpu_atombios_scratch_regs_lock(adev, true);
3506aaa36a97SAlex Deucher
3507aaa36a97SAlex Deucher if (connector) {
3508aaa36a97SAlex Deucher struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3509aaa36a97SAlex Deucher
3510aaa36a97SAlex Deucher /* select the clock/data port if it uses a router */
3511aaa36a97SAlex Deucher if (amdgpu_connector->router.cd_valid)
3512aaa36a97SAlex Deucher amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3513aaa36a97SAlex Deucher
3514aaa36a97SAlex Deucher /* turn eDP panel on for mode set */
3515aaa36a97SAlex Deucher if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3516aaa36a97SAlex Deucher amdgpu_atombios_encoder_set_edp_panel_power(connector,
3517aaa36a97SAlex Deucher ATOM_TRANSMITTER_ACTION_POWER_ON);
3518aaa36a97SAlex Deucher }
3519aaa36a97SAlex Deucher
3520aaa36a97SAlex Deucher /* this is needed for the pll/ss setup to work correctly in some cases */
3521aaa36a97SAlex Deucher amdgpu_atombios_encoder_set_crtc_source(encoder);
3522aaa36a97SAlex Deucher /* set up the FMT blocks */
3523aaa36a97SAlex Deucher dce_v11_0_program_fmt(encoder);
3524aaa36a97SAlex Deucher }
3525aaa36a97SAlex Deucher
dce_v11_0_encoder_commit(struct drm_encoder * encoder)3526aaa36a97SAlex Deucher static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
3527aaa36a97SAlex Deucher {
3528aaa36a97SAlex Deucher struct drm_device *dev = encoder->dev;
35291348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
3530aaa36a97SAlex Deucher
3531aaa36a97SAlex Deucher /* need to call this here as we need the crtc set up */
3532aaa36a97SAlex Deucher amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3533aaa36a97SAlex Deucher amdgpu_atombios_scratch_regs_lock(adev, false);
3534aaa36a97SAlex Deucher }
3535aaa36a97SAlex Deucher
dce_v11_0_encoder_disable(struct drm_encoder * encoder)3536aaa36a97SAlex Deucher static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
3537aaa36a97SAlex Deucher {
3538aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3539aaa36a97SAlex Deucher struct amdgpu_encoder_atom_dig *dig;
3540aaa36a97SAlex Deucher
3541aaa36a97SAlex Deucher amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3542aaa36a97SAlex Deucher
3543aaa36a97SAlex Deucher if (amdgpu_atombios_encoder_is_digital(encoder)) {
3544aaa36a97SAlex Deucher if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3545aaa36a97SAlex Deucher dce_v11_0_afmt_enable(encoder, false);
3546aaa36a97SAlex Deucher dig = amdgpu_encoder->enc_priv;
3547aaa36a97SAlex Deucher dig->dig_encoder = -1;
3548aaa36a97SAlex Deucher }
3549aaa36a97SAlex Deucher amdgpu_encoder->active_device = 0;
3550aaa36a97SAlex Deucher }
3551aaa36a97SAlex Deucher
3552aaa36a97SAlex Deucher /* these are handled by the primary encoders */
dce_v11_0_ext_prepare(struct drm_encoder * encoder)3553aaa36a97SAlex Deucher static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
3554aaa36a97SAlex Deucher {
3555aaa36a97SAlex Deucher
3556aaa36a97SAlex Deucher }
3557aaa36a97SAlex Deucher
dce_v11_0_ext_commit(struct drm_encoder * encoder)3558aaa36a97SAlex Deucher static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
3559aaa36a97SAlex Deucher {
3560aaa36a97SAlex Deucher
3561aaa36a97SAlex Deucher }
3562aaa36a97SAlex Deucher
3563aaa36a97SAlex Deucher static void
dce_v11_0_ext_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)3564aaa36a97SAlex Deucher dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
3565aaa36a97SAlex Deucher struct drm_display_mode *mode,
3566aaa36a97SAlex Deucher struct drm_display_mode *adjusted_mode)
3567aaa36a97SAlex Deucher {
3568aaa36a97SAlex Deucher
3569aaa36a97SAlex Deucher }
3570aaa36a97SAlex Deucher
dce_v11_0_ext_disable(struct drm_encoder * encoder)3571aaa36a97SAlex Deucher static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
3572aaa36a97SAlex Deucher {
3573aaa36a97SAlex Deucher
3574aaa36a97SAlex Deucher }
3575aaa36a97SAlex Deucher
3576aaa36a97SAlex Deucher static void
dce_v11_0_ext_dpms(struct drm_encoder * encoder,int mode)3577aaa36a97SAlex Deucher dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
3578aaa36a97SAlex Deucher {
3579aaa36a97SAlex Deucher
3580aaa36a97SAlex Deucher }
3581aaa36a97SAlex Deucher
3582aaa36a97SAlex Deucher static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
3583aaa36a97SAlex Deucher .dpms = dce_v11_0_ext_dpms,
3584aaa36a97SAlex Deucher .prepare = dce_v11_0_ext_prepare,
3585aaa36a97SAlex Deucher .mode_set = dce_v11_0_ext_mode_set,
3586aaa36a97SAlex Deucher .commit = dce_v11_0_ext_commit,
3587aaa36a97SAlex Deucher .disable = dce_v11_0_ext_disable,
3588aaa36a97SAlex Deucher /* no detect for TMDS/LVDS yet */
3589aaa36a97SAlex Deucher };
3590aaa36a97SAlex Deucher
3591aaa36a97SAlex Deucher static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
3592aaa36a97SAlex Deucher .dpms = amdgpu_atombios_encoder_dpms,
3593aaa36a97SAlex Deucher .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3594aaa36a97SAlex Deucher .prepare = dce_v11_0_encoder_prepare,
3595aaa36a97SAlex Deucher .mode_set = dce_v11_0_encoder_mode_set,
3596aaa36a97SAlex Deucher .commit = dce_v11_0_encoder_commit,
3597aaa36a97SAlex Deucher .disable = dce_v11_0_encoder_disable,
3598aaa36a97SAlex Deucher .detect = amdgpu_atombios_encoder_dig_detect,
3599aaa36a97SAlex Deucher };
3600aaa36a97SAlex Deucher
3601aaa36a97SAlex Deucher static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
3602aaa36a97SAlex Deucher .dpms = amdgpu_atombios_encoder_dpms,
3603aaa36a97SAlex Deucher .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3604aaa36a97SAlex Deucher .prepare = dce_v11_0_encoder_prepare,
3605aaa36a97SAlex Deucher .mode_set = dce_v11_0_encoder_mode_set,
3606aaa36a97SAlex Deucher .commit = dce_v11_0_encoder_commit,
3607aaa36a97SAlex Deucher .detect = amdgpu_atombios_encoder_dac_detect,
3608aaa36a97SAlex Deucher };
3609aaa36a97SAlex Deucher
dce_v11_0_encoder_destroy(struct drm_encoder * encoder)3610aaa36a97SAlex Deucher static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
3611aaa36a97SAlex Deucher {
3612aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3613aaa36a97SAlex Deucher if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3614aaa36a97SAlex Deucher amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3615aaa36a97SAlex Deucher kfree(amdgpu_encoder->enc_priv);
3616aaa36a97SAlex Deucher drm_encoder_cleanup(encoder);
3617aaa36a97SAlex Deucher kfree(amdgpu_encoder);
3618aaa36a97SAlex Deucher }
3619aaa36a97SAlex Deucher
3620aaa36a97SAlex Deucher static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
3621aaa36a97SAlex Deucher .destroy = dce_v11_0_encoder_destroy,
3622aaa36a97SAlex Deucher };
3623aaa36a97SAlex Deucher
dce_v11_0_encoder_add(struct amdgpu_device * adev,uint32_t encoder_enum,uint32_t supported_device,u16 caps)3624aaa36a97SAlex Deucher static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
3625aaa36a97SAlex Deucher uint32_t encoder_enum,
3626aaa36a97SAlex Deucher uint32_t supported_device,
3627aaa36a97SAlex Deucher u16 caps)
3628aaa36a97SAlex Deucher {
36294a580877SLuben Tuikov struct drm_device *dev = adev_to_drm(adev);
3630aaa36a97SAlex Deucher struct drm_encoder *encoder;
3631aaa36a97SAlex Deucher struct amdgpu_encoder *amdgpu_encoder;
3632aaa36a97SAlex Deucher
3633aaa36a97SAlex Deucher /* see if we already added it */
3634aaa36a97SAlex Deucher list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3635aaa36a97SAlex Deucher amdgpu_encoder = to_amdgpu_encoder(encoder);
3636aaa36a97SAlex Deucher if (amdgpu_encoder->encoder_enum == encoder_enum) {
3637aaa36a97SAlex Deucher amdgpu_encoder->devices |= supported_device;
3638aaa36a97SAlex Deucher return;
3639aaa36a97SAlex Deucher }
3640aaa36a97SAlex Deucher
3641aaa36a97SAlex Deucher }
3642aaa36a97SAlex Deucher
3643aaa36a97SAlex Deucher /* add a new one */
3644aaa36a97SAlex Deucher amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3645aaa36a97SAlex Deucher if (!amdgpu_encoder)
3646aaa36a97SAlex Deucher return;
3647aaa36a97SAlex Deucher
3648aaa36a97SAlex Deucher encoder = &amdgpu_encoder->base;
3649aaa36a97SAlex Deucher switch (adev->mode_info.num_crtc) {
3650aaa36a97SAlex Deucher case 1:
3651aaa36a97SAlex Deucher encoder->possible_crtcs = 0x1;
3652aaa36a97SAlex Deucher break;
3653aaa36a97SAlex Deucher case 2:
3654aaa36a97SAlex Deucher default:
3655aaa36a97SAlex Deucher encoder->possible_crtcs = 0x3;
3656aaa36a97SAlex Deucher break;
36574ce3bd45SAlex Deucher case 3:
36584ce3bd45SAlex Deucher encoder->possible_crtcs = 0x7;
36594ce3bd45SAlex Deucher break;
3660aaa36a97SAlex Deucher case 4:
3661aaa36a97SAlex Deucher encoder->possible_crtcs = 0xf;
3662aaa36a97SAlex Deucher break;
36634ce3bd45SAlex Deucher case 5:
36644ce3bd45SAlex Deucher encoder->possible_crtcs = 0x1f;
36654ce3bd45SAlex Deucher break;
3666aaa36a97SAlex Deucher case 6:
3667aaa36a97SAlex Deucher encoder->possible_crtcs = 0x3f;
3668aaa36a97SAlex Deucher break;
3669aaa36a97SAlex Deucher }
3670aaa36a97SAlex Deucher
3671aaa36a97SAlex Deucher amdgpu_encoder->enc_priv = NULL;
3672aaa36a97SAlex Deucher
3673aaa36a97SAlex Deucher amdgpu_encoder->encoder_enum = encoder_enum;
3674aaa36a97SAlex Deucher amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3675aaa36a97SAlex Deucher amdgpu_encoder->devices = supported_device;
3676aaa36a97SAlex Deucher amdgpu_encoder->rmx_type = RMX_OFF;
3677aaa36a97SAlex Deucher amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3678aaa36a97SAlex Deucher amdgpu_encoder->is_ext_encoder = false;
3679aaa36a97SAlex Deucher amdgpu_encoder->caps = caps;
3680aaa36a97SAlex Deucher
3681aaa36a97SAlex Deucher switch (amdgpu_encoder->encoder_id) {
3682aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3683aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3684aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
368513a3d91fSVille Syrjälä DRM_MODE_ENCODER_DAC, NULL);
3686aaa36a97SAlex Deucher drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
3687aaa36a97SAlex Deucher break;
3688aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3689aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3690aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3691aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3692aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3693aaa36a97SAlex Deucher if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3694aaa36a97SAlex Deucher amdgpu_encoder->rmx_type = RMX_FULL;
3695aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
369613a3d91fSVille Syrjälä DRM_MODE_ENCODER_LVDS, NULL);
3697aaa36a97SAlex Deucher amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3698aaa36a97SAlex Deucher } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3699aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
370013a3d91fSVille Syrjälä DRM_MODE_ENCODER_DAC, NULL);
3701aaa36a97SAlex Deucher amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3702aaa36a97SAlex Deucher } else {
3703aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
370413a3d91fSVille Syrjälä DRM_MODE_ENCODER_TMDS, NULL);
3705aaa36a97SAlex Deucher amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3706aaa36a97SAlex Deucher }
3707aaa36a97SAlex Deucher drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
3708aaa36a97SAlex Deucher break;
3709aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_SI170B:
3710aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_CH7303:
3711aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3712aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3713aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_TITFP513:
3714aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_VT1623:
3715aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_HDMI_SI1930:
3716aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_TRAVIS:
3717aaa36a97SAlex Deucher case ENCODER_OBJECT_ID_NUTMEG:
3718aaa36a97SAlex Deucher /* these are handled by the primary encoders */
3719aaa36a97SAlex Deucher amdgpu_encoder->is_ext_encoder = true;
3720aaa36a97SAlex Deucher if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3721aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
372213a3d91fSVille Syrjälä DRM_MODE_ENCODER_LVDS, NULL);
3723aaa36a97SAlex Deucher else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3724aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
372513a3d91fSVille Syrjälä DRM_MODE_ENCODER_DAC, NULL);
3726aaa36a97SAlex Deucher else
3727aaa36a97SAlex Deucher drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
372813a3d91fSVille Syrjälä DRM_MODE_ENCODER_TMDS, NULL);
3729aaa36a97SAlex Deucher drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
3730aaa36a97SAlex Deucher break;
3731aaa36a97SAlex Deucher }
3732aaa36a97SAlex Deucher }
3733aaa36a97SAlex Deucher
3734aaa36a97SAlex Deucher static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
3735aaa36a97SAlex Deucher .bandwidth_update = &dce_v11_0_bandwidth_update,
3736aaa36a97SAlex Deucher .vblank_get_counter = &dce_v11_0_vblank_get_counter,
3737aaa36a97SAlex Deucher .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3738aaa36a97SAlex Deucher .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3739aaa36a97SAlex Deucher .hpd_sense = &dce_v11_0_hpd_sense,
3740aaa36a97SAlex Deucher .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
3741aaa36a97SAlex Deucher .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
3742aaa36a97SAlex Deucher .page_flip = &dce_v11_0_page_flip,
3743aaa36a97SAlex Deucher .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
3744aaa36a97SAlex Deucher .add_encoder = &dce_v11_0_encoder_add,
3745aaa36a97SAlex Deucher .add_connector = &amdgpu_connector_add,
3746aaa36a97SAlex Deucher };
3747aaa36a97SAlex Deucher
dce_v11_0_set_display_funcs(struct amdgpu_device * adev)3748aaa36a97SAlex Deucher static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
3749aaa36a97SAlex Deucher {
3750aaa36a97SAlex Deucher adev->mode_info.funcs = &dce_v11_0_display_funcs;
3751aaa36a97SAlex Deucher }
3752aaa36a97SAlex Deucher
3753aaa36a97SAlex Deucher static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
3754aaa36a97SAlex Deucher .set = dce_v11_0_set_crtc_irq_state,
3755aaa36a97SAlex Deucher .process = dce_v11_0_crtc_irq,
3756aaa36a97SAlex Deucher };
3757aaa36a97SAlex Deucher
3758aaa36a97SAlex Deucher static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
3759aaa36a97SAlex Deucher .set = dce_v11_0_set_pageflip_irq_state,
3760aaa36a97SAlex Deucher .process = dce_v11_0_pageflip_irq,
3761aaa36a97SAlex Deucher };
3762aaa36a97SAlex Deucher
3763aaa36a97SAlex Deucher static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
3764aaa36a97SAlex Deucher .set = dce_v11_0_set_hpd_irq_state,
3765aaa36a97SAlex Deucher .process = dce_v11_0_hpd_irq,
3766aaa36a97SAlex Deucher };
3767aaa36a97SAlex Deucher
dce_v11_0_set_irq_funcs(struct amdgpu_device * adev)3768aaa36a97SAlex Deucher static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
3769aaa36a97SAlex Deucher {
3770d794b9f8SMichel Dänzer if (adev->mode_info.num_crtc > 0)
3771d794b9f8SMichel Dänzer adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VLINE1 + adev->mode_info.num_crtc;
3772d794b9f8SMichel Dänzer else
3773d794b9f8SMichel Dänzer adev->crtc_irq.num_types = 0;
3774aaa36a97SAlex Deucher adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
3775aaa36a97SAlex Deucher
3776d794b9f8SMichel Dänzer adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
3777aaa36a97SAlex Deucher adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
3778aaa36a97SAlex Deucher
3779d794b9f8SMichel Dänzer adev->hpd_irq.num_types = adev->mode_info.num_hpd;
3780aaa36a97SAlex Deucher adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;
3781aaa36a97SAlex Deucher }
3782a1255107SAlex Deucher
3783a1255107SAlex Deucher const struct amdgpu_ip_block_version dce_v11_0_ip_block =
3784a1255107SAlex Deucher {
3785a1255107SAlex Deucher .type = AMD_IP_BLOCK_TYPE_DCE,
3786a1255107SAlex Deucher .major = 11,
3787a1255107SAlex Deucher .minor = 0,
3788a1255107SAlex Deucher .rev = 0,
3789a1255107SAlex Deucher .funcs = &dce_v11_0_ip_funcs,
3790a1255107SAlex Deucher };
3791a1255107SAlex Deucher
3792a1255107SAlex Deucher const struct amdgpu_ip_block_version dce_v11_2_ip_block =
3793a1255107SAlex Deucher {
3794a1255107SAlex Deucher .type = AMD_IP_BLOCK_TYPE_DCE,
3795a1255107SAlex Deucher .major = 11,
3796a1255107SAlex Deucher .minor = 2,
3797a1255107SAlex Deucher .rev = 0,
3798a1255107SAlex Deucher .funcs = &dce_v11_0_ip_funcs,
3799a1255107SAlex Deucher };
3800