Linux Audio

Check our new training course

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