2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <drm/drm_fourcc.h>
25 #include <drm/drm_modeset_helper.h>
26 #include <drm/drm_modeset_helper_vtables.h>
27 #include <drm/drm_vblank.h>
30 #include "amdgpu_pm.h"
31 #include "amdgpu_i2c.h"
34 #include "amdgpu_atombios.h"
35 #include "atombios_crtc.h"
36 #include "atombios_encoders.h"
37 #include "amdgpu_pll.h"
38 #include "amdgpu_connectors.h"
39 #include "amdgpu_display.h"
40 #include "dce_v11_0.h"
42 #include "dce/dce_11_0_d.h"
43 #include "dce/dce_11_0_sh_mask.h"
44 #include "dce/dce_11_0_enum.h"
45 #include "oss/oss_3_0_d.h"
46 #include "oss/oss_3_0_sh_mask.h"
47 #include "gmc/gmc_8_1_d.h"
48 #include "gmc/gmc_8_1_sh_mask.h"
50 #include "ivsrcid/ivsrcid_vislands30.h"
52 static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev);
53 static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev);
55 static const u32 crtc_offsets[] =
57 CRTC0_REGISTER_OFFSET,
58 CRTC1_REGISTER_OFFSET,
59 CRTC2_REGISTER_OFFSET,
60 CRTC3_REGISTER_OFFSET,
61 CRTC4_REGISTER_OFFSET,
62 CRTC5_REGISTER_OFFSET,
66 static const u32 hpd_offsets[] =
76 static const uint32_t dig_offsets[] = {
94 } interrupt_status_offsets[] = { {
95 .reg = mmDISP_INTERRUPT_STATUS,
96 .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
97 .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
98 .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
100 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
101 .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
102 .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
103 .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
105 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
106 .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
107 .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
108 .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
110 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
111 .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
112 .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
113 .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
115 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
116 .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
117 .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
118 .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
120 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
121 .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
122 .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
123 .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
126 static const u32 cz_golden_settings_a11[] =
128 mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
129 mmFBC_MISC, 0x1f311fff, 0x14300000,
132 static const u32 cz_mgcg_cgcg_init[] =
134 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
135 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
138 static const u32 stoney_golden_settings_a11[] =
140 mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
141 mmFBC_MISC, 0x1f311fff, 0x14302000,
144 static const u32 polaris11_golden_settings_a11[] =
146 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
147 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
148 mmFBC_DEBUG1, 0xffffffff, 0x00000008,
149 mmFBC_MISC, 0x9f313fff, 0x14302008,
150 mmHDMI_CONTROL, 0x313f031f, 0x00000011,
153 static const u32 polaris10_golden_settings_a11[] =
155 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
156 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
157 mmFBC_MISC, 0x9f313fff, 0x14302008,
158 mmHDMI_CONTROL, 0x313f031f, 0x00000011,
161 static void dce_v11_0_init_golden_registers(struct amdgpu_device *adev)
163 switch (adev->asic_type) {
165 amdgpu_device_program_register_sequence(adev,
167 ARRAY_SIZE(cz_mgcg_cgcg_init));
168 amdgpu_device_program_register_sequence(adev,
169 cz_golden_settings_a11,
170 ARRAY_SIZE(cz_golden_settings_a11));
173 amdgpu_device_program_register_sequence(adev,
174 stoney_golden_settings_a11,
175 ARRAY_SIZE(stoney_golden_settings_a11));
179 amdgpu_device_program_register_sequence(adev,
180 polaris11_golden_settings_a11,
181 ARRAY_SIZE(polaris11_golden_settings_a11));
185 amdgpu_device_program_register_sequence(adev,
186 polaris10_golden_settings_a11,
187 ARRAY_SIZE(polaris10_golden_settings_a11));
194 static u32 dce_v11_0_audio_endpt_rreg(struct amdgpu_device *adev,
195 u32 block_offset, u32 reg)
200 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
201 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
202 r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
203 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
208 static void dce_v11_0_audio_endpt_wreg(struct amdgpu_device *adev,
209 u32 block_offset, u32 reg, u32 v)
213 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
214 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
215 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
216 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
219 static u32 dce_v11_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
221 if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
224 return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
227 static void dce_v11_0_pageflip_interrupt_init(struct amdgpu_device *adev)
231 /* Enable pflip interrupts */
232 for (i = 0; i < adev->mode_info.num_crtc; i++)
233 amdgpu_irq_get(adev, &adev->pageflip_irq, i);
236 static void dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
240 /* Disable pflip interrupts */
241 for (i = 0; i < adev->mode_info.num_crtc; i++)
242 amdgpu_irq_put(adev, &adev->pageflip_irq, i);
246 * dce_v11_0_page_flip - pageflip callback.
248 * @adev: amdgpu_device pointer
249 * @crtc_id: crtc to cleanup pageflip on
250 * @crtc_base: new address of the crtc (GPU MC address)
251 * @async: asynchronous flip
253 * Triggers the actual pageflip by updating the primary
254 * surface base address.
256 static void dce_v11_0_page_flip(struct amdgpu_device *adev,
257 int crtc_id, u64 crtc_base, bool async)
259 struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
260 struct drm_framebuffer *fb = amdgpu_crtc->base.primary->fb;
263 /* flip immediate for async, default is vsync */
264 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
265 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
266 GRPH_SURFACE_UPDATE_IMMEDIATE_EN, async ? 1 : 0);
267 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
269 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset,
270 fb->pitches[0] / fb->format->cpp[0]);
271 /* update the scanout addresses */
272 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
273 upper_32_bits(crtc_base));
274 /* writing to the low address triggers the update */
275 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
276 lower_32_bits(crtc_base));
278 RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
281 static int dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
282 u32 *vbl, u32 *position)
284 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
287 *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
288 *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
294 * dce_v11_0_hpd_sense - hpd sense callback.
296 * @adev: amdgpu_device pointer
297 * @hpd: hpd (hotplug detect) pin
299 * Checks if a digital monitor is connected (evergreen+).
300 * Returns true if connected, false if not connected.
302 static bool dce_v11_0_hpd_sense(struct amdgpu_device *adev,
303 enum amdgpu_hpd_id hpd)
305 bool connected = false;
307 if (hpd >= adev->mode_info.num_hpd)
310 if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[hpd]) &
311 DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
318 * dce_v11_0_hpd_set_polarity - hpd set polarity callback.
320 * @adev: amdgpu_device pointer
321 * @hpd: hpd (hotplug detect) pin
323 * Set the polarity of the hpd pin (evergreen+).
325 static void dce_v11_0_hpd_set_polarity(struct amdgpu_device *adev,
326 enum amdgpu_hpd_id hpd)
329 bool connected = dce_v11_0_hpd_sense(adev, hpd);
331 if (hpd >= adev->mode_info.num_hpd)
334 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
336 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
338 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
339 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
343 * dce_v11_0_hpd_init - hpd setup callback.
345 * @adev: amdgpu_device pointer
347 * Setup the hpd pins used by the card (evergreen+).
348 * Enable the pin, set the polarity, and enable the hpd interrupts.
350 static void dce_v11_0_hpd_init(struct amdgpu_device *adev)
352 struct drm_device *dev = adev_to_drm(adev);
353 struct drm_connector *connector;
354 struct drm_connector_list_iter iter;
357 drm_connector_list_iter_begin(dev, &iter);
358 drm_for_each_connector_iter(connector, &iter) {
359 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
361 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
364 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
365 connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
366 /* don't try to enable hpd on eDP or LVDS avoid breaking the
367 * aux dp channel on imac and help (but not completely fix)
368 * https://bugzilla.redhat.com/show_bug.cgi?id=726143
369 * also avoid interrupt storms during dpms.
371 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
372 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
373 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
377 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
378 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
379 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
381 tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd]);
382 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
383 DC_HPD_CONNECT_INT_DELAY,
384 AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
385 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
386 DC_HPD_DISCONNECT_INT_DELAY,
387 AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
388 WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
390 dce_v11_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
391 amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
393 drm_connector_list_iter_end(&iter);
397 * dce_v11_0_hpd_fini - hpd tear down callback.
399 * @adev: amdgpu_device pointer
401 * Tear down the hpd pins used by the card (evergreen+).
402 * Disable the hpd interrupts.
404 static void dce_v11_0_hpd_fini(struct amdgpu_device *adev)
406 struct drm_device *dev = adev_to_drm(adev);
407 struct drm_connector *connector;
408 struct drm_connector_list_iter iter;
411 drm_connector_list_iter_begin(dev, &iter);
412 drm_for_each_connector_iter(connector, &iter) {
413 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
415 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
418 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
419 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
420 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
422 amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
424 drm_connector_list_iter_end(&iter);
427 static u32 dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
429 return mmDC_GPIO_HPD_A;
432 static bool dce_v11_0_is_display_hung(struct amdgpu_device *adev)
438 for (i = 0; i < adev->mode_info.num_crtc; i++) {
439 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
440 if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
441 crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
442 crtc_hung |= (1 << i);
446 for (j = 0; j < 10; j++) {
447 for (i = 0; i < adev->mode_info.num_crtc; i++) {
448 if (crtc_hung & (1 << i)) {
449 tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
450 if (tmp != crtc_status[i])
451 crtc_hung &= ~(1 << i);
462 static void dce_v11_0_set_vga_render_state(struct amdgpu_device *adev,
467 /* Lockout access through VGA aperture*/
468 tmp = RREG32(mmVGA_HDP_CONTROL);
470 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
472 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
473 WREG32(mmVGA_HDP_CONTROL, tmp);
475 /* disable VGA render */
476 tmp = RREG32(mmVGA_RENDER_CONTROL);
478 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
480 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
481 WREG32(mmVGA_RENDER_CONTROL, tmp);
484 static int dce_v11_0_get_num_crtc (struct amdgpu_device *adev)
488 switch (adev->asic_type) {
509 void dce_v11_0_disable_dce(struct amdgpu_device *adev)
511 /*Disable VGA render and enabled crtc, if has DCE engine*/
512 if (amdgpu_atombios_has_dce_engine_info(adev)) {
516 dce_v11_0_set_vga_render_state(adev, false);
519 for (i = 0; i < dce_v11_0_get_num_crtc(adev); i++) {
520 crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
521 CRTC_CONTROL, CRTC_MASTER_EN);
523 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
524 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
525 tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
526 WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
527 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
533 static void dce_v11_0_program_fmt(struct drm_encoder *encoder)
535 struct drm_device *dev = encoder->dev;
536 struct amdgpu_device *adev = drm_to_adev(dev);
537 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
538 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
539 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
542 enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
545 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
546 bpc = amdgpu_connector_get_monitor_bpc(connector);
547 dither = amdgpu_connector->dither;
550 /* LVDS/eDP FMT is set up by atom */
551 if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
554 /* not needed for analog */
555 if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
556 (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
564 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
565 /* XXX sort out optimal dither settings */
566 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
567 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
568 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
569 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
571 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
572 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
576 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
577 /* XXX sort out optimal dither settings */
578 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
579 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
580 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
581 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
582 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
584 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
585 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
589 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
590 /* XXX sort out optimal dither settings */
591 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
592 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
593 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
594 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
595 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
597 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
598 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
606 WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
610 /* display watermark setup */
612 * dce_v11_0_line_buffer_adjust - Set up the line buffer
614 * @adev: amdgpu_device pointer
615 * @amdgpu_crtc: the selected display controller
616 * @mode: the current display mode on the selected display
619 * Setup up the line buffer allocation for
620 * the selected display controller (CIK).
621 * Returns the line buffer size in pixels.
623 static u32 dce_v11_0_line_buffer_adjust(struct amdgpu_device *adev,
624 struct amdgpu_crtc *amdgpu_crtc,
625 struct drm_display_mode *mode)
627 u32 tmp, buffer_alloc, i, mem_cfg;
628 u32 pipe_offset = amdgpu_crtc->crtc_id;
631 * There are 6 line buffers, one for each display controllers.
632 * There are 3 partitions per LB. Select the number of partitions
633 * to enable based on the display width. For display widths larger
634 * than 4096, you need use to use 2 display controllers and combine
635 * them using the stereo blender.
637 if (amdgpu_crtc->base.enabled && mode) {
638 if (mode->crtc_hdisplay < 1920) {
641 } else if (mode->crtc_hdisplay < 2560) {
644 } else if (mode->crtc_hdisplay < 4096) {
646 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
648 DRM_DEBUG_KMS("Mode too big for LB!\n");
650 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
657 tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
658 tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
659 WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
661 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
662 tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
663 WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
665 for (i = 0; i < adev->usec_timeout; i++) {
666 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
667 if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
672 if (amdgpu_crtc->base.enabled && mode) {
684 /* controller not enabled, so no lb used */
689 * cik_get_number_of_dram_channels - get the number of dram channels
691 * @adev: amdgpu_device pointer
693 * Look up the number of video ram channels (CIK).
694 * Used for display watermark bandwidth calculations
695 * Returns the number of dram channels
697 static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
699 u32 tmp = RREG32(mmMC_SHARED_CHMAP);
701 switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
724 struct dce10_wm_params {
725 u32 dram_channels; /* number of dram channels */
726 u32 yclk; /* bandwidth per dram data pin in kHz */
727 u32 sclk; /* engine clock in kHz */
728 u32 disp_clk; /* display clock in kHz */
729 u32 src_width; /* viewport width */
730 u32 active_time; /* active display time in ns */
731 u32 blank_time; /* blank time in ns */
732 bool interlaced; /* mode is interlaced */
733 fixed20_12 vsc; /* vertical scale ratio */
734 u32 num_heads; /* number of active crtcs */
735 u32 bytes_per_pixel; /* bytes per pixel display + overlay */
736 u32 lb_size; /* line buffer allocated to pipe */
737 u32 vtaps; /* vertical scaler taps */
741 * dce_v11_0_dram_bandwidth - get the dram bandwidth
743 * @wm: watermark calculation data
745 * Calculate the raw dram bandwidth (CIK).
746 * Used for display watermark bandwidth calculations
747 * Returns the dram bandwidth in MBytes/s
749 static u32 dce_v11_0_dram_bandwidth(struct dce10_wm_params *wm)
751 /* Calculate raw DRAM Bandwidth */
752 fixed20_12 dram_efficiency; /* 0.7 */
753 fixed20_12 yclk, dram_channels, bandwidth;
756 a.full = dfixed_const(1000);
757 yclk.full = dfixed_const(wm->yclk);
758 yclk.full = dfixed_div(yclk, a);
759 dram_channels.full = dfixed_const(wm->dram_channels * 4);
760 a.full = dfixed_const(10);
761 dram_efficiency.full = dfixed_const(7);
762 dram_efficiency.full = dfixed_div(dram_efficiency, a);
763 bandwidth.full = dfixed_mul(dram_channels, yclk);
764 bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
766 return dfixed_trunc(bandwidth);
770 * dce_v11_0_dram_bandwidth_for_display - get the dram bandwidth for display
772 * @wm: watermark calculation data
774 * Calculate the dram bandwidth used for display (CIK).
775 * Used for display watermark bandwidth calculations
776 * Returns the dram bandwidth for display in MBytes/s
778 static u32 dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
780 /* Calculate DRAM Bandwidth and the part allocated to display. */
781 fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
782 fixed20_12 yclk, dram_channels, bandwidth;
785 a.full = dfixed_const(1000);
786 yclk.full = dfixed_const(wm->yclk);
787 yclk.full = dfixed_div(yclk, a);
788 dram_channels.full = dfixed_const(wm->dram_channels * 4);
789 a.full = dfixed_const(10);
790 disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
791 disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
792 bandwidth.full = dfixed_mul(dram_channels, yclk);
793 bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
795 return dfixed_trunc(bandwidth);
799 * dce_v11_0_data_return_bandwidth - get the data return bandwidth
801 * @wm: watermark calculation data
803 * Calculate the data return bandwidth used for display (CIK).
804 * Used for display watermark bandwidth calculations
805 * Returns the data return bandwidth in MBytes/s
807 static u32 dce_v11_0_data_return_bandwidth(struct dce10_wm_params *wm)
809 /* Calculate the display Data return Bandwidth */
810 fixed20_12 return_efficiency; /* 0.8 */
811 fixed20_12 sclk, bandwidth;
814 a.full = dfixed_const(1000);
815 sclk.full = dfixed_const(wm->sclk);
816 sclk.full = dfixed_div(sclk, a);
817 a.full = dfixed_const(10);
818 return_efficiency.full = dfixed_const(8);
819 return_efficiency.full = dfixed_div(return_efficiency, a);
820 a.full = dfixed_const(32);
821 bandwidth.full = dfixed_mul(a, sclk);
822 bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
824 return dfixed_trunc(bandwidth);
828 * dce_v11_0_dmif_request_bandwidth - get the dmif bandwidth
830 * @wm: watermark calculation data
832 * Calculate the dmif bandwidth used for display (CIK).
833 * Used for display watermark bandwidth calculations
834 * Returns the dmif bandwidth in MBytes/s
836 static u32 dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
838 /* Calculate the DMIF Request Bandwidth */
839 fixed20_12 disp_clk_request_efficiency; /* 0.8 */
840 fixed20_12 disp_clk, bandwidth;
843 a.full = dfixed_const(1000);
844 disp_clk.full = dfixed_const(wm->disp_clk);
845 disp_clk.full = dfixed_div(disp_clk, a);
846 a.full = dfixed_const(32);
847 b.full = dfixed_mul(a, disp_clk);
849 a.full = dfixed_const(10);
850 disp_clk_request_efficiency.full = dfixed_const(8);
851 disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
853 bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
855 return dfixed_trunc(bandwidth);
859 * dce_v11_0_available_bandwidth - get the min available bandwidth
861 * @wm: watermark calculation data
863 * Calculate the min available bandwidth used for display (CIK).
864 * Used for display watermark bandwidth calculations
865 * Returns the min available bandwidth in MBytes/s
867 static u32 dce_v11_0_available_bandwidth(struct dce10_wm_params *wm)
869 /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
870 u32 dram_bandwidth = dce_v11_0_dram_bandwidth(wm);
871 u32 data_return_bandwidth = dce_v11_0_data_return_bandwidth(wm);
872 u32 dmif_req_bandwidth = dce_v11_0_dmif_request_bandwidth(wm);
874 return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
878 * dce_v11_0_average_bandwidth - get the average available bandwidth
880 * @wm: watermark calculation data
882 * Calculate the average available bandwidth used for display (CIK).
883 * Used for display watermark bandwidth calculations
884 * Returns the average available bandwidth in MBytes/s
886 static u32 dce_v11_0_average_bandwidth(struct dce10_wm_params *wm)
888 /* Calculate the display mode Average Bandwidth
889 * DisplayMode should contain the source and destination dimensions,
893 fixed20_12 line_time;
894 fixed20_12 src_width;
895 fixed20_12 bandwidth;
898 a.full = dfixed_const(1000);
899 line_time.full = dfixed_const(wm->active_time + wm->blank_time);
900 line_time.full = dfixed_div(line_time, a);
901 bpp.full = dfixed_const(wm->bytes_per_pixel);
902 src_width.full = dfixed_const(wm->src_width);
903 bandwidth.full = dfixed_mul(src_width, bpp);
904 bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
905 bandwidth.full = dfixed_div(bandwidth, line_time);
907 return dfixed_trunc(bandwidth);
911 * dce_v11_0_latency_watermark - get the latency watermark
913 * @wm: watermark calculation data
915 * Calculate the latency watermark (CIK).
916 * Used for display watermark bandwidth calculations
917 * Returns the latency watermark in ns
919 static u32 dce_v11_0_latency_watermark(struct dce10_wm_params *wm)
921 /* First calculate the latency in ns */
922 u32 mc_latency = 2000; /* 2000 ns. */
923 u32 available_bandwidth = dce_v11_0_available_bandwidth(wm);
924 u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
925 u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
926 u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
927 u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
928 (wm->num_heads * cursor_line_pair_return_time);
929 u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
930 u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
931 u32 tmp, dmif_size = 12288;
934 if (wm->num_heads == 0)
937 a.full = dfixed_const(2);
938 b.full = dfixed_const(1);
939 if ((wm->vsc.full > a.full) ||
940 ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
942 ((wm->vsc.full >= a.full) && wm->interlaced))
943 max_src_lines_per_dst_line = 4;
945 max_src_lines_per_dst_line = 2;
947 a.full = dfixed_const(available_bandwidth);
948 b.full = dfixed_const(wm->num_heads);
949 a.full = dfixed_div(a, b);
950 tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
951 tmp = min(dfixed_trunc(a), tmp);
953 lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
955 a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
956 b.full = dfixed_const(1000);
957 c.full = dfixed_const(lb_fill_bw);
958 b.full = dfixed_div(c, b);
959 a.full = dfixed_div(a, b);
960 line_fill_time = dfixed_trunc(a);
962 if (line_fill_time < wm->active_time)
965 return latency + (line_fill_time - wm->active_time);
970 * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
971 * average and available dram bandwidth
973 * @wm: watermark calculation data
975 * Check if the display average bandwidth fits in the display
976 * dram bandwidth (CIK).
977 * Used for display watermark bandwidth calculations
978 * Returns true if the display fits, false if not.
980 static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
982 if (dce_v11_0_average_bandwidth(wm) <=
983 (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
990 * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
991 * average and available bandwidth
993 * @wm: watermark calculation data
995 * Check if the display average bandwidth fits in the display
996 * available bandwidth (CIK).
997 * Used for display watermark bandwidth calculations
998 * Returns true if the display fits, false if not.
1000 static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
1002 if (dce_v11_0_average_bandwidth(wm) <=
1003 (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
1010 * dce_v11_0_check_latency_hiding - check latency hiding
1012 * @wm: watermark calculation data
1014 * Check latency hiding (CIK).
1015 * Used for display watermark bandwidth calculations
1016 * Returns true if the display fits, false if not.
1018 static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
1020 u32 lb_partitions = wm->lb_size / wm->src_width;
1021 u32 line_time = wm->active_time + wm->blank_time;
1022 u32 latency_tolerant_lines;
1026 a.full = dfixed_const(1);
1027 if (wm->vsc.full > a.full)
1028 latency_tolerant_lines = 1;
1030 if (lb_partitions <= (wm->vtaps + 1))
1031 latency_tolerant_lines = 1;
1033 latency_tolerant_lines = 2;
1036 latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
1038 if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
1045 * dce_v11_0_program_watermarks - program display watermarks
1047 * @adev: amdgpu_device pointer
1048 * @amdgpu_crtc: the selected display controller
1049 * @lb_size: line buffer size
1050 * @num_heads: number of display controllers in use
1052 * Calculate and program the display watermarks for the
1053 * selected display controller (CIK).
1055 static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
1056 struct amdgpu_crtc *amdgpu_crtc,
1057 u32 lb_size, u32 num_heads)
1059 struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
1060 struct dce10_wm_params wm_low, wm_high;
1063 u32 latency_watermark_a = 0, latency_watermark_b = 0;
1064 u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
1066 if (amdgpu_crtc->base.enabled && num_heads && mode) {
1067 active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
1069 line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
1071 line_time = min(line_time, (u32)65535);
1073 /* watermark for high clocks */
1074 if (adev->pm.dpm_enabled) {
1076 amdgpu_dpm_get_mclk(adev, false) * 10;
1078 amdgpu_dpm_get_sclk(adev, false) * 10;
1080 wm_high.yclk = adev->pm.current_mclk * 10;
1081 wm_high.sclk = adev->pm.current_sclk * 10;
1084 wm_high.disp_clk = mode->clock;
1085 wm_high.src_width = mode->crtc_hdisplay;
1086 wm_high.active_time = active_time;
1087 wm_high.blank_time = line_time - wm_high.active_time;
1088 wm_high.interlaced = false;
1089 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1090 wm_high.interlaced = true;
1091 wm_high.vsc = amdgpu_crtc->vsc;
1093 if (amdgpu_crtc->rmx_type != RMX_OFF)
1095 wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1096 wm_high.lb_size = lb_size;
1097 wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1098 wm_high.num_heads = num_heads;
1100 /* set for high clocks */
1101 latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
1103 /* possibly force display priority to high */
1104 /* should really do this at mode validation time... */
1105 if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1106 !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1107 !dce_v11_0_check_latency_hiding(&wm_high) ||
1108 (adev->mode_info.disp_priority == 2)) {
1109 DRM_DEBUG_KMS("force priority to high\n");
1112 /* watermark for low clocks */
1113 if (adev->pm.dpm_enabled) {
1115 amdgpu_dpm_get_mclk(adev, true) * 10;
1117 amdgpu_dpm_get_sclk(adev, true) * 10;
1119 wm_low.yclk = adev->pm.current_mclk * 10;
1120 wm_low.sclk = adev->pm.current_sclk * 10;
1123 wm_low.disp_clk = mode->clock;
1124 wm_low.src_width = mode->crtc_hdisplay;
1125 wm_low.active_time = active_time;
1126 wm_low.blank_time = line_time - wm_low.active_time;
1127 wm_low.interlaced = false;
1128 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1129 wm_low.interlaced = true;
1130 wm_low.vsc = amdgpu_crtc->vsc;
1132 if (amdgpu_crtc->rmx_type != RMX_OFF)
1134 wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1135 wm_low.lb_size = lb_size;
1136 wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1137 wm_low.num_heads = num_heads;
1139 /* set for low clocks */
1140 latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
1142 /* possibly force display priority to high */
1143 /* should really do this at mode validation time... */
1144 if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1145 !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1146 !dce_v11_0_check_latency_hiding(&wm_low) ||
1147 (adev->mode_info.disp_priority == 2)) {
1148 DRM_DEBUG_KMS("force priority to high\n");
1150 lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
1154 wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1155 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
1156 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1157 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1158 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
1159 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1160 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1162 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
1163 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1164 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1165 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
1166 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1167 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1168 /* restore original selection */
1169 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1171 /* save values for DPM */
1172 amdgpu_crtc->line_time = line_time;
1173 amdgpu_crtc->wm_high = latency_watermark_a;
1174 amdgpu_crtc->wm_low = latency_watermark_b;
1175 /* Save number of lines the linebuffer leads before the scanout */
1176 amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
1180 * dce_v11_0_bandwidth_update - program display watermarks
1182 * @adev: amdgpu_device pointer
1184 * Calculate and program the display watermarks and line
1185 * buffer allocation (CIK).
1187 static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
1189 struct drm_display_mode *mode = NULL;
1190 u32 num_heads = 0, lb_size;
1193 amdgpu_display_update_priority(adev);
1195 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1196 if (adev->mode_info.crtcs[i]->base.enabled)
1199 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1200 mode = &adev->mode_info.crtcs[i]->base.mode;
1201 lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1202 dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1203 lb_size, num_heads);
1207 static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
1212 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1213 offset = adev->mode_info.audio.pin[i].offset;
1214 tmp = RREG32_AUDIO_ENDPT(offset,
1215 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1217 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1218 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1219 adev->mode_info.audio.pin[i].connected = false;
1221 adev->mode_info.audio.pin[i].connected = true;
1225 static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
1229 dce_v11_0_audio_get_connected_pins(adev);
1231 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1232 if (adev->mode_info.audio.pin[i].connected)
1233 return &adev->mode_info.audio.pin[i];
1235 DRM_ERROR("No connected audio pins found!\n");
1239 static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1241 struct amdgpu_device *adev = drm_to_adev(encoder->dev);
1242 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1243 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1246 if (!dig || !dig->afmt || !dig->afmt->pin)
1249 tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
1250 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
1251 WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
1254 static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
1255 struct drm_display_mode *mode)
1257 struct drm_device *dev = encoder->dev;
1258 struct amdgpu_device *adev = drm_to_adev(dev);
1259 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1260 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1261 struct drm_connector *connector;
1262 struct drm_connector_list_iter iter;
1263 struct amdgpu_connector *amdgpu_connector = NULL;
1267 if (!dig || !dig->afmt || !dig->afmt->pin)
1270 drm_connector_list_iter_begin(dev, &iter);
1271 drm_for_each_connector_iter(connector, &iter) {
1272 if (connector->encoder == encoder) {
1273 amdgpu_connector = to_amdgpu_connector(connector);
1277 drm_connector_list_iter_end(&iter);
1279 if (!amdgpu_connector) {
1280 DRM_ERROR("Couldn't find encoder's connector\n");
1284 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1286 if (connector->latency_present[interlace]) {
1287 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1288 VIDEO_LIPSYNC, connector->video_latency[interlace]);
1289 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1290 AUDIO_LIPSYNC, connector->audio_latency[interlace]);
1292 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1294 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1297 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1298 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1301 static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1303 struct drm_device *dev = encoder->dev;
1304 struct amdgpu_device *adev = drm_to_adev(dev);
1305 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1306 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1307 struct drm_connector *connector;
1308 struct drm_connector_list_iter iter;
1309 struct amdgpu_connector *amdgpu_connector = NULL;
1314 if (!dig || !dig->afmt || !dig->afmt->pin)
1317 drm_connector_list_iter_begin(dev, &iter);
1318 drm_for_each_connector_iter(connector, &iter) {
1319 if (connector->encoder == encoder) {
1320 amdgpu_connector = to_amdgpu_connector(connector);
1324 drm_connector_list_iter_end(&iter);
1326 if (!amdgpu_connector) {
1327 DRM_ERROR("Couldn't find encoder's connector\n");
1331 sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
1332 if (sad_count < 0) {
1333 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1337 /* program the speaker allocation */
1338 tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1339 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1340 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1343 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1344 HDMI_CONNECTION, 1);
1346 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1347 SPEAKER_ALLOCATION, sadb[0]);
1349 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1350 SPEAKER_ALLOCATION, 5); /* stereo */
1351 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1352 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1357 static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
1359 struct drm_device *dev = encoder->dev;
1360 struct amdgpu_device *adev = drm_to_adev(dev);
1361 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1362 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1363 struct drm_connector *connector;
1364 struct drm_connector_list_iter iter;
1365 struct amdgpu_connector *amdgpu_connector = NULL;
1366 struct cea_sad *sads;
1369 static const u16 eld_reg_to_type[][2] = {
1370 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1371 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1372 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1373 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1374 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1375 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1376 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1377 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1378 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1379 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1380 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1381 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1384 if (!dig || !dig->afmt || !dig->afmt->pin)
1387 drm_connector_list_iter_begin(dev, &iter);
1388 drm_for_each_connector_iter(connector, &iter) {
1389 if (connector->encoder == encoder) {
1390 amdgpu_connector = to_amdgpu_connector(connector);
1394 drm_connector_list_iter_end(&iter);
1396 if (!amdgpu_connector) {
1397 DRM_ERROR("Couldn't find encoder's connector\n");
1401 sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
1403 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1408 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1410 u8 stereo_freqs = 0;
1411 int max_channels = -1;
1414 for (j = 0; j < sad_count; j++) {
1415 struct cea_sad *sad = &sads[j];
1417 if (sad->format == eld_reg_to_type[i][1]) {
1418 if (sad->channels > max_channels) {
1419 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1420 MAX_CHANNELS, sad->channels);
1421 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1422 DESCRIPTOR_BYTE_2, sad->byte2);
1423 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1424 SUPPORTED_FREQUENCIES, sad->freq);
1425 max_channels = sad->channels;
1428 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1429 stereo_freqs |= sad->freq;
1435 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1436 SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
1437 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
1443 static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
1444 struct amdgpu_audio_pin *pin,
1450 WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1451 enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1454 static const u32 pin_offsets[] =
1456 AUD0_REGISTER_OFFSET,
1457 AUD1_REGISTER_OFFSET,
1458 AUD2_REGISTER_OFFSET,
1459 AUD3_REGISTER_OFFSET,
1460 AUD4_REGISTER_OFFSET,
1461 AUD5_REGISTER_OFFSET,
1462 AUD6_REGISTER_OFFSET,
1463 AUD7_REGISTER_OFFSET,
1466 static int dce_v11_0_audio_init(struct amdgpu_device *adev)
1473 adev->mode_info.audio.enabled = true;
1475 switch (adev->asic_type) {
1478 adev->mode_info.audio.num_pins = 7;
1480 case CHIP_POLARIS10:
1482 adev->mode_info.audio.num_pins = 8;
1484 case CHIP_POLARIS11:
1485 case CHIP_POLARIS12:
1486 adev->mode_info.audio.num_pins = 6;
1492 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1493 adev->mode_info.audio.pin[i].channels = -1;
1494 adev->mode_info.audio.pin[i].rate = -1;
1495 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1496 adev->mode_info.audio.pin[i].status_bits = 0;
1497 adev->mode_info.audio.pin[i].category_code = 0;
1498 adev->mode_info.audio.pin[i].connected = false;
1499 adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1500 adev->mode_info.audio.pin[i].id = i;
1501 /* disable audio. it will be set up later */
1502 /* XXX remove once we switch to ip funcs */
1503 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1509 static void dce_v11_0_audio_fini(struct amdgpu_device *adev)
1516 if (!adev->mode_info.audio.enabled)
1519 for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1520 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1522 adev->mode_info.audio.enabled = false;
1526 * update the N and CTS parameters for a given pixel clock rate
1528 static void dce_v11_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1530 struct drm_device *dev = encoder->dev;
1531 struct amdgpu_device *adev = drm_to_adev(dev);
1532 struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1533 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1534 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1537 tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
1538 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
1539 WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
1540 tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
1541 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
1542 WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
1544 tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
1545 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
1546 WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
1547 tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
1548 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
1549 WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
1551 tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
1552 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
1553 WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
1554 tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
1555 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
1556 WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
1561 * build a HDMI Video Info Frame
1563 static void dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1564 void *buffer, size_t size)
1566 struct drm_device *dev = encoder->dev;
1567 struct amdgpu_device *adev = drm_to_adev(dev);
1568 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1569 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1570 uint8_t *frame = buffer + 3;
1571 uint8_t *header = buffer;
1573 WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
1574 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1575 WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
1576 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1577 WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
1578 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1579 WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
1580 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1583 static void dce_v11_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1585 struct drm_device *dev = encoder->dev;
1586 struct amdgpu_device *adev = drm_to_adev(dev);
1587 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1588 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1589 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1590 u32 dto_phase = 24 * 1000;
1591 u32 dto_modulo = clock;
1594 if (!dig || !dig->afmt)
1597 /* XXX two dtos; generally use dto0 for hdmi */
1598 /* Express [24MHz / target pixel clock] as an exact rational
1599 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
1600 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1602 tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
1603 tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
1604 amdgpu_crtc->crtc_id);
1605 WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
1606 WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1607 WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1611 * update the info frames with the data from the current display mode
1613 static void dce_v11_0_afmt_setmode(struct drm_encoder *encoder,
1614 struct drm_display_mode *mode)
1616 struct drm_device *dev = encoder->dev;
1617 struct amdgpu_device *adev = drm_to_adev(dev);
1618 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1619 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1620 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1621 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1622 struct hdmi_avi_infoframe frame;
1627 if (!dig || !dig->afmt)
1630 /* Silent, r600_hdmi_enable will raise WARN for us */
1631 if (!dig->afmt->enabled)
1634 /* hdmi deep color mode general control packets setup, if bpc > 8 */
1635 if (encoder->crtc) {
1636 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1637 bpc = amdgpu_crtc->bpc;
1640 /* disable audio prior to setting up hw */
1641 dig->afmt->pin = dce_v11_0_audio_get_pin(adev);
1642 dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
1644 dce_v11_0_audio_set_dto(encoder, mode->clock);
1646 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1647 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
1648 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
1650 WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
1652 tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
1659 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
1660 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
1661 DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1662 connector->name, bpc);
1665 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1666 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
1667 DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1671 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1672 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
1673 DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1677 WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
1679 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1680 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
1681 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
1682 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
1683 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
1685 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1686 /* enable audio info frames (frames won't be set until audio is enabled) */
1687 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
1688 /* required for audio info values to be updated */
1689 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
1690 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1692 tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
1693 /* required for audio info values to be updated */
1694 tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
1695 WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1697 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1698 /* anything other than 0 */
1699 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
1700 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1702 WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
1704 tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1705 /* set the default audio delay */
1706 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
1707 /* should be suffient for all audio modes and small enough for all hblanks */
1708 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
1709 WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1711 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1712 /* allow 60958 channel status fields to be updated */
1713 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
1714 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1716 tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
1718 /* clear SW CTS value */
1719 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
1721 /* select SW CTS value */
1722 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
1723 /* allow hw to sent ACR packets when required */
1724 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
1725 WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
1727 dce_v11_0_afmt_update_ACR(encoder, mode->clock);
1729 tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
1730 tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
1731 WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
1733 tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
1734 tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
1735 WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
1737 tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
1738 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
1739 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
1740 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
1741 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
1742 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
1743 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
1744 WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
1746 dce_v11_0_audio_write_speaker_allocation(encoder);
1748 WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
1749 (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1751 dce_v11_0_afmt_audio_select_pin(encoder);
1752 dce_v11_0_audio_write_sad_regs(encoder);
1753 dce_v11_0_audio_write_latency_fields(encoder, mode);
1755 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
1757 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1761 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1763 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1767 dce_v11_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1769 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1770 /* enable AVI info frames */
1771 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
1772 /* required for audio info values to be updated */
1773 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
1774 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1776 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1777 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
1778 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1780 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1781 /* send audio packets */
1782 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
1783 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1785 WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
1786 WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
1787 WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
1788 WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
1790 /* enable audio after to setting up hw */
1791 dce_v11_0_audio_enable(adev, dig->afmt->pin, true);
1794 static void dce_v11_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1796 struct drm_device *dev = encoder->dev;
1797 struct amdgpu_device *adev = drm_to_adev(dev);
1798 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1799 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1801 if (!dig || !dig->afmt)
1804 /* Silent, r600_hdmi_enable will raise WARN for us */
1805 if (enable && dig->afmt->enabled)
1807 if (!enable && !dig->afmt->enabled)
1810 if (!enable && dig->afmt->pin) {
1811 dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
1812 dig->afmt->pin = NULL;
1815 dig->afmt->enabled = enable;
1817 DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1818 enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1821 static int dce_v11_0_afmt_init(struct amdgpu_device *adev)
1825 for (i = 0; i < adev->mode_info.num_dig; i++)
1826 adev->mode_info.afmt[i] = NULL;
1828 /* DCE11 has audio blocks tied to DIG encoders */
1829 for (i = 0; i < adev->mode_info.num_dig; i++) {
1830 adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1831 if (adev->mode_info.afmt[i]) {
1832 adev->mode_info.afmt[i]->offset = dig_offsets[i];
1833 adev->mode_info.afmt[i]->id = i;
1836 for (j = 0; j < i; j++) {
1837 kfree(adev->mode_info.afmt[j]);
1838 adev->mode_info.afmt[j] = NULL;
1846 static void dce_v11_0_afmt_fini(struct amdgpu_device *adev)
1850 for (i = 0; i < adev->mode_info.num_dig; i++) {
1851 kfree(adev->mode_info.afmt[i]);
1852 adev->mode_info.afmt[i] = NULL;
1856 static const u32 vga_control_regs[6] =
1866 static void dce_v11_0_vga_enable(struct drm_crtc *crtc, bool enable)
1868 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1869 struct drm_device *dev = crtc->dev;
1870 struct amdgpu_device *adev = drm_to_adev(dev);
1873 vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
1875 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
1877 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
1880 static void dce_v11_0_grph_enable(struct drm_crtc *crtc, bool enable)
1882 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1883 struct drm_device *dev = crtc->dev;
1884 struct amdgpu_device *adev = drm_to_adev(dev);
1887 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
1889 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
1892 static int dce_v11_0_crtc_do_set_base(struct drm_crtc *crtc,
1893 struct drm_framebuffer *fb,
1894 int x, int y, int atomic)
1896 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1897 struct drm_device *dev = crtc->dev;
1898 struct amdgpu_device *adev = drm_to_adev(dev);
1899 struct drm_framebuffer *target_fb;
1900 struct drm_gem_object *obj;
1901 struct amdgpu_bo *abo;
1902 uint64_t fb_location, tiling_flags;
1903 uint32_t fb_format, fb_pitch_pixels;
1904 u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
1906 u32 tmp, viewport_w, viewport_h;
1908 bool bypass_lut = false;
1911 if (!atomic && !crtc->primary->fb) {
1912 DRM_DEBUG_KMS("No FB bound\n");
1919 target_fb = crtc->primary->fb;
1921 /* If atomic, assume fb object is pinned & idle & fenced and
1922 * just update base pointers
1924 obj = target_fb->obj[0];
1925 abo = gem_to_amdgpu_bo(obj);
1926 r = amdgpu_bo_reserve(abo, false);
1927 if (unlikely(r != 0))
1931 r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM);
1932 if (unlikely(r != 0)) {
1933 amdgpu_bo_unreserve(abo);
1937 fb_location = amdgpu_bo_gpu_offset(abo);
1939 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
1940 amdgpu_bo_unreserve(abo);
1942 pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1944 switch (target_fb->format->format) {
1946 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
1947 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1949 case DRM_FORMAT_XRGB4444:
1950 case DRM_FORMAT_ARGB4444:
1951 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1952 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
1954 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1958 case DRM_FORMAT_XRGB1555:
1959 case DRM_FORMAT_ARGB1555:
1960 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1961 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1963 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1967 case DRM_FORMAT_BGRX5551:
1968 case DRM_FORMAT_BGRA5551:
1969 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1970 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
1972 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1976 case DRM_FORMAT_RGB565:
1977 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1978 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1980 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1984 case DRM_FORMAT_XRGB8888:
1985 case DRM_FORMAT_ARGB8888:
1986 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1987 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1989 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1993 case DRM_FORMAT_XRGB2101010:
1994 case DRM_FORMAT_ARGB2101010:
1995 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1996 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1998 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2001 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2004 case DRM_FORMAT_BGRX1010102:
2005 case DRM_FORMAT_BGRA1010102:
2006 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2007 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
2009 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2012 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2015 case DRM_FORMAT_XBGR8888:
2016 case DRM_FORMAT_ABGR8888:
2017 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2018 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2019 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_RED_CROSSBAR, 2);
2020 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_BLUE_CROSSBAR, 2);
2022 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2027 DRM_ERROR("Unsupported screen format %p4cc\n",
2028 &target_fb->format->format);
2032 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
2033 unsigned bankw, bankh, mtaspect, tile_split, num_banks;
2035 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2036 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2037 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2038 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2039 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2041 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
2042 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2043 ARRAY_2D_TILED_THIN1);
2044 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
2046 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
2047 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
2048 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
2050 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
2051 ADDR_SURF_MICRO_TILING_DISPLAY);
2052 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
2053 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2054 ARRAY_1D_TILED_THIN1);
2057 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
2060 dce_v11_0_vga_enable(crtc, false);
2062 /* Make sure surface address is updated at vertical blank rather than
2065 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
2066 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
2067 GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
2068 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2070 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2071 upper_32_bits(fb_location));
2072 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2073 upper_32_bits(fb_location));
2074 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2075 (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
2076 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2077 (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
2078 WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
2079 WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
2082 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
2083 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
2084 * retain the full precision throughout the pipeline.
2086 tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
2088 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
2090 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
2091 WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
2094 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
2096 WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
2097 WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
2098 WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
2099 WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
2100 WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
2101 WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
2103 fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
2104 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
2106 dce_v11_0_grph_enable(crtc, true);
2108 WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
2113 WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
2115 viewport_w = crtc->mode.hdisplay;
2116 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
2117 WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
2118 (viewport_w << 16) | viewport_h);
2120 /* set pageflip to happen anywhere in vblank interval */
2121 WREG32(mmCRTC_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
2123 if (!atomic && fb && fb != crtc->primary->fb) {
2124 abo = gem_to_amdgpu_bo(fb->obj[0]);
2125 r = amdgpu_bo_reserve(abo, true);
2126 if (unlikely(r != 0))
2128 amdgpu_bo_unpin(abo);
2129 amdgpu_bo_unreserve(abo);
2132 /* Bytes per pixel may have changed */
2133 dce_v11_0_bandwidth_update(adev);
2138 static void dce_v11_0_set_interleave(struct drm_crtc *crtc,
2139 struct drm_display_mode *mode)
2141 struct drm_device *dev = crtc->dev;
2142 struct amdgpu_device *adev = drm_to_adev(dev);
2143 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2146 tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
2147 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2148 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
2150 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
2151 WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
2154 static void dce_v11_0_crtc_load_lut(struct drm_crtc *crtc)
2156 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2157 struct drm_device *dev = crtc->dev;
2158 struct amdgpu_device *adev = drm_to_adev(dev);
2163 DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2165 tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2166 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
2167 WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2169 tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
2170 tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
2171 WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2173 tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2174 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
2175 WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2177 WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2179 WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2180 WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2181 WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2183 WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2184 WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2185 WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2187 WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2188 WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2190 WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
2191 r = crtc->gamma_store;
2192 g = r + crtc->gamma_size;
2193 b = g + crtc->gamma_size;
2194 for (i = 0; i < 256; i++) {
2195 WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
2196 ((*r++ & 0xffc0) << 14) |
2197 ((*g++ & 0xffc0) << 4) |
2201 tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2202 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
2203 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
2204 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR2_DEGAMMA_MODE, 0);
2205 WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2207 tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
2208 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
2209 WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2211 tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2212 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
2213 WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2215 tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2216 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
2217 WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2219 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
2220 WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
2221 /* XXX this only needs to be programmed once per crtc at startup,
2222 * not sure where the best place for it is
2224 tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
2225 tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
2226 WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2229 static int dce_v11_0_pick_dig_encoder(struct drm_encoder *encoder)
2231 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2232 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2234 switch (amdgpu_encoder->encoder_id) {
2235 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2240 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2245 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2250 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2253 DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2259 * dce_v11_0_pick_pll - Allocate a PPLL for use by the crtc.
2263 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
2264 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
2265 * monitors a dedicated PPLL must be used. If a particular board has
2266 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2267 * as there is no need to program the PLL itself. If we are not able to
2268 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2269 * avoid messing up an existing monitor.
2271 * Asic specific PLL information
2275 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2277 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2280 static u32 dce_v11_0_pick_pll(struct drm_crtc *crtc)
2282 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2283 struct drm_device *dev = crtc->dev;
2284 struct amdgpu_device *adev = drm_to_adev(dev);
2288 if ((adev->asic_type == CHIP_POLARIS10) ||
2289 (adev->asic_type == CHIP_POLARIS11) ||
2290 (adev->asic_type == CHIP_POLARIS12) ||
2291 (adev->asic_type == CHIP_VEGAM)) {
2292 struct amdgpu_encoder *amdgpu_encoder =
2293 to_amdgpu_encoder(amdgpu_crtc->encoder);
2294 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2296 if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2299 switch (amdgpu_encoder->encoder_id) {
2300 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2302 return ATOM_COMBOPHY_PLL1;
2304 return ATOM_COMBOPHY_PLL0;
2305 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2307 return ATOM_COMBOPHY_PLL3;
2309 return ATOM_COMBOPHY_PLL2;
2310 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2312 return ATOM_COMBOPHY_PLL5;
2314 return ATOM_COMBOPHY_PLL4;
2316 DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2317 return ATOM_PPLL_INVALID;
2321 if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2322 if (adev->clock.dp_extclk)
2323 /* skip PPLL programming if using ext clock */
2324 return ATOM_PPLL_INVALID;
2326 /* use the same PPLL for all DP monitors */
2327 pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2328 if (pll != ATOM_PPLL_INVALID)
2332 /* use the same PPLL for all monitors with the same clock */
2333 pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2334 if (pll != ATOM_PPLL_INVALID)
2338 /* XXX need to determine what plls are available on each DCE11 part */
2339 pll_in_use = amdgpu_pll_get_use_mask(crtc);
2340 if (adev->flags & AMD_IS_APU) {
2341 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2343 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2345 DRM_ERROR("unable to allocate a PPLL\n");
2346 return ATOM_PPLL_INVALID;
2348 if (!(pll_in_use & (1 << ATOM_PPLL2)))
2350 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2352 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2354 DRM_ERROR("unable to allocate a PPLL\n");
2355 return ATOM_PPLL_INVALID;
2357 return ATOM_PPLL_INVALID;
2360 static void dce_v11_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2362 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2363 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2366 cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2368 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
2370 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
2371 WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2374 static void dce_v11_0_hide_cursor(struct drm_crtc *crtc)
2376 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2377 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2380 tmp = RREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2381 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
2382 WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2385 static void dce_v11_0_show_cursor(struct drm_crtc *crtc)
2387 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2388 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2391 WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2392 upper_32_bits(amdgpu_crtc->cursor_addr));
2393 WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2394 lower_32_bits(amdgpu_crtc->cursor_addr));
2396 tmp = RREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2397 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
2398 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
2399 WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2402 static int dce_v11_0_cursor_move_locked(struct drm_crtc *crtc,
2405 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2406 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2407 int xorigin = 0, yorigin = 0;
2409 amdgpu_crtc->cursor_x = x;
2410 amdgpu_crtc->cursor_y = y;
2412 /* avivo cursor are offset into the total surface */
2415 DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2418 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2422 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2426 WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2427 WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
2428 WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
2429 ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
2434 static int dce_v11_0_crtc_cursor_move(struct drm_crtc *crtc,
2439 dce_v11_0_lock_cursor(crtc, true);
2440 ret = dce_v11_0_cursor_move_locked(crtc, x, y);
2441 dce_v11_0_lock_cursor(crtc, false);
2446 static int dce_v11_0_crtc_cursor_set2(struct drm_crtc *crtc,
2447 struct drm_file *file_priv,
2454 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2455 struct drm_gem_object *obj;
2456 struct amdgpu_bo *aobj;
2460 /* turn off cursor */
2461 dce_v11_0_hide_cursor(crtc);
2466 if ((width > amdgpu_crtc->max_cursor_width) ||
2467 (height > amdgpu_crtc->max_cursor_height)) {
2468 DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2472 obj = drm_gem_object_lookup(file_priv, handle);
2474 DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2478 aobj = gem_to_amdgpu_bo(obj);
2479 ret = amdgpu_bo_reserve(aobj, false);
2481 drm_gem_object_put(obj);
2485 ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
2486 amdgpu_bo_unreserve(aobj);
2488 DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2489 drm_gem_object_put(obj);
2492 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2494 dce_v11_0_lock_cursor(crtc, true);
2496 if (width != amdgpu_crtc->cursor_width ||
2497 height != amdgpu_crtc->cursor_height ||
2498 hot_x != amdgpu_crtc->cursor_hot_x ||
2499 hot_y != amdgpu_crtc->cursor_hot_y) {
2502 x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
2503 y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
2505 dce_v11_0_cursor_move_locked(crtc, x, y);
2507 amdgpu_crtc->cursor_width = width;
2508 amdgpu_crtc->cursor_height = height;
2509 amdgpu_crtc->cursor_hot_x = hot_x;
2510 amdgpu_crtc->cursor_hot_y = hot_y;
2513 dce_v11_0_show_cursor(crtc);
2514 dce_v11_0_lock_cursor(crtc, false);
2517 if (amdgpu_crtc->cursor_bo) {
2518 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2519 ret = amdgpu_bo_reserve(aobj, true);
2520 if (likely(ret == 0)) {
2521 amdgpu_bo_unpin(aobj);
2522 amdgpu_bo_unreserve(aobj);
2524 drm_gem_object_put(amdgpu_crtc->cursor_bo);
2527 amdgpu_crtc->cursor_bo = obj;
2531 static void dce_v11_0_cursor_reset(struct drm_crtc *crtc)
2533 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2535 if (amdgpu_crtc->cursor_bo) {
2536 dce_v11_0_lock_cursor(crtc, true);
2538 dce_v11_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
2539 amdgpu_crtc->cursor_y);
2541 dce_v11_0_show_cursor(crtc);
2543 dce_v11_0_lock_cursor(crtc, false);
2547 static int dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2548 u16 *blue, uint32_t size,
2549 struct drm_modeset_acquire_ctx *ctx)
2551 dce_v11_0_crtc_load_lut(crtc);
2556 static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
2558 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2560 drm_crtc_cleanup(crtc);
2564 static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
2565 .cursor_set2 = dce_v11_0_crtc_cursor_set2,
2566 .cursor_move = dce_v11_0_crtc_cursor_move,
2567 .gamma_set = dce_v11_0_crtc_gamma_set,
2568 .set_config = amdgpu_display_crtc_set_config,
2569 .destroy = dce_v11_0_crtc_destroy,
2570 .page_flip_target = amdgpu_display_crtc_page_flip_target,
2571 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
2572 .enable_vblank = amdgpu_enable_vblank_kms,
2573 .disable_vblank = amdgpu_disable_vblank_kms,
2574 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
2577 static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2579 struct drm_device *dev = crtc->dev;
2580 struct amdgpu_device *adev = drm_to_adev(dev);
2581 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2585 case DRM_MODE_DPMS_ON:
2586 amdgpu_crtc->enabled = true;
2587 amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2588 dce_v11_0_vga_enable(crtc, true);
2589 amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2590 dce_v11_0_vga_enable(crtc, false);
2591 /* Make sure VBLANK and PFLIP interrupts are still enabled */
2592 type = amdgpu_display_crtc_idx_to_irq_type(adev,
2593 amdgpu_crtc->crtc_id);
2594 amdgpu_irq_update(adev, &adev->crtc_irq, type);
2595 amdgpu_irq_update(adev, &adev->pageflip_irq, type);
2596 drm_crtc_vblank_on(crtc);
2597 dce_v11_0_crtc_load_lut(crtc);
2599 case DRM_MODE_DPMS_STANDBY:
2600 case DRM_MODE_DPMS_SUSPEND:
2601 case DRM_MODE_DPMS_OFF:
2602 drm_crtc_vblank_off(crtc);
2603 if (amdgpu_crtc->enabled) {
2604 dce_v11_0_vga_enable(crtc, true);
2605 amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2606 dce_v11_0_vga_enable(crtc, false);
2608 amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2609 amdgpu_crtc->enabled = false;
2612 /* adjust pm to dpms */
2613 amdgpu_dpm_compute_clocks(adev);
2616 static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
2618 /* disable crtc pair power gating before programming */
2619 amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2620 amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2621 dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2624 static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
2626 dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2627 amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2630 static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
2632 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2633 struct drm_device *dev = crtc->dev;
2634 struct amdgpu_device *adev = drm_to_adev(dev);
2635 struct amdgpu_atom_ss ss;
2638 dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2639 if (crtc->primary->fb) {
2641 struct amdgpu_bo *abo;
2643 abo = gem_to_amdgpu_bo(crtc->primary->fb->obj[0]);
2644 r = amdgpu_bo_reserve(abo, true);
2646 DRM_ERROR("failed to reserve abo before unpin\n");
2648 amdgpu_bo_unpin(abo);
2649 amdgpu_bo_unreserve(abo);
2652 /* disable the GRPH */
2653 dce_v11_0_grph_enable(crtc, false);
2655 amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2657 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2658 if (adev->mode_info.crtcs[i] &&
2659 adev->mode_info.crtcs[i]->enabled &&
2660 i != amdgpu_crtc->crtc_id &&
2661 amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2662 /* one other crtc is using this pll don't turn
2669 switch (amdgpu_crtc->pll_id) {
2673 /* disable the ppll */
2674 amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2675 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2677 case ATOM_COMBOPHY_PLL0:
2678 case ATOM_COMBOPHY_PLL1:
2679 case ATOM_COMBOPHY_PLL2:
2680 case ATOM_COMBOPHY_PLL3:
2681 case ATOM_COMBOPHY_PLL4:
2682 case ATOM_COMBOPHY_PLL5:
2683 /* disable the ppll */
2684 amdgpu_atombios_crtc_program_pll(crtc, ATOM_CRTC_INVALID, amdgpu_crtc->pll_id,
2685 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2691 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2692 amdgpu_crtc->adjusted_clock = 0;
2693 amdgpu_crtc->encoder = NULL;
2694 amdgpu_crtc->connector = NULL;
2697 static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
2698 struct drm_display_mode *mode,
2699 struct drm_display_mode *adjusted_mode,
2700 int x, int y, struct drm_framebuffer *old_fb)
2702 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2703 struct drm_device *dev = crtc->dev;
2704 struct amdgpu_device *adev = drm_to_adev(dev);
2706 if (!amdgpu_crtc->adjusted_clock)
2709 if ((adev->asic_type == CHIP_POLARIS10) ||
2710 (adev->asic_type == CHIP_POLARIS11) ||
2711 (adev->asic_type == CHIP_POLARIS12) ||
2712 (adev->asic_type == CHIP_VEGAM)) {
2713 struct amdgpu_encoder *amdgpu_encoder =
2714 to_amdgpu_encoder(amdgpu_crtc->encoder);
2716 amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
2718 /* SetPixelClock calculates the plls and ss values now */
2719 amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id,
2720 amdgpu_crtc->pll_id,
2721 encoder_mode, amdgpu_encoder->encoder_id,
2722 adjusted_mode->clock, 0, 0, 0, 0,
2723 amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
2725 amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
2727 amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2728 dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2729 amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2730 amdgpu_atombios_crtc_scaler_setup(crtc);
2731 dce_v11_0_cursor_reset(crtc);
2732 /* update the hw version fpr dpm */
2733 amdgpu_crtc->hw_mode = *adjusted_mode;
2738 static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
2739 const struct drm_display_mode *mode,
2740 struct drm_display_mode *adjusted_mode)
2742 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2743 struct drm_device *dev = crtc->dev;
2744 struct drm_encoder *encoder;
2746 /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2747 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2748 if (encoder->crtc == crtc) {
2749 amdgpu_crtc->encoder = encoder;
2750 amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2754 if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2755 amdgpu_crtc->encoder = NULL;
2756 amdgpu_crtc->connector = NULL;
2759 if (!amdgpu_display_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2761 if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2764 amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
2765 /* if we can't get a PPLL for a non-DP encoder, fail */
2766 if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2767 !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2773 static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2774 struct drm_framebuffer *old_fb)
2776 return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2779 static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2780 struct drm_framebuffer *fb,
2781 int x, int y, enum mode_set_atomic state)
2783 return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
2786 static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
2787 .dpms = dce_v11_0_crtc_dpms,
2788 .mode_fixup = dce_v11_0_crtc_mode_fixup,
2789 .mode_set = dce_v11_0_crtc_mode_set,
2790 .mode_set_base = dce_v11_0_crtc_set_base,
2791 .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
2792 .prepare = dce_v11_0_crtc_prepare,
2793 .commit = dce_v11_0_crtc_commit,
2794 .disable = dce_v11_0_crtc_disable,
2795 .get_scanout_position = amdgpu_crtc_get_scanout_position,
2798 static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
2800 struct amdgpu_crtc *amdgpu_crtc;
2802 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2803 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2804 if (amdgpu_crtc == NULL)
2807 drm_crtc_init(adev_to_drm(adev), &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
2809 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2810 amdgpu_crtc->crtc_id = index;
2811 adev->mode_info.crtcs[index] = amdgpu_crtc;
2813 amdgpu_crtc->max_cursor_width = 128;
2814 amdgpu_crtc->max_cursor_height = 128;
2815 adev_to_drm(adev)->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
2816 adev_to_drm(adev)->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2818 switch (amdgpu_crtc->crtc_id) {
2821 amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
2824 amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
2827 amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
2830 amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
2833 amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
2836 amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
2840 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2841 amdgpu_crtc->adjusted_clock = 0;
2842 amdgpu_crtc->encoder = NULL;
2843 amdgpu_crtc->connector = NULL;
2844 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
2849 static int dce_v11_0_early_init(void *handle)
2851 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2853 adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
2854 adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
2856 dce_v11_0_set_display_funcs(adev);
2858 adev->mode_info.num_crtc = dce_v11_0_get_num_crtc(adev);
2860 switch (adev->asic_type) {
2862 adev->mode_info.num_hpd = 6;
2863 adev->mode_info.num_dig = 9;
2866 adev->mode_info.num_hpd = 6;
2867 adev->mode_info.num_dig = 9;
2869 case CHIP_POLARIS10:
2871 adev->mode_info.num_hpd = 6;
2872 adev->mode_info.num_dig = 6;
2874 case CHIP_POLARIS11:
2875 case CHIP_POLARIS12:
2876 adev->mode_info.num_hpd = 5;
2877 adev->mode_info.num_dig = 5;
2880 /* FIXME: not supported yet */
2884 dce_v11_0_set_irq_funcs(adev);
2889 static int dce_v11_0_sw_init(void *handle)
2892 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2894 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2895 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
2900 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; i < 20; i += 2) {
2901 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i, &adev->pageflip_irq);
2907 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
2911 adev_to_drm(adev)->mode_config.funcs = &amdgpu_mode_funcs;
2913 adev_to_drm(adev)->mode_config.async_page_flip = true;
2915 adev_to_drm(adev)->mode_config.max_width = 16384;
2916 adev_to_drm(adev)->mode_config.max_height = 16384;
2918 adev_to_drm(adev)->mode_config.preferred_depth = 24;
2919 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
2921 adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
2923 r = amdgpu_display_modeset_create_props(adev);
2927 adev_to_drm(adev)->mode_config.max_width = 16384;
2928 adev_to_drm(adev)->mode_config.max_height = 16384;
2931 /* allocate crtcs */
2932 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2933 r = dce_v11_0_crtc_init(adev, i);
2938 if (amdgpu_atombios_get_connector_info_from_object_table(adev))
2939 amdgpu_display_print_display_setup(adev_to_drm(adev));
2944 r = dce_v11_0_afmt_init(adev);
2948 r = dce_v11_0_audio_init(adev);
2952 /* Disable vblank IRQs aggressively for power-saving */
2953 /* XXX: can this be enabled for DC? */
2954 adev_to_drm(adev)->vblank_disable_immediate = true;
2956 r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
2960 INIT_DELAYED_WORK(&adev->hotplug_work,
2961 amdgpu_display_hotplug_work_func);
2963 drm_kms_helper_poll_init(adev_to_drm(adev));
2965 adev->mode_info.mode_config_initialized = true;
2969 static int dce_v11_0_sw_fini(void *handle)
2971 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2973 kfree(adev->mode_info.bios_hardcoded_edid);
2975 drm_kms_helper_poll_fini(adev_to_drm(adev));
2977 dce_v11_0_audio_fini(adev);
2979 dce_v11_0_afmt_fini(adev);
2981 drm_mode_config_cleanup(adev_to_drm(adev));
2982 adev->mode_info.mode_config_initialized = false;
2987 static int dce_v11_0_hw_init(void *handle)
2990 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2992 dce_v11_0_init_golden_registers(adev);
2994 /* disable vga render */
2995 dce_v11_0_set_vga_render_state(adev, false);
2996 /* init dig PHYs, disp eng pll */
2997 amdgpu_atombios_crtc_powergate_init(adev);
2998 amdgpu_atombios_encoder_init_dig(adev);
2999 if ((adev->asic_type == CHIP_POLARIS10) ||
3000 (adev->asic_type == CHIP_POLARIS11) ||
3001 (adev->asic_type == CHIP_POLARIS12) ||
3002 (adev->asic_type == CHIP_VEGAM)) {
3003 amdgpu_atombios_crtc_set_dce_clock(adev, adev->clock.default_dispclk,
3004 DCE_CLOCK_TYPE_DISPCLK, ATOM_GCK_DFS);
3005 amdgpu_atombios_crtc_set_dce_clock(adev, 0,
3006 DCE_CLOCK_TYPE_DPREFCLK, ATOM_GCK_DFS);
3008 amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
3011 /* initialize hpd */
3012 dce_v11_0_hpd_init(adev);
3014 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3015 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3018 dce_v11_0_pageflip_interrupt_init(adev);
3023 static int dce_v11_0_hw_fini(void *handle)
3026 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3028 dce_v11_0_hpd_fini(adev);
3030 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3031 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3034 dce_v11_0_pageflip_interrupt_fini(adev);
3036 flush_delayed_work(&adev->hotplug_work);
3041 static int dce_v11_0_suspend(void *handle)
3043 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3046 r = amdgpu_display_suspend_helper(adev);
3050 adev->mode_info.bl_level =
3051 amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
3053 return dce_v11_0_hw_fini(handle);
3056 static int dce_v11_0_resume(void *handle)
3058 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3061 amdgpu_atombios_encoder_set_backlight_level_to_reg(adev,
3062 adev->mode_info.bl_level);
3064 ret = dce_v11_0_hw_init(handle);
3066 /* turn on the BL */
3067 if (adev->mode_info.bl_encoder) {
3068 u8 bl_level = amdgpu_display_backlight_get_level(adev,
3069 adev->mode_info.bl_encoder);
3070 amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
3076 return amdgpu_display_resume_helper(adev);
3079 static bool dce_v11_0_is_idle(void *handle)
3084 static int dce_v11_0_wait_for_idle(void *handle)
3089 static int dce_v11_0_soft_reset(void *handle)
3091 u32 srbm_soft_reset = 0, tmp;
3092 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3094 if (dce_v11_0_is_display_hung(adev))
3095 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
3097 if (srbm_soft_reset) {
3098 tmp = RREG32(mmSRBM_SOFT_RESET);
3099 tmp |= srbm_soft_reset;
3100 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
3101 WREG32(mmSRBM_SOFT_RESET, tmp);
3102 tmp = RREG32(mmSRBM_SOFT_RESET);
3106 tmp &= ~srbm_soft_reset;
3107 WREG32(mmSRBM_SOFT_RESET, tmp);
3108 tmp = RREG32(mmSRBM_SOFT_RESET);
3110 /* Wait a little for things to settle down */
3116 static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
3118 enum amdgpu_interrupt_state state)
3120 u32 lb_interrupt_mask;
3122 if (crtc >= adev->mode_info.num_crtc) {
3123 DRM_DEBUG("invalid crtc %d\n", crtc);
3128 case AMDGPU_IRQ_STATE_DISABLE:
3129 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3130 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3131 VBLANK_INTERRUPT_MASK, 0);
3132 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3134 case AMDGPU_IRQ_STATE_ENABLE:
3135 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3136 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3137 VBLANK_INTERRUPT_MASK, 1);
3138 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3145 static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
3147 enum amdgpu_interrupt_state state)
3149 u32 lb_interrupt_mask;
3151 if (crtc >= adev->mode_info.num_crtc) {
3152 DRM_DEBUG("invalid crtc %d\n", crtc);
3157 case AMDGPU_IRQ_STATE_DISABLE:
3158 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3159 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3160 VLINE_INTERRUPT_MASK, 0);
3161 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3163 case AMDGPU_IRQ_STATE_ENABLE:
3164 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3165 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3166 VLINE_INTERRUPT_MASK, 1);
3167 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3174 static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
3175 struct amdgpu_irq_src *source,
3177 enum amdgpu_interrupt_state state)
3181 if (hpd >= adev->mode_info.num_hpd) {
3182 DRM_DEBUG("invalid hdp %d\n", hpd);
3187 case AMDGPU_IRQ_STATE_DISABLE:
3188 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3189 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
3190 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3192 case AMDGPU_IRQ_STATE_ENABLE:
3193 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3194 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
3195 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3204 static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
3205 struct amdgpu_irq_src *source,
3207 enum amdgpu_interrupt_state state)
3210 case AMDGPU_CRTC_IRQ_VBLANK1:
3211 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3213 case AMDGPU_CRTC_IRQ_VBLANK2:
3214 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3216 case AMDGPU_CRTC_IRQ_VBLANK3:
3217 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3219 case AMDGPU_CRTC_IRQ_VBLANK4:
3220 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3222 case AMDGPU_CRTC_IRQ_VBLANK5:
3223 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3225 case AMDGPU_CRTC_IRQ_VBLANK6:
3226 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3228 case AMDGPU_CRTC_IRQ_VLINE1:
3229 dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
3231 case AMDGPU_CRTC_IRQ_VLINE2:
3232 dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
3234 case AMDGPU_CRTC_IRQ_VLINE3:
3235 dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
3237 case AMDGPU_CRTC_IRQ_VLINE4:
3238 dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
3240 case AMDGPU_CRTC_IRQ_VLINE5:
3241 dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
3243 case AMDGPU_CRTC_IRQ_VLINE6:
3244 dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
3252 static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
3253 struct amdgpu_irq_src *src,
3255 enum amdgpu_interrupt_state state)
3259 if (type >= adev->mode_info.num_crtc) {
3260 DRM_ERROR("invalid pageflip crtc %d\n", type);
3264 reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3265 if (state == AMDGPU_IRQ_STATE_DISABLE)
3266 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3267 reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3269 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3270 reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3275 static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
3276 struct amdgpu_irq_src *source,
3277 struct amdgpu_iv_entry *entry)
3279 unsigned long flags;
3281 struct amdgpu_crtc *amdgpu_crtc;
3282 struct amdgpu_flip_work *works;
3284 crtc_id = (entry->src_id - 8) >> 1;
3285 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3287 if (crtc_id >= adev->mode_info.num_crtc) {
3288 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3292 if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
3293 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
3294 WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
3295 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3297 /* IRQ could occur when in initial stage */
3298 if(amdgpu_crtc == NULL)
3301 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
3302 works = amdgpu_crtc->pflip_works;
3303 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
3304 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3305 "AMDGPU_FLIP_SUBMITTED(%d)\n",
3306 amdgpu_crtc->pflip_status,
3307 AMDGPU_FLIP_SUBMITTED);
3308 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3312 /* page flip completed. clean up */
3313 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3314 amdgpu_crtc->pflip_works = NULL;
3316 /* wakeup usersapce */
3318 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
3320 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3322 drm_crtc_vblank_put(&amdgpu_crtc->base);
3323 schedule_work(&works->unpin_work);
3328 static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
3333 if (hpd >= adev->mode_info.num_hpd) {
3334 DRM_DEBUG("invalid hdp %d\n", hpd);
3338 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3339 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
3340 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3343 static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
3348 if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
3349 DRM_DEBUG("invalid crtc %d\n", crtc);
3353 tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
3354 tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
3355 WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
3358 static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
3363 if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
3364 DRM_DEBUG("invalid crtc %d\n", crtc);
3368 tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
3369 tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
3370 WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
3373 static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
3374 struct amdgpu_irq_src *source,
3375 struct amdgpu_iv_entry *entry)
3377 unsigned crtc = entry->src_id - 1;
3378 uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3379 unsigned int irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
3382 switch (entry->src_data[0]) {
3383 case 0: /* vblank */
3384 if (disp_int & interrupt_status_offsets[crtc].vblank)
3385 dce_v11_0_crtc_vblank_int_ack(adev, crtc);
3387 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3389 if (amdgpu_irq_enabled(adev, source, irq_type)) {
3390 drm_handle_vblank(adev_to_drm(adev), crtc);
3392 DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3396 if (disp_int & interrupt_status_offsets[crtc].vline)
3397 dce_v11_0_crtc_vline_int_ack(adev, crtc);
3399 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3401 DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3405 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3412 static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
3413 struct amdgpu_irq_src *source,
3414 struct amdgpu_iv_entry *entry)
3416 uint32_t disp_int, mask;
3419 if (entry->src_data[0] >= adev->mode_info.num_hpd) {
3420 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3424 hpd = entry->src_data[0];
3425 disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3426 mask = interrupt_status_offsets[hpd].hpd;
3428 if (disp_int & mask) {
3429 dce_v11_0_hpd_int_ack(adev, hpd);
3430 schedule_delayed_work(&adev->hotplug_work, 0);
3431 DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3437 static int dce_v11_0_set_clockgating_state(void *handle,
3438 enum amd_clockgating_state state)
3443 static int dce_v11_0_set_powergating_state(void *handle,
3444 enum amd_powergating_state state)
3449 static const struct amd_ip_funcs dce_v11_0_ip_funcs = {
3450 .name = "dce_v11_0",
3451 .early_init = dce_v11_0_early_init,
3453 .sw_init = dce_v11_0_sw_init,
3454 .sw_fini = dce_v11_0_sw_fini,
3455 .hw_init = dce_v11_0_hw_init,
3456 .hw_fini = dce_v11_0_hw_fini,
3457 .suspend = dce_v11_0_suspend,
3458 .resume = dce_v11_0_resume,
3459 .is_idle = dce_v11_0_is_idle,
3460 .wait_for_idle = dce_v11_0_wait_for_idle,
3461 .soft_reset = dce_v11_0_soft_reset,
3462 .set_clockgating_state = dce_v11_0_set_clockgating_state,
3463 .set_powergating_state = dce_v11_0_set_powergating_state,
3467 dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
3468 struct drm_display_mode *mode,
3469 struct drm_display_mode *adjusted_mode)
3471 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3473 amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3475 /* need to call this here rather than in prepare() since we need some crtc info */
3476 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3478 /* set scaler clears this on some chips */
3479 dce_v11_0_set_interleave(encoder->crtc, mode);
3481 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3482 dce_v11_0_afmt_enable(encoder, true);
3483 dce_v11_0_afmt_setmode(encoder, adjusted_mode);
3487 static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
3489 struct amdgpu_device *adev = drm_to_adev(encoder->dev);
3490 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3491 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3493 if ((amdgpu_encoder->active_device &
3494 (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3495 (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3496 ENCODER_OBJECT_ID_NONE)) {
3497 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3499 dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
3500 if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3501 dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3505 amdgpu_atombios_scratch_regs_lock(adev, true);
3508 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3510 /* select the clock/data port if it uses a router */
3511 if (amdgpu_connector->router.cd_valid)
3512 amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3514 /* turn eDP panel on for mode set */
3515 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3516 amdgpu_atombios_encoder_set_edp_panel_power(connector,
3517 ATOM_TRANSMITTER_ACTION_POWER_ON);
3520 /* this is needed for the pll/ss setup to work correctly in some cases */
3521 amdgpu_atombios_encoder_set_crtc_source(encoder);
3522 /* set up the FMT blocks */
3523 dce_v11_0_program_fmt(encoder);
3526 static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
3528 struct drm_device *dev = encoder->dev;
3529 struct amdgpu_device *adev = drm_to_adev(dev);
3531 /* need to call this here as we need the crtc set up */
3532 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3533 amdgpu_atombios_scratch_regs_lock(adev, false);
3536 static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
3538 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3539 struct amdgpu_encoder_atom_dig *dig;
3541 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3543 if (amdgpu_atombios_encoder_is_digital(encoder)) {
3544 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3545 dce_v11_0_afmt_enable(encoder, false);
3546 dig = amdgpu_encoder->enc_priv;
3547 dig->dig_encoder = -1;
3549 amdgpu_encoder->active_device = 0;
3552 /* these are handled by the primary encoders */
3553 static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
3558 static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
3564 dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
3565 struct drm_display_mode *mode,
3566 struct drm_display_mode *adjusted_mode)
3571 static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
3577 dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
3582 static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
3583 .dpms = dce_v11_0_ext_dpms,
3584 .prepare = dce_v11_0_ext_prepare,
3585 .mode_set = dce_v11_0_ext_mode_set,
3586 .commit = dce_v11_0_ext_commit,
3587 .disable = dce_v11_0_ext_disable,
3588 /* no detect for TMDS/LVDS yet */
3591 static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
3592 .dpms = amdgpu_atombios_encoder_dpms,
3593 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3594 .prepare = dce_v11_0_encoder_prepare,
3595 .mode_set = dce_v11_0_encoder_mode_set,
3596 .commit = dce_v11_0_encoder_commit,
3597 .disable = dce_v11_0_encoder_disable,
3598 .detect = amdgpu_atombios_encoder_dig_detect,
3601 static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
3602 .dpms = amdgpu_atombios_encoder_dpms,
3603 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3604 .prepare = dce_v11_0_encoder_prepare,
3605 .mode_set = dce_v11_0_encoder_mode_set,
3606 .commit = dce_v11_0_encoder_commit,
3607 .detect = amdgpu_atombios_encoder_dac_detect,
3610 static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
3612 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3613 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3614 amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3615 kfree(amdgpu_encoder->enc_priv);
3616 drm_encoder_cleanup(encoder);
3617 kfree(amdgpu_encoder);
3620 static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
3621 .destroy = dce_v11_0_encoder_destroy,
3624 static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
3625 uint32_t encoder_enum,
3626 uint32_t supported_device,
3629 struct drm_device *dev = adev_to_drm(adev);
3630 struct drm_encoder *encoder;
3631 struct amdgpu_encoder *amdgpu_encoder;
3633 /* see if we already added it */
3634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3635 amdgpu_encoder = to_amdgpu_encoder(encoder);
3636 if (amdgpu_encoder->encoder_enum == encoder_enum) {
3637 amdgpu_encoder->devices |= supported_device;
3644 amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3645 if (!amdgpu_encoder)
3648 encoder = &amdgpu_encoder->base;
3649 switch (adev->mode_info.num_crtc) {
3651 encoder->possible_crtcs = 0x1;
3655 encoder->possible_crtcs = 0x3;
3658 encoder->possible_crtcs = 0x7;
3661 encoder->possible_crtcs = 0xf;
3664 encoder->possible_crtcs = 0x1f;
3667 encoder->possible_crtcs = 0x3f;
3671 amdgpu_encoder->enc_priv = NULL;
3673 amdgpu_encoder->encoder_enum = encoder_enum;
3674 amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3675 amdgpu_encoder->devices = supported_device;
3676 amdgpu_encoder->rmx_type = RMX_OFF;
3677 amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3678 amdgpu_encoder->is_ext_encoder = false;
3679 amdgpu_encoder->caps = caps;
3681 switch (amdgpu_encoder->encoder_id) {
3682 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3683 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3684 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3685 DRM_MODE_ENCODER_DAC, NULL);
3686 drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
3688 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3689 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3690 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3691 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3692 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3693 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3694 amdgpu_encoder->rmx_type = RMX_FULL;
3695 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3696 DRM_MODE_ENCODER_LVDS, NULL);
3697 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3698 } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3699 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3700 DRM_MODE_ENCODER_DAC, NULL);
3701 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3703 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3704 DRM_MODE_ENCODER_TMDS, NULL);
3705 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3707 drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
3709 case ENCODER_OBJECT_ID_SI170B:
3710 case ENCODER_OBJECT_ID_CH7303:
3711 case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3712 case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3713 case ENCODER_OBJECT_ID_TITFP513:
3714 case ENCODER_OBJECT_ID_VT1623:
3715 case ENCODER_OBJECT_ID_HDMI_SI1930:
3716 case ENCODER_OBJECT_ID_TRAVIS:
3717 case ENCODER_OBJECT_ID_NUTMEG:
3718 /* these are handled by the primary encoders */
3719 amdgpu_encoder->is_ext_encoder = true;
3720 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3721 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3722 DRM_MODE_ENCODER_LVDS, NULL);
3723 else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3724 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3725 DRM_MODE_ENCODER_DAC, NULL);
3727 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3728 DRM_MODE_ENCODER_TMDS, NULL);
3729 drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
3734 static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
3735 .bandwidth_update = &dce_v11_0_bandwidth_update,
3736 .vblank_get_counter = &dce_v11_0_vblank_get_counter,
3737 .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3738 .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3739 .hpd_sense = &dce_v11_0_hpd_sense,
3740 .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
3741 .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
3742 .page_flip = &dce_v11_0_page_flip,
3743 .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
3744 .add_encoder = &dce_v11_0_encoder_add,
3745 .add_connector = &amdgpu_connector_add,
3748 static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
3750 adev->mode_info.funcs = &dce_v11_0_display_funcs;
3753 static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
3754 .set = dce_v11_0_set_crtc_irq_state,
3755 .process = dce_v11_0_crtc_irq,
3758 static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
3759 .set = dce_v11_0_set_pageflip_irq_state,
3760 .process = dce_v11_0_pageflip_irq,
3763 static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
3764 .set = dce_v11_0_set_hpd_irq_state,
3765 .process = dce_v11_0_hpd_irq,
3768 static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
3770 if (adev->mode_info.num_crtc > 0)
3771 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VLINE1 + adev->mode_info.num_crtc;
3773 adev->crtc_irq.num_types = 0;
3774 adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
3776 adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
3777 adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
3779 adev->hpd_irq.num_types = adev->mode_info.num_hpd;
3780 adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;
3783 const struct amdgpu_ip_block_version dce_v11_0_ip_block =
3785 .type = AMD_IP_BLOCK_TYPE_DCE,
3789 .funcs = &dce_v11_0_ip_funcs,
3792 const struct amdgpu_ip_block_version dce_v11_2_ip_block =
3794 .type = AMD_IP_BLOCK_TYPE_DCE,
3798 .funcs = &dce_v11_0_ip_funcs,