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