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.
25 #include "amdgpu_pm.h"
26 #include "amdgpu_i2c.h"
29 #include "amdgpu_atombios.h"
30 #include "atombios_crtc.h"
31 #include "atombios_encoders.h"
32 #include "amdgpu_pll.h"
33 #include "amdgpu_connectors.h"
35 #include "dce/dce_11_0_d.h"
36 #include "dce/dce_11_0_sh_mask.h"
37 #include "dce/dce_11_0_enum.h"
38 #include "oss/oss_3_0_d.h"
39 #include "oss/oss_3_0_sh_mask.h"
40 #include "gmc/gmc_8_1_d.h"
41 #include "gmc/gmc_8_1_sh_mask.h"
43 static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev);
44 static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev);
46 static const u32 crtc_offsets[] =
48 CRTC0_REGISTER_OFFSET,
49 CRTC1_REGISTER_OFFSET,
50 CRTC2_REGISTER_OFFSET,
51 CRTC3_REGISTER_OFFSET,
52 CRTC4_REGISTER_OFFSET,
53 CRTC5_REGISTER_OFFSET,
57 static const u32 hpd_offsets[] =
67 static const uint32_t dig_offsets[] = {
85 } interrupt_status_offsets[] = { {
86 .reg = mmDISP_INTERRUPT_STATUS,
87 .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
88 .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
89 .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
91 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
92 .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
93 .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
94 .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
96 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
97 .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
98 .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
99 .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
101 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
102 .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
103 .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
104 .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
106 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
107 .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
108 .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
109 .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
111 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
112 .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
113 .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
114 .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
117 static const u32 cz_golden_settings_a11[] =
119 mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
120 mmFBC_MISC, 0x1f311fff, 0x14300000,
123 static const u32 cz_mgcg_cgcg_init[] =
125 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
126 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
129 static const u32 stoney_golden_settings_a11[] =
131 mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
132 mmFBC_MISC, 0x1f311fff, 0x14302000,
136 static void dce_v11_0_init_golden_registers(struct amdgpu_device *adev)
138 switch (adev->asic_type) {
140 amdgpu_program_register_sequence(adev,
142 (const u32)ARRAY_SIZE(cz_mgcg_cgcg_init));
143 amdgpu_program_register_sequence(adev,
144 cz_golden_settings_a11,
145 (const u32)ARRAY_SIZE(cz_golden_settings_a11));
148 amdgpu_program_register_sequence(adev,
149 stoney_golden_settings_a11,
150 (const u32)ARRAY_SIZE(stoney_golden_settings_a11));
157 static u32 dce_v11_0_audio_endpt_rreg(struct amdgpu_device *adev,
158 u32 block_offset, u32 reg)
163 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
164 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
165 r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
166 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
171 static void dce_v11_0_audio_endpt_wreg(struct amdgpu_device *adev,
172 u32 block_offset, u32 reg, u32 v)
176 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
177 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
178 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
179 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
182 static bool dce_v11_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
184 if (RREG32(mmCRTC_STATUS + crtc_offsets[crtc]) &
185 CRTC_V_BLANK_START_END__CRTC_V_BLANK_START_MASK)
191 static bool dce_v11_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
195 pos1 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
196 pos2 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
205 * dce_v11_0_vblank_wait - vblank wait asic callback.
207 * @adev: amdgpu_device pointer
208 * @crtc: crtc to wait for vblank on
210 * Wait for vblank on the requested crtc (evergreen+).
212 static void dce_v11_0_vblank_wait(struct amdgpu_device *adev, int crtc)
216 if (crtc >= adev->mode_info.num_crtc)
219 if (!(RREG32(mmCRTC_CONTROL + crtc_offsets[crtc]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK))
222 /* depending on when we hit vblank, we may be close to active; if so,
223 * wait for another frame.
225 while (dce_v11_0_is_in_vblank(adev, crtc)) {
226 if (i++ % 100 == 0) {
227 if (!dce_v11_0_is_counter_moving(adev, crtc))
232 while (!dce_v11_0_is_in_vblank(adev, crtc)) {
233 if (i++ % 100 == 0) {
234 if (!dce_v11_0_is_counter_moving(adev, crtc))
240 static u32 dce_v11_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
242 if (crtc >= adev->mode_info.num_crtc)
245 return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
248 static void dce_v11_0_pageflip_interrupt_init(struct amdgpu_device *adev)
252 /* Enable pflip interrupts */
253 for (i = 0; i < adev->mode_info.num_crtc; i++)
254 amdgpu_irq_get(adev, &adev->pageflip_irq, i);
257 static void dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
261 /* Disable pflip interrupts */
262 for (i = 0; i < adev->mode_info.num_crtc; i++)
263 amdgpu_irq_put(adev, &adev->pageflip_irq, i);
267 * dce_v11_0_page_flip - pageflip callback.
269 * @adev: amdgpu_device pointer
270 * @crtc_id: crtc to cleanup pageflip on
271 * @crtc_base: new address of the crtc (GPU MC address)
273 * Triggers the actual pageflip by updating the primary
274 * surface base address.
276 static void dce_v11_0_page_flip(struct amdgpu_device *adev,
277 int crtc_id, u64 crtc_base)
279 struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
281 /* update the scanout addresses */
282 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
283 upper_32_bits(crtc_base));
284 /* writing to the low address triggers the update */
285 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
286 lower_32_bits(crtc_base));
288 RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
291 static int dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
292 u32 *vbl, u32 *position)
294 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
297 *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
298 *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
304 * dce_v11_0_hpd_sense - hpd sense callback.
306 * @adev: amdgpu_device pointer
307 * @hpd: hpd (hotplug detect) pin
309 * Checks if a digital monitor is connected (evergreen+).
310 * Returns true if connected, false if not connected.
312 static bool dce_v11_0_hpd_sense(struct amdgpu_device *adev,
313 enum amdgpu_hpd_id hpd)
316 bool connected = false;
341 if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[idx]) &
342 DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
349 * dce_v11_0_hpd_set_polarity - hpd set polarity callback.
351 * @adev: amdgpu_device pointer
352 * @hpd: hpd (hotplug detect) pin
354 * Set the polarity of the hpd pin (evergreen+).
356 static void dce_v11_0_hpd_set_polarity(struct amdgpu_device *adev,
357 enum amdgpu_hpd_id hpd)
360 bool connected = dce_v11_0_hpd_sense(adev, hpd);
386 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx]);
388 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
390 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
391 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx], tmp);
395 * dce_v11_0_hpd_init - hpd setup callback.
397 * @adev: amdgpu_device pointer
399 * Setup the hpd pins used by the card (evergreen+).
400 * Enable the pin, set the polarity, and enable the hpd interrupts.
402 static void dce_v11_0_hpd_init(struct amdgpu_device *adev)
404 struct drm_device *dev = adev->ddev;
405 struct drm_connector *connector;
409 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
410 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
412 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
413 connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
414 /* don't try to enable hpd on eDP or LVDS avoid breaking the
415 * aux dp channel on imac and help (but not completely fix)
416 * https://bugzilla.redhat.com/show_bug.cgi?id=726143
417 * also avoid interrupt storms during dpms.
422 switch (amdgpu_connector->hpd.hpd) {
445 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
446 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
447 WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
449 tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx]);
450 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
451 DC_HPD_CONNECT_INT_DELAY,
452 AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
453 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
454 DC_HPD_DISCONNECT_INT_DELAY,
455 AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
456 WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx], tmp);
458 dce_v11_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
459 amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
464 * dce_v11_0_hpd_fini - hpd tear down callback.
466 * @adev: amdgpu_device pointer
468 * Tear down the hpd pins used by the card (evergreen+).
469 * Disable the hpd interrupts.
471 static void dce_v11_0_hpd_fini(struct amdgpu_device *adev)
473 struct drm_device *dev = adev->ddev;
474 struct drm_connector *connector;
478 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
479 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
481 switch (amdgpu_connector->hpd.hpd) {
504 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
505 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
506 WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
508 amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
512 static u32 dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
514 return mmDC_GPIO_HPD_A;
517 static bool dce_v11_0_is_display_hung(struct amdgpu_device *adev)
523 for (i = 0; i < adev->mode_info.num_crtc; i++) {
524 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
525 if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
526 crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
527 crtc_hung |= (1 << i);
531 for (j = 0; j < 10; j++) {
532 for (i = 0; i < adev->mode_info.num_crtc; i++) {
533 if (crtc_hung & (1 << i)) {
534 tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
535 if (tmp != crtc_status[i])
536 crtc_hung &= ~(1 << i);
547 static void dce_v11_0_stop_mc_access(struct amdgpu_device *adev,
548 struct amdgpu_mode_mc_save *save)
550 u32 crtc_enabled, tmp;
553 save->vga_render_control = RREG32(mmVGA_RENDER_CONTROL);
554 save->vga_hdp_control = RREG32(mmVGA_HDP_CONTROL);
556 /* disable VGA render */
557 tmp = RREG32(mmVGA_RENDER_CONTROL);
558 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
559 WREG32(mmVGA_RENDER_CONTROL, tmp);
561 /* blank the display controllers */
562 for (i = 0; i < adev->mode_info.num_crtc; i++) {
563 crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
564 CRTC_CONTROL, CRTC_MASTER_EN);
570 save->crtc_enabled[i] = true;
571 tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
572 if (REG_GET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN) == 0) {
573 amdgpu_display_vblank_wait(adev, i);
574 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
575 tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 1);
576 WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
577 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
579 /* wait for the next frame */
580 frame_count = amdgpu_display_vblank_get_counter(adev, i);
581 for (j = 0; j < adev->usec_timeout; j++) {
582 if (amdgpu_display_vblank_get_counter(adev, i) != frame_count)
586 tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
587 if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK) == 0) {
588 tmp = REG_SET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK, 1);
589 WREG32(mmGRPH_UPDATE + crtc_offsets[i], tmp);
591 tmp = RREG32(mmCRTC_MASTER_UPDATE_LOCK + crtc_offsets[i]);
592 if (REG_GET_FIELD(tmp, CRTC_MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK) == 0) {
593 tmp = REG_SET_FIELD(tmp, CRTC_MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK, 1);
594 WREG32(mmCRTC_MASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
597 /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
598 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
599 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
600 tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
601 WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
602 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
603 save->crtc_enabled[i] = false;
607 save->crtc_enabled[i] = false;
612 static void dce_v11_0_resume_mc_access(struct amdgpu_device *adev,
613 struct amdgpu_mode_mc_save *save)
615 u32 tmp, frame_count;
618 /* update crtc base addresses */
619 for (i = 0; i < adev->mode_info.num_crtc; i++) {
620 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
621 upper_32_bits(adev->mc.vram_start));
622 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
623 upper_32_bits(adev->mc.vram_start));
624 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
625 (u32)adev->mc.vram_start);
626 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + crtc_offsets[i],
627 (u32)adev->mc.vram_start);
629 if (save->crtc_enabled[i]) {
630 tmp = RREG32(mmCRTC_MASTER_UPDATE_MODE + crtc_offsets[i]);
631 if (REG_GET_FIELD(tmp, CRTC_MASTER_UPDATE_MODE, MASTER_UPDATE_MODE) != 3) {
632 tmp = REG_SET_FIELD(tmp, CRTC_MASTER_UPDATE_MODE, MASTER_UPDATE_MODE, 3);
633 WREG32(mmCRTC_MASTER_UPDATE_MODE + crtc_offsets[i], tmp);
635 tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
636 if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK)) {
637 tmp = REG_SET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK, 0);
638 WREG32(mmGRPH_UPDATE + crtc_offsets[i], tmp);
640 tmp = RREG32(mmCRTC_MASTER_UPDATE_LOCK + crtc_offsets[i]);
641 if (REG_GET_FIELD(tmp, CRTC_MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK)) {
642 tmp = REG_SET_FIELD(tmp, CRTC_MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK, 0);
643 WREG32(mmCRTC_MASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
645 for (j = 0; j < adev->usec_timeout; j++) {
646 tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
647 if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_SURFACE_UPDATE_PENDING) == 0)
651 tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
652 tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 0);
653 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
654 WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
655 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
656 /* wait for the next frame */
657 frame_count = amdgpu_display_vblank_get_counter(adev, i);
658 for (j = 0; j < adev->usec_timeout; j++) {
659 if (amdgpu_display_vblank_get_counter(adev, i) != frame_count)
666 WREG32(mmVGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
667 WREG32(mmVGA_MEMORY_BASE_ADDRESS, lower_32_bits(adev->mc.vram_start));
669 /* Unlock vga access */
670 WREG32(mmVGA_HDP_CONTROL, save->vga_hdp_control);
672 WREG32(mmVGA_RENDER_CONTROL, save->vga_render_control);
675 static void dce_v11_0_set_vga_render_state(struct amdgpu_device *adev,
680 /* Lockout access through VGA aperture*/
681 tmp = RREG32(mmVGA_HDP_CONTROL);
683 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
685 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
686 WREG32(mmVGA_HDP_CONTROL, tmp);
688 /* disable VGA render */
689 tmp = RREG32(mmVGA_RENDER_CONTROL);
691 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
693 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
694 WREG32(mmVGA_RENDER_CONTROL, tmp);
697 static void dce_v11_0_program_fmt(struct drm_encoder *encoder)
699 struct drm_device *dev = encoder->dev;
700 struct amdgpu_device *adev = dev->dev_private;
701 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
702 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
703 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
706 enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
709 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
710 bpc = amdgpu_connector_get_monitor_bpc(connector);
711 dither = amdgpu_connector->dither;
714 /* LVDS/eDP FMT is set up by atom */
715 if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
718 /* not needed for analog */
719 if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
720 (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
728 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
729 /* XXX sort out optimal dither settings */
730 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
731 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
732 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
733 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
735 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
736 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
740 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
741 /* XXX sort out optimal dither settings */
742 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
743 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
744 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
745 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
746 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
748 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
749 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
753 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
754 /* XXX sort out optimal dither settings */
755 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
756 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
757 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
758 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
759 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
761 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
762 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
770 WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
774 /* display watermark setup */
776 * dce_v11_0_line_buffer_adjust - Set up the line buffer
778 * @adev: amdgpu_device pointer
779 * @amdgpu_crtc: the selected display controller
780 * @mode: the current display mode on the selected display
783 * Setup up the line buffer allocation for
784 * the selected display controller (CIK).
785 * Returns the line buffer size in pixels.
787 static u32 dce_v11_0_line_buffer_adjust(struct amdgpu_device *adev,
788 struct amdgpu_crtc *amdgpu_crtc,
789 struct drm_display_mode *mode)
791 u32 tmp, buffer_alloc, i, mem_cfg;
792 u32 pipe_offset = amdgpu_crtc->crtc_id;
795 * There are 6 line buffers, one for each display controllers.
796 * There are 3 partitions per LB. Select the number of partitions
797 * to enable based on the display width. For display widths larger
798 * than 4096, you need use to use 2 display controllers and combine
799 * them using the stereo blender.
801 if (amdgpu_crtc->base.enabled && mode) {
802 if (mode->crtc_hdisplay < 1920) {
805 } else if (mode->crtc_hdisplay < 2560) {
808 } else if (mode->crtc_hdisplay < 4096) {
810 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
812 DRM_DEBUG_KMS("Mode too big for LB!\n");
814 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
821 tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
822 tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
823 WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
825 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
826 tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
827 WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
829 for (i = 0; i < adev->usec_timeout; i++) {
830 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
831 if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
836 if (amdgpu_crtc->base.enabled && mode) {
848 /* controller not enabled, so no lb used */
853 * cik_get_number_of_dram_channels - get the number of dram channels
855 * @adev: amdgpu_device pointer
857 * Look up the number of video ram channels (CIK).
858 * Used for display watermark bandwidth calculations
859 * Returns the number of dram channels
861 static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
863 u32 tmp = RREG32(mmMC_SHARED_CHMAP);
865 switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
888 struct dce10_wm_params {
889 u32 dram_channels; /* number of dram channels */
890 u32 yclk; /* bandwidth per dram data pin in kHz */
891 u32 sclk; /* engine clock in kHz */
892 u32 disp_clk; /* display clock in kHz */
893 u32 src_width; /* viewport width */
894 u32 active_time; /* active display time in ns */
895 u32 blank_time; /* blank time in ns */
896 bool interlaced; /* mode is interlaced */
897 fixed20_12 vsc; /* vertical scale ratio */
898 u32 num_heads; /* number of active crtcs */
899 u32 bytes_per_pixel; /* bytes per pixel display + overlay */
900 u32 lb_size; /* line buffer allocated to pipe */
901 u32 vtaps; /* vertical scaler taps */
905 * dce_v11_0_dram_bandwidth - get the dram bandwidth
907 * @wm: watermark calculation data
909 * Calculate the raw dram bandwidth (CIK).
910 * Used for display watermark bandwidth calculations
911 * Returns the dram bandwidth in MBytes/s
913 static u32 dce_v11_0_dram_bandwidth(struct dce10_wm_params *wm)
915 /* Calculate raw DRAM Bandwidth */
916 fixed20_12 dram_efficiency; /* 0.7 */
917 fixed20_12 yclk, dram_channels, bandwidth;
920 a.full = dfixed_const(1000);
921 yclk.full = dfixed_const(wm->yclk);
922 yclk.full = dfixed_div(yclk, a);
923 dram_channels.full = dfixed_const(wm->dram_channels * 4);
924 a.full = dfixed_const(10);
925 dram_efficiency.full = dfixed_const(7);
926 dram_efficiency.full = dfixed_div(dram_efficiency, a);
927 bandwidth.full = dfixed_mul(dram_channels, yclk);
928 bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
930 return dfixed_trunc(bandwidth);
934 * dce_v11_0_dram_bandwidth_for_display - get the dram bandwidth for display
936 * @wm: watermark calculation data
938 * Calculate the dram bandwidth used for display (CIK).
939 * Used for display watermark bandwidth calculations
940 * Returns the dram bandwidth for display in MBytes/s
942 static u32 dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
944 /* Calculate DRAM Bandwidth and the part allocated to display. */
945 fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
946 fixed20_12 yclk, dram_channels, bandwidth;
949 a.full = dfixed_const(1000);
950 yclk.full = dfixed_const(wm->yclk);
951 yclk.full = dfixed_div(yclk, a);
952 dram_channels.full = dfixed_const(wm->dram_channels * 4);
953 a.full = dfixed_const(10);
954 disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
955 disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
956 bandwidth.full = dfixed_mul(dram_channels, yclk);
957 bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
959 return dfixed_trunc(bandwidth);
963 * dce_v11_0_data_return_bandwidth - get the data return bandwidth
965 * @wm: watermark calculation data
967 * Calculate the data return bandwidth used for display (CIK).
968 * Used for display watermark bandwidth calculations
969 * Returns the data return bandwidth in MBytes/s
971 static u32 dce_v11_0_data_return_bandwidth(struct dce10_wm_params *wm)
973 /* Calculate the display Data return Bandwidth */
974 fixed20_12 return_efficiency; /* 0.8 */
975 fixed20_12 sclk, bandwidth;
978 a.full = dfixed_const(1000);
979 sclk.full = dfixed_const(wm->sclk);
980 sclk.full = dfixed_div(sclk, a);
981 a.full = dfixed_const(10);
982 return_efficiency.full = dfixed_const(8);
983 return_efficiency.full = dfixed_div(return_efficiency, a);
984 a.full = dfixed_const(32);
985 bandwidth.full = dfixed_mul(a, sclk);
986 bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
988 return dfixed_trunc(bandwidth);
992 * dce_v11_0_dmif_request_bandwidth - get the dmif bandwidth
994 * @wm: watermark calculation data
996 * Calculate the dmif bandwidth used for display (CIK).
997 * Used for display watermark bandwidth calculations
998 * Returns the dmif bandwidth in MBytes/s
1000 static u32 dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
1002 /* Calculate the DMIF Request Bandwidth */
1003 fixed20_12 disp_clk_request_efficiency; /* 0.8 */
1004 fixed20_12 disp_clk, bandwidth;
1007 a.full = dfixed_const(1000);
1008 disp_clk.full = dfixed_const(wm->disp_clk);
1009 disp_clk.full = dfixed_div(disp_clk, a);
1010 a.full = dfixed_const(32);
1011 b.full = dfixed_mul(a, disp_clk);
1013 a.full = dfixed_const(10);
1014 disp_clk_request_efficiency.full = dfixed_const(8);
1015 disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
1017 bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
1019 return dfixed_trunc(bandwidth);
1023 * dce_v11_0_available_bandwidth - get the min available bandwidth
1025 * @wm: watermark calculation data
1027 * Calculate the min available bandwidth used for display (CIK).
1028 * Used for display watermark bandwidth calculations
1029 * Returns the min available bandwidth in MBytes/s
1031 static u32 dce_v11_0_available_bandwidth(struct dce10_wm_params *wm)
1033 /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
1034 u32 dram_bandwidth = dce_v11_0_dram_bandwidth(wm);
1035 u32 data_return_bandwidth = dce_v11_0_data_return_bandwidth(wm);
1036 u32 dmif_req_bandwidth = dce_v11_0_dmif_request_bandwidth(wm);
1038 return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
1042 * dce_v11_0_average_bandwidth - get the average available bandwidth
1044 * @wm: watermark calculation data
1046 * Calculate the average available bandwidth used for display (CIK).
1047 * Used for display watermark bandwidth calculations
1048 * Returns the average available bandwidth in MBytes/s
1050 static u32 dce_v11_0_average_bandwidth(struct dce10_wm_params *wm)
1052 /* Calculate the display mode Average Bandwidth
1053 * DisplayMode should contain the source and destination dimensions,
1057 fixed20_12 line_time;
1058 fixed20_12 src_width;
1059 fixed20_12 bandwidth;
1062 a.full = dfixed_const(1000);
1063 line_time.full = dfixed_const(wm->active_time + wm->blank_time);
1064 line_time.full = dfixed_div(line_time, a);
1065 bpp.full = dfixed_const(wm->bytes_per_pixel);
1066 src_width.full = dfixed_const(wm->src_width);
1067 bandwidth.full = dfixed_mul(src_width, bpp);
1068 bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
1069 bandwidth.full = dfixed_div(bandwidth, line_time);
1071 return dfixed_trunc(bandwidth);
1075 * dce_v11_0_latency_watermark - get the latency watermark
1077 * @wm: watermark calculation data
1079 * Calculate the latency watermark (CIK).
1080 * Used for display watermark bandwidth calculations
1081 * Returns the latency watermark in ns
1083 static u32 dce_v11_0_latency_watermark(struct dce10_wm_params *wm)
1085 /* First calculate the latency in ns */
1086 u32 mc_latency = 2000; /* 2000 ns. */
1087 u32 available_bandwidth = dce_v11_0_available_bandwidth(wm);
1088 u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
1089 u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
1090 u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
1091 u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
1092 (wm->num_heads * cursor_line_pair_return_time);
1093 u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
1094 u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
1095 u32 tmp, dmif_size = 12288;
1098 if (wm->num_heads == 0)
1101 a.full = dfixed_const(2);
1102 b.full = dfixed_const(1);
1103 if ((wm->vsc.full > a.full) ||
1104 ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
1106 ((wm->vsc.full >= a.full) && wm->interlaced))
1107 max_src_lines_per_dst_line = 4;
1109 max_src_lines_per_dst_line = 2;
1111 a.full = dfixed_const(available_bandwidth);
1112 b.full = dfixed_const(wm->num_heads);
1113 a.full = dfixed_div(a, b);
1115 b.full = dfixed_const(mc_latency + 512);
1116 c.full = dfixed_const(wm->disp_clk);
1117 b.full = dfixed_div(b, c);
1119 c.full = dfixed_const(dmif_size);
1120 b.full = dfixed_div(c, b);
1122 tmp = min(dfixed_trunc(a), dfixed_trunc(b));
1124 b.full = dfixed_const(1000);
1125 c.full = dfixed_const(wm->disp_clk);
1126 b.full = dfixed_div(c, b);
1127 c.full = dfixed_const(wm->bytes_per_pixel);
1128 b.full = dfixed_mul(b, c);
1130 lb_fill_bw = min(tmp, dfixed_trunc(b));
1132 a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
1133 b.full = dfixed_const(1000);
1134 c.full = dfixed_const(lb_fill_bw);
1135 b.full = dfixed_div(c, b);
1136 a.full = dfixed_div(a, b);
1137 line_fill_time = dfixed_trunc(a);
1139 if (line_fill_time < wm->active_time)
1142 return latency + (line_fill_time - wm->active_time);
1147 * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
1148 * average and available dram bandwidth
1150 * @wm: watermark calculation data
1152 * Check if the display average bandwidth fits in the display
1153 * dram bandwidth (CIK).
1154 * Used for display watermark bandwidth calculations
1155 * Returns true if the display fits, false if not.
1157 static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
1159 if (dce_v11_0_average_bandwidth(wm) <=
1160 (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
1167 * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
1168 * average and available bandwidth
1170 * @wm: watermark calculation data
1172 * Check if the display average bandwidth fits in the display
1173 * available bandwidth (CIK).
1174 * Used for display watermark bandwidth calculations
1175 * Returns true if the display fits, false if not.
1177 static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
1179 if (dce_v11_0_average_bandwidth(wm) <=
1180 (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
1187 * dce_v11_0_check_latency_hiding - check latency hiding
1189 * @wm: watermark calculation data
1191 * Check latency hiding (CIK).
1192 * Used for display watermark bandwidth calculations
1193 * Returns true if the display fits, false if not.
1195 static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
1197 u32 lb_partitions = wm->lb_size / wm->src_width;
1198 u32 line_time = wm->active_time + wm->blank_time;
1199 u32 latency_tolerant_lines;
1203 a.full = dfixed_const(1);
1204 if (wm->vsc.full > a.full)
1205 latency_tolerant_lines = 1;
1207 if (lb_partitions <= (wm->vtaps + 1))
1208 latency_tolerant_lines = 1;
1210 latency_tolerant_lines = 2;
1213 latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
1215 if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
1222 * dce_v11_0_program_watermarks - program display watermarks
1224 * @adev: amdgpu_device pointer
1225 * @amdgpu_crtc: the selected display controller
1226 * @lb_size: line buffer size
1227 * @num_heads: number of display controllers in use
1229 * Calculate and program the display watermarks for the
1230 * selected display controller (CIK).
1232 static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
1233 struct amdgpu_crtc *amdgpu_crtc,
1234 u32 lb_size, u32 num_heads)
1236 struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
1237 struct dce10_wm_params wm_low, wm_high;
1240 u32 latency_watermark_a = 0, latency_watermark_b = 0;
1243 if (amdgpu_crtc->base.enabled && num_heads && mode) {
1244 pixel_period = 1000000 / (u32)mode->clock;
1245 line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
1247 /* watermark for high clocks */
1248 if (adev->pm.dpm_enabled) {
1250 amdgpu_dpm_get_mclk(adev, false) * 10;
1252 amdgpu_dpm_get_sclk(adev, false) * 10;
1254 wm_high.yclk = adev->pm.current_mclk * 10;
1255 wm_high.sclk = adev->pm.current_sclk * 10;
1258 wm_high.disp_clk = mode->clock;
1259 wm_high.src_width = mode->crtc_hdisplay;
1260 wm_high.active_time = mode->crtc_hdisplay * pixel_period;
1261 wm_high.blank_time = line_time - wm_high.active_time;
1262 wm_high.interlaced = false;
1263 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1264 wm_high.interlaced = true;
1265 wm_high.vsc = amdgpu_crtc->vsc;
1267 if (amdgpu_crtc->rmx_type != RMX_OFF)
1269 wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1270 wm_high.lb_size = lb_size;
1271 wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1272 wm_high.num_heads = num_heads;
1274 /* set for high clocks */
1275 latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
1277 /* possibly force display priority to high */
1278 /* should really do this at mode validation time... */
1279 if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1280 !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1281 !dce_v11_0_check_latency_hiding(&wm_high) ||
1282 (adev->mode_info.disp_priority == 2)) {
1283 DRM_DEBUG_KMS("force priority to high\n");
1286 /* watermark for low clocks */
1287 if (adev->pm.dpm_enabled) {
1289 amdgpu_dpm_get_mclk(adev, true) * 10;
1291 amdgpu_dpm_get_sclk(adev, true) * 10;
1293 wm_low.yclk = adev->pm.current_mclk * 10;
1294 wm_low.sclk = adev->pm.current_sclk * 10;
1297 wm_low.disp_clk = mode->clock;
1298 wm_low.src_width = mode->crtc_hdisplay;
1299 wm_low.active_time = mode->crtc_hdisplay * pixel_period;
1300 wm_low.blank_time = line_time - wm_low.active_time;
1301 wm_low.interlaced = false;
1302 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1303 wm_low.interlaced = true;
1304 wm_low.vsc = amdgpu_crtc->vsc;
1306 if (amdgpu_crtc->rmx_type != RMX_OFF)
1308 wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1309 wm_low.lb_size = lb_size;
1310 wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1311 wm_low.num_heads = num_heads;
1313 /* set for low clocks */
1314 latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
1316 /* possibly force display priority to high */
1317 /* should really do this at mode validation time... */
1318 if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1319 !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1320 !dce_v11_0_check_latency_hiding(&wm_low) ||
1321 (adev->mode_info.disp_priority == 2)) {
1322 DRM_DEBUG_KMS("force priority to high\n");
1327 wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1328 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
1329 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1330 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1331 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
1332 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1333 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1335 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
1336 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1337 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1338 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
1339 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1340 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1341 /* restore original selection */
1342 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1344 /* save values for DPM */
1345 amdgpu_crtc->line_time = line_time;
1346 amdgpu_crtc->wm_high = latency_watermark_a;
1347 amdgpu_crtc->wm_low = latency_watermark_b;
1351 * dce_v11_0_bandwidth_update - program display watermarks
1353 * @adev: amdgpu_device pointer
1355 * Calculate and program the display watermarks and line
1356 * buffer allocation (CIK).
1358 static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
1360 struct drm_display_mode *mode = NULL;
1361 u32 num_heads = 0, lb_size;
1364 amdgpu_update_display_priority(adev);
1366 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1367 if (adev->mode_info.crtcs[i]->base.enabled)
1370 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1371 mode = &adev->mode_info.crtcs[i]->base.mode;
1372 lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1373 dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1374 lb_size, num_heads);
1378 static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
1383 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1384 offset = adev->mode_info.audio.pin[i].offset;
1385 tmp = RREG32_AUDIO_ENDPT(offset,
1386 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1388 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1389 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1390 adev->mode_info.audio.pin[i].connected = false;
1392 adev->mode_info.audio.pin[i].connected = true;
1396 static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
1400 dce_v11_0_audio_get_connected_pins(adev);
1402 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1403 if (adev->mode_info.audio.pin[i].connected)
1404 return &adev->mode_info.audio.pin[i];
1406 DRM_ERROR("No connected audio pins found!\n");
1410 static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1412 struct amdgpu_device *adev = encoder->dev->dev_private;
1413 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1414 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1417 if (!dig || !dig->afmt || !dig->afmt->pin)
1420 tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
1421 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
1422 WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
1425 static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
1426 struct drm_display_mode *mode)
1428 struct amdgpu_device *adev = encoder->dev->dev_private;
1429 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1430 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1431 struct drm_connector *connector;
1432 struct amdgpu_connector *amdgpu_connector = NULL;
1436 if (!dig || !dig->afmt || !dig->afmt->pin)
1439 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1440 if (connector->encoder == encoder) {
1441 amdgpu_connector = to_amdgpu_connector(connector);
1446 if (!amdgpu_connector) {
1447 DRM_ERROR("Couldn't find encoder's connector\n");
1451 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1453 if (connector->latency_present[interlace]) {
1454 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1455 VIDEO_LIPSYNC, connector->video_latency[interlace]);
1456 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1457 AUDIO_LIPSYNC, connector->audio_latency[interlace]);
1459 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1461 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1464 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1465 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1468 static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1470 struct amdgpu_device *adev = encoder->dev->dev_private;
1471 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1472 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1473 struct drm_connector *connector;
1474 struct amdgpu_connector *amdgpu_connector = NULL;
1479 if (!dig || !dig->afmt || !dig->afmt->pin)
1482 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1483 if (connector->encoder == encoder) {
1484 amdgpu_connector = to_amdgpu_connector(connector);
1489 if (!amdgpu_connector) {
1490 DRM_ERROR("Couldn't find encoder's connector\n");
1494 sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
1495 if (sad_count < 0) {
1496 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1500 /* program the speaker allocation */
1501 tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1502 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1503 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1506 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1507 HDMI_CONNECTION, 1);
1509 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1510 SPEAKER_ALLOCATION, sadb[0]);
1512 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1513 SPEAKER_ALLOCATION, 5); /* stereo */
1514 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1515 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1520 static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
1522 struct amdgpu_device *adev = encoder->dev->dev_private;
1523 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1524 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1525 struct drm_connector *connector;
1526 struct amdgpu_connector *amdgpu_connector = NULL;
1527 struct cea_sad *sads;
1530 static const u16 eld_reg_to_type[][2] = {
1531 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1532 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1533 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1534 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1535 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1536 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1537 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1538 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1539 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1540 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1541 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1542 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1545 if (!dig || !dig->afmt || !dig->afmt->pin)
1548 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1549 if (connector->encoder == encoder) {
1550 amdgpu_connector = to_amdgpu_connector(connector);
1555 if (!amdgpu_connector) {
1556 DRM_ERROR("Couldn't find encoder's connector\n");
1560 sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
1561 if (sad_count <= 0) {
1562 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1567 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1569 u8 stereo_freqs = 0;
1570 int max_channels = -1;
1573 for (j = 0; j < sad_count; j++) {
1574 struct cea_sad *sad = &sads[j];
1576 if (sad->format == eld_reg_to_type[i][1]) {
1577 if (sad->channels > max_channels) {
1578 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1579 MAX_CHANNELS, sad->channels);
1580 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1581 DESCRIPTOR_BYTE_2, sad->byte2);
1582 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1583 SUPPORTED_FREQUENCIES, sad->freq);
1584 max_channels = sad->channels;
1587 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1588 stereo_freqs |= sad->freq;
1594 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1595 SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
1596 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
1602 static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
1603 struct amdgpu_audio_pin *pin,
1609 WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1610 enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1613 static const u32 pin_offsets[] =
1615 AUD0_REGISTER_OFFSET,
1616 AUD1_REGISTER_OFFSET,
1617 AUD2_REGISTER_OFFSET,
1618 AUD3_REGISTER_OFFSET,
1619 AUD4_REGISTER_OFFSET,
1620 AUD5_REGISTER_OFFSET,
1621 AUD6_REGISTER_OFFSET,
1624 static int dce_v11_0_audio_init(struct amdgpu_device *adev)
1631 adev->mode_info.audio.enabled = true;
1633 adev->mode_info.audio.num_pins = 7;
1635 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1636 adev->mode_info.audio.pin[i].channels = -1;
1637 adev->mode_info.audio.pin[i].rate = -1;
1638 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1639 adev->mode_info.audio.pin[i].status_bits = 0;
1640 adev->mode_info.audio.pin[i].category_code = 0;
1641 adev->mode_info.audio.pin[i].connected = false;
1642 adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1643 adev->mode_info.audio.pin[i].id = i;
1644 /* disable audio. it will be set up later */
1645 /* XXX remove once we switch to ip funcs */
1646 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1652 static void dce_v11_0_audio_fini(struct amdgpu_device *adev)
1656 if (!adev->mode_info.audio.enabled)
1659 for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1660 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1662 adev->mode_info.audio.enabled = false;
1666 * update the N and CTS parameters for a given pixel clock rate
1668 static void dce_v11_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1670 struct drm_device *dev = encoder->dev;
1671 struct amdgpu_device *adev = dev->dev_private;
1672 struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1673 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1674 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1677 tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
1678 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
1679 WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
1680 tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
1681 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
1682 WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
1684 tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
1685 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
1686 WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
1687 tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
1688 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
1689 WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
1691 tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
1692 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
1693 WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
1694 tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
1695 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
1696 WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
1701 * build a HDMI Video Info Frame
1703 static void dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1704 void *buffer, size_t size)
1706 struct drm_device *dev = encoder->dev;
1707 struct amdgpu_device *adev = dev->dev_private;
1708 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1709 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1710 uint8_t *frame = buffer + 3;
1711 uint8_t *header = buffer;
1713 WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
1714 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1715 WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
1716 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1717 WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
1718 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1719 WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
1720 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1723 static void dce_v11_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1725 struct drm_device *dev = encoder->dev;
1726 struct amdgpu_device *adev = dev->dev_private;
1727 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1728 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1729 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1730 u32 dto_phase = 24 * 1000;
1731 u32 dto_modulo = clock;
1734 if (!dig || !dig->afmt)
1737 /* XXX two dtos; generally use dto0 for hdmi */
1738 /* Express [24MHz / target pixel clock] as an exact rational
1739 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
1740 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1742 tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
1743 tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
1744 amdgpu_crtc->crtc_id);
1745 WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
1746 WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1747 WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1751 * update the info frames with the data from the current display mode
1753 static void dce_v11_0_afmt_setmode(struct drm_encoder *encoder,
1754 struct drm_display_mode *mode)
1756 struct drm_device *dev = encoder->dev;
1757 struct amdgpu_device *adev = dev->dev_private;
1758 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1759 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1760 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1761 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1762 struct hdmi_avi_infoframe frame;
1767 if (!dig || !dig->afmt)
1770 /* Silent, r600_hdmi_enable will raise WARN for us */
1771 if (!dig->afmt->enabled)
1774 /* hdmi deep color mode general control packets setup, if bpc > 8 */
1775 if (encoder->crtc) {
1776 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1777 bpc = amdgpu_crtc->bpc;
1780 /* disable audio prior to setting up hw */
1781 dig->afmt->pin = dce_v11_0_audio_get_pin(adev);
1782 dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
1784 dce_v11_0_audio_set_dto(encoder, mode->clock);
1786 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1787 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
1788 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
1790 WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
1792 tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
1799 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
1800 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
1801 DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1802 connector->name, bpc);
1805 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1806 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
1807 DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1811 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1812 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
1813 DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1817 WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
1819 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1820 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
1821 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
1822 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
1823 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
1825 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1826 /* enable audio info frames (frames won't be set until audio is enabled) */
1827 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
1828 /* required for audio info values to be updated */
1829 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
1830 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1832 tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
1833 /* required for audio info values to be updated */
1834 tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
1835 WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1837 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1838 /* anything other than 0 */
1839 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
1840 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1842 WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
1844 tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1845 /* set the default audio delay */
1846 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
1847 /* should be suffient for all audio modes and small enough for all hblanks */
1848 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
1849 WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1851 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1852 /* allow 60958 channel status fields to be updated */
1853 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
1854 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1856 tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
1858 /* clear SW CTS value */
1859 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
1861 /* select SW CTS value */
1862 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
1863 /* allow hw to sent ACR packets when required */
1864 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
1865 WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
1867 dce_v11_0_afmt_update_ACR(encoder, mode->clock);
1869 tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
1870 tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
1871 WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
1873 tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
1874 tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
1875 WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
1877 tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
1878 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
1879 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
1880 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
1881 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
1882 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
1883 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
1884 WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
1886 dce_v11_0_audio_write_speaker_allocation(encoder);
1888 WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
1889 (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1891 dce_v11_0_afmt_audio_select_pin(encoder);
1892 dce_v11_0_audio_write_sad_regs(encoder);
1893 dce_v11_0_audio_write_latency_fields(encoder, mode);
1895 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1897 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1901 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1903 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1907 dce_v11_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1909 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1910 /* enable AVI info frames */
1911 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
1912 /* required for audio info values to be updated */
1913 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
1914 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1916 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1917 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
1918 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1920 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1921 /* send audio packets */
1922 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
1923 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1925 WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
1926 WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
1927 WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
1928 WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
1930 /* enable audio after to setting up hw */
1931 dce_v11_0_audio_enable(adev, dig->afmt->pin, true);
1934 static void dce_v11_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1936 struct drm_device *dev = encoder->dev;
1937 struct amdgpu_device *adev = dev->dev_private;
1938 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1939 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1941 if (!dig || !dig->afmt)
1944 /* Silent, r600_hdmi_enable will raise WARN for us */
1945 if (enable && dig->afmt->enabled)
1947 if (!enable && !dig->afmt->enabled)
1950 if (!enable && dig->afmt->pin) {
1951 dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
1952 dig->afmt->pin = NULL;
1955 dig->afmt->enabled = enable;
1957 DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1958 enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1961 static void dce_v11_0_afmt_init(struct amdgpu_device *adev)
1965 for (i = 0; i < adev->mode_info.num_dig; i++)
1966 adev->mode_info.afmt[i] = NULL;
1968 /* DCE11 has audio blocks tied to DIG encoders */
1969 for (i = 0; i < adev->mode_info.num_dig; i++) {
1970 adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1971 if (adev->mode_info.afmt[i]) {
1972 adev->mode_info.afmt[i]->offset = dig_offsets[i];
1973 adev->mode_info.afmt[i]->id = i;
1978 static void dce_v11_0_afmt_fini(struct amdgpu_device *adev)
1982 for (i = 0; i < adev->mode_info.num_dig; i++) {
1983 kfree(adev->mode_info.afmt[i]);
1984 adev->mode_info.afmt[i] = NULL;
1988 static const u32 vga_control_regs[6] =
1998 static void dce_v11_0_vga_enable(struct drm_crtc *crtc, bool enable)
2000 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2001 struct drm_device *dev = crtc->dev;
2002 struct amdgpu_device *adev = dev->dev_private;
2005 vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
2007 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
2009 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
2012 static void dce_v11_0_grph_enable(struct drm_crtc *crtc, bool enable)
2014 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2015 struct drm_device *dev = crtc->dev;
2016 struct amdgpu_device *adev = dev->dev_private;
2019 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
2021 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
2024 static int dce_v11_0_crtc_do_set_base(struct drm_crtc *crtc,
2025 struct drm_framebuffer *fb,
2026 int x, int y, int atomic)
2028 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2029 struct drm_device *dev = crtc->dev;
2030 struct amdgpu_device *adev = dev->dev_private;
2031 struct amdgpu_framebuffer *amdgpu_fb;
2032 struct drm_framebuffer *target_fb;
2033 struct drm_gem_object *obj;
2034 struct amdgpu_bo *rbo;
2035 uint64_t fb_location, tiling_flags;
2036 uint32_t fb_format, fb_pitch_pixels;
2037 u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
2039 u32 tmp, viewport_w, viewport_h;
2041 bool bypass_lut = false;
2044 if (!atomic && !crtc->primary->fb) {
2045 DRM_DEBUG_KMS("No FB bound\n");
2050 amdgpu_fb = to_amdgpu_framebuffer(fb);
2054 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
2055 target_fb = crtc->primary->fb;
2058 /* If atomic, assume fb object is pinned & idle & fenced and
2059 * just update base pointers
2061 obj = amdgpu_fb->obj;
2062 rbo = gem_to_amdgpu_bo(obj);
2063 r = amdgpu_bo_reserve(rbo, false);
2064 if (unlikely(r != 0))
2068 fb_location = amdgpu_bo_gpu_offset(rbo);
2070 r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
2071 if (unlikely(r != 0)) {
2072 amdgpu_bo_unreserve(rbo);
2077 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
2078 amdgpu_bo_unreserve(rbo);
2080 pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2082 switch (target_fb->pixel_format) {
2084 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
2085 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2087 case DRM_FORMAT_XRGB4444:
2088 case DRM_FORMAT_ARGB4444:
2089 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2090 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
2092 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2096 case DRM_FORMAT_XRGB1555:
2097 case DRM_FORMAT_ARGB1555:
2098 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2099 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2101 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2105 case DRM_FORMAT_BGRX5551:
2106 case DRM_FORMAT_BGRA5551:
2107 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2108 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
2110 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2114 case DRM_FORMAT_RGB565:
2115 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2116 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
2118 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2122 case DRM_FORMAT_XRGB8888:
2123 case DRM_FORMAT_ARGB8888:
2124 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2125 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2127 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2131 case DRM_FORMAT_XRGB2101010:
2132 case DRM_FORMAT_ARGB2101010:
2133 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2134 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
2136 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2139 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2142 case DRM_FORMAT_BGRX1010102:
2143 case DRM_FORMAT_BGRA1010102:
2144 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2145 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
2147 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2150 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2154 DRM_ERROR("Unsupported screen format %s\n",
2155 drm_get_format_name(target_fb->pixel_format));
2159 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
2160 unsigned bankw, bankh, mtaspect, tile_split, num_banks;
2162 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2163 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2164 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2165 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2166 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2168 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
2169 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2170 ARRAY_2D_TILED_THIN1);
2171 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
2173 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
2174 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
2175 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
2177 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
2178 ADDR_SURF_MICRO_TILING_DISPLAY);
2179 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
2180 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2181 ARRAY_1D_TILED_THIN1);
2184 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
2187 dce_v11_0_vga_enable(crtc, false);
2189 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2190 upper_32_bits(fb_location));
2191 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2192 upper_32_bits(fb_location));
2193 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2194 (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
2195 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2196 (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
2197 WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
2198 WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
2201 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
2202 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
2203 * retain the full precision throughout the pipeline.
2205 tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
2207 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
2209 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
2210 WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
2213 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
2215 WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
2216 WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
2217 WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
2218 WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
2219 WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
2220 WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
2222 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
2223 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
2225 dce_v11_0_grph_enable(crtc, true);
2227 WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
2232 WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
2234 viewport_w = crtc->mode.hdisplay;
2235 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
2236 WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
2237 (viewport_w << 16) | viewport_h);
2239 /* pageflip setup */
2240 /* make sure flip is at vb rather than hb */
2241 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
2242 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
2243 GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
2244 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2246 /* set pageflip to happen only at start of vblank interval (front porch) */
2247 WREG32(mmCRTC_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 3);
2249 if (!atomic && fb && fb != crtc->primary->fb) {
2250 amdgpu_fb = to_amdgpu_framebuffer(fb);
2251 rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
2252 r = amdgpu_bo_reserve(rbo, false);
2253 if (unlikely(r != 0))
2255 amdgpu_bo_unpin(rbo);
2256 amdgpu_bo_unreserve(rbo);
2259 /* Bytes per pixel may have changed */
2260 dce_v11_0_bandwidth_update(adev);
2265 static void dce_v11_0_set_interleave(struct drm_crtc *crtc,
2266 struct drm_display_mode *mode)
2268 struct drm_device *dev = crtc->dev;
2269 struct amdgpu_device *adev = dev->dev_private;
2270 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2273 tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
2274 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2275 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
2277 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
2278 WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
2281 static void dce_v11_0_crtc_load_lut(struct drm_crtc *crtc)
2283 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2284 struct drm_device *dev = crtc->dev;
2285 struct amdgpu_device *adev = dev->dev_private;
2289 DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2291 tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2292 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
2293 WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2295 tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
2296 tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
2297 WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2299 tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2300 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
2301 WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2303 WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2305 WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2306 WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2307 WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2309 WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2310 WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2311 WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2313 WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2314 WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2316 WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
2317 for (i = 0; i < 256; i++) {
2318 WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
2319 (amdgpu_crtc->lut_r[i] << 20) |
2320 (amdgpu_crtc->lut_g[i] << 10) |
2321 (amdgpu_crtc->lut_b[i] << 0));
2324 tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2325 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
2326 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
2327 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR2_DEGAMMA_MODE, 0);
2328 WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2330 tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
2331 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
2332 WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2334 tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2335 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
2336 WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2338 tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2339 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
2340 WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2342 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
2343 WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
2344 /* XXX this only needs to be programmed once per crtc at startup,
2345 * not sure where the best place for it is
2347 tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
2348 tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
2349 WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2352 static int dce_v11_0_pick_dig_encoder(struct drm_encoder *encoder)
2354 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2355 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2357 switch (amdgpu_encoder->encoder_id) {
2358 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2364 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2370 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2376 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2380 DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2386 * dce_v11_0_pick_pll - Allocate a PPLL for use by the crtc.
2390 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
2391 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
2392 * monitors a dedicated PPLL must be used. If a particular board has
2393 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2394 * as there is no need to program the PLL itself. If we are not able to
2395 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2396 * avoid messing up an existing monitor.
2398 * Asic specific PLL information
2402 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2404 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2407 static u32 dce_v11_0_pick_pll(struct drm_crtc *crtc)
2409 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2410 struct drm_device *dev = crtc->dev;
2411 struct amdgpu_device *adev = dev->dev_private;
2415 if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2416 if (adev->clock.dp_extclk)
2417 /* skip PPLL programming if using ext clock */
2418 return ATOM_PPLL_INVALID;
2420 /* use the same PPLL for all DP monitors */
2421 pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2422 if (pll != ATOM_PPLL_INVALID)
2426 /* use the same PPLL for all monitors with the same clock */
2427 pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2428 if (pll != ATOM_PPLL_INVALID)
2432 /* XXX need to determine what plls are available on each DCE11 part */
2433 pll_in_use = amdgpu_pll_get_use_mask(crtc);
2434 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY) {
2435 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2437 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2439 DRM_ERROR("unable to allocate a PPLL\n");
2440 return ATOM_PPLL_INVALID;
2442 if (!(pll_in_use & (1 << ATOM_PPLL2)))
2444 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2446 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2448 DRM_ERROR("unable to allocate a PPLL\n");
2449 return ATOM_PPLL_INVALID;
2451 return ATOM_PPLL_INVALID;
2454 static void dce_v11_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2456 struct amdgpu_device *adev = crtc->dev->dev_private;
2457 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2460 cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2462 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
2464 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
2465 WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2468 static void dce_v11_0_hide_cursor(struct drm_crtc *crtc)
2470 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2471 struct amdgpu_device *adev = crtc->dev->dev_private;
2474 tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2475 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
2476 WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2479 static void dce_v11_0_show_cursor(struct drm_crtc *crtc)
2481 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2482 struct amdgpu_device *adev = crtc->dev->dev_private;
2485 WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2486 upper_32_bits(amdgpu_crtc->cursor_addr));
2487 WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2488 lower_32_bits(amdgpu_crtc->cursor_addr));
2490 tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2491 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
2492 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
2493 WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2496 static int dce_v11_0_cursor_move_locked(struct drm_crtc *crtc,
2499 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2500 struct amdgpu_device *adev = crtc->dev->dev_private;
2501 int xorigin = 0, yorigin = 0;
2503 /* avivo cursor are offset into the total surface */
2506 DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2509 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2513 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2517 WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2518 WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
2519 WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
2520 ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
2522 amdgpu_crtc->cursor_x = x;
2523 amdgpu_crtc->cursor_y = y;
2528 static int dce_v11_0_crtc_cursor_move(struct drm_crtc *crtc,
2533 dce_v11_0_lock_cursor(crtc, true);
2534 ret = dce_v11_0_cursor_move_locked(crtc, x, y);
2535 dce_v11_0_lock_cursor(crtc, false);
2540 static int dce_v11_0_crtc_cursor_set2(struct drm_crtc *crtc,
2541 struct drm_file *file_priv,
2548 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2549 struct drm_gem_object *obj;
2550 struct amdgpu_bo *aobj;
2554 /* turn off cursor */
2555 dce_v11_0_hide_cursor(crtc);
2560 if ((width > amdgpu_crtc->max_cursor_width) ||
2561 (height > amdgpu_crtc->max_cursor_height)) {
2562 DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2566 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
2568 DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2572 aobj = gem_to_amdgpu_bo(obj);
2573 ret = amdgpu_bo_reserve(aobj, false);
2575 drm_gem_object_unreference_unlocked(obj);
2579 ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
2580 amdgpu_bo_unreserve(aobj);
2582 DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2583 drm_gem_object_unreference_unlocked(obj);
2587 amdgpu_crtc->cursor_width = width;
2588 amdgpu_crtc->cursor_height = height;
2590 dce_v11_0_lock_cursor(crtc, true);
2592 if (hot_x != amdgpu_crtc->cursor_hot_x ||
2593 hot_y != amdgpu_crtc->cursor_hot_y) {
2596 x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
2597 y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
2599 dce_v11_0_cursor_move_locked(crtc, x, y);
2601 amdgpu_crtc->cursor_hot_x = hot_x;
2602 amdgpu_crtc->cursor_hot_y = hot_y;
2605 dce_v11_0_show_cursor(crtc);
2606 dce_v11_0_lock_cursor(crtc, false);
2609 if (amdgpu_crtc->cursor_bo) {
2610 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2611 ret = amdgpu_bo_reserve(aobj, false);
2612 if (likely(ret == 0)) {
2613 amdgpu_bo_unpin(aobj);
2614 amdgpu_bo_unreserve(aobj);
2616 drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
2619 amdgpu_crtc->cursor_bo = obj;
2623 static void dce_v11_0_cursor_reset(struct drm_crtc *crtc)
2625 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2627 if (amdgpu_crtc->cursor_bo) {
2628 dce_v11_0_lock_cursor(crtc, true);
2630 dce_v11_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
2631 amdgpu_crtc->cursor_y);
2633 dce_v11_0_show_cursor(crtc);
2635 dce_v11_0_lock_cursor(crtc, false);
2639 static void dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2640 u16 *blue, uint32_t start, uint32_t size)
2642 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2643 int end = (start + size > 256) ? 256 : start + size, i;
2645 /* userspace palettes are always correct as is */
2646 for (i = start; i < end; i++) {
2647 amdgpu_crtc->lut_r[i] = red[i] >> 6;
2648 amdgpu_crtc->lut_g[i] = green[i] >> 6;
2649 amdgpu_crtc->lut_b[i] = blue[i] >> 6;
2651 dce_v11_0_crtc_load_lut(crtc);
2654 static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
2656 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2658 drm_crtc_cleanup(crtc);
2659 destroy_workqueue(amdgpu_crtc->pflip_queue);
2663 static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
2664 .cursor_set2 = dce_v11_0_crtc_cursor_set2,
2665 .cursor_move = dce_v11_0_crtc_cursor_move,
2666 .gamma_set = dce_v11_0_crtc_gamma_set,
2667 .set_config = amdgpu_crtc_set_config,
2668 .destroy = dce_v11_0_crtc_destroy,
2669 .page_flip = amdgpu_crtc_page_flip,
2672 static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2674 struct drm_device *dev = crtc->dev;
2675 struct amdgpu_device *adev = dev->dev_private;
2676 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2680 case DRM_MODE_DPMS_ON:
2681 amdgpu_crtc->enabled = true;
2682 amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2683 dce_v11_0_vga_enable(crtc, true);
2684 amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2685 dce_v11_0_vga_enable(crtc, false);
2686 /* Make sure VBLANK and PFLIP interrupts are still enabled */
2687 type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
2688 amdgpu_irq_update(adev, &adev->crtc_irq, type);
2689 amdgpu_irq_update(adev, &adev->pageflip_irq, type);
2690 drm_vblank_post_modeset(dev, amdgpu_crtc->crtc_id);
2691 dce_v11_0_crtc_load_lut(crtc);
2693 case DRM_MODE_DPMS_STANDBY:
2694 case DRM_MODE_DPMS_SUSPEND:
2695 case DRM_MODE_DPMS_OFF:
2696 drm_vblank_pre_modeset(dev, amdgpu_crtc->crtc_id);
2697 if (amdgpu_crtc->enabled) {
2698 dce_v11_0_vga_enable(crtc, true);
2699 amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2700 dce_v11_0_vga_enable(crtc, false);
2702 amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2703 amdgpu_crtc->enabled = false;
2706 /* adjust pm to dpms */
2707 amdgpu_pm_compute_clocks(adev);
2710 static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
2712 /* disable crtc pair power gating before programming */
2713 amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2714 amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2715 dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2718 static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
2720 dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2721 amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2724 static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
2726 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2727 struct drm_device *dev = crtc->dev;
2728 struct amdgpu_device *adev = dev->dev_private;
2729 struct amdgpu_atom_ss ss;
2732 dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2733 if (crtc->primary->fb) {
2735 struct amdgpu_framebuffer *amdgpu_fb;
2736 struct amdgpu_bo *rbo;
2738 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
2739 rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
2740 r = amdgpu_bo_reserve(rbo, false);
2742 DRM_ERROR("failed to reserve rbo before unpin\n");
2744 amdgpu_bo_unpin(rbo);
2745 amdgpu_bo_unreserve(rbo);
2748 /* disable the GRPH */
2749 dce_v11_0_grph_enable(crtc, false);
2751 amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2753 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2754 if (adev->mode_info.crtcs[i] &&
2755 adev->mode_info.crtcs[i]->enabled &&
2756 i != amdgpu_crtc->crtc_id &&
2757 amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2758 /* one other crtc is using this pll don't turn
2765 switch (amdgpu_crtc->pll_id) {
2769 /* disable the ppll */
2770 amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2771 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2777 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2778 amdgpu_crtc->adjusted_clock = 0;
2779 amdgpu_crtc->encoder = NULL;
2780 amdgpu_crtc->connector = NULL;
2783 static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
2784 struct drm_display_mode *mode,
2785 struct drm_display_mode *adjusted_mode,
2786 int x, int y, struct drm_framebuffer *old_fb)
2788 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2790 if (!amdgpu_crtc->adjusted_clock)
2793 amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
2794 amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2795 dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2796 amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2797 amdgpu_atombios_crtc_scaler_setup(crtc);
2798 dce_v11_0_cursor_reset(crtc);
2799 /* update the hw version fpr dpm */
2800 amdgpu_crtc->hw_mode = *adjusted_mode;
2805 static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
2806 const struct drm_display_mode *mode,
2807 struct drm_display_mode *adjusted_mode)
2809 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2810 struct drm_device *dev = crtc->dev;
2811 struct drm_encoder *encoder;
2813 /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2814 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2815 if (encoder->crtc == crtc) {
2816 amdgpu_crtc->encoder = encoder;
2817 amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2821 if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2822 amdgpu_crtc->encoder = NULL;
2823 amdgpu_crtc->connector = NULL;
2826 if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2828 if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2831 amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
2832 /* if we can't get a PPLL for a non-DP encoder, fail */
2833 if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2834 !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2840 static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2841 struct drm_framebuffer *old_fb)
2843 return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2846 static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2847 struct drm_framebuffer *fb,
2848 int x, int y, enum mode_set_atomic state)
2850 return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
2853 static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
2854 .dpms = dce_v11_0_crtc_dpms,
2855 .mode_fixup = dce_v11_0_crtc_mode_fixup,
2856 .mode_set = dce_v11_0_crtc_mode_set,
2857 .mode_set_base = dce_v11_0_crtc_set_base,
2858 .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
2859 .prepare = dce_v11_0_crtc_prepare,
2860 .commit = dce_v11_0_crtc_commit,
2861 .load_lut = dce_v11_0_crtc_load_lut,
2862 .disable = dce_v11_0_crtc_disable,
2865 static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
2867 struct amdgpu_crtc *amdgpu_crtc;
2870 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2871 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2872 if (amdgpu_crtc == NULL)
2875 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
2877 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2878 amdgpu_crtc->crtc_id = index;
2879 amdgpu_crtc->pflip_queue = create_singlethread_workqueue("amdgpu-pageflip-queue");
2880 adev->mode_info.crtcs[index] = amdgpu_crtc;
2882 amdgpu_crtc->max_cursor_width = 128;
2883 amdgpu_crtc->max_cursor_height = 128;
2884 adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
2885 adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2887 for (i = 0; i < 256; i++) {
2888 amdgpu_crtc->lut_r[i] = i << 2;
2889 amdgpu_crtc->lut_g[i] = i << 2;
2890 amdgpu_crtc->lut_b[i] = i << 2;
2893 switch (amdgpu_crtc->crtc_id) {
2896 amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
2899 amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
2902 amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
2905 amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
2908 amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
2911 amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
2915 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2916 amdgpu_crtc->adjusted_clock = 0;
2917 amdgpu_crtc->encoder = NULL;
2918 amdgpu_crtc->connector = NULL;
2919 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
2924 static int dce_v11_0_early_init(void *handle)
2926 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2928 adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
2929 adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
2931 dce_v11_0_set_display_funcs(adev);
2932 dce_v11_0_set_irq_funcs(adev);
2934 switch (adev->asic_type) {
2936 adev->mode_info.num_crtc = 3;
2937 adev->mode_info.num_hpd = 6;
2938 adev->mode_info.num_dig = 9;
2941 adev->mode_info.num_crtc = 2;
2942 adev->mode_info.num_hpd = 6;
2943 adev->mode_info.num_dig = 9;
2946 /* FIXME: not supported yet */
2953 static int dce_v11_0_sw_init(void *handle)
2956 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2958 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2959 r = amdgpu_irq_add_id(adev, i + 1, &adev->crtc_irq);
2964 for (i = 8; i < 20; i += 2) {
2965 r = amdgpu_irq_add_id(adev, i, &adev->pageflip_irq);
2971 r = amdgpu_irq_add_id(adev, 42, &adev->hpd_irq);
2975 adev->mode_info.mode_config_initialized = true;
2977 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
2979 adev->ddev->mode_config.max_width = 16384;
2980 adev->ddev->mode_config.max_height = 16384;
2982 adev->ddev->mode_config.preferred_depth = 24;
2983 adev->ddev->mode_config.prefer_shadow = 1;
2985 adev->ddev->mode_config.fb_base = adev->mc.aper_base;
2987 r = amdgpu_modeset_create_props(adev);
2991 adev->ddev->mode_config.max_width = 16384;
2992 adev->ddev->mode_config.max_height = 16384;
2994 /* allocate crtcs */
2995 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2996 r = dce_v11_0_crtc_init(adev, i);
3001 if (amdgpu_atombios_get_connector_info_from_object_table(adev))
3002 amdgpu_print_display_setup(adev->ddev);
3007 dce_v11_0_afmt_init(adev);
3009 r = dce_v11_0_audio_init(adev);
3013 drm_kms_helper_poll_init(adev->ddev);
3018 static int dce_v11_0_sw_fini(void *handle)
3020 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3022 kfree(adev->mode_info.bios_hardcoded_edid);
3024 drm_kms_helper_poll_fini(adev->ddev);
3026 dce_v11_0_audio_fini(adev);
3028 dce_v11_0_afmt_fini(adev);
3030 adev->mode_info.mode_config_initialized = false;
3035 static int dce_v11_0_hw_init(void *handle)
3038 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3040 dce_v11_0_init_golden_registers(adev);
3042 /* init dig PHYs, disp eng pll */
3043 amdgpu_atombios_crtc_powergate_init(adev);
3044 amdgpu_atombios_encoder_init_dig(adev);
3045 amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
3047 /* initialize hpd */
3048 dce_v11_0_hpd_init(adev);
3050 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3051 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3054 dce_v11_0_pageflip_interrupt_init(adev);
3059 static int dce_v11_0_hw_fini(void *handle)
3062 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3064 dce_v11_0_hpd_fini(adev);
3066 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3067 dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3070 dce_v11_0_pageflip_interrupt_fini(adev);
3075 static int dce_v11_0_suspend(void *handle)
3077 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3079 amdgpu_atombios_scratch_regs_save(adev);
3081 return dce_v11_0_hw_fini(handle);
3084 static int dce_v11_0_resume(void *handle)
3086 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3089 ret = dce_v11_0_hw_init(handle);
3091 amdgpu_atombios_scratch_regs_restore(adev);
3093 /* turn on the BL */
3094 if (adev->mode_info.bl_encoder) {
3095 u8 bl_level = amdgpu_display_backlight_get_level(adev,
3096 adev->mode_info.bl_encoder);
3097 amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
3104 static bool dce_v11_0_is_idle(void *handle)
3109 static int dce_v11_0_wait_for_idle(void *handle)
3114 static void dce_v11_0_print_status(void *handle)
3116 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3118 dev_info(adev->dev, "DCE 10.x registers\n");
3122 static int dce_v11_0_soft_reset(void *handle)
3124 u32 srbm_soft_reset = 0, tmp;
3125 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3127 if (dce_v11_0_is_display_hung(adev))
3128 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
3130 if (srbm_soft_reset) {
3131 dce_v11_0_print_status((void *)adev);
3133 tmp = RREG32(mmSRBM_SOFT_RESET);
3134 tmp |= srbm_soft_reset;
3135 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
3136 WREG32(mmSRBM_SOFT_RESET, tmp);
3137 tmp = RREG32(mmSRBM_SOFT_RESET);
3141 tmp &= ~srbm_soft_reset;
3142 WREG32(mmSRBM_SOFT_RESET, tmp);
3143 tmp = RREG32(mmSRBM_SOFT_RESET);
3145 /* Wait a little for things to settle down */
3147 dce_v11_0_print_status((void *)adev);
3152 static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
3154 enum amdgpu_interrupt_state state)
3156 u32 lb_interrupt_mask;
3158 if (crtc >= adev->mode_info.num_crtc) {
3159 DRM_DEBUG("invalid crtc %d\n", crtc);
3164 case AMDGPU_IRQ_STATE_DISABLE:
3165 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3166 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3167 VBLANK_INTERRUPT_MASK, 0);
3168 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3170 case AMDGPU_IRQ_STATE_ENABLE:
3171 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3172 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3173 VBLANK_INTERRUPT_MASK, 1);
3174 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3181 static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
3183 enum amdgpu_interrupt_state state)
3185 u32 lb_interrupt_mask;
3187 if (crtc >= adev->mode_info.num_crtc) {
3188 DRM_DEBUG("invalid crtc %d\n", crtc);
3193 case AMDGPU_IRQ_STATE_DISABLE:
3194 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3195 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3196 VLINE_INTERRUPT_MASK, 0);
3197 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3199 case AMDGPU_IRQ_STATE_ENABLE:
3200 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3201 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3202 VLINE_INTERRUPT_MASK, 1);
3203 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3210 static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
3211 struct amdgpu_irq_src *source,
3213 enum amdgpu_interrupt_state state)
3217 if (hpd >= adev->mode_info.num_hpd) {
3218 DRM_DEBUG("invalid hdp %d\n", hpd);
3223 case AMDGPU_IRQ_STATE_DISABLE:
3224 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3225 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
3226 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3228 case AMDGPU_IRQ_STATE_ENABLE:
3229 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3230 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
3231 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3240 static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
3241 struct amdgpu_irq_src *source,
3243 enum amdgpu_interrupt_state state)
3246 case AMDGPU_CRTC_IRQ_VBLANK1:
3247 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3249 case AMDGPU_CRTC_IRQ_VBLANK2:
3250 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3252 case AMDGPU_CRTC_IRQ_VBLANK3:
3253 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3255 case AMDGPU_CRTC_IRQ_VBLANK4:
3256 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3258 case AMDGPU_CRTC_IRQ_VBLANK5:
3259 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3261 case AMDGPU_CRTC_IRQ_VBLANK6:
3262 dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3264 case AMDGPU_CRTC_IRQ_VLINE1:
3265 dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
3267 case AMDGPU_CRTC_IRQ_VLINE2:
3268 dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
3270 case AMDGPU_CRTC_IRQ_VLINE3:
3271 dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
3273 case AMDGPU_CRTC_IRQ_VLINE4:
3274 dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
3276 case AMDGPU_CRTC_IRQ_VLINE5:
3277 dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
3279 case AMDGPU_CRTC_IRQ_VLINE6:
3280 dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
3288 static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
3289 struct amdgpu_irq_src *src,
3291 enum amdgpu_interrupt_state state)
3295 if (type >= adev->mode_info.num_crtc) {
3296 DRM_ERROR("invalid pageflip crtc %d\n", type);
3300 reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3301 if (state == AMDGPU_IRQ_STATE_DISABLE)
3302 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3303 reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3305 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3306 reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3311 static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
3312 struct amdgpu_irq_src *source,
3313 struct amdgpu_iv_entry *entry)
3315 unsigned long flags;
3317 struct amdgpu_crtc *amdgpu_crtc;
3318 struct amdgpu_flip_work *works;
3320 crtc_id = (entry->src_id - 8) >> 1;
3321 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3323 if (crtc_id >= adev->mode_info.num_crtc) {
3324 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3328 if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
3329 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
3330 WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
3331 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3333 /* IRQ could occur when in initial stage */
3334 if(amdgpu_crtc == NULL)
3337 spin_lock_irqsave(&adev->ddev->event_lock, flags);
3338 works = amdgpu_crtc->pflip_works;
3339 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
3340 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3341 "AMDGPU_FLIP_SUBMITTED(%d)\n",
3342 amdgpu_crtc->pflip_status,
3343 AMDGPU_FLIP_SUBMITTED);
3344 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
3348 /* page flip completed. clean up */
3349 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3350 amdgpu_crtc->pflip_works = NULL;
3352 /* wakeup usersapce */
3354 drm_send_vblank_event(adev->ddev, crtc_id, works->event);
3356 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
3358 drm_vblank_put(adev->ddev, amdgpu_crtc->crtc_id);
3359 queue_work(amdgpu_crtc->pflip_queue, &works->unpin_work);
3364 static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
3369 if (hpd >= adev->mode_info.num_hpd) {
3370 DRM_DEBUG("invalid hdp %d\n", hpd);
3374 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3375 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
3376 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3379 static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
3384 if (crtc >= adev->mode_info.num_crtc) {
3385 DRM_DEBUG("invalid crtc %d\n", crtc);
3389 tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
3390 tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
3391 WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
3394 static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
3399 if (crtc >= adev->mode_info.num_crtc) {
3400 DRM_DEBUG("invalid crtc %d\n", crtc);
3404 tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
3405 tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
3406 WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
3409 static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
3410 struct amdgpu_irq_src *source,
3411 struct amdgpu_iv_entry *entry)
3413 unsigned crtc = entry->src_id - 1;
3414 uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3415 unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
3417 switch (entry->src_data) {
3418 case 0: /* vblank */
3419 if (disp_int & interrupt_status_offsets[crtc].vblank)
3420 dce_v11_0_crtc_vblank_int_ack(adev, crtc);
3422 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3424 if (amdgpu_irq_enabled(adev, source, irq_type)) {
3425 drm_handle_vblank(adev->ddev, crtc);
3427 DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3431 if (disp_int & interrupt_status_offsets[crtc].vline)
3432 dce_v11_0_crtc_vline_int_ack(adev, crtc);
3434 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3436 DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3440 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
3447 static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
3448 struct amdgpu_irq_src *source,
3449 struct amdgpu_iv_entry *entry)
3451 uint32_t disp_int, mask;
3454 if (entry->src_data >= adev->mode_info.num_hpd) {
3455 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
3459 hpd = entry->src_data;
3460 disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3461 mask = interrupt_status_offsets[hpd].hpd;
3463 if (disp_int & mask) {
3464 dce_v11_0_hpd_int_ack(adev, hpd);
3465 schedule_work(&adev->hotplug_work);
3466 DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3472 static int dce_v11_0_set_clockgating_state(void *handle,
3473 enum amd_clockgating_state state)
3478 static int dce_v11_0_set_powergating_state(void *handle,
3479 enum amd_powergating_state state)
3484 const struct amd_ip_funcs dce_v11_0_ip_funcs = {
3485 .early_init = dce_v11_0_early_init,
3487 .sw_init = dce_v11_0_sw_init,
3488 .sw_fini = dce_v11_0_sw_fini,
3489 .hw_init = dce_v11_0_hw_init,
3490 .hw_fini = dce_v11_0_hw_fini,
3491 .suspend = dce_v11_0_suspend,
3492 .resume = dce_v11_0_resume,
3493 .is_idle = dce_v11_0_is_idle,
3494 .wait_for_idle = dce_v11_0_wait_for_idle,
3495 .soft_reset = dce_v11_0_soft_reset,
3496 .print_status = dce_v11_0_print_status,
3497 .set_clockgating_state = dce_v11_0_set_clockgating_state,
3498 .set_powergating_state = dce_v11_0_set_powergating_state,
3502 dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
3503 struct drm_display_mode *mode,
3504 struct drm_display_mode *adjusted_mode)
3506 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3508 amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3510 /* need to call this here rather than in prepare() since we need some crtc info */
3511 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3513 /* set scaler clears this on some chips */
3514 dce_v11_0_set_interleave(encoder->crtc, mode);
3516 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3517 dce_v11_0_afmt_enable(encoder, true);
3518 dce_v11_0_afmt_setmode(encoder, adjusted_mode);
3522 static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
3524 struct amdgpu_device *adev = encoder->dev->dev_private;
3525 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3526 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3528 if ((amdgpu_encoder->active_device &
3529 (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3530 (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3531 ENCODER_OBJECT_ID_NONE)) {
3532 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3534 dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
3535 if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3536 dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3540 amdgpu_atombios_scratch_regs_lock(adev, true);
3543 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3545 /* select the clock/data port if it uses a router */
3546 if (amdgpu_connector->router.cd_valid)
3547 amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3549 /* turn eDP panel on for mode set */
3550 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3551 amdgpu_atombios_encoder_set_edp_panel_power(connector,
3552 ATOM_TRANSMITTER_ACTION_POWER_ON);
3555 /* this is needed for the pll/ss setup to work correctly in some cases */
3556 amdgpu_atombios_encoder_set_crtc_source(encoder);
3557 /* set up the FMT blocks */
3558 dce_v11_0_program_fmt(encoder);
3561 static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
3563 struct drm_device *dev = encoder->dev;
3564 struct amdgpu_device *adev = dev->dev_private;
3566 /* need to call this here as we need the crtc set up */
3567 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3568 amdgpu_atombios_scratch_regs_lock(adev, false);
3571 static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
3573 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3574 struct amdgpu_encoder_atom_dig *dig;
3576 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3578 if (amdgpu_atombios_encoder_is_digital(encoder)) {
3579 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3580 dce_v11_0_afmt_enable(encoder, false);
3581 dig = amdgpu_encoder->enc_priv;
3582 dig->dig_encoder = -1;
3584 amdgpu_encoder->active_device = 0;
3587 /* these are handled by the primary encoders */
3588 static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
3593 static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
3599 dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
3600 struct drm_display_mode *mode,
3601 struct drm_display_mode *adjusted_mode)
3606 static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
3612 dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
3617 static bool dce_v11_0_ext_mode_fixup(struct drm_encoder *encoder,
3618 const struct drm_display_mode *mode,
3619 struct drm_display_mode *adjusted_mode)
3624 static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
3625 .dpms = dce_v11_0_ext_dpms,
3626 .mode_fixup = dce_v11_0_ext_mode_fixup,
3627 .prepare = dce_v11_0_ext_prepare,
3628 .mode_set = dce_v11_0_ext_mode_set,
3629 .commit = dce_v11_0_ext_commit,
3630 .disable = dce_v11_0_ext_disable,
3631 /* no detect for TMDS/LVDS yet */
3634 static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
3635 .dpms = amdgpu_atombios_encoder_dpms,
3636 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3637 .prepare = dce_v11_0_encoder_prepare,
3638 .mode_set = dce_v11_0_encoder_mode_set,
3639 .commit = dce_v11_0_encoder_commit,
3640 .disable = dce_v11_0_encoder_disable,
3641 .detect = amdgpu_atombios_encoder_dig_detect,
3644 static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
3645 .dpms = amdgpu_atombios_encoder_dpms,
3646 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3647 .prepare = dce_v11_0_encoder_prepare,
3648 .mode_set = dce_v11_0_encoder_mode_set,
3649 .commit = dce_v11_0_encoder_commit,
3650 .detect = amdgpu_atombios_encoder_dac_detect,
3653 static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
3655 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3656 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3657 amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3658 kfree(amdgpu_encoder->enc_priv);
3659 drm_encoder_cleanup(encoder);
3660 kfree(amdgpu_encoder);
3663 static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
3664 .destroy = dce_v11_0_encoder_destroy,
3667 static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
3668 uint32_t encoder_enum,
3669 uint32_t supported_device,
3672 struct drm_device *dev = adev->ddev;
3673 struct drm_encoder *encoder;
3674 struct amdgpu_encoder *amdgpu_encoder;
3676 /* see if we already added it */
3677 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3678 amdgpu_encoder = to_amdgpu_encoder(encoder);
3679 if (amdgpu_encoder->encoder_enum == encoder_enum) {
3680 amdgpu_encoder->devices |= supported_device;
3687 amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3688 if (!amdgpu_encoder)
3691 encoder = &amdgpu_encoder->base;
3692 switch (adev->mode_info.num_crtc) {
3694 encoder->possible_crtcs = 0x1;
3698 encoder->possible_crtcs = 0x3;
3701 encoder->possible_crtcs = 0xf;
3704 encoder->possible_crtcs = 0x3f;
3708 amdgpu_encoder->enc_priv = NULL;
3710 amdgpu_encoder->encoder_enum = encoder_enum;
3711 amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3712 amdgpu_encoder->devices = supported_device;
3713 amdgpu_encoder->rmx_type = RMX_OFF;
3714 amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3715 amdgpu_encoder->is_ext_encoder = false;
3716 amdgpu_encoder->caps = caps;
3718 switch (amdgpu_encoder->encoder_id) {
3719 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3720 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3721 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3722 DRM_MODE_ENCODER_DAC);
3723 drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
3725 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3726 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3727 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3728 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3729 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3730 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3731 amdgpu_encoder->rmx_type = RMX_FULL;
3732 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3733 DRM_MODE_ENCODER_LVDS);
3734 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3735 } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3736 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3737 DRM_MODE_ENCODER_DAC);
3738 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3740 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3741 DRM_MODE_ENCODER_TMDS);
3742 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3744 drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
3746 case ENCODER_OBJECT_ID_SI170B:
3747 case ENCODER_OBJECT_ID_CH7303:
3748 case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3749 case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3750 case ENCODER_OBJECT_ID_TITFP513:
3751 case ENCODER_OBJECT_ID_VT1623:
3752 case ENCODER_OBJECT_ID_HDMI_SI1930:
3753 case ENCODER_OBJECT_ID_TRAVIS:
3754 case ENCODER_OBJECT_ID_NUTMEG:
3755 /* these are handled by the primary encoders */
3756 amdgpu_encoder->is_ext_encoder = true;
3757 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3758 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3759 DRM_MODE_ENCODER_LVDS);
3760 else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3761 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3762 DRM_MODE_ENCODER_DAC);
3764 drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
3765 DRM_MODE_ENCODER_TMDS);
3766 drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
3771 static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
3772 .set_vga_render_state = &dce_v11_0_set_vga_render_state,
3773 .bandwidth_update = &dce_v11_0_bandwidth_update,
3774 .vblank_get_counter = &dce_v11_0_vblank_get_counter,
3775 .vblank_wait = &dce_v11_0_vblank_wait,
3776 .is_display_hung = &dce_v11_0_is_display_hung,
3777 .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3778 .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3779 .hpd_sense = &dce_v11_0_hpd_sense,
3780 .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
3781 .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
3782 .page_flip = &dce_v11_0_page_flip,
3783 .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
3784 .add_encoder = &dce_v11_0_encoder_add,
3785 .add_connector = &amdgpu_connector_add,
3786 .stop_mc_access = &dce_v11_0_stop_mc_access,
3787 .resume_mc_access = &dce_v11_0_resume_mc_access,
3790 static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
3792 if (adev->mode_info.funcs == NULL)
3793 adev->mode_info.funcs = &dce_v11_0_display_funcs;
3796 static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
3797 .set = dce_v11_0_set_crtc_irq_state,
3798 .process = dce_v11_0_crtc_irq,
3801 static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
3802 .set = dce_v11_0_set_pageflip_irq_state,
3803 .process = dce_v11_0_pageflip_irq,
3806 static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
3807 .set = dce_v11_0_set_hpd_irq_state,
3808 .process = dce_v11_0_hpd_irq,
3811 static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
3813 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
3814 adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
3816 adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
3817 adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
3819 adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
3820 adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;