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"
34 #include "dce_v10_0.h"
36 #include "dce/dce_10_0_d.h"
37 #include "dce/dce_10_0_sh_mask.h"
38 #include "dce/dce_10_0_enum.h"
39 #include "oss/oss_3_0_d.h"
40 #include "oss/oss_3_0_sh_mask.h"
41 #include "gmc/gmc_8_1_d.h"
42 #include "gmc/gmc_8_1_sh_mask.h"
44 static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev);
45 static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev);
47 static const u32 crtc_offsets[] =
49 CRTC0_REGISTER_OFFSET,
50 CRTC1_REGISTER_OFFSET,
51 CRTC2_REGISTER_OFFSET,
52 CRTC3_REGISTER_OFFSET,
53 CRTC4_REGISTER_OFFSET,
54 CRTC5_REGISTER_OFFSET,
58 static const u32 hpd_offsets[] =
68 static const uint32_t dig_offsets[] = {
84 } interrupt_status_offsets[] = { {
85 .reg = mmDISP_INTERRUPT_STATUS,
86 .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
87 .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
88 .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
90 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
91 .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
92 .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
93 .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
95 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
96 .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
97 .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
98 .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
100 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
101 .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
102 .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
103 .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
105 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
106 .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
107 .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
108 .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
110 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
111 .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
112 .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
113 .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
116 static const u32 golden_settings_tonga_a11[] =
118 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
119 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
120 mmFBC_MISC, 0x1f311fff, 0x12300000,
121 mmHDMI_CONTROL, 0x31000111, 0x00000011,
124 static const u32 tonga_mgcg_cgcg_init[] =
126 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
127 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
130 static const u32 golden_settings_fiji_a10[] =
132 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
133 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
134 mmFBC_MISC, 0x1f311fff, 0x12300000,
135 mmHDMI_CONTROL, 0x31000111, 0x00000011,
138 static const u32 fiji_mgcg_cgcg_init[] =
140 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
141 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
144 static void dce_v10_0_init_golden_registers(struct amdgpu_device *adev)
146 switch (adev->asic_type) {
148 amdgpu_program_register_sequence(adev,
150 (const u32)ARRAY_SIZE(fiji_mgcg_cgcg_init));
151 amdgpu_program_register_sequence(adev,
152 golden_settings_fiji_a10,
153 (const u32)ARRAY_SIZE(golden_settings_fiji_a10));
156 amdgpu_program_register_sequence(adev,
157 tonga_mgcg_cgcg_init,
158 (const u32)ARRAY_SIZE(tonga_mgcg_cgcg_init));
159 amdgpu_program_register_sequence(adev,
160 golden_settings_tonga_a11,
161 (const u32)ARRAY_SIZE(golden_settings_tonga_a11));
168 static u32 dce_v10_0_audio_endpt_rreg(struct amdgpu_device *adev,
169 u32 block_offset, u32 reg)
174 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
175 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
176 r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
177 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
182 static void dce_v10_0_audio_endpt_wreg(struct amdgpu_device *adev,
183 u32 block_offset, u32 reg, u32 v)
187 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
188 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
189 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
190 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
193 static bool dce_v10_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
195 if (RREG32(mmCRTC_STATUS + crtc_offsets[crtc]) &
196 CRTC_V_BLANK_START_END__CRTC_V_BLANK_START_MASK)
202 static bool dce_v10_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
206 pos1 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
207 pos2 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
216 * dce_v10_0_vblank_wait - vblank wait asic callback.
218 * @adev: amdgpu_device pointer
219 * @crtc: crtc to wait for vblank on
221 * Wait for vblank on the requested crtc (evergreen+).
223 static void dce_v10_0_vblank_wait(struct amdgpu_device *adev, int crtc)
227 if (crtc >= adev->mode_info.num_crtc)
230 if (!(RREG32(mmCRTC_CONTROL + crtc_offsets[crtc]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK))
233 /* depending on when we hit vblank, we may be close to active; if so,
234 * wait for another frame.
236 while (dce_v10_0_is_in_vblank(adev, crtc)) {
239 if (!dce_v10_0_is_counter_moving(adev, crtc))
244 while (!dce_v10_0_is_in_vblank(adev, crtc)) {
247 if (!dce_v10_0_is_counter_moving(adev, crtc))
253 static u32 dce_v10_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
255 if (crtc >= adev->mode_info.num_crtc)
258 return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
261 static void dce_v10_0_pageflip_interrupt_init(struct amdgpu_device *adev)
265 /* Enable pflip interrupts */
266 for (i = 0; i < adev->mode_info.num_crtc; i++)
267 amdgpu_irq_get(adev, &adev->pageflip_irq, i);
270 static void dce_v10_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
274 /* Disable pflip interrupts */
275 for (i = 0; i < adev->mode_info.num_crtc; i++)
276 amdgpu_irq_put(adev, &adev->pageflip_irq, i);
280 * dce_v10_0_page_flip - pageflip callback.
282 * @adev: amdgpu_device pointer
283 * @crtc_id: crtc to cleanup pageflip on
284 * @crtc_base: new address of the crtc (GPU MC address)
286 * Triggers the actual pageflip by updating the primary
287 * surface base address.
289 static void dce_v10_0_page_flip(struct amdgpu_device *adev,
290 int crtc_id, u64 crtc_base, bool async)
292 struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
295 /* flip at hsync for async, default is vsync */
296 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
297 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
298 GRPH_SURFACE_UPDATE_H_RETRACE_EN, async ? 1 : 0);
299 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
300 /* update the primary scanout address */
301 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
302 upper_32_bits(crtc_base));
303 /* writing to the low address triggers the update */
304 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
305 lower_32_bits(crtc_base));
307 RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
310 static int dce_v10_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
311 u32 *vbl, u32 *position)
313 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
316 *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
317 *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
323 * dce_v10_0_hpd_sense - hpd sense callback.
325 * @adev: amdgpu_device pointer
326 * @hpd: hpd (hotplug detect) pin
328 * Checks if a digital monitor is connected (evergreen+).
329 * Returns true if connected, false if not connected.
331 static bool dce_v10_0_hpd_sense(struct amdgpu_device *adev,
332 enum amdgpu_hpd_id hpd)
334 bool connected = false;
336 if (hpd >= adev->mode_info.num_hpd)
339 if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[hpd]) &
340 DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
347 * dce_v10_0_hpd_set_polarity - hpd set polarity callback.
349 * @adev: amdgpu_device pointer
350 * @hpd: hpd (hotplug detect) pin
352 * Set the polarity of the hpd pin (evergreen+).
354 static void dce_v10_0_hpd_set_polarity(struct amdgpu_device *adev,
355 enum amdgpu_hpd_id hpd)
358 bool connected = dce_v10_0_hpd_sense(adev, hpd);
360 if (hpd >= adev->mode_info.num_hpd)
363 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
365 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
367 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
368 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
372 * dce_v10_0_hpd_init - hpd setup callback.
374 * @adev: amdgpu_device pointer
376 * Setup the hpd pins used by the card (evergreen+).
377 * Enable the pin, set the polarity, and enable the hpd interrupts.
379 static void dce_v10_0_hpd_init(struct amdgpu_device *adev)
381 struct drm_device *dev = adev->ddev;
382 struct drm_connector *connector;
385 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
386 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
388 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
391 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
392 connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
393 /* don't try to enable hpd on eDP or LVDS avoid breaking the
394 * aux dp channel on imac and help (but not completely fix)
395 * https://bugzilla.redhat.com/show_bug.cgi?id=726143
396 * also avoid interrupt storms during dpms.
398 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
399 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
400 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
404 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
405 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
406 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
408 tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd]);
409 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
410 DC_HPD_CONNECT_INT_DELAY,
411 AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
412 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
413 DC_HPD_DISCONNECT_INT_DELAY,
414 AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
415 WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
417 dce_v10_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
418 amdgpu_irq_get(adev, &adev->hpd_irq,
419 amdgpu_connector->hpd.hpd);
424 * dce_v10_0_hpd_fini - hpd tear down callback.
426 * @adev: amdgpu_device pointer
428 * Tear down the hpd pins used by the card (evergreen+).
429 * Disable the hpd interrupts.
431 static void dce_v10_0_hpd_fini(struct amdgpu_device *adev)
433 struct drm_device *dev = adev->ddev;
434 struct drm_connector *connector;
437 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
438 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
440 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
443 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
444 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
445 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
447 amdgpu_irq_put(adev, &adev->hpd_irq,
448 amdgpu_connector->hpd.hpd);
452 static u32 dce_v10_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
454 return mmDC_GPIO_HPD_A;
457 static bool dce_v10_0_is_display_hung(struct amdgpu_device *adev)
463 for (i = 0; i < adev->mode_info.num_crtc; i++) {
464 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
465 if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
466 crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
467 crtc_hung |= (1 << i);
471 for (j = 0; j < 10; j++) {
472 for (i = 0; i < adev->mode_info.num_crtc; i++) {
473 if (crtc_hung & (1 << i)) {
474 tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
475 if (tmp != crtc_status[i])
476 crtc_hung &= ~(1 << i);
487 static void dce_v10_0_stop_mc_access(struct amdgpu_device *adev,
488 struct amdgpu_mode_mc_save *save)
490 u32 crtc_enabled, tmp;
493 save->vga_render_control = RREG32(mmVGA_RENDER_CONTROL);
494 save->vga_hdp_control = RREG32(mmVGA_HDP_CONTROL);
496 /* disable VGA render */
497 tmp = RREG32(mmVGA_RENDER_CONTROL);
498 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
499 WREG32(mmVGA_RENDER_CONTROL, tmp);
501 /* blank the display controllers */
502 for (i = 0; i < adev->mode_info.num_crtc; i++) {
503 crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
504 CRTC_CONTROL, CRTC_MASTER_EN);
510 save->crtc_enabled[i] = true;
511 tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
512 if (REG_GET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN) == 0) {
513 amdgpu_display_vblank_wait(adev, i);
514 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
515 tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 1);
516 WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
517 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
519 /* wait for the next frame */
520 frame_count = amdgpu_display_vblank_get_counter(adev, i);
521 for (j = 0; j < adev->usec_timeout; j++) {
522 if (amdgpu_display_vblank_get_counter(adev, i) != frame_count)
526 tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
527 if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK) == 0) {
528 tmp = REG_SET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK, 1);
529 WREG32(mmGRPH_UPDATE + crtc_offsets[i], tmp);
531 tmp = RREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i]);
532 if (REG_GET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK) == 0) {
533 tmp = REG_SET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK, 1);
534 WREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
537 /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
538 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
539 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
540 tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
541 WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
542 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
543 save->crtc_enabled[i] = false;
547 save->crtc_enabled[i] = false;
552 static void dce_v10_0_resume_mc_access(struct amdgpu_device *adev,
553 struct amdgpu_mode_mc_save *save)
555 u32 tmp, frame_count;
558 /* update crtc base addresses */
559 for (i = 0; i < adev->mode_info.num_crtc; i++) {
560 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
561 upper_32_bits(adev->mc.vram_start));
562 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
563 upper_32_bits(adev->mc.vram_start));
564 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
565 (u32)adev->mc.vram_start);
566 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + crtc_offsets[i],
567 (u32)adev->mc.vram_start);
569 if (save->crtc_enabled[i]) {
570 tmp = RREG32(mmMASTER_UPDATE_MODE + crtc_offsets[i]);
571 if (REG_GET_FIELD(tmp, MASTER_UPDATE_MODE, MASTER_UPDATE_MODE) != 0) {
572 tmp = REG_SET_FIELD(tmp, MASTER_UPDATE_MODE, MASTER_UPDATE_MODE, 0);
573 WREG32(mmMASTER_UPDATE_MODE + crtc_offsets[i], tmp);
575 tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
576 if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK)) {
577 tmp = REG_SET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK, 0);
578 WREG32(mmGRPH_UPDATE + crtc_offsets[i], tmp);
580 tmp = RREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i]);
581 if (REG_GET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK)) {
582 tmp = REG_SET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK, 0);
583 WREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
585 for (j = 0; j < adev->usec_timeout; j++) {
586 tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
587 if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_SURFACE_UPDATE_PENDING) == 0)
591 tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
592 tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 0);
593 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
594 WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
595 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
596 /* wait for the next frame */
597 frame_count = amdgpu_display_vblank_get_counter(adev, i);
598 for (j = 0; j < adev->usec_timeout; j++) {
599 if (amdgpu_display_vblank_get_counter(adev, i) != frame_count)
606 WREG32(mmVGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
607 WREG32(mmVGA_MEMORY_BASE_ADDRESS, lower_32_bits(adev->mc.vram_start));
609 /* Unlock vga access */
610 WREG32(mmVGA_HDP_CONTROL, save->vga_hdp_control);
612 WREG32(mmVGA_RENDER_CONTROL, save->vga_render_control);
615 static void dce_v10_0_set_vga_render_state(struct amdgpu_device *adev,
620 /* Lockout access through VGA aperture*/
621 tmp = RREG32(mmVGA_HDP_CONTROL);
623 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
625 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
626 WREG32(mmVGA_HDP_CONTROL, tmp);
628 /* disable VGA render */
629 tmp = RREG32(mmVGA_RENDER_CONTROL);
631 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
633 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
634 WREG32(mmVGA_RENDER_CONTROL, tmp);
637 static int dce_v10_0_get_num_crtc(struct amdgpu_device *adev)
641 switch (adev->asic_type) {
652 void dce_v10_0_disable_dce(struct amdgpu_device *adev)
654 /*Disable VGA render and enabled crtc, if has DCE engine*/
655 if (amdgpu_atombios_has_dce_engine_info(adev)) {
659 dce_v10_0_set_vga_render_state(adev, false);
662 for (i = 0; i < dce_v10_0_get_num_crtc(adev); i++) {
663 crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
664 CRTC_CONTROL, CRTC_MASTER_EN);
666 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
667 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
668 tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
669 WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
670 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
676 static void dce_v10_0_program_fmt(struct drm_encoder *encoder)
678 struct drm_device *dev = encoder->dev;
679 struct amdgpu_device *adev = dev->dev_private;
680 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
681 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
682 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
685 enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
688 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
689 bpc = amdgpu_connector_get_monitor_bpc(connector);
690 dither = amdgpu_connector->dither;
693 /* LVDS/eDP FMT is set up by atom */
694 if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
697 /* not needed for analog */
698 if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
699 (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
707 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
708 /* XXX sort out optimal dither settings */
709 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
710 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
711 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
712 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
714 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
715 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
719 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
720 /* XXX sort out optimal dither settings */
721 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
722 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
723 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
724 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
725 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
727 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
728 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
732 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
733 /* XXX sort out optimal dither settings */
734 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
735 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
736 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
737 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
738 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
740 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
741 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
749 WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
753 /* display watermark setup */
755 * dce_v10_0_line_buffer_adjust - Set up the line buffer
757 * @adev: amdgpu_device pointer
758 * @amdgpu_crtc: the selected display controller
759 * @mode: the current display mode on the selected display
762 * Setup up the line buffer allocation for
763 * the selected display controller (CIK).
764 * Returns the line buffer size in pixels.
766 static u32 dce_v10_0_line_buffer_adjust(struct amdgpu_device *adev,
767 struct amdgpu_crtc *amdgpu_crtc,
768 struct drm_display_mode *mode)
770 u32 tmp, buffer_alloc, i, mem_cfg;
771 u32 pipe_offset = amdgpu_crtc->crtc_id;
774 * There are 6 line buffers, one for each display controllers.
775 * There are 3 partitions per LB. Select the number of partitions
776 * to enable based on the display width. For display widths larger
777 * than 4096, you need use to use 2 display controllers and combine
778 * them using the stereo blender.
780 if (amdgpu_crtc->base.enabled && mode) {
781 if (mode->crtc_hdisplay < 1920) {
784 } else if (mode->crtc_hdisplay < 2560) {
787 } else if (mode->crtc_hdisplay < 4096) {
789 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
791 DRM_DEBUG_KMS("Mode too big for LB!\n");
793 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
800 tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
801 tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
802 WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
804 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
805 tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
806 WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
808 for (i = 0; i < adev->usec_timeout; i++) {
809 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
810 if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
815 if (amdgpu_crtc->base.enabled && mode) {
827 /* controller not enabled, so no lb used */
832 * cik_get_number_of_dram_channels - get the number of dram channels
834 * @adev: amdgpu_device pointer
836 * Look up the number of video ram channels (CIK).
837 * Used for display watermark bandwidth calculations
838 * Returns the number of dram channels
840 static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
842 u32 tmp = RREG32(mmMC_SHARED_CHMAP);
844 switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
867 struct dce10_wm_params {
868 u32 dram_channels; /* number of dram channels */
869 u32 yclk; /* bandwidth per dram data pin in kHz */
870 u32 sclk; /* engine clock in kHz */
871 u32 disp_clk; /* display clock in kHz */
872 u32 src_width; /* viewport width */
873 u32 active_time; /* active display time in ns */
874 u32 blank_time; /* blank time in ns */
875 bool interlaced; /* mode is interlaced */
876 fixed20_12 vsc; /* vertical scale ratio */
877 u32 num_heads; /* number of active crtcs */
878 u32 bytes_per_pixel; /* bytes per pixel display + overlay */
879 u32 lb_size; /* line buffer allocated to pipe */
880 u32 vtaps; /* vertical scaler taps */
884 * dce_v10_0_dram_bandwidth - get the dram bandwidth
886 * @wm: watermark calculation data
888 * Calculate the raw dram bandwidth (CIK).
889 * Used for display watermark bandwidth calculations
890 * Returns the dram bandwidth in MBytes/s
892 static u32 dce_v10_0_dram_bandwidth(struct dce10_wm_params *wm)
894 /* Calculate raw DRAM Bandwidth */
895 fixed20_12 dram_efficiency; /* 0.7 */
896 fixed20_12 yclk, dram_channels, bandwidth;
899 a.full = dfixed_const(1000);
900 yclk.full = dfixed_const(wm->yclk);
901 yclk.full = dfixed_div(yclk, a);
902 dram_channels.full = dfixed_const(wm->dram_channels * 4);
903 a.full = dfixed_const(10);
904 dram_efficiency.full = dfixed_const(7);
905 dram_efficiency.full = dfixed_div(dram_efficiency, a);
906 bandwidth.full = dfixed_mul(dram_channels, yclk);
907 bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
909 return dfixed_trunc(bandwidth);
913 * dce_v10_0_dram_bandwidth_for_display - get the dram bandwidth for display
915 * @wm: watermark calculation data
917 * Calculate the dram bandwidth used for display (CIK).
918 * Used for display watermark bandwidth calculations
919 * Returns the dram bandwidth for display in MBytes/s
921 static u32 dce_v10_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
923 /* Calculate DRAM Bandwidth and the part allocated to display. */
924 fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
925 fixed20_12 yclk, dram_channels, bandwidth;
928 a.full = dfixed_const(1000);
929 yclk.full = dfixed_const(wm->yclk);
930 yclk.full = dfixed_div(yclk, a);
931 dram_channels.full = dfixed_const(wm->dram_channels * 4);
932 a.full = dfixed_const(10);
933 disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
934 disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
935 bandwidth.full = dfixed_mul(dram_channels, yclk);
936 bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
938 return dfixed_trunc(bandwidth);
942 * dce_v10_0_data_return_bandwidth - get the data return bandwidth
944 * @wm: watermark calculation data
946 * Calculate the data return bandwidth used for display (CIK).
947 * Used for display watermark bandwidth calculations
948 * Returns the data return bandwidth in MBytes/s
950 static u32 dce_v10_0_data_return_bandwidth(struct dce10_wm_params *wm)
952 /* Calculate the display Data return Bandwidth */
953 fixed20_12 return_efficiency; /* 0.8 */
954 fixed20_12 sclk, bandwidth;
957 a.full = dfixed_const(1000);
958 sclk.full = dfixed_const(wm->sclk);
959 sclk.full = dfixed_div(sclk, a);
960 a.full = dfixed_const(10);
961 return_efficiency.full = dfixed_const(8);
962 return_efficiency.full = dfixed_div(return_efficiency, a);
963 a.full = dfixed_const(32);
964 bandwidth.full = dfixed_mul(a, sclk);
965 bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
967 return dfixed_trunc(bandwidth);
971 * dce_v10_0_dmif_request_bandwidth - get the dmif bandwidth
973 * @wm: watermark calculation data
975 * Calculate the dmif bandwidth used for display (CIK).
976 * Used for display watermark bandwidth calculations
977 * Returns the dmif bandwidth in MBytes/s
979 static u32 dce_v10_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
981 /* Calculate the DMIF Request Bandwidth */
982 fixed20_12 disp_clk_request_efficiency; /* 0.8 */
983 fixed20_12 disp_clk, bandwidth;
986 a.full = dfixed_const(1000);
987 disp_clk.full = dfixed_const(wm->disp_clk);
988 disp_clk.full = dfixed_div(disp_clk, a);
989 a.full = dfixed_const(32);
990 b.full = dfixed_mul(a, disp_clk);
992 a.full = dfixed_const(10);
993 disp_clk_request_efficiency.full = dfixed_const(8);
994 disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
996 bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
998 return dfixed_trunc(bandwidth);
1002 * dce_v10_0_available_bandwidth - get the min available bandwidth
1004 * @wm: watermark calculation data
1006 * Calculate the min available bandwidth used for display (CIK).
1007 * Used for display watermark bandwidth calculations
1008 * Returns the min available bandwidth in MBytes/s
1010 static u32 dce_v10_0_available_bandwidth(struct dce10_wm_params *wm)
1012 /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
1013 u32 dram_bandwidth = dce_v10_0_dram_bandwidth(wm);
1014 u32 data_return_bandwidth = dce_v10_0_data_return_bandwidth(wm);
1015 u32 dmif_req_bandwidth = dce_v10_0_dmif_request_bandwidth(wm);
1017 return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
1021 * dce_v10_0_average_bandwidth - get the average available bandwidth
1023 * @wm: watermark calculation data
1025 * Calculate the average available bandwidth used for display (CIK).
1026 * Used for display watermark bandwidth calculations
1027 * Returns the average available bandwidth in MBytes/s
1029 static u32 dce_v10_0_average_bandwidth(struct dce10_wm_params *wm)
1031 /* Calculate the display mode Average Bandwidth
1032 * DisplayMode should contain the source and destination dimensions,
1036 fixed20_12 line_time;
1037 fixed20_12 src_width;
1038 fixed20_12 bandwidth;
1041 a.full = dfixed_const(1000);
1042 line_time.full = dfixed_const(wm->active_time + wm->blank_time);
1043 line_time.full = dfixed_div(line_time, a);
1044 bpp.full = dfixed_const(wm->bytes_per_pixel);
1045 src_width.full = dfixed_const(wm->src_width);
1046 bandwidth.full = dfixed_mul(src_width, bpp);
1047 bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
1048 bandwidth.full = dfixed_div(bandwidth, line_time);
1050 return dfixed_trunc(bandwidth);
1054 * dce_v10_0_latency_watermark - get the latency watermark
1056 * @wm: watermark calculation data
1058 * Calculate the latency watermark (CIK).
1059 * Used for display watermark bandwidth calculations
1060 * Returns the latency watermark in ns
1062 static u32 dce_v10_0_latency_watermark(struct dce10_wm_params *wm)
1064 /* First calculate the latency in ns */
1065 u32 mc_latency = 2000; /* 2000 ns. */
1066 u32 available_bandwidth = dce_v10_0_available_bandwidth(wm);
1067 u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
1068 u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
1069 u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
1070 u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
1071 (wm->num_heads * cursor_line_pair_return_time);
1072 u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
1073 u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
1074 u32 tmp, dmif_size = 12288;
1077 if (wm->num_heads == 0)
1080 a.full = dfixed_const(2);
1081 b.full = dfixed_const(1);
1082 if ((wm->vsc.full > a.full) ||
1083 ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
1085 ((wm->vsc.full >= a.full) && wm->interlaced))
1086 max_src_lines_per_dst_line = 4;
1088 max_src_lines_per_dst_line = 2;
1090 a.full = dfixed_const(available_bandwidth);
1091 b.full = dfixed_const(wm->num_heads);
1092 a.full = dfixed_div(a, b);
1093 tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
1094 tmp = min(dfixed_trunc(a), tmp);
1096 lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
1098 a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
1099 b.full = dfixed_const(1000);
1100 c.full = dfixed_const(lb_fill_bw);
1101 b.full = dfixed_div(c, b);
1102 a.full = dfixed_div(a, b);
1103 line_fill_time = dfixed_trunc(a);
1105 if (line_fill_time < wm->active_time)
1108 return latency + (line_fill_time - wm->active_time);
1113 * dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display - check
1114 * average and available dram bandwidth
1116 * @wm: watermark calculation data
1118 * Check if the display average bandwidth fits in the display
1119 * dram bandwidth (CIK).
1120 * Used for display watermark bandwidth calculations
1121 * Returns true if the display fits, false if not.
1123 static bool dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
1125 if (dce_v10_0_average_bandwidth(wm) <=
1126 (dce_v10_0_dram_bandwidth_for_display(wm) / wm->num_heads))
1133 * dce_v10_0_average_bandwidth_vs_available_bandwidth - check
1134 * average and available bandwidth
1136 * @wm: watermark calculation data
1138 * Check if the display average bandwidth fits in the display
1139 * available bandwidth (CIK).
1140 * Used for display watermark bandwidth calculations
1141 * Returns true if the display fits, false if not.
1143 static bool dce_v10_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
1145 if (dce_v10_0_average_bandwidth(wm) <=
1146 (dce_v10_0_available_bandwidth(wm) / wm->num_heads))
1153 * dce_v10_0_check_latency_hiding - check latency hiding
1155 * @wm: watermark calculation data
1157 * Check latency hiding (CIK).
1158 * Used for display watermark bandwidth calculations
1159 * Returns true if the display fits, false if not.
1161 static bool dce_v10_0_check_latency_hiding(struct dce10_wm_params *wm)
1163 u32 lb_partitions = wm->lb_size / wm->src_width;
1164 u32 line_time = wm->active_time + wm->blank_time;
1165 u32 latency_tolerant_lines;
1169 a.full = dfixed_const(1);
1170 if (wm->vsc.full > a.full)
1171 latency_tolerant_lines = 1;
1173 if (lb_partitions <= (wm->vtaps + 1))
1174 latency_tolerant_lines = 1;
1176 latency_tolerant_lines = 2;
1179 latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
1181 if (dce_v10_0_latency_watermark(wm) <= latency_hiding)
1188 * dce_v10_0_program_watermarks - program display watermarks
1190 * @adev: amdgpu_device pointer
1191 * @amdgpu_crtc: the selected display controller
1192 * @lb_size: line buffer size
1193 * @num_heads: number of display controllers in use
1195 * Calculate and program the display watermarks for the
1196 * selected display controller (CIK).
1198 static void dce_v10_0_program_watermarks(struct amdgpu_device *adev,
1199 struct amdgpu_crtc *amdgpu_crtc,
1200 u32 lb_size, u32 num_heads)
1202 struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
1203 struct dce10_wm_params wm_low, wm_high;
1206 u32 latency_watermark_a = 0, latency_watermark_b = 0;
1207 u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
1209 if (amdgpu_crtc->base.enabled && num_heads && mode) {
1210 active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
1212 line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
1214 line_time = min(line_time, (u32)65535);
1216 /* watermark for high clocks */
1217 if (adev->pm.dpm_enabled) {
1219 amdgpu_dpm_get_mclk(adev, false) * 10;
1221 amdgpu_dpm_get_sclk(adev, false) * 10;
1223 wm_high.yclk = adev->pm.current_mclk * 10;
1224 wm_high.sclk = adev->pm.current_sclk * 10;
1227 wm_high.disp_clk = mode->clock;
1228 wm_high.src_width = mode->crtc_hdisplay;
1229 wm_high.active_time = active_time;
1230 wm_high.blank_time = line_time - wm_high.active_time;
1231 wm_high.interlaced = false;
1232 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1233 wm_high.interlaced = true;
1234 wm_high.vsc = amdgpu_crtc->vsc;
1236 if (amdgpu_crtc->rmx_type != RMX_OFF)
1238 wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1239 wm_high.lb_size = lb_size;
1240 wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1241 wm_high.num_heads = num_heads;
1243 /* set for high clocks */
1244 latency_watermark_a = min(dce_v10_0_latency_watermark(&wm_high), (u32)65535);
1246 /* possibly force display priority to high */
1247 /* should really do this at mode validation time... */
1248 if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1249 !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1250 !dce_v10_0_check_latency_hiding(&wm_high) ||
1251 (adev->mode_info.disp_priority == 2)) {
1252 DRM_DEBUG_KMS("force priority to high\n");
1255 /* watermark for low clocks */
1256 if (adev->pm.dpm_enabled) {
1258 amdgpu_dpm_get_mclk(adev, true) * 10;
1260 amdgpu_dpm_get_sclk(adev, true) * 10;
1262 wm_low.yclk = adev->pm.current_mclk * 10;
1263 wm_low.sclk = adev->pm.current_sclk * 10;
1266 wm_low.disp_clk = mode->clock;
1267 wm_low.src_width = mode->crtc_hdisplay;
1268 wm_low.active_time = active_time;
1269 wm_low.blank_time = line_time - wm_low.active_time;
1270 wm_low.interlaced = false;
1271 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1272 wm_low.interlaced = true;
1273 wm_low.vsc = amdgpu_crtc->vsc;
1275 if (amdgpu_crtc->rmx_type != RMX_OFF)
1277 wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1278 wm_low.lb_size = lb_size;
1279 wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1280 wm_low.num_heads = num_heads;
1282 /* set for low clocks */
1283 latency_watermark_b = min(dce_v10_0_latency_watermark(&wm_low), (u32)65535);
1285 /* possibly force display priority to high */
1286 /* should really do this at mode validation time... */
1287 if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1288 !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1289 !dce_v10_0_check_latency_hiding(&wm_low) ||
1290 (adev->mode_info.disp_priority == 2)) {
1291 DRM_DEBUG_KMS("force priority to high\n");
1293 lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
1297 wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1298 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
1299 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1300 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1301 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
1302 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1303 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1305 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
1306 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1307 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1308 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
1309 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1310 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1311 /* restore original selection */
1312 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1314 /* save values for DPM */
1315 amdgpu_crtc->line_time = line_time;
1316 amdgpu_crtc->wm_high = latency_watermark_a;
1317 amdgpu_crtc->wm_low = latency_watermark_b;
1318 /* Save number of lines the linebuffer leads before the scanout */
1319 amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
1323 * dce_v10_0_bandwidth_update - program display watermarks
1325 * @adev: amdgpu_device pointer
1327 * Calculate and program the display watermarks and line
1328 * buffer allocation (CIK).
1330 static void dce_v10_0_bandwidth_update(struct amdgpu_device *adev)
1332 struct drm_display_mode *mode = NULL;
1333 u32 num_heads = 0, lb_size;
1336 amdgpu_update_display_priority(adev);
1338 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1339 if (adev->mode_info.crtcs[i]->base.enabled)
1342 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1343 mode = &adev->mode_info.crtcs[i]->base.mode;
1344 lb_size = dce_v10_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1345 dce_v10_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1346 lb_size, num_heads);
1350 static void dce_v10_0_audio_get_connected_pins(struct amdgpu_device *adev)
1355 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1356 offset = adev->mode_info.audio.pin[i].offset;
1357 tmp = RREG32_AUDIO_ENDPT(offset,
1358 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1360 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1361 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1362 adev->mode_info.audio.pin[i].connected = false;
1364 adev->mode_info.audio.pin[i].connected = true;
1368 static struct amdgpu_audio_pin *dce_v10_0_audio_get_pin(struct amdgpu_device *adev)
1372 dce_v10_0_audio_get_connected_pins(adev);
1374 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1375 if (adev->mode_info.audio.pin[i].connected)
1376 return &adev->mode_info.audio.pin[i];
1378 DRM_ERROR("No connected audio pins found!\n");
1382 static void dce_v10_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1384 struct amdgpu_device *adev = encoder->dev->dev_private;
1385 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1386 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1389 if (!dig || !dig->afmt || !dig->afmt->pin)
1392 tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
1393 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
1394 WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
1397 static void dce_v10_0_audio_write_latency_fields(struct drm_encoder *encoder,
1398 struct drm_display_mode *mode)
1400 struct amdgpu_device *adev = encoder->dev->dev_private;
1401 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1402 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1403 struct drm_connector *connector;
1404 struct amdgpu_connector *amdgpu_connector = NULL;
1408 if (!dig || !dig->afmt || !dig->afmt->pin)
1411 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1412 if (connector->encoder == encoder) {
1413 amdgpu_connector = to_amdgpu_connector(connector);
1418 if (!amdgpu_connector) {
1419 DRM_ERROR("Couldn't find encoder's connector\n");
1423 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1425 if (connector->latency_present[interlace]) {
1426 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1427 VIDEO_LIPSYNC, connector->video_latency[interlace]);
1428 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1429 AUDIO_LIPSYNC, connector->audio_latency[interlace]);
1431 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1433 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1436 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1437 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1440 static void dce_v10_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1442 struct amdgpu_device *adev = encoder->dev->dev_private;
1443 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1444 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1445 struct drm_connector *connector;
1446 struct amdgpu_connector *amdgpu_connector = NULL;
1451 if (!dig || !dig->afmt || !dig->afmt->pin)
1454 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1455 if (connector->encoder == encoder) {
1456 amdgpu_connector = to_amdgpu_connector(connector);
1461 if (!amdgpu_connector) {
1462 DRM_ERROR("Couldn't find encoder's connector\n");
1466 sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
1467 if (sad_count < 0) {
1468 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1472 /* program the speaker allocation */
1473 tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1474 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1475 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1478 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1479 HDMI_CONNECTION, 1);
1481 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1482 SPEAKER_ALLOCATION, sadb[0]);
1484 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1485 SPEAKER_ALLOCATION, 5); /* stereo */
1486 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1487 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1492 static void dce_v10_0_audio_write_sad_regs(struct drm_encoder *encoder)
1494 struct amdgpu_device *adev = encoder->dev->dev_private;
1495 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1496 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1497 struct drm_connector *connector;
1498 struct amdgpu_connector *amdgpu_connector = NULL;
1499 struct cea_sad *sads;
1502 static const u16 eld_reg_to_type[][2] = {
1503 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1504 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1505 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1506 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1507 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1508 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1509 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1510 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1511 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1512 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1513 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1514 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1517 if (!dig || !dig->afmt || !dig->afmt->pin)
1520 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1521 if (connector->encoder == encoder) {
1522 amdgpu_connector = to_amdgpu_connector(connector);
1527 if (!amdgpu_connector) {
1528 DRM_ERROR("Couldn't find encoder's connector\n");
1532 sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
1533 if (sad_count <= 0) {
1534 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1539 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1541 u8 stereo_freqs = 0;
1542 int max_channels = -1;
1545 for (j = 0; j < sad_count; j++) {
1546 struct cea_sad *sad = &sads[j];
1548 if (sad->format == eld_reg_to_type[i][1]) {
1549 if (sad->channels > max_channels) {
1550 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1551 MAX_CHANNELS, sad->channels);
1552 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1553 DESCRIPTOR_BYTE_2, sad->byte2);
1554 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1555 SUPPORTED_FREQUENCIES, sad->freq);
1556 max_channels = sad->channels;
1559 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1560 stereo_freqs |= sad->freq;
1566 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1567 SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
1568 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
1574 static void dce_v10_0_audio_enable(struct amdgpu_device *adev,
1575 struct amdgpu_audio_pin *pin,
1581 WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1582 enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1585 static const u32 pin_offsets[] =
1587 AUD0_REGISTER_OFFSET,
1588 AUD1_REGISTER_OFFSET,
1589 AUD2_REGISTER_OFFSET,
1590 AUD3_REGISTER_OFFSET,
1591 AUD4_REGISTER_OFFSET,
1592 AUD5_REGISTER_OFFSET,
1593 AUD6_REGISTER_OFFSET,
1596 static int dce_v10_0_audio_init(struct amdgpu_device *adev)
1603 adev->mode_info.audio.enabled = true;
1605 adev->mode_info.audio.num_pins = 7;
1607 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1608 adev->mode_info.audio.pin[i].channels = -1;
1609 adev->mode_info.audio.pin[i].rate = -1;
1610 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1611 adev->mode_info.audio.pin[i].status_bits = 0;
1612 adev->mode_info.audio.pin[i].category_code = 0;
1613 adev->mode_info.audio.pin[i].connected = false;
1614 adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1615 adev->mode_info.audio.pin[i].id = i;
1616 /* disable audio. it will be set up later */
1617 /* XXX remove once we switch to ip funcs */
1618 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1624 static void dce_v10_0_audio_fini(struct amdgpu_device *adev)
1631 if (!adev->mode_info.audio.enabled)
1634 for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1635 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1637 adev->mode_info.audio.enabled = false;
1641 * update the N and CTS parameters for a given pixel clock rate
1643 static void dce_v10_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1645 struct drm_device *dev = encoder->dev;
1646 struct amdgpu_device *adev = dev->dev_private;
1647 struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1648 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1649 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1652 tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
1653 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
1654 WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
1655 tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
1656 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
1657 WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
1659 tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
1660 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
1661 WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
1662 tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
1663 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
1664 WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
1666 tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
1667 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
1668 WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
1669 tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
1670 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
1671 WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
1676 * build a HDMI Video Info Frame
1678 static void dce_v10_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1679 void *buffer, size_t size)
1681 struct drm_device *dev = encoder->dev;
1682 struct amdgpu_device *adev = dev->dev_private;
1683 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1684 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1685 uint8_t *frame = buffer + 3;
1686 uint8_t *header = buffer;
1688 WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
1689 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1690 WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
1691 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1692 WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
1693 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1694 WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
1695 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1698 static void dce_v10_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1700 struct drm_device *dev = encoder->dev;
1701 struct amdgpu_device *adev = dev->dev_private;
1702 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1703 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1704 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1705 u32 dto_phase = 24 * 1000;
1706 u32 dto_modulo = clock;
1709 if (!dig || !dig->afmt)
1712 /* XXX two dtos; generally use dto0 for hdmi */
1713 /* Express [24MHz / target pixel clock] as an exact rational
1714 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
1715 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1717 tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
1718 tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
1719 amdgpu_crtc->crtc_id);
1720 WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
1721 WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1722 WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1726 * update the info frames with the data from the current display mode
1728 static void dce_v10_0_afmt_setmode(struct drm_encoder *encoder,
1729 struct drm_display_mode *mode)
1731 struct drm_device *dev = encoder->dev;
1732 struct amdgpu_device *adev = dev->dev_private;
1733 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1734 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1735 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1736 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1737 struct hdmi_avi_infoframe frame;
1742 if (!dig || !dig->afmt)
1745 /* Silent, r600_hdmi_enable will raise WARN for us */
1746 if (!dig->afmt->enabled)
1749 /* hdmi deep color mode general control packets setup, if bpc > 8 */
1750 if (encoder->crtc) {
1751 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1752 bpc = amdgpu_crtc->bpc;
1755 /* disable audio prior to setting up hw */
1756 dig->afmt->pin = dce_v10_0_audio_get_pin(adev);
1757 dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
1759 dce_v10_0_audio_set_dto(encoder, mode->clock);
1761 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1762 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
1763 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
1765 WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
1767 tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
1774 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
1775 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
1776 DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1777 connector->name, bpc);
1780 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1781 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
1782 DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1786 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1787 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
1788 DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1792 WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
1794 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1795 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
1796 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
1797 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
1798 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
1800 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1801 /* enable audio info frames (frames won't be set until audio is enabled) */
1802 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
1803 /* required for audio info values to be updated */
1804 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
1805 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1807 tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
1808 /* required for audio info values to be updated */
1809 tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
1810 WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1812 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1813 /* anything other than 0 */
1814 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
1815 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1817 WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
1819 tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1820 /* set the default audio delay */
1821 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
1822 /* should be suffient for all audio modes and small enough for all hblanks */
1823 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
1824 WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1826 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1827 /* allow 60958 channel status fields to be updated */
1828 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
1829 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1831 tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
1833 /* clear SW CTS value */
1834 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
1836 /* select SW CTS value */
1837 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
1838 /* allow hw to sent ACR packets when required */
1839 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
1840 WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
1842 dce_v10_0_afmt_update_ACR(encoder, mode->clock);
1844 tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
1845 tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
1846 WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
1848 tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
1849 tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
1850 WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
1852 tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
1853 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
1854 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
1855 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
1856 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
1857 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
1858 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
1859 WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
1861 dce_v10_0_audio_write_speaker_allocation(encoder);
1863 WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
1864 (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1866 dce_v10_0_afmt_audio_select_pin(encoder);
1867 dce_v10_0_audio_write_sad_regs(encoder);
1868 dce_v10_0_audio_write_latency_fields(encoder, mode);
1870 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1872 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1876 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1878 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1882 dce_v10_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1884 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1885 /* enable AVI info frames */
1886 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
1887 /* required for audio info values to be updated */
1888 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
1889 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1891 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1892 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
1893 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1895 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1896 /* send audio packets */
1897 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
1898 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1900 WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
1901 WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
1902 WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
1903 WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
1905 /* enable audio after to setting up hw */
1906 dce_v10_0_audio_enable(adev, dig->afmt->pin, true);
1909 static void dce_v10_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1911 struct drm_device *dev = encoder->dev;
1912 struct amdgpu_device *adev = dev->dev_private;
1913 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1914 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1916 if (!dig || !dig->afmt)
1919 /* Silent, r600_hdmi_enable will raise WARN for us */
1920 if (enable && dig->afmt->enabled)
1922 if (!enable && !dig->afmt->enabled)
1925 if (!enable && dig->afmt->pin) {
1926 dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
1927 dig->afmt->pin = NULL;
1930 dig->afmt->enabled = enable;
1932 DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1933 enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1936 static int dce_v10_0_afmt_init(struct amdgpu_device *adev)
1940 for (i = 0; i < adev->mode_info.num_dig; i++)
1941 adev->mode_info.afmt[i] = NULL;
1943 /* DCE10 has audio blocks tied to DIG encoders */
1944 for (i = 0; i < adev->mode_info.num_dig; i++) {
1945 adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1946 if (adev->mode_info.afmt[i]) {
1947 adev->mode_info.afmt[i]->offset = dig_offsets[i];
1948 adev->mode_info.afmt[i]->id = i;
1951 for (j = 0; j < i; j++) {
1952 kfree(adev->mode_info.afmt[j]);
1953 adev->mode_info.afmt[j] = NULL;
1961 static void dce_v10_0_afmt_fini(struct amdgpu_device *adev)
1965 for (i = 0; i < adev->mode_info.num_dig; i++) {
1966 kfree(adev->mode_info.afmt[i]);
1967 adev->mode_info.afmt[i] = NULL;
1971 static const u32 vga_control_regs[6] =
1981 static void dce_v10_0_vga_enable(struct drm_crtc *crtc, bool enable)
1983 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1984 struct drm_device *dev = crtc->dev;
1985 struct amdgpu_device *adev = dev->dev_private;
1988 vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
1990 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
1992 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
1995 static void dce_v10_0_grph_enable(struct drm_crtc *crtc, bool enable)
1997 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1998 struct drm_device *dev = crtc->dev;
1999 struct amdgpu_device *adev = dev->dev_private;
2002 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
2004 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
2007 static int dce_v10_0_crtc_do_set_base(struct drm_crtc *crtc,
2008 struct drm_framebuffer *fb,
2009 int x, int y, int atomic)
2011 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2012 struct drm_device *dev = crtc->dev;
2013 struct amdgpu_device *adev = dev->dev_private;
2014 struct amdgpu_framebuffer *amdgpu_fb;
2015 struct drm_framebuffer *target_fb;
2016 struct drm_gem_object *obj;
2017 struct amdgpu_bo *abo;
2018 uint64_t fb_location, tiling_flags;
2019 uint32_t fb_format, fb_pitch_pixels;
2020 u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
2022 u32 tmp, viewport_w, viewport_h;
2024 bool bypass_lut = false;
2025 struct drm_format_name_buf format_name;
2028 if (!atomic && !crtc->primary->fb) {
2029 DRM_DEBUG_KMS("No FB bound\n");
2034 amdgpu_fb = to_amdgpu_framebuffer(fb);
2037 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
2038 target_fb = crtc->primary->fb;
2041 /* If atomic, assume fb object is pinned & idle & fenced and
2042 * just update base pointers
2044 obj = amdgpu_fb->obj;
2045 abo = gem_to_amdgpu_bo(obj);
2046 r = amdgpu_bo_reserve(abo, false);
2047 if (unlikely(r != 0))
2051 fb_location = amdgpu_bo_gpu_offset(abo);
2053 r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
2054 if (unlikely(r != 0)) {
2055 amdgpu_bo_unreserve(abo);
2060 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
2061 amdgpu_bo_unreserve(abo);
2063 pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2065 switch (target_fb->format->format) {
2067 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
2068 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2070 case DRM_FORMAT_XRGB4444:
2071 case DRM_FORMAT_ARGB4444:
2072 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2073 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
2075 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2079 case DRM_FORMAT_XRGB1555:
2080 case DRM_FORMAT_ARGB1555:
2081 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2082 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2084 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2088 case DRM_FORMAT_BGRX5551:
2089 case DRM_FORMAT_BGRA5551:
2090 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2091 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
2093 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2097 case DRM_FORMAT_RGB565:
2098 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
2099 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
2101 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2105 case DRM_FORMAT_XRGB8888:
2106 case DRM_FORMAT_ARGB8888:
2107 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2108 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
2110 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2114 case DRM_FORMAT_XRGB2101010:
2115 case DRM_FORMAT_ARGB2101010:
2116 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2117 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
2119 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2122 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2125 case DRM_FORMAT_BGRX1010102:
2126 case DRM_FORMAT_BGRA1010102:
2127 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
2128 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
2130 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
2133 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
2137 DRM_ERROR("Unsupported screen format %s\n",
2138 drm_get_format_name(target_fb->format->format, &format_name));
2142 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
2143 unsigned bankw, bankh, mtaspect, tile_split, num_banks;
2145 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2146 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2147 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2148 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2149 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2151 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
2152 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2153 ARRAY_2D_TILED_THIN1);
2154 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
2156 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
2157 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
2158 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
2160 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
2161 ADDR_SURF_MICRO_TILING_DISPLAY);
2162 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
2163 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2164 ARRAY_1D_TILED_THIN1);
2167 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
2170 dce_v10_0_vga_enable(crtc, false);
2172 /* Make sure surface address is updated at vertical blank rather than
2175 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
2176 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
2177 GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
2178 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2180 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2181 upper_32_bits(fb_location));
2182 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2183 upper_32_bits(fb_location));
2184 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2185 (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
2186 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2187 (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
2188 WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
2189 WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
2192 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
2193 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
2194 * retain the full precision throughout the pipeline.
2196 tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
2198 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
2200 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
2201 WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
2204 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
2206 WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
2207 WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
2208 WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
2209 WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
2210 WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
2211 WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
2213 fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
2214 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
2216 dce_v10_0_grph_enable(crtc, true);
2218 WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
2223 WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
2225 viewport_w = crtc->mode.hdisplay;
2226 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
2227 WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
2228 (viewport_w << 16) | viewport_h);
2230 /* set pageflip to happen anywhere in vblank interval */
2231 WREG32(mmMASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
2233 if (!atomic && fb && fb != crtc->primary->fb) {
2234 amdgpu_fb = to_amdgpu_framebuffer(fb);
2235 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
2236 r = amdgpu_bo_reserve(abo, true);
2237 if (unlikely(r != 0))
2239 amdgpu_bo_unpin(abo);
2240 amdgpu_bo_unreserve(abo);
2243 /* Bytes per pixel may have changed */
2244 dce_v10_0_bandwidth_update(adev);
2249 static void dce_v10_0_set_interleave(struct drm_crtc *crtc,
2250 struct drm_display_mode *mode)
2252 struct drm_device *dev = crtc->dev;
2253 struct amdgpu_device *adev = dev->dev_private;
2254 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2257 tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
2258 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2259 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
2261 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
2262 WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
2265 static void dce_v10_0_crtc_load_lut(struct drm_crtc *crtc)
2267 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2268 struct drm_device *dev = crtc->dev;
2269 struct amdgpu_device *adev = dev->dev_private;
2273 DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2275 tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2276 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
2277 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_OVL_MODE, 0);
2278 WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2280 tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
2281 tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
2282 WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2284 tmp = RREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset);
2285 tmp = REG_SET_FIELD(tmp, PRESCALE_OVL_CONTROL, OVL_PRESCALE_BYPASS, 1);
2286 WREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2288 tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2289 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
2290 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, OVL_INPUT_GAMMA_MODE, 0);
2291 WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2293 WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2295 WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2296 WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2297 WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2299 WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2300 WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2301 WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2303 WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2304 WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2306 WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
2307 for (i = 0; i < 256; i++) {
2308 WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
2309 (amdgpu_crtc->lut_r[i] << 20) |
2310 (amdgpu_crtc->lut_g[i] << 10) |
2311 (amdgpu_crtc->lut_b[i] << 0));
2314 tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2315 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
2316 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, OVL_DEGAMMA_MODE, 0);
2317 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
2318 WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2320 tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
2321 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
2322 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, OVL_GAMUT_REMAP_MODE, 0);
2323 WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2325 tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2326 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
2327 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, OVL_REGAMMA_MODE, 0);
2328 WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2330 tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2331 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
2332 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_OVL_MODE, 0);
2333 WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2335 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
2336 WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
2337 /* XXX this only needs to be programmed once per crtc at startup,
2338 * not sure where the best place for it is
2340 tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
2341 tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
2342 WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2345 static int dce_v10_0_pick_dig_encoder(struct drm_encoder *encoder)
2347 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2348 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2350 switch (amdgpu_encoder->encoder_id) {
2351 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2357 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2363 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2369 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2373 DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2379 * dce_v10_0_pick_pll - Allocate a PPLL for use by the crtc.
2383 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
2384 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
2385 * monitors a dedicated PPLL must be used. If a particular board has
2386 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2387 * as there is no need to program the PLL itself. If we are not able to
2388 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2389 * avoid messing up an existing monitor.
2391 * Asic specific PLL information
2395 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2397 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2400 static u32 dce_v10_0_pick_pll(struct drm_crtc *crtc)
2402 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2403 struct drm_device *dev = crtc->dev;
2404 struct amdgpu_device *adev = dev->dev_private;
2408 if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2409 if (adev->clock.dp_extclk)
2410 /* skip PPLL programming if using ext clock */
2411 return ATOM_PPLL_INVALID;
2413 /* use the same PPLL for all DP monitors */
2414 pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2415 if (pll != ATOM_PPLL_INVALID)
2419 /* use the same PPLL for all monitors with the same clock */
2420 pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2421 if (pll != ATOM_PPLL_INVALID)
2425 /* DCE10 has PPLL0, PPLL1, and PPLL2 */
2426 pll_in_use = amdgpu_pll_get_use_mask(crtc);
2427 if (!(pll_in_use & (1 << ATOM_PPLL2)))
2429 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2431 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2433 DRM_ERROR("unable to allocate a PPLL\n");
2434 return ATOM_PPLL_INVALID;
2437 static void dce_v10_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2439 struct amdgpu_device *adev = crtc->dev->dev_private;
2440 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2443 cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2445 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
2447 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
2448 WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2451 static void dce_v10_0_hide_cursor(struct drm_crtc *crtc)
2453 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2454 struct amdgpu_device *adev = crtc->dev->dev_private;
2457 tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2458 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
2459 WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2462 static void dce_v10_0_show_cursor(struct drm_crtc *crtc)
2464 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2465 struct amdgpu_device *adev = crtc->dev->dev_private;
2468 WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2469 upper_32_bits(amdgpu_crtc->cursor_addr));
2470 WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2471 lower_32_bits(amdgpu_crtc->cursor_addr));
2473 tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2474 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
2475 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
2476 WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2479 static int dce_v10_0_cursor_move_locked(struct drm_crtc *crtc,
2482 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2483 struct amdgpu_device *adev = crtc->dev->dev_private;
2484 int xorigin = 0, yorigin = 0;
2486 amdgpu_crtc->cursor_x = x;
2487 amdgpu_crtc->cursor_y = y;
2489 /* avivo cursor are offset into the total surface */
2492 DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2495 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2499 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2503 WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2504 WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
2505 WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
2506 ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
2511 static int dce_v10_0_crtc_cursor_move(struct drm_crtc *crtc,
2516 dce_v10_0_lock_cursor(crtc, true);
2517 ret = dce_v10_0_cursor_move_locked(crtc, x, y);
2518 dce_v10_0_lock_cursor(crtc, false);
2523 static int dce_v10_0_crtc_cursor_set2(struct drm_crtc *crtc,
2524 struct drm_file *file_priv,
2531 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2532 struct drm_gem_object *obj;
2533 struct amdgpu_bo *aobj;
2537 /* turn off cursor */
2538 dce_v10_0_hide_cursor(crtc);
2543 if ((width > amdgpu_crtc->max_cursor_width) ||
2544 (height > amdgpu_crtc->max_cursor_height)) {
2545 DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2549 obj = drm_gem_object_lookup(file_priv, handle);
2551 DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2555 aobj = gem_to_amdgpu_bo(obj);
2556 ret = amdgpu_bo_reserve(aobj, false);
2558 drm_gem_object_unreference_unlocked(obj);
2562 ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
2563 amdgpu_bo_unreserve(aobj);
2565 DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2566 drm_gem_object_unreference_unlocked(obj);
2570 dce_v10_0_lock_cursor(crtc, true);
2572 if (width != amdgpu_crtc->cursor_width ||
2573 height != amdgpu_crtc->cursor_height ||
2574 hot_x != amdgpu_crtc->cursor_hot_x ||
2575 hot_y != amdgpu_crtc->cursor_hot_y) {
2578 x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
2579 y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
2581 dce_v10_0_cursor_move_locked(crtc, x, y);
2583 amdgpu_crtc->cursor_width = width;
2584 amdgpu_crtc->cursor_height = height;
2585 amdgpu_crtc->cursor_hot_x = hot_x;
2586 amdgpu_crtc->cursor_hot_y = hot_y;
2589 dce_v10_0_show_cursor(crtc);
2590 dce_v10_0_lock_cursor(crtc, false);
2593 if (amdgpu_crtc->cursor_bo) {
2594 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2595 ret = amdgpu_bo_reserve(aobj, true);
2596 if (likely(ret == 0)) {
2597 amdgpu_bo_unpin(aobj);
2598 amdgpu_bo_unreserve(aobj);
2600 drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
2603 amdgpu_crtc->cursor_bo = obj;
2607 static void dce_v10_0_cursor_reset(struct drm_crtc *crtc)
2609 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2611 if (amdgpu_crtc->cursor_bo) {
2612 dce_v10_0_lock_cursor(crtc, true);
2614 dce_v10_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
2615 amdgpu_crtc->cursor_y);
2617 dce_v10_0_show_cursor(crtc);
2619 dce_v10_0_lock_cursor(crtc, false);
2623 static int dce_v10_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2624 u16 *blue, uint32_t size,
2625 struct drm_modeset_acquire_ctx *ctx)
2627 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2630 /* userspace palettes are always correct as is */
2631 for (i = 0; i < size; i++) {
2632 amdgpu_crtc->lut_r[i] = red[i] >> 6;
2633 amdgpu_crtc->lut_g[i] = green[i] >> 6;
2634 amdgpu_crtc->lut_b[i] = blue[i] >> 6;
2636 dce_v10_0_crtc_load_lut(crtc);
2641 static void dce_v10_0_crtc_destroy(struct drm_crtc *crtc)
2643 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2645 drm_crtc_cleanup(crtc);
2649 static const struct drm_crtc_funcs dce_v10_0_crtc_funcs = {
2650 .cursor_set2 = dce_v10_0_crtc_cursor_set2,
2651 .cursor_move = dce_v10_0_crtc_cursor_move,
2652 .gamma_set = dce_v10_0_crtc_gamma_set,
2653 .set_config = amdgpu_crtc_set_config,
2654 .destroy = dce_v10_0_crtc_destroy,
2655 .page_flip_target = amdgpu_crtc_page_flip_target,
2658 static void dce_v10_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2660 struct drm_device *dev = crtc->dev;
2661 struct amdgpu_device *adev = dev->dev_private;
2662 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2666 case DRM_MODE_DPMS_ON:
2667 amdgpu_crtc->enabled = true;
2668 amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2669 dce_v10_0_vga_enable(crtc, true);
2670 amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2671 dce_v10_0_vga_enable(crtc, false);
2672 /* Make sure VBLANK and PFLIP interrupts are still enabled */
2673 type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
2674 amdgpu_irq_update(adev, &adev->crtc_irq, type);
2675 amdgpu_irq_update(adev, &adev->pageflip_irq, type);
2676 drm_crtc_vblank_on(crtc);
2677 dce_v10_0_crtc_load_lut(crtc);
2679 case DRM_MODE_DPMS_STANDBY:
2680 case DRM_MODE_DPMS_SUSPEND:
2681 case DRM_MODE_DPMS_OFF:
2682 drm_crtc_vblank_off(crtc);
2683 if (amdgpu_crtc->enabled) {
2684 dce_v10_0_vga_enable(crtc, true);
2685 amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2686 dce_v10_0_vga_enable(crtc, false);
2688 amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2689 amdgpu_crtc->enabled = false;
2692 /* adjust pm to dpms */
2693 amdgpu_pm_compute_clocks(adev);
2696 static void dce_v10_0_crtc_prepare(struct drm_crtc *crtc)
2698 /* disable crtc pair power gating before programming */
2699 amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2700 amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2701 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2704 static void dce_v10_0_crtc_commit(struct drm_crtc *crtc)
2706 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2707 amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2710 static void dce_v10_0_crtc_disable(struct drm_crtc *crtc)
2712 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2713 struct drm_device *dev = crtc->dev;
2714 struct amdgpu_device *adev = dev->dev_private;
2715 struct amdgpu_atom_ss ss;
2718 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2719 if (crtc->primary->fb) {
2721 struct amdgpu_framebuffer *amdgpu_fb;
2722 struct amdgpu_bo *abo;
2724 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
2725 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
2726 r = amdgpu_bo_reserve(abo, true);
2728 DRM_ERROR("failed to reserve abo before unpin\n");
2730 amdgpu_bo_unpin(abo);
2731 amdgpu_bo_unreserve(abo);
2734 /* disable the GRPH */
2735 dce_v10_0_grph_enable(crtc, false);
2737 amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2739 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2740 if (adev->mode_info.crtcs[i] &&
2741 adev->mode_info.crtcs[i]->enabled &&
2742 i != amdgpu_crtc->crtc_id &&
2743 amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2744 /* one other crtc is using this pll don't turn
2751 switch (amdgpu_crtc->pll_id) {
2755 /* disable the ppll */
2756 amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2757 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2763 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2764 amdgpu_crtc->adjusted_clock = 0;
2765 amdgpu_crtc->encoder = NULL;
2766 amdgpu_crtc->connector = NULL;
2769 static int dce_v10_0_crtc_mode_set(struct drm_crtc *crtc,
2770 struct drm_display_mode *mode,
2771 struct drm_display_mode *adjusted_mode,
2772 int x, int y, struct drm_framebuffer *old_fb)
2774 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2776 if (!amdgpu_crtc->adjusted_clock)
2779 amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
2780 amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2781 dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2782 amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2783 amdgpu_atombios_crtc_scaler_setup(crtc);
2784 dce_v10_0_cursor_reset(crtc);
2785 /* update the hw version fpr dpm */
2786 amdgpu_crtc->hw_mode = *adjusted_mode;
2791 static bool dce_v10_0_crtc_mode_fixup(struct drm_crtc *crtc,
2792 const struct drm_display_mode *mode,
2793 struct drm_display_mode *adjusted_mode)
2795 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2796 struct drm_device *dev = crtc->dev;
2797 struct drm_encoder *encoder;
2799 /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2800 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2801 if (encoder->crtc == crtc) {
2802 amdgpu_crtc->encoder = encoder;
2803 amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2807 if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2808 amdgpu_crtc->encoder = NULL;
2809 amdgpu_crtc->connector = NULL;
2812 if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2814 if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2817 amdgpu_crtc->pll_id = dce_v10_0_pick_pll(crtc);
2818 /* if we can't get a PPLL for a non-DP encoder, fail */
2819 if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2820 !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2826 static int dce_v10_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2827 struct drm_framebuffer *old_fb)
2829 return dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2832 static int dce_v10_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2833 struct drm_framebuffer *fb,
2834 int x, int y, enum mode_set_atomic state)
2836 return dce_v10_0_crtc_do_set_base(crtc, fb, x, y, 1);
2839 static const struct drm_crtc_helper_funcs dce_v10_0_crtc_helper_funcs = {
2840 .dpms = dce_v10_0_crtc_dpms,
2841 .mode_fixup = dce_v10_0_crtc_mode_fixup,
2842 .mode_set = dce_v10_0_crtc_mode_set,
2843 .mode_set_base = dce_v10_0_crtc_set_base,
2844 .mode_set_base_atomic = dce_v10_0_crtc_set_base_atomic,
2845 .prepare = dce_v10_0_crtc_prepare,
2846 .commit = dce_v10_0_crtc_commit,
2847 .load_lut = dce_v10_0_crtc_load_lut,
2848 .disable = dce_v10_0_crtc_disable,
2851 static int dce_v10_0_crtc_init(struct amdgpu_device *adev, int index)
2853 struct amdgpu_crtc *amdgpu_crtc;
2856 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2857 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2858 if (amdgpu_crtc == NULL)
2861 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v10_0_crtc_funcs);
2863 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2864 amdgpu_crtc->crtc_id = index;
2865 adev->mode_info.crtcs[index] = amdgpu_crtc;
2867 amdgpu_crtc->max_cursor_width = 128;
2868 amdgpu_crtc->max_cursor_height = 128;
2869 adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
2870 adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2872 for (i = 0; i < 256; i++) {
2873 amdgpu_crtc->lut_r[i] = i << 2;
2874 amdgpu_crtc->lut_g[i] = i << 2;
2875 amdgpu_crtc->lut_b[i] = i << 2;
2878 switch (amdgpu_crtc->crtc_id) {
2881 amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
2884 amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
2887 amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
2890 amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
2893 amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
2896 amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
2900 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2901 amdgpu_crtc->adjusted_clock = 0;
2902 amdgpu_crtc->encoder = NULL;
2903 amdgpu_crtc->connector = NULL;
2904 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v10_0_crtc_helper_funcs);
2909 static int dce_v10_0_early_init(void *handle)
2911 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2913 adev->audio_endpt_rreg = &dce_v10_0_audio_endpt_rreg;
2914 adev->audio_endpt_wreg = &dce_v10_0_audio_endpt_wreg;
2916 dce_v10_0_set_display_funcs(adev);
2917 dce_v10_0_set_irq_funcs(adev);
2919 adev->mode_info.num_crtc = dce_v10_0_get_num_crtc(adev);
2921 switch (adev->asic_type) {
2924 adev->mode_info.num_hpd = 6;
2925 adev->mode_info.num_dig = 7;
2928 /* FIXME: not supported yet */
2935 static int dce_v10_0_sw_init(void *handle)
2938 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2940 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2941 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
2946 for (i = 8; i < 20; i += 2) {
2947 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i, &adev->pageflip_irq);
2953 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 42, &adev->hpd_irq);
2957 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
2959 adev->ddev->mode_config.async_page_flip = true;
2961 adev->ddev->mode_config.max_width = 16384;
2962 adev->ddev->mode_config.max_height = 16384;
2964 adev->ddev->mode_config.preferred_depth = 24;
2965 adev->ddev->mode_config.prefer_shadow = 1;
2967 adev->ddev->mode_config.fb_base = adev->mc.aper_base;
2969 r = amdgpu_modeset_create_props(adev);
2973 adev->ddev->mode_config.max_width = 16384;
2974 adev->ddev->mode_config.max_height = 16384;
2976 /* allocate crtcs */
2977 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2978 r = dce_v10_0_crtc_init(adev, i);
2983 if (amdgpu_atombios_get_connector_info_from_object_table(adev))
2984 amdgpu_print_display_setup(adev->ddev);
2989 r = dce_v10_0_afmt_init(adev);
2993 r = dce_v10_0_audio_init(adev);
2997 drm_kms_helper_poll_init(adev->ddev);
2999 adev->mode_info.mode_config_initialized = true;
3003 static int dce_v10_0_sw_fini(void *handle)
3005 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3007 kfree(adev->mode_info.bios_hardcoded_edid);
3009 drm_kms_helper_poll_fini(adev->ddev);
3011 dce_v10_0_audio_fini(adev);
3013 dce_v10_0_afmt_fini(adev);
3015 drm_mode_config_cleanup(adev->ddev);
3016 adev->mode_info.mode_config_initialized = false;
3021 static int dce_v10_0_hw_init(void *handle)
3024 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3026 dce_v10_0_init_golden_registers(adev);
3028 /* init dig PHYs, disp eng pll */
3029 amdgpu_atombios_encoder_init_dig(adev);
3030 amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
3032 /* initialize hpd */
3033 dce_v10_0_hpd_init(adev);
3035 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3036 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3039 dce_v10_0_pageflip_interrupt_init(adev);
3044 static int dce_v10_0_hw_fini(void *handle)
3047 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3049 dce_v10_0_hpd_fini(adev);
3051 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
3052 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
3055 dce_v10_0_pageflip_interrupt_fini(adev);
3060 static int dce_v10_0_suspend(void *handle)
3062 return dce_v10_0_hw_fini(handle);
3065 static int dce_v10_0_resume(void *handle)
3067 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3070 ret = dce_v10_0_hw_init(handle);
3072 /* turn on the BL */
3073 if (adev->mode_info.bl_encoder) {
3074 u8 bl_level = amdgpu_display_backlight_get_level(adev,
3075 adev->mode_info.bl_encoder);
3076 amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
3083 static bool dce_v10_0_is_idle(void *handle)
3088 static int dce_v10_0_wait_for_idle(void *handle)
3093 static bool dce_v10_0_check_soft_reset(void *handle)
3095 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3097 return dce_v10_0_is_display_hung(adev);
3100 static int dce_v10_0_soft_reset(void *handle)
3102 u32 srbm_soft_reset = 0, tmp;
3103 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3105 if (dce_v10_0_is_display_hung(adev))
3106 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
3108 if (srbm_soft_reset) {
3109 tmp = RREG32(mmSRBM_SOFT_RESET);
3110 tmp |= srbm_soft_reset;
3111 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
3112 WREG32(mmSRBM_SOFT_RESET, tmp);
3113 tmp = RREG32(mmSRBM_SOFT_RESET);
3117 tmp &= ~srbm_soft_reset;
3118 WREG32(mmSRBM_SOFT_RESET, tmp);
3119 tmp = RREG32(mmSRBM_SOFT_RESET);
3121 /* Wait a little for things to settle down */
3127 static void dce_v10_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
3129 enum amdgpu_interrupt_state state)
3131 u32 lb_interrupt_mask;
3133 if (crtc >= adev->mode_info.num_crtc) {
3134 DRM_DEBUG("invalid crtc %d\n", crtc);
3139 case AMDGPU_IRQ_STATE_DISABLE:
3140 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3141 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3142 VBLANK_INTERRUPT_MASK, 0);
3143 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3145 case AMDGPU_IRQ_STATE_ENABLE:
3146 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3147 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3148 VBLANK_INTERRUPT_MASK, 1);
3149 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3156 static void dce_v10_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
3158 enum amdgpu_interrupt_state state)
3160 u32 lb_interrupt_mask;
3162 if (crtc >= adev->mode_info.num_crtc) {
3163 DRM_DEBUG("invalid crtc %d\n", crtc);
3168 case AMDGPU_IRQ_STATE_DISABLE:
3169 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3170 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3171 VLINE_INTERRUPT_MASK, 0);
3172 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3174 case AMDGPU_IRQ_STATE_ENABLE:
3175 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3176 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3177 VLINE_INTERRUPT_MASK, 1);
3178 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3185 static int dce_v10_0_set_hpd_irq_state(struct amdgpu_device *adev,
3186 struct amdgpu_irq_src *source,
3188 enum amdgpu_interrupt_state state)
3192 if (hpd >= adev->mode_info.num_hpd) {
3193 DRM_DEBUG("invalid hdp %d\n", hpd);
3198 case AMDGPU_IRQ_STATE_DISABLE:
3199 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3200 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
3201 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3203 case AMDGPU_IRQ_STATE_ENABLE:
3204 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3205 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
3206 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3215 static int dce_v10_0_set_crtc_irq_state(struct amdgpu_device *adev,
3216 struct amdgpu_irq_src *source,
3218 enum amdgpu_interrupt_state state)
3221 case AMDGPU_CRTC_IRQ_VBLANK1:
3222 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3224 case AMDGPU_CRTC_IRQ_VBLANK2:
3225 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3227 case AMDGPU_CRTC_IRQ_VBLANK3:
3228 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3230 case AMDGPU_CRTC_IRQ_VBLANK4:
3231 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3233 case AMDGPU_CRTC_IRQ_VBLANK5:
3234 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3236 case AMDGPU_CRTC_IRQ_VBLANK6:
3237 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3239 case AMDGPU_CRTC_IRQ_VLINE1:
3240 dce_v10_0_set_crtc_vline_interrupt_state(adev, 0, state);
3242 case AMDGPU_CRTC_IRQ_VLINE2:
3243 dce_v10_0_set_crtc_vline_interrupt_state(adev, 1, state);
3245 case AMDGPU_CRTC_IRQ_VLINE3:
3246 dce_v10_0_set_crtc_vline_interrupt_state(adev, 2, state);
3248 case AMDGPU_CRTC_IRQ_VLINE4:
3249 dce_v10_0_set_crtc_vline_interrupt_state(adev, 3, state);
3251 case AMDGPU_CRTC_IRQ_VLINE5:
3252 dce_v10_0_set_crtc_vline_interrupt_state(adev, 4, state);
3254 case AMDGPU_CRTC_IRQ_VLINE6:
3255 dce_v10_0_set_crtc_vline_interrupt_state(adev, 5, state);
3263 static int dce_v10_0_set_pageflip_irq_state(struct amdgpu_device *adev,
3264 struct amdgpu_irq_src *src,
3266 enum amdgpu_interrupt_state state)
3270 if (type >= adev->mode_info.num_crtc) {
3271 DRM_ERROR("invalid pageflip crtc %d\n", type);
3275 reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3276 if (state == AMDGPU_IRQ_STATE_DISABLE)
3277 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3278 reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3280 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3281 reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3286 static int dce_v10_0_pageflip_irq(struct amdgpu_device *adev,
3287 struct amdgpu_irq_src *source,
3288 struct amdgpu_iv_entry *entry)
3290 unsigned long flags;
3292 struct amdgpu_crtc *amdgpu_crtc;
3293 struct amdgpu_flip_work *works;
3295 crtc_id = (entry->src_id - 8) >> 1;
3296 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3298 if (crtc_id >= adev->mode_info.num_crtc) {
3299 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3303 if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
3304 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
3305 WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
3306 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3308 /* IRQ could occur when in initial stage */
3309 if (amdgpu_crtc == NULL)
3312 spin_lock_irqsave(&adev->ddev->event_lock, flags);
3313 works = amdgpu_crtc->pflip_works;
3314 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
3315 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3316 "AMDGPU_FLIP_SUBMITTED(%d)\n",
3317 amdgpu_crtc->pflip_status,
3318 AMDGPU_FLIP_SUBMITTED);
3319 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
3323 /* page flip completed. clean up */
3324 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3325 amdgpu_crtc->pflip_works = NULL;
3327 /* wakeup usersapce */
3329 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
3331 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
3333 drm_crtc_vblank_put(&amdgpu_crtc->base);
3334 schedule_work(&works->unpin_work);
3339 static void dce_v10_0_hpd_int_ack(struct amdgpu_device *adev,
3344 if (hpd >= adev->mode_info.num_hpd) {
3345 DRM_DEBUG("invalid hdp %d\n", hpd);
3349 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3350 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
3351 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3354 static void dce_v10_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
3359 if (crtc >= adev->mode_info.num_crtc) {
3360 DRM_DEBUG("invalid crtc %d\n", crtc);
3364 tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
3365 tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
3366 WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
3369 static void dce_v10_0_crtc_vline_int_ack(struct amdgpu_device *adev,
3374 if (crtc >= adev->mode_info.num_crtc) {
3375 DRM_DEBUG("invalid crtc %d\n", crtc);
3379 tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
3380 tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
3381 WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
3384 static int dce_v10_0_crtc_irq(struct amdgpu_device *adev,
3385 struct amdgpu_irq_src *source,
3386 struct amdgpu_iv_entry *entry)
3388 unsigned crtc = entry->src_id - 1;
3389 uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3390 unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
3392 switch (entry->src_data[0]) {
3393 case 0: /* vblank */
3394 if (disp_int & interrupt_status_offsets[crtc].vblank)
3395 dce_v10_0_crtc_vblank_int_ack(adev, crtc);
3397 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3399 if (amdgpu_irq_enabled(adev, source, irq_type)) {
3400 drm_handle_vblank(adev->ddev, crtc);
3402 DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3406 if (disp_int & interrupt_status_offsets[crtc].vline)
3407 dce_v10_0_crtc_vline_int_ack(adev, crtc);
3409 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3411 DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3415 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3422 static int dce_v10_0_hpd_irq(struct amdgpu_device *adev,
3423 struct amdgpu_irq_src *source,
3424 struct amdgpu_iv_entry *entry)
3426 uint32_t disp_int, mask;
3429 if (entry->src_data[0] >= adev->mode_info.num_hpd) {
3430 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3434 hpd = entry->src_data[0];
3435 disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3436 mask = interrupt_status_offsets[hpd].hpd;
3438 if (disp_int & mask) {
3439 dce_v10_0_hpd_int_ack(adev, hpd);
3440 schedule_work(&adev->hotplug_work);
3441 DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3447 static int dce_v10_0_set_clockgating_state(void *handle,
3448 enum amd_clockgating_state state)
3453 static int dce_v10_0_set_powergating_state(void *handle,
3454 enum amd_powergating_state state)
3459 static const struct amd_ip_funcs dce_v10_0_ip_funcs = {
3460 .name = "dce_v10_0",
3461 .early_init = dce_v10_0_early_init,
3463 .sw_init = dce_v10_0_sw_init,
3464 .sw_fini = dce_v10_0_sw_fini,
3465 .hw_init = dce_v10_0_hw_init,
3466 .hw_fini = dce_v10_0_hw_fini,
3467 .suspend = dce_v10_0_suspend,
3468 .resume = dce_v10_0_resume,
3469 .is_idle = dce_v10_0_is_idle,
3470 .wait_for_idle = dce_v10_0_wait_for_idle,
3471 .check_soft_reset = dce_v10_0_check_soft_reset,
3472 .soft_reset = dce_v10_0_soft_reset,
3473 .set_clockgating_state = dce_v10_0_set_clockgating_state,
3474 .set_powergating_state = dce_v10_0_set_powergating_state,
3478 dce_v10_0_encoder_mode_set(struct drm_encoder *encoder,
3479 struct drm_display_mode *mode,
3480 struct drm_display_mode *adjusted_mode)
3482 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3484 amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3486 /* need to call this here rather than in prepare() since we need some crtc info */
3487 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3489 /* set scaler clears this on some chips */
3490 dce_v10_0_set_interleave(encoder->crtc, mode);
3492 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3493 dce_v10_0_afmt_enable(encoder, true);
3494 dce_v10_0_afmt_setmode(encoder, adjusted_mode);
3498 static void dce_v10_0_encoder_prepare(struct drm_encoder *encoder)
3500 struct amdgpu_device *adev = encoder->dev->dev_private;
3501 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3502 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3504 if ((amdgpu_encoder->active_device &
3505 (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3506 (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3507 ENCODER_OBJECT_ID_NONE)) {
3508 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3510 dig->dig_encoder = dce_v10_0_pick_dig_encoder(encoder);
3511 if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3512 dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3516 amdgpu_atombios_scratch_regs_lock(adev, true);
3519 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3521 /* select the clock/data port if it uses a router */
3522 if (amdgpu_connector->router.cd_valid)
3523 amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3525 /* turn eDP panel on for mode set */
3526 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3527 amdgpu_atombios_encoder_set_edp_panel_power(connector,
3528 ATOM_TRANSMITTER_ACTION_POWER_ON);
3531 /* this is needed for the pll/ss setup to work correctly in some cases */
3532 amdgpu_atombios_encoder_set_crtc_source(encoder);
3533 /* set up the FMT blocks */
3534 dce_v10_0_program_fmt(encoder);
3537 static void dce_v10_0_encoder_commit(struct drm_encoder *encoder)
3539 struct drm_device *dev = encoder->dev;
3540 struct amdgpu_device *adev = dev->dev_private;
3542 /* need to call this here as we need the crtc set up */
3543 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3544 amdgpu_atombios_scratch_regs_lock(adev, false);
3547 static void dce_v10_0_encoder_disable(struct drm_encoder *encoder)
3549 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3550 struct amdgpu_encoder_atom_dig *dig;
3552 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3554 if (amdgpu_atombios_encoder_is_digital(encoder)) {
3555 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3556 dce_v10_0_afmt_enable(encoder, false);
3557 dig = amdgpu_encoder->enc_priv;
3558 dig->dig_encoder = -1;
3560 amdgpu_encoder->active_device = 0;
3563 /* these are handled by the primary encoders */
3564 static void dce_v10_0_ext_prepare(struct drm_encoder *encoder)
3569 static void dce_v10_0_ext_commit(struct drm_encoder *encoder)
3575 dce_v10_0_ext_mode_set(struct drm_encoder *encoder,
3576 struct drm_display_mode *mode,
3577 struct drm_display_mode *adjusted_mode)
3582 static void dce_v10_0_ext_disable(struct drm_encoder *encoder)
3588 dce_v10_0_ext_dpms(struct drm_encoder *encoder, int mode)
3593 static const struct drm_encoder_helper_funcs dce_v10_0_ext_helper_funcs = {
3594 .dpms = dce_v10_0_ext_dpms,
3595 .prepare = dce_v10_0_ext_prepare,
3596 .mode_set = dce_v10_0_ext_mode_set,
3597 .commit = dce_v10_0_ext_commit,
3598 .disable = dce_v10_0_ext_disable,
3599 /* no detect for TMDS/LVDS yet */
3602 static const struct drm_encoder_helper_funcs dce_v10_0_dig_helper_funcs = {
3603 .dpms = amdgpu_atombios_encoder_dpms,
3604 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3605 .prepare = dce_v10_0_encoder_prepare,
3606 .mode_set = dce_v10_0_encoder_mode_set,
3607 .commit = dce_v10_0_encoder_commit,
3608 .disable = dce_v10_0_encoder_disable,
3609 .detect = amdgpu_atombios_encoder_dig_detect,
3612 static const struct drm_encoder_helper_funcs dce_v10_0_dac_helper_funcs = {
3613 .dpms = amdgpu_atombios_encoder_dpms,
3614 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3615 .prepare = dce_v10_0_encoder_prepare,
3616 .mode_set = dce_v10_0_encoder_mode_set,
3617 .commit = dce_v10_0_encoder_commit,
3618 .detect = amdgpu_atombios_encoder_dac_detect,
3621 static void dce_v10_0_encoder_destroy(struct drm_encoder *encoder)
3623 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3624 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3625 amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3626 kfree(amdgpu_encoder->enc_priv);
3627 drm_encoder_cleanup(encoder);
3628 kfree(amdgpu_encoder);
3631 static const struct drm_encoder_funcs dce_v10_0_encoder_funcs = {
3632 .destroy = dce_v10_0_encoder_destroy,
3635 static void dce_v10_0_encoder_add(struct amdgpu_device *adev,
3636 uint32_t encoder_enum,
3637 uint32_t supported_device,
3640 struct drm_device *dev = adev->ddev;
3641 struct drm_encoder *encoder;
3642 struct amdgpu_encoder *amdgpu_encoder;
3644 /* see if we already added it */
3645 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3646 amdgpu_encoder = to_amdgpu_encoder(encoder);
3647 if (amdgpu_encoder->encoder_enum == encoder_enum) {
3648 amdgpu_encoder->devices |= supported_device;
3655 amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3656 if (!amdgpu_encoder)
3659 encoder = &amdgpu_encoder->base;
3660 switch (adev->mode_info.num_crtc) {
3662 encoder->possible_crtcs = 0x1;
3666 encoder->possible_crtcs = 0x3;
3669 encoder->possible_crtcs = 0xf;
3672 encoder->possible_crtcs = 0x3f;
3676 amdgpu_encoder->enc_priv = NULL;
3678 amdgpu_encoder->encoder_enum = encoder_enum;
3679 amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3680 amdgpu_encoder->devices = supported_device;
3681 amdgpu_encoder->rmx_type = RMX_OFF;
3682 amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3683 amdgpu_encoder->is_ext_encoder = false;
3684 amdgpu_encoder->caps = caps;
3686 switch (amdgpu_encoder->encoder_id) {
3687 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3688 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3689 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3690 DRM_MODE_ENCODER_DAC, NULL);
3691 drm_encoder_helper_add(encoder, &dce_v10_0_dac_helper_funcs);
3693 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3694 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3695 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3696 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3697 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3698 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3699 amdgpu_encoder->rmx_type = RMX_FULL;
3700 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3701 DRM_MODE_ENCODER_LVDS, NULL);
3702 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3703 } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3704 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3705 DRM_MODE_ENCODER_DAC, NULL);
3706 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3708 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3709 DRM_MODE_ENCODER_TMDS, NULL);
3710 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3712 drm_encoder_helper_add(encoder, &dce_v10_0_dig_helper_funcs);
3714 case ENCODER_OBJECT_ID_SI170B:
3715 case ENCODER_OBJECT_ID_CH7303:
3716 case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3717 case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3718 case ENCODER_OBJECT_ID_TITFP513:
3719 case ENCODER_OBJECT_ID_VT1623:
3720 case ENCODER_OBJECT_ID_HDMI_SI1930:
3721 case ENCODER_OBJECT_ID_TRAVIS:
3722 case ENCODER_OBJECT_ID_NUTMEG:
3723 /* these are handled by the primary encoders */
3724 amdgpu_encoder->is_ext_encoder = true;
3725 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3726 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3727 DRM_MODE_ENCODER_LVDS, NULL);
3728 else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3729 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3730 DRM_MODE_ENCODER_DAC, NULL);
3732 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3733 DRM_MODE_ENCODER_TMDS, NULL);
3734 drm_encoder_helper_add(encoder, &dce_v10_0_ext_helper_funcs);
3739 static const struct amdgpu_display_funcs dce_v10_0_display_funcs = {
3740 .set_vga_render_state = &dce_v10_0_set_vga_render_state,
3741 .bandwidth_update = &dce_v10_0_bandwidth_update,
3742 .vblank_get_counter = &dce_v10_0_vblank_get_counter,
3743 .vblank_wait = &dce_v10_0_vblank_wait,
3744 .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3745 .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3746 .hpd_sense = &dce_v10_0_hpd_sense,
3747 .hpd_set_polarity = &dce_v10_0_hpd_set_polarity,
3748 .hpd_get_gpio_reg = &dce_v10_0_hpd_get_gpio_reg,
3749 .page_flip = &dce_v10_0_page_flip,
3750 .page_flip_get_scanoutpos = &dce_v10_0_crtc_get_scanoutpos,
3751 .add_encoder = &dce_v10_0_encoder_add,
3752 .add_connector = &amdgpu_connector_add,
3753 .stop_mc_access = &dce_v10_0_stop_mc_access,
3754 .resume_mc_access = &dce_v10_0_resume_mc_access,
3757 static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev)
3759 if (adev->mode_info.funcs == NULL)
3760 adev->mode_info.funcs = &dce_v10_0_display_funcs;
3763 static const struct amdgpu_irq_src_funcs dce_v10_0_crtc_irq_funcs = {
3764 .set = dce_v10_0_set_crtc_irq_state,
3765 .process = dce_v10_0_crtc_irq,
3768 static const struct amdgpu_irq_src_funcs dce_v10_0_pageflip_irq_funcs = {
3769 .set = dce_v10_0_set_pageflip_irq_state,
3770 .process = dce_v10_0_pageflip_irq,
3773 static const struct amdgpu_irq_src_funcs dce_v10_0_hpd_irq_funcs = {
3774 .set = dce_v10_0_set_hpd_irq_state,
3775 .process = dce_v10_0_hpd_irq,
3778 static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev)
3780 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
3781 adev->crtc_irq.funcs = &dce_v10_0_crtc_irq_funcs;
3783 adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
3784 adev->pageflip_irq.funcs = &dce_v10_0_pageflip_irq_funcs;
3786 adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
3787 adev->hpd_irq.funcs = &dce_v10_0_hpd_irq_funcs;
3790 const struct amdgpu_ip_block_version dce_v10_0_ip_block =
3792 .type = AMD_IP_BLOCK_TYPE_DCE,
3796 .funcs = &dce_v10_0_ip_funcs,
3799 const struct amdgpu_ip_block_version dce_v10_1_ip_block =
3801 .type = AMD_IP_BLOCK_TYPE_DCE,
3805 .funcs = &dce_v10_0_ip_funcs,