Linux Audio

Check our new training course

Loading...
v4.6
   1/*
   2 * Copyright © 2012-2014 Intel Corporation
   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 (including the next
  12 * paragraph) shall be included in all copies or substantial portions of the
  13 * Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21 * IN THE SOFTWARE.
  22 *
  23 * Authors:
  24 *    Eugeni Dodonov <eugeni.dodonov@intel.com>
  25 *    Daniel Vetter <daniel.vetter@ffwll.ch>
  26 *
  27 */
  28
  29#include <linux/pm_runtime.h>
  30#include <linux/vgaarb.h>
  31
  32#include "i915_drv.h"
  33#include "intel_drv.h"
  34
  35/**
  36 * DOC: runtime pm
  37 *
  38 * The i915 driver supports dynamic enabling and disabling of entire hardware
  39 * blocks at runtime. This is especially important on the display side where
  40 * software is supposed to control many power gates manually on recent hardware,
  41 * since on the GT side a lot of the power management is done by the hardware.
  42 * But even there some manual control at the device level is required.
  43 *
  44 * Since i915 supports a diverse set of platforms with a unified codebase and
  45 * hardware engineers just love to shuffle functionality around between power
  46 * domains there's a sizeable amount of indirection required. This file provides
  47 * generic functions to the driver for grabbing and releasing references for
  48 * abstract power domains. It then maps those to the actual power wells
  49 * present for a given platform.
  50 */
  51
  52#define for_each_power_well(i, power_well, domain_mask, power_domains)	\
  53	for (i = 0;							\
  54	     i < (power_domains)->power_well_count &&			\
  55		 ((power_well) = &(power_domains)->power_wells[i]);	\
  56	     i++)							\
  57		for_each_if ((power_well)->domains & (domain_mask))
  58
  59#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
  60	for (i = (power_domains)->power_well_count - 1;			 \
  61	     i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
  62	     i--)							 \
  63		for_each_if ((power_well)->domains & (domain_mask))
  64
  65bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
  66				    int power_well_id);
 
 
 
 
  67
  68const char *
  69intel_display_power_domain_str(enum intel_display_power_domain domain)
  70{
  71	switch (domain) {
  72	case POWER_DOMAIN_PIPE_A:
  73		return "PIPE_A";
  74	case POWER_DOMAIN_PIPE_B:
  75		return "PIPE_B";
  76	case POWER_DOMAIN_PIPE_C:
  77		return "PIPE_C";
  78	case POWER_DOMAIN_PIPE_A_PANEL_FITTER:
  79		return "PIPE_A_PANEL_FITTER";
  80	case POWER_DOMAIN_PIPE_B_PANEL_FITTER:
  81		return "PIPE_B_PANEL_FITTER";
  82	case POWER_DOMAIN_PIPE_C_PANEL_FITTER:
  83		return "PIPE_C_PANEL_FITTER";
  84	case POWER_DOMAIN_TRANSCODER_A:
  85		return "TRANSCODER_A";
  86	case POWER_DOMAIN_TRANSCODER_B:
  87		return "TRANSCODER_B";
  88	case POWER_DOMAIN_TRANSCODER_C:
  89		return "TRANSCODER_C";
  90	case POWER_DOMAIN_TRANSCODER_EDP:
  91		return "TRANSCODER_EDP";
 
 
 
 
  92	case POWER_DOMAIN_PORT_DDI_A_LANES:
  93		return "PORT_DDI_A_LANES";
  94	case POWER_DOMAIN_PORT_DDI_B_LANES:
  95		return "PORT_DDI_B_LANES";
  96	case POWER_DOMAIN_PORT_DDI_C_LANES:
  97		return "PORT_DDI_C_LANES";
  98	case POWER_DOMAIN_PORT_DDI_D_LANES:
  99		return "PORT_DDI_D_LANES";
 100	case POWER_DOMAIN_PORT_DDI_E_LANES:
 101		return "PORT_DDI_E_LANES";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 102	case POWER_DOMAIN_PORT_DSI:
 103		return "PORT_DSI";
 104	case POWER_DOMAIN_PORT_CRT:
 105		return "PORT_CRT";
 106	case POWER_DOMAIN_PORT_OTHER:
 107		return "PORT_OTHER";
 108	case POWER_DOMAIN_VGA:
 109		return "VGA";
 110	case POWER_DOMAIN_AUDIO:
 111		return "AUDIO";
 112	case POWER_DOMAIN_PLLS:
 113		return "PLLS";
 114	case POWER_DOMAIN_AUX_A:
 115		return "AUX_A";
 116	case POWER_DOMAIN_AUX_B:
 117		return "AUX_B";
 118	case POWER_DOMAIN_AUX_C:
 119		return "AUX_C";
 120	case POWER_DOMAIN_AUX_D:
 121		return "AUX_D";
 
 
 
 
 122	case POWER_DOMAIN_GMBUS:
 123		return "GMBUS";
 124	case POWER_DOMAIN_INIT:
 125		return "INIT";
 126	case POWER_DOMAIN_MODESET:
 127		return "MODESET";
 
 
 128	default:
 129		MISSING_CASE(domain);
 130		return "?";
 131	}
 132}
 133
 134static void intel_power_well_enable(struct drm_i915_private *dev_priv,
 135				    struct i915_power_well *power_well)
 136{
 137	DRM_DEBUG_KMS("enabling %s\n", power_well->name);
 138	power_well->ops->enable(dev_priv, power_well);
 139	power_well->hw_enabled = true;
 140}
 141
 142static void intel_power_well_disable(struct drm_i915_private *dev_priv,
 143				     struct i915_power_well *power_well)
 144{
 145	DRM_DEBUG_KMS("disabling %s\n", power_well->name);
 146	power_well->hw_enabled = false;
 147	power_well->ops->disable(dev_priv, power_well);
 148}
 149
 150/*
 151 * We should only use the power well if we explicitly asked the hardware to
 152 * enable it, so check if it's enabled and also check if we've requested it to
 153 * be enabled.
 154 */
 155static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
 156				   struct i915_power_well *power_well)
 157{
 158	return I915_READ(HSW_PWR_WELL_DRIVER) ==
 159		     (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
 
 
 
 
 
 
 
 
 
 
 160}
 161
 162/**
 163 * __intel_display_power_is_enabled - unlocked check for a power domain
 164 * @dev_priv: i915 device instance
 165 * @domain: power domain to check
 166 *
 167 * This is the unlocked version of intel_display_power_is_enabled() and should
 168 * only be used from error capture and recovery code where deadlocks are
 169 * possible.
 170 *
 171 * Returns:
 172 * True when the power domain is enabled, false otherwise.
 173 */
 174bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
 175				      enum intel_display_power_domain domain)
 176{
 177	struct i915_power_domains *power_domains;
 178	struct i915_power_well *power_well;
 179	bool is_enabled;
 180	int i;
 181
 182	if (dev_priv->pm.suspended)
 183		return false;
 184
 185	power_domains = &dev_priv->power_domains;
 186
 187	is_enabled = true;
 188
 189	for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
 190		if (power_well->always_on)
 191			continue;
 192
 193		if (!power_well->hw_enabled) {
 194			is_enabled = false;
 195			break;
 196		}
 197	}
 198
 199	return is_enabled;
 200}
 201
 202/**
 203 * intel_display_power_is_enabled - check for a power domain
 204 * @dev_priv: i915 device instance
 205 * @domain: power domain to check
 206 *
 207 * This function can be used to check the hw power domain state. It is mostly
 208 * used in hardware state readout functions. Everywhere else code should rely
 209 * upon explicit power domain reference counting to ensure that the hardware
 210 * block is powered up before accessing it.
 211 *
 212 * Callers must hold the relevant modesetting locks to ensure that concurrent
 213 * threads can't disable the power well while the caller tries to read a few
 214 * registers.
 215 *
 216 * Returns:
 217 * True when the power domain is enabled, false otherwise.
 218 */
 219bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
 220				    enum intel_display_power_domain domain)
 221{
 222	struct i915_power_domains *power_domains;
 223	bool ret;
 224
 225	power_domains = &dev_priv->power_domains;
 226
 227	mutex_lock(&power_domains->lock);
 228	ret = __intel_display_power_is_enabled(dev_priv, domain);
 229	mutex_unlock(&power_domains->lock);
 230
 231	return ret;
 232}
 233
 234/**
 235 * intel_display_set_init_power - set the initial power domain state
 236 * @dev_priv: i915 device instance
 237 * @enable: whether to enable or disable the initial power domain state
 238 *
 239 * For simplicity our driver load/unload and system suspend/resume code assumes
 240 * that all power domains are always enabled. This functions controls the state
 241 * of this little hack. While the initial power domain state is enabled runtime
 242 * pm is effectively disabled.
 243 */
 244void intel_display_set_init_power(struct drm_i915_private *dev_priv,
 245				  bool enable)
 246{
 247	if (dev_priv->power_domains.init_power_on == enable)
 248		return;
 249
 250	if (enable)
 251		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
 252	else
 253		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
 254
 255	dev_priv->power_domains.init_power_on = enable;
 256}
 257
 258/*
 259 * Starting with Haswell, we have a "Power Down Well" that can be turned off
 260 * when not needed anymore. We have 4 registers that can request the power well
 261 * to be enabled, and it will only be disabled if none of the registers is
 262 * requesting it to be enabled.
 263 */
 264static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
 
 265{
 266	struct drm_device *dev = dev_priv->dev;
 267
 268	/*
 269	 * After we re-enable the power well, if we touch VGA register 0x3d5
 270	 * we'll get unclaimed register interrupts. This stops after we write
 271	 * anything to the VGA MSR register. The vgacon module uses this
 272	 * register all the time, so if we unbind our driver and, as a
 273	 * consequence, bind vgacon, we'll get stuck in an infinite loop at
 274	 * console_unlock(). So make here we touch the VGA MSR register, making
 275	 * sure vgacon can keep working normally without triggering interrupts
 276	 * and error messages.
 277	 */
 278	vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
 279	outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
 280	vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
 
 
 281
 282	if (IS_BROADWELL(dev))
 283		gen8_irq_power_well_post_enable(dev_priv,
 284						1 << PIPE_C | 1 << PIPE_B);
 285}
 286
 287static void hsw_power_well_pre_disable(struct drm_i915_private *dev_priv)
 
 288{
 289	if (IS_BROADWELL(dev_priv))
 290		gen8_irq_power_well_pre_disable(dev_priv,
 291						1 << PIPE_C | 1 << PIPE_B);
 292}
 293
 294static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
 295				       struct i915_power_well *power_well)
 
 296{
 297	struct drm_device *dev = dev_priv->dev;
 298
 299	/*
 300	 * After we re-enable the power well, if we touch VGA register 0x3d5
 301	 * we'll get unclaimed register interrupts. This stops after we write
 302	 * anything to the VGA MSR register. The vgacon module uses this
 303	 * register all the time, so if we unbind our driver and, as a
 304	 * consequence, bind vgacon, we'll get stuck in an infinite loop at
 305	 * console_unlock(). So make here we touch the VGA MSR register, making
 306	 * sure vgacon can keep working normally without triggering interrupts
 307	 * and error messages.
 308	 */
 309	if (power_well->data == SKL_DISP_PW_2) {
 310		vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
 311		outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
 312		vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
 
 
 
 
 313
 314		gen8_irq_power_well_post_enable(dev_priv,
 315						1 << PIPE_C | 1 << PIPE_B);
 316	}
 317}
 318
 319static void skl_power_well_pre_disable(struct drm_i915_private *dev_priv,
 320				       struct i915_power_well *power_well)
 321{
 322	if (power_well->data == SKL_DISP_PW_2)
 323		gen8_irq_power_well_pre_disable(dev_priv,
 324						1 << PIPE_C | 1 << PIPE_B);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 325}
 326
 327static void hsw_set_power_well(struct drm_i915_private *dev_priv,
 328			       struct i915_power_well *power_well, bool enable)
 
 
 
 
 
 
 
 
 
 329{
 330	bool is_enabled, enable_requested;
 331	uint32_t tmp;
 
 
 332
 333	tmp = I915_READ(HSW_PWR_WELL_DRIVER);
 334	is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
 335	enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
 336
 337	if (enable) {
 338		if (!enable_requested)
 339			I915_WRITE(HSW_PWR_WELL_DRIVER,
 340				   HSW_PWR_WELL_ENABLE_REQUEST);
 341
 342		if (!is_enabled) {
 343			DRM_DEBUG_KMS("Enabling power well\n");
 344			if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
 345				      HSW_PWR_WELL_STATE_ENABLED), 20))
 346				DRM_ERROR("Timeout enabling power well\n");
 347			hsw_power_well_post_enable(dev_priv);
 348		}
 349
 350	} else {
 351		if (enable_requested) {
 352			hsw_power_well_pre_disable(dev_priv);
 353			I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
 354			POSTING_READ(HSW_PWR_WELL_DRIVER);
 355			DRM_DEBUG_KMS("Requesting to disable the power well\n");
 356		}
 
 
 
 
 357	}
 
 
 
 
 
 
 358}
 359
 360#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS (		\
 361	BIT(POWER_DOMAIN_TRANSCODER_A) |		\
 362	BIT(POWER_DOMAIN_PIPE_B) |			\
 363	BIT(POWER_DOMAIN_TRANSCODER_B) |		\
 364	BIT(POWER_DOMAIN_PIPE_C) |			\
 365	BIT(POWER_DOMAIN_TRANSCODER_C) |		\
 366	BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
 367	BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
 368	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
 369	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
 370	BIT(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
 371	BIT(POWER_DOMAIN_PORT_DDI_E_LANES) |		\
 372	BIT(POWER_DOMAIN_AUX_B) |                       \
 373	BIT(POWER_DOMAIN_AUX_C) |			\
 374	BIT(POWER_DOMAIN_AUX_D) |			\
 375	BIT(POWER_DOMAIN_AUDIO) |			\
 376	BIT(POWER_DOMAIN_VGA) |				\
 377	BIT(POWER_DOMAIN_INIT))
 378#define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS (		\
 379	BIT(POWER_DOMAIN_PORT_DDI_A_LANES) |		\
 380	BIT(POWER_DOMAIN_PORT_DDI_E_LANES) |		\
 381	BIT(POWER_DOMAIN_INIT))
 382#define SKL_DISPLAY_DDI_B_POWER_DOMAINS (		\
 383	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
 384	BIT(POWER_DOMAIN_INIT))
 385#define SKL_DISPLAY_DDI_C_POWER_DOMAINS (		\
 386	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
 387	BIT(POWER_DOMAIN_INIT))
 388#define SKL_DISPLAY_DDI_D_POWER_DOMAINS (		\
 389	BIT(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
 390	BIT(POWER_DOMAIN_INIT))
 391#define SKL_DISPLAY_DC_OFF_POWER_DOMAINS (		\
 392	SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
 393	BIT(POWER_DOMAIN_MODESET) |			\
 394	BIT(POWER_DOMAIN_AUX_A) |			\
 395	BIT(POWER_DOMAIN_INIT))
 396#define SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS (		\
 397	(POWER_DOMAIN_MASK & ~(				\
 398	SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
 399	SKL_DISPLAY_DC_OFF_POWER_DOMAINS)) |		\
 400	BIT(POWER_DOMAIN_INIT))
 401
 402#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS (		\
 403	BIT(POWER_DOMAIN_TRANSCODER_A) |		\
 404	BIT(POWER_DOMAIN_PIPE_B) |			\
 405	BIT(POWER_DOMAIN_TRANSCODER_B) |		\
 406	BIT(POWER_DOMAIN_PIPE_C) |			\
 407	BIT(POWER_DOMAIN_TRANSCODER_C) |		\
 408	BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
 409	BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
 410	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
 411	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
 412	BIT(POWER_DOMAIN_AUX_B) |			\
 413	BIT(POWER_DOMAIN_AUX_C) |			\
 414	BIT(POWER_DOMAIN_AUDIO) |			\
 415	BIT(POWER_DOMAIN_VGA) |				\
 416	BIT(POWER_DOMAIN_GMBUS) |			\
 417	BIT(POWER_DOMAIN_INIT))
 418#define BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS (		\
 419	BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
 420	BIT(POWER_DOMAIN_PIPE_A) |			\
 421	BIT(POWER_DOMAIN_TRANSCODER_EDP) |		\
 422	BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) |		\
 423	BIT(POWER_DOMAIN_PORT_DDI_A_LANES) |		\
 424	BIT(POWER_DOMAIN_AUX_A) |			\
 425	BIT(POWER_DOMAIN_PLLS) |			\
 426	BIT(POWER_DOMAIN_INIT))
 427#define BXT_DISPLAY_DC_OFF_POWER_DOMAINS (		\
 428	BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
 429	BIT(POWER_DOMAIN_MODESET) |			\
 430	BIT(POWER_DOMAIN_AUX_A) |			\
 431	BIT(POWER_DOMAIN_INIT))
 432#define BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS (		\
 433	(POWER_DOMAIN_MASK & ~(BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS |	\
 434	BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS)) |	\
 435	BIT(POWER_DOMAIN_INIT))
 436
 437static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
 438{
 439	struct drm_device *dev = dev_priv->dev;
 440
 441	WARN(!IS_BROXTON(dev), "Platform doesn't support DC9.\n");
 442	WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
 443		"DC9 already programmed to be enabled.\n");
 444	WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
 445		"DC5 still not disabled to enable DC9.\n");
 446	WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
 447	WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
 
 
 448
 449	 /*
 450	  * TODO: check for the following to verify the conditions to enter DC9
 451	  * state are satisfied:
 452	  * 1] Check relevant display engine registers to verify if mode set
 453	  * disable sequence was followed.
 454	  * 2] Check if display uninitialize sequence is initialized.
 455	  */
 456}
 457
 458static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
 459{
 460	WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
 461	WARN(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
 462		"DC9 already programmed to be disabled.\n");
 463	WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
 464		"DC5 still not disabled.\n");
 465
 466	 /*
 467	  * TODO: check for the following to verify DC9 state was indeed
 468	  * entered before programming to disable it:
 469	  * 1] Check relevant display engine registers to verify if mode
 470	  *  set disable sequence was followed.
 471	  * 2] Check if display uninitialize sequence is initialized.
 472	  */
 473}
 474
 475static void gen9_set_dc_state_debugmask(struct drm_i915_private *dev_priv)
 476{
 477	uint32_t val, mask;
 478
 479	mask = DC_STATE_DEBUG_MASK_MEMORY_UP;
 480
 481	if (IS_BROXTON(dev_priv))
 482		mask |= DC_STATE_DEBUG_MASK_CORES;
 483
 484	/* The below bit doesn't need to be cleared ever afterwards */
 485	val = I915_READ(DC_STATE_DEBUG);
 486	if ((val & mask) != mask) {
 487		val |= mask;
 488		I915_WRITE(DC_STATE_DEBUG, val);
 489		POSTING_READ(DC_STATE_DEBUG);
 490	}
 491}
 492
 493static void gen9_write_dc_state(struct drm_i915_private *dev_priv,
 494				u32 state)
 495{
 496	int rewrites = 0;
 497	int rereads = 0;
 498	u32 v;
 499
 500	I915_WRITE(DC_STATE_EN, state);
 501
 502	/* It has been observed that disabling the dc6 state sometimes
 503	 * doesn't stick and dmc keeps returning old value. Make sure
 504	 * the write really sticks enough times and also force rewrite until
 505	 * we are confident that state is exactly what we want.
 506	 */
 507	do  {
 508		v = I915_READ(DC_STATE_EN);
 509
 510		if (v != state) {
 511			I915_WRITE(DC_STATE_EN, state);
 512			rewrites++;
 513			rereads = 0;
 514		} else if (rereads++ > 5) {
 515			break;
 516		}
 517
 518	} while (rewrites < 100);
 519
 520	if (v != state)
 521		DRM_ERROR("Writing dc state to 0x%x failed, now 0x%x\n",
 522			  state, v);
 523
 524	/* Most of the times we need one retry, avoid spam */
 525	if (rewrites > 1)
 526		DRM_DEBUG_KMS("Rewrote dc state to 0x%x %d times\n",
 527			      state, rewrites);
 528}
 529
 530static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
 531{
 532	uint32_t val;
 533	uint32_t mask;
 534
 535	mask = DC_STATE_EN_UPTO_DC5;
 536	if (IS_BROXTON(dev_priv))
 537		mask |= DC_STATE_EN_DC9;
 538	else
 539		mask |= DC_STATE_EN_UPTO_DC6;
 540
 541	WARN_ON_ONCE(state & ~mask);
 
 
 
 
 
 542
 543	if (i915.enable_dc == 0)
 544		state = DC_STATE_DISABLE;
 545	else if (i915.enable_dc == 1 && state > DC_STATE_EN_UPTO_DC5)
 546		state = DC_STATE_EN_UPTO_DC5;
 
 
 
 
 
 
 
 
 
 
 547
 548	val = I915_READ(DC_STATE_EN);
 
 549	DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
 550		      val & mask, state);
 551
 552	/* Check if DMC is ignoring our DC state requests */
 553	if ((val & mask) != dev_priv->csr.dc_state)
 554		DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
 555			  dev_priv->csr.dc_state, val & mask);
 556
 557	val &= ~mask;
 558	val |= state;
 559
 560	gen9_write_dc_state(dev_priv, val);
 561
 562	dev_priv->csr.dc_state = val & mask;
 563}
 564
 565void bxt_enable_dc9(struct drm_i915_private *dev_priv)
 566{
 567	assert_can_enable_dc9(dev_priv);
 568
 569	DRM_DEBUG_KMS("Enabling DC9\n");
 570
 
 571	gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
 572}
 573
 574void bxt_disable_dc9(struct drm_i915_private *dev_priv)
 575{
 576	assert_can_disable_dc9(dev_priv);
 577
 578	DRM_DEBUG_KMS("Disabling DC9\n");
 579
 580	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 
 
 581}
 582
 583static void assert_csr_loaded(struct drm_i915_private *dev_priv)
 584{
 585	WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
 586		  "CSR program storage start is NULL\n");
 587	WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
 588	WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
 589}
 590
 591static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
 592{
 593	struct drm_device *dev = dev_priv->dev;
 594	bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
 595					SKL_DISP_PW_2);
 596
 597	WARN_ONCE(!IS_SKYLAKE(dev) && !IS_KABYLAKE(dev),
 598		  "Platform doesn't support DC5.\n");
 599	WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
 600	WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
 601
 602	WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
 603		  "DC5 already programmed to be enabled.\n");
 604	assert_rpm_wakelock_held(dev_priv);
 605
 606	assert_csr_loaded(dev_priv);
 607}
 608
 609static void assert_can_disable_dc5(struct drm_i915_private *dev_priv)
 610{
 611	/*
 612	 * During initialization, the firmware may not be loaded yet.
 613	 * We still want to make sure that the DC enabling flag is cleared.
 614	 */
 615	if (dev_priv->power_domains.initializing)
 616		return;
 617
 618	assert_rpm_wakelock_held(dev_priv);
 619}
 620
 621static void gen9_enable_dc5(struct drm_i915_private *dev_priv)
 622{
 623	assert_can_enable_dc5(dev_priv);
 624
 625	DRM_DEBUG_KMS("Enabling DC5\n");
 626
 
 
 
 
 
 627	gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
 628}
 629
 630static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
 631{
 632	struct drm_device *dev = dev_priv->dev;
 633
 634	WARN_ONCE(!IS_SKYLAKE(dev) && !IS_KABYLAKE(dev),
 635		  "Platform doesn't support DC6.\n");
 636	WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
 637	WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
 638		  "Backlight is not disabled.\n");
 639	WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
 640		  "DC6 already programmed to be enabled.\n");
 641
 642	assert_csr_loaded(dev_priv);
 643}
 644
 645static void assert_can_disable_dc6(struct drm_i915_private *dev_priv)
 646{
 647	/*
 648	 * During initialization, the firmware may not be loaded yet.
 649	 * We still want to make sure that the DC enabling flag is cleared.
 650	 */
 651	if (dev_priv->power_domains.initializing)
 652		return;
 653
 654	WARN_ONCE(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
 655		  "DC6 already programmed to be disabled.\n");
 656}
 657
 658static void gen9_disable_dc5_dc6(struct drm_i915_private *dev_priv)
 659{
 660	assert_can_disable_dc5(dev_priv);
 661
 662	if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
 663	    i915.enable_dc != 0 && i915.enable_dc != 1)
 664		assert_can_disable_dc6(dev_priv);
 665
 666	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 667}
 668
 669void skl_enable_dc6(struct drm_i915_private *dev_priv)
 670{
 671	assert_can_enable_dc6(dev_priv);
 672
 673	DRM_DEBUG_KMS("Enabling DC6\n");
 674
 675	gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
 
 
 
 676
 
 677}
 678
 679void skl_disable_dc6(struct drm_i915_private *dev_priv)
 680{
 681	assert_can_disable_dc6(dev_priv);
 682
 683	DRM_DEBUG_KMS("Disabling DC6\n");
 684
 685	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 686}
 687
 688static void skl_set_power_well(struct drm_i915_private *dev_priv,
 689			struct i915_power_well *power_well, bool enable)
 690{
 691	uint32_t tmp, fuse_status;
 692	uint32_t req_mask, state_mask;
 693	bool is_enabled, enable_requested, check_fuse_status = false;
 694
 695	tmp = I915_READ(HSW_PWR_WELL_DRIVER);
 696	fuse_status = I915_READ(SKL_FUSE_STATUS);
 697
 698	switch (power_well->data) {
 699	case SKL_DISP_PW_1:
 700		if (wait_for((I915_READ(SKL_FUSE_STATUS) &
 701			SKL_FUSE_PG0_DIST_STATUS), 1)) {
 702			DRM_ERROR("PG0 not enabled\n");
 703			return;
 704		}
 705		break;
 706	case SKL_DISP_PW_2:
 707		if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
 708			DRM_ERROR("PG1 in disabled state\n");
 709			return;
 710		}
 711		break;
 712	case SKL_DISP_PW_DDI_A_E:
 713	case SKL_DISP_PW_DDI_B:
 714	case SKL_DISP_PW_DDI_C:
 715	case SKL_DISP_PW_DDI_D:
 716	case SKL_DISP_PW_MISC_IO:
 717		break;
 718	default:
 719		WARN(1, "Unknown power well %lu\n", power_well->data);
 720		return;
 721	}
 722
 723	req_mask = SKL_POWER_WELL_REQ(power_well->data);
 724	enable_requested = tmp & req_mask;
 725	state_mask = SKL_POWER_WELL_STATE(power_well->data);
 726	is_enabled = tmp & state_mask;
 727
 728	if (!enable && enable_requested)
 729		skl_power_well_pre_disable(dev_priv, power_well);
 730
 731	if (enable) {
 732		if (!enable_requested) {
 733			WARN((tmp & state_mask) &&
 734				!I915_READ(HSW_PWR_WELL_BIOS),
 735				"Invalid for power well status to be enabled, unless done by the BIOS, \
 736				when request is to disable!\n");
 737			I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
 738		}
 739
 740		if (!is_enabled) {
 741			DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
 742			if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
 743				state_mask), 1))
 744				DRM_ERROR("%s enable timeout\n",
 745					power_well->name);
 746			check_fuse_status = true;
 747		}
 748	} else {
 749		if (enable_requested) {
 750			I915_WRITE(HSW_PWR_WELL_DRIVER,	tmp & ~req_mask);
 751			POSTING_READ(HSW_PWR_WELL_DRIVER);
 752			DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
 753		}
 754	}
 755
 756	if (check_fuse_status) {
 757		if (power_well->data == SKL_DISP_PW_1) {
 758			if (wait_for((I915_READ(SKL_FUSE_STATUS) &
 759				SKL_FUSE_PG1_DIST_STATUS), 1))
 760				DRM_ERROR("PG1 distributing status timeout\n");
 761		} else if (power_well->data == SKL_DISP_PW_2) {
 762			if (wait_for((I915_READ(SKL_FUSE_STATUS) &
 763				SKL_FUSE_PG2_DIST_STATUS), 1))
 764				DRM_ERROR("PG2 distributing status timeout\n");
 765		}
 766	}
 767
 768	if (enable && !is_enabled)
 769		skl_power_well_post_enable(dev_priv, power_well);
 770}
 771
 772static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
 773				   struct i915_power_well *power_well)
 774{
 775	hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
 
 
 776
 777	/*
 778	 * We're taking over the BIOS, so clear any requests made by it since
 779	 * the driver is in charge now.
 780	 */
 781	if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
 782		I915_WRITE(HSW_PWR_WELL_BIOS, 0);
 783}
 784
 785static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
 786				  struct i915_power_well *power_well)
 787{
 788	hsw_set_power_well(dev_priv, power_well, true);
 789}
 790
 791static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
 792				   struct i915_power_well *power_well)
 793{
 794	hsw_set_power_well(dev_priv, power_well, false);
 795}
 796
 797static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
 798					struct i915_power_well *power_well)
 799{
 800	uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
 801		SKL_POWER_WELL_STATE(power_well->data);
 802
 803	return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
 804}
 805
 806static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
 807				struct i915_power_well *power_well)
 808{
 809	skl_set_power_well(dev_priv, power_well, power_well->count > 0);
 810
 811	/* Clear any request made by BIOS as driver is taking over */
 812	I915_WRITE(HSW_PWR_WELL_BIOS, 0);
 813}
 814
 815static void skl_power_well_enable(struct drm_i915_private *dev_priv,
 816				struct i915_power_well *power_well)
 817{
 818	skl_set_power_well(dev_priv, power_well, true);
 819}
 820
 821static void skl_power_well_disable(struct drm_i915_private *dev_priv,
 822				struct i915_power_well *power_well)
 823{
 824	skl_set_power_well(dev_priv, power_well, false);
 
 
 
 
 
 
 
 
 
 825}
 826
 827static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
 828					   struct i915_power_well *power_well)
 829{
 830	return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
 831}
 832
 
 
 
 
 
 
 
 
 
 833static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
 834					  struct i915_power_well *power_well)
 835{
 836	gen9_disable_dc5_dc6(dev_priv);
 
 
 
 
 
 
 
 
 
 
 
 837}
 838
 839static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
 840					   struct i915_power_well *power_well)
 841{
 842	if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
 843	    i915.enable_dc != 0 && i915.enable_dc != 1)
 
 
 844		skl_enable_dc6(dev_priv);
 845	else
 846		gen9_enable_dc5(dev_priv);
 847}
 848
 849static void gen9_dc_off_power_well_sync_hw(struct drm_i915_private *dev_priv,
 850					   struct i915_power_well *power_well)
 851{
 852	if (power_well->count > 0) {
 853		gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 854	} else {
 855		if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
 856		    i915.enable_dc != 0 &&
 857		    i915.enable_dc != 1)
 858			gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
 859		else
 860			gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
 861	}
 862}
 863
 864static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
 865					   struct i915_power_well *power_well)
 866{
 867}
 868
 869static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
 870					     struct i915_power_well *power_well)
 871{
 872	return true;
 873}
 874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 875static void vlv_set_power_well(struct drm_i915_private *dev_priv,
 876			       struct i915_power_well *power_well, bool enable)
 877{
 878	enum punit_power_well power_well_id = power_well->data;
 879	u32 mask;
 880	u32 state;
 881	u32 ctrl;
 882
 883	mask = PUNIT_PWRGT_MASK(power_well_id);
 884	state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
 885			 PUNIT_PWRGT_PWR_GATE(power_well_id);
 886
 887	mutex_lock(&dev_priv->rps.hw_lock);
 888
 889#define COND \
 890	((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
 891
 892	if (COND)
 893		goto out;
 894
 895	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
 896	ctrl &= ~mask;
 897	ctrl |= state;
 898	vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
 899
 900	if (wait_for(COND, 100))
 901		DRM_ERROR("timeout setting power well state %08x (%08x)\n",
 902			  state,
 903			  vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
 904
 905#undef COND
 906
 907out:
 908	mutex_unlock(&dev_priv->rps.hw_lock);
 909}
 910
 911static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
 912				   struct i915_power_well *power_well)
 913{
 914	vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
 915}
 916
 917static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
 918				  struct i915_power_well *power_well)
 919{
 920	vlv_set_power_well(dev_priv, power_well, true);
 921}
 922
 923static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
 924				   struct i915_power_well *power_well)
 925{
 926	vlv_set_power_well(dev_priv, power_well, false);
 927}
 928
 929static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
 930				   struct i915_power_well *power_well)
 931{
 932	int power_well_id = power_well->data;
 933	bool enabled = false;
 934	u32 mask;
 935	u32 state;
 936	u32 ctrl;
 937
 938	mask = PUNIT_PWRGT_MASK(power_well_id);
 939	ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
 940
 941	mutex_lock(&dev_priv->rps.hw_lock);
 942
 943	state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
 944	/*
 945	 * We only ever set the power-on and power-gate states, anything
 946	 * else is unexpected.
 947	 */
 948	WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
 949		state != PUNIT_PWRGT_PWR_GATE(power_well_id));
 950	if (state == ctrl)
 951		enabled = true;
 952
 953	/*
 954	 * A transient state at this point would mean some unexpected party
 955	 * is poking at the power controls too.
 956	 */
 957	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
 958	WARN_ON(ctrl != state);
 959
 960	mutex_unlock(&dev_priv->rps.hw_lock);
 961
 962	return enabled;
 963}
 964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 965static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
 966{
 
 967	enum pipe pipe;
 968
 969	/*
 970	 * Enable the CRI clock source so we can get at the
 971	 * display and the reference clock for VGA
 972	 * hotplug / manual detection. Supposedly DSI also
 973	 * needs the ref clock up and running.
 974	 *
 975	 * CHV DPLL B/C have some issues if VGA mode is enabled.
 976	 */
 977	for_each_pipe(dev_priv->dev, pipe) {
 978		u32 val = I915_READ(DPLL(pipe));
 979
 980		val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
 981		if (pipe != PIPE_A)
 982			val |= DPLL_INTEGRATED_CRI_CLK_VLV;
 983
 984		I915_WRITE(DPLL(pipe), val);
 985	}
 986
 
 
 987	spin_lock_irq(&dev_priv->irq_lock);
 988	valleyview_enable_display_irqs(dev_priv);
 989	spin_unlock_irq(&dev_priv->irq_lock);
 990
 991	/*
 992	 * During driver initialization/resume we can avoid restoring the
 993	 * part of the HW/SW state that will be inited anyway explicitly.
 994	 */
 995	if (dev_priv->power_domains.initializing)
 996		return;
 997
 998	intel_hpd_init(dev_priv);
 999
1000	i915_redisable_vga_power_on(dev_priv->dev);
 
 
 
 
 
 
 
 
1001}
1002
1003static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
1004{
1005	spin_lock_irq(&dev_priv->irq_lock);
1006	valleyview_disable_display_irqs(dev_priv);
1007	spin_unlock_irq(&dev_priv->irq_lock);
1008
1009	/* make sure we're done processing display irqs */
1010	synchronize_irq(dev_priv->dev->irq);
1011
1012	vlv_power_sequencer_reset(dev_priv);
 
 
 
 
1013}
1014
1015static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
1016					  struct i915_power_well *power_well)
1017{
1018	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1019
1020	vlv_set_power_well(dev_priv, power_well, true);
1021
1022	vlv_display_power_well_init(dev_priv);
1023}
1024
1025static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
1026					   struct i915_power_well *power_well)
1027{
1028	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1029
1030	vlv_display_power_well_deinit(dev_priv);
1031
1032	vlv_set_power_well(dev_priv, power_well, false);
1033}
1034
1035static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1036					   struct i915_power_well *power_well)
1037{
1038	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1039
1040	/* since ref/cri clock was enabled */
1041	udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1042
1043	vlv_set_power_well(dev_priv, power_well, true);
1044
1045	/*
1046	 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1047	 *  6.	De-assert cmn_reset/side_reset. Same as VLV X0.
1048	 *   a.	GUnit 0x2110 bit[0] set to 1 (def 0)
1049	 *   b.	The other bits such as sfr settings / modesel may all
1050	 *	be set to 0.
1051	 *
1052	 * This should only be done on init and resume from S3 with
1053	 * both PLLs disabled, or we risk losing DPIO and PLL
1054	 * synchronization.
1055	 */
1056	I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1057}
1058
1059static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1060					    struct i915_power_well *power_well)
1061{
1062	enum pipe pipe;
1063
1064	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1065
1066	for_each_pipe(dev_priv, pipe)
1067		assert_pll_disabled(dev_priv, pipe);
1068
1069	/* Assert common reset */
1070	I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1071
1072	vlv_set_power_well(dev_priv, power_well, false);
1073}
1074
1075#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
1076
1077static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1078						 int power_well_id)
 
1079{
1080	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1081	int i;
1082
1083	for (i = 0; i < power_domains->power_well_count; i++) {
1084		struct i915_power_well *power_well;
1085
1086		power_well = &power_domains->power_wells[i];
1087		if (power_well->data == power_well_id)
1088			return power_well;
1089	}
1090
1091	return NULL;
1092}
1093
1094#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1095
1096static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1097{
1098	struct i915_power_well *cmn_bc =
1099		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1100	struct i915_power_well *cmn_d =
1101		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1102	u32 phy_control = dev_priv->chv_phy_control;
1103	u32 phy_status = 0;
1104	u32 phy_status_mask = 0xffffffff;
1105	u32 tmp;
1106
1107	/*
1108	 * The BIOS can leave the PHY is some weird state
1109	 * where it doesn't fully power down some parts.
1110	 * Disable the asserts until the PHY has been fully
1111	 * reset (ie. the power well has been disabled at
1112	 * least once).
1113	 */
1114	if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1115		phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1116				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1117				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1118				     PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1119				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1120				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1121
1122	if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1123		phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1124				     PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1125				     PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1126
1127	if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1128		phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1129
1130		/* this assumes override is only used to enable lanes */
1131		if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1132			phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1133
1134		if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1135			phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1136
1137		/* CL1 is on whenever anything is on in either channel */
1138		if (BITS_SET(phy_control,
1139			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1140			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1141			phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1142
1143		/*
1144		 * The DPLLB check accounts for the pipe B + port A usage
1145		 * with CL2 powered up but all the lanes in the second channel
1146		 * powered down.
1147		 */
1148		if (BITS_SET(phy_control,
1149			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1150		    (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1151			phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1152
1153		if (BITS_SET(phy_control,
1154			     PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1155			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1156		if (BITS_SET(phy_control,
1157			     PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1158			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1159
1160		if (BITS_SET(phy_control,
1161			     PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1162			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1163		if (BITS_SET(phy_control,
1164			     PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1165			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1166	}
1167
1168	if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1169		phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1170
1171		/* this assumes override is only used to enable lanes */
1172		if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1173			phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1174
1175		if (BITS_SET(phy_control,
1176			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1177			phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1178
1179		if (BITS_SET(phy_control,
1180			     PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1181			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1182		if (BITS_SET(phy_control,
1183			     PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1184			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1185	}
1186
1187	phy_status &= phy_status_mask;
1188
1189	/*
1190	 * The PHY may be busy with some initial calibration and whatnot,
1191	 * so the power state can take a while to actually change.
1192	 */
1193	if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
1194		WARN(phy_status != tmp,
1195		     "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1196		     tmp, phy_status, dev_priv->chv_phy_control);
 
 
 
 
1197}
1198
1199#undef BITS_SET
1200
1201static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1202					   struct i915_power_well *power_well)
1203{
1204	enum dpio_phy phy;
1205	enum pipe pipe;
1206	uint32_t tmp;
1207
1208	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1209		     power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1210
1211	if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1212		pipe = PIPE_A;
1213		phy = DPIO_PHY0;
1214	} else {
1215		pipe = PIPE_C;
1216		phy = DPIO_PHY1;
1217	}
1218
1219	/* since ref/cri clock was enabled */
1220	udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1221	vlv_set_power_well(dev_priv, power_well, true);
1222
1223	/* Poll for phypwrgood signal */
1224	if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
 
 
 
 
1225		DRM_ERROR("Display PHY %d is not power up\n", phy);
1226
1227	mutex_lock(&dev_priv->sb_lock);
1228
1229	/* Enable dynamic power down */
1230	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
1231	tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1232		DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
1233	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1234
1235	if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1236		tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1237		tmp |= DPIO_DYNPWRDOWNEN_CH1;
1238		vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
1239	} else {
1240		/*
1241		 * Force the non-existing CL2 off. BXT does this
1242		 * too, so maybe it saves some power even though
1243		 * CL2 doesn't exist?
1244		 */
1245		tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1246		tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1247		vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
1248	}
1249
1250	mutex_unlock(&dev_priv->sb_lock);
1251
1252	dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1253	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1254
1255	DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1256		      phy, dev_priv->chv_phy_control);
1257
1258	assert_chv_phy_status(dev_priv);
1259}
1260
1261static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1262					    struct i915_power_well *power_well)
1263{
1264	enum dpio_phy phy;
1265
1266	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1267		     power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1268
1269	if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1270		phy = DPIO_PHY0;
1271		assert_pll_disabled(dev_priv, PIPE_A);
1272		assert_pll_disabled(dev_priv, PIPE_B);
1273	} else {
1274		phy = DPIO_PHY1;
1275		assert_pll_disabled(dev_priv, PIPE_C);
1276	}
1277
1278	dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1279	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1280
1281	vlv_set_power_well(dev_priv, power_well, false);
1282
1283	DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1284		      phy, dev_priv->chv_phy_control);
1285
1286	/* PHY is fully reset now, so we can enable the PHY state asserts */
1287	dev_priv->chv_phy_assert[phy] = true;
1288
1289	assert_chv_phy_status(dev_priv);
1290}
1291
1292static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1293				     enum dpio_channel ch, bool override, unsigned int mask)
1294{
1295	enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1296	u32 reg, val, expected, actual;
1297
1298	/*
1299	 * The BIOS can leave the PHY is some weird state
1300	 * where it doesn't fully power down some parts.
1301	 * Disable the asserts until the PHY has been fully
1302	 * reset (ie. the power well has been disabled at
1303	 * least once).
1304	 */
1305	if (!dev_priv->chv_phy_assert[phy])
1306		return;
1307
1308	if (ch == DPIO_CH0)
1309		reg = _CHV_CMN_DW0_CH0;
1310	else
1311		reg = _CHV_CMN_DW6_CH1;
1312
1313	mutex_lock(&dev_priv->sb_lock);
1314	val = vlv_dpio_read(dev_priv, pipe, reg);
1315	mutex_unlock(&dev_priv->sb_lock);
1316
1317	/*
1318	 * This assumes !override is only used when the port is disabled.
1319	 * All lanes should power down even without the override when
1320	 * the port is disabled.
1321	 */
1322	if (!override || mask == 0xf) {
1323		expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1324		/*
1325		 * If CH1 common lane is not active anymore
1326		 * (eg. for pipe B DPLL) the entire channel will
1327		 * shut down, which causes the common lane registers
1328		 * to read as 0. That means we can't actually check
1329		 * the lane power down status bits, but as the entire
1330		 * register reads as 0 it's a good indication that the
1331		 * channel is indeed entirely powered down.
1332		 */
1333		if (ch == DPIO_CH1 && val == 0)
1334			expected = 0;
1335	} else if (mask != 0x0) {
1336		expected = DPIO_ANYDL_POWERDOWN;
1337	} else {
1338		expected = 0;
1339	}
1340
1341	if (ch == DPIO_CH0)
1342		actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1343	else
1344		actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1345	actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1346
1347	WARN(actual != expected,
1348	     "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1349	     !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1350	     !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1351	     reg, val);
1352}
1353
1354bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1355			  enum dpio_channel ch, bool override)
1356{
1357	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1358	bool was_override;
1359
1360	mutex_lock(&power_domains->lock);
1361
1362	was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1363
1364	if (override == was_override)
1365		goto out;
1366
1367	if (override)
1368		dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1369	else
1370		dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1371
1372	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1373
1374	DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1375		      phy, ch, dev_priv->chv_phy_control);
1376
1377	assert_chv_phy_status(dev_priv);
1378
1379out:
1380	mutex_unlock(&power_domains->lock);
1381
1382	return was_override;
1383}
1384
1385void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1386			     bool override, unsigned int mask)
1387{
1388	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1389	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1390	enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1391	enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1392
1393	mutex_lock(&power_domains->lock);
1394
1395	dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1396	dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1397
1398	if (override)
1399		dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1400	else
1401		dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1402
1403	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1404
1405	DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1406		      phy, ch, mask, dev_priv->chv_phy_control);
1407
1408	assert_chv_phy_status(dev_priv);
1409
1410	assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1411
1412	mutex_unlock(&power_domains->lock);
1413}
1414
1415static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1416					struct i915_power_well *power_well)
1417{
1418	enum pipe pipe = power_well->data;
1419	bool enabled;
1420	u32 state, ctrl;
1421
1422	mutex_lock(&dev_priv->rps.hw_lock);
1423
1424	state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1425	/*
1426	 * We only ever set the power-on and power-gate states, anything
1427	 * else is unexpected.
1428	 */
1429	WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1430	enabled = state == DP_SSS_PWR_ON(pipe);
1431
1432	/*
1433	 * A transient state at this point would mean some unexpected party
1434	 * is poking at the power controls too.
1435	 */
1436	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1437	WARN_ON(ctrl << 16 != state);
1438
1439	mutex_unlock(&dev_priv->rps.hw_lock);
1440
1441	return enabled;
1442}
1443
1444static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1445				    struct i915_power_well *power_well,
1446				    bool enable)
1447{
1448	enum pipe pipe = power_well->data;
1449	u32 state;
1450	u32 ctrl;
1451
1452	state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1453
1454	mutex_lock(&dev_priv->rps.hw_lock);
1455
1456#define COND \
1457	((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1458
1459	if (COND)
1460		goto out;
1461
1462	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1463	ctrl &= ~DP_SSC_MASK(pipe);
1464	ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1465	vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1466
1467	if (wait_for(COND, 100))
1468		DRM_ERROR("timeout setting power well state %08x (%08x)\n",
1469			  state,
1470			  vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1471
1472#undef COND
1473
1474out:
1475	mutex_unlock(&dev_priv->rps.hw_lock);
1476}
1477
1478static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1479					struct i915_power_well *power_well)
1480{
1481	WARN_ON_ONCE(power_well->data != PIPE_A);
1482
1483	chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1484}
1485
1486static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1487				       struct i915_power_well *power_well)
1488{
1489	WARN_ON_ONCE(power_well->data != PIPE_A);
1490
1491	chv_set_pipe_power_well(dev_priv, power_well, true);
1492
1493	vlv_display_power_well_init(dev_priv);
1494}
1495
1496static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1497					struct i915_power_well *power_well)
1498{
1499	WARN_ON_ONCE(power_well->data != PIPE_A);
1500
1501	vlv_display_power_well_deinit(dev_priv);
1502
1503	chv_set_pipe_power_well(dev_priv, power_well, false);
1504}
1505
1506static void
1507__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1508				 enum intel_display_power_domain domain)
1509{
1510	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1511	struct i915_power_well *power_well;
1512	int i;
1513
1514	for_each_power_well(i, power_well, BIT(domain), power_domains) {
1515		if (!power_well->count++)
1516			intel_power_well_enable(dev_priv, power_well);
1517	}
1518
1519	power_domains->domain_use_count[domain]++;
1520}
1521
1522/**
1523 * intel_display_power_get - grab a power domain reference
1524 * @dev_priv: i915 device instance
1525 * @domain: power domain to reference
1526 *
1527 * This function grabs a power domain reference for @domain and ensures that the
1528 * power domain and all its parents are powered up. Therefore users should only
1529 * grab a reference to the innermost power domain they need.
1530 *
1531 * Any power domain reference obtained by this function must have a symmetric
1532 * call to intel_display_power_put() to release the reference again.
1533 */
1534void intel_display_power_get(struct drm_i915_private *dev_priv,
1535			     enum intel_display_power_domain domain)
1536{
1537	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1538
1539	intel_runtime_pm_get(dev_priv);
1540
1541	mutex_lock(&power_domains->lock);
1542
1543	__intel_display_power_get_domain(dev_priv, domain);
1544
1545	mutex_unlock(&power_domains->lock);
1546}
1547
1548/**
1549 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
1550 * @dev_priv: i915 device instance
1551 * @domain: power domain to reference
1552 *
1553 * This function grabs a power domain reference for @domain and ensures that the
1554 * power domain and all its parents are powered up. Therefore users should only
1555 * grab a reference to the innermost power domain they need.
1556 *
1557 * Any power domain reference obtained by this function must have a symmetric
1558 * call to intel_display_power_put() to release the reference again.
1559 */
1560bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1561					enum intel_display_power_domain domain)
1562{
1563	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1564	bool is_enabled;
1565
1566	if (!intel_runtime_pm_get_if_in_use(dev_priv))
1567		return false;
1568
1569	mutex_lock(&power_domains->lock);
1570
1571	if (__intel_display_power_is_enabled(dev_priv, domain)) {
1572		__intel_display_power_get_domain(dev_priv, domain);
1573		is_enabled = true;
1574	} else {
1575		is_enabled = false;
1576	}
1577
1578	mutex_unlock(&power_domains->lock);
1579
1580	if (!is_enabled)
1581		intel_runtime_pm_put(dev_priv);
1582
1583	return is_enabled;
1584}
1585
1586/**
1587 * intel_display_power_put - release a power domain reference
1588 * @dev_priv: i915 device instance
1589 * @domain: power domain to reference
1590 *
1591 * This function drops the power domain reference obtained by
1592 * intel_display_power_get() and might power down the corresponding hardware
1593 * block right away if this is the last reference.
1594 */
1595void intel_display_power_put(struct drm_i915_private *dev_priv,
1596			     enum intel_display_power_domain domain)
1597{
1598	struct i915_power_domains *power_domains;
1599	struct i915_power_well *power_well;
1600	int i;
1601
1602	power_domains = &dev_priv->power_domains;
1603
1604	mutex_lock(&power_domains->lock);
1605
1606	WARN(!power_domains->domain_use_count[domain],
1607	     "Use count on domain %s is already zero\n",
1608	     intel_display_power_domain_str(domain));
1609	power_domains->domain_use_count[domain]--;
1610
1611	for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
1612		WARN(!power_well->count,
1613		     "Use count on power well %s is already zero",
1614		     power_well->name);
1615
1616		if (!--power_well->count)
1617			intel_power_well_disable(dev_priv, power_well);
1618	}
1619
1620	mutex_unlock(&power_domains->lock);
1621
1622	intel_runtime_pm_put(dev_priv);
1623}
1624
1625#define HSW_ALWAYS_ON_POWER_DOMAINS (			\
1626	BIT(POWER_DOMAIN_PIPE_A) |			\
1627	BIT(POWER_DOMAIN_TRANSCODER_EDP) |		\
1628	BIT(POWER_DOMAIN_PORT_DDI_A_LANES) |		\
1629	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1630	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1631	BIT(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
1632	BIT(POWER_DOMAIN_PORT_CRT) |			\
1633	BIT(POWER_DOMAIN_PLLS) |			\
1634	BIT(POWER_DOMAIN_AUX_A) |			\
1635	BIT(POWER_DOMAIN_AUX_B) |			\
1636	BIT(POWER_DOMAIN_AUX_C) |			\
1637	BIT(POWER_DOMAIN_AUX_D) |			\
1638	BIT(POWER_DOMAIN_GMBUS) |			\
1639	BIT(POWER_DOMAIN_INIT))
1640#define HSW_DISPLAY_POWER_DOMAINS (				\
1641	(POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) |	\
1642	BIT(POWER_DOMAIN_INIT))
1643
1644#define BDW_ALWAYS_ON_POWER_DOMAINS (			\
1645	HSW_ALWAYS_ON_POWER_DOMAINS |			\
1646	BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
1647#define BDW_DISPLAY_POWER_DOMAINS (				\
1648	(POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) |	\
1649	BIT(POWER_DOMAIN_INIT))
1650
1651#define VLV_ALWAYS_ON_POWER_DOMAINS	BIT(POWER_DOMAIN_INIT)
1652#define VLV_DISPLAY_POWER_DOMAINS	POWER_DOMAIN_MASK
1653
1654#define VLV_DPIO_CMN_BC_POWER_DOMAINS (		\
1655	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1656	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1657	BIT(POWER_DOMAIN_PORT_CRT) |		\
1658	BIT(POWER_DOMAIN_AUX_B) |		\
1659	BIT(POWER_DOMAIN_AUX_C) |		\
1660	BIT(POWER_DOMAIN_INIT))
1661
1662#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS (	\
1663	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1664	BIT(POWER_DOMAIN_AUX_B) |		\
1665	BIT(POWER_DOMAIN_INIT))
1666
1667#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS (	\
1668	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1669	BIT(POWER_DOMAIN_AUX_B) |		\
1670	BIT(POWER_DOMAIN_INIT))
1671
1672#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS (	\
1673	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1674	BIT(POWER_DOMAIN_AUX_C) |		\
1675	BIT(POWER_DOMAIN_INIT))
1676
1677#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS (	\
1678	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1679	BIT(POWER_DOMAIN_AUX_C) |		\
1680	BIT(POWER_DOMAIN_INIT))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1681
1682#define CHV_DPIO_CMN_BC_POWER_DOMAINS (		\
1683	BIT(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1684	BIT(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1685	BIT(POWER_DOMAIN_AUX_B) |		\
1686	BIT(POWER_DOMAIN_AUX_C) |		\
1687	BIT(POWER_DOMAIN_INIT))
1688
1689#define CHV_DPIO_CMN_D_POWER_DOMAINS (		\
1690	BIT(POWER_DOMAIN_PORT_DDI_D_LANES) |	\
1691	BIT(POWER_DOMAIN_AUX_D) |		\
1692	BIT(POWER_DOMAIN_INIT))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1693
1694static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1695	.sync_hw = i9xx_always_on_power_well_noop,
1696	.enable = i9xx_always_on_power_well_noop,
1697	.disable = i9xx_always_on_power_well_noop,
1698	.is_enabled = i9xx_always_on_power_well_enabled,
1699};
1700
1701static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1702	.sync_hw = chv_pipe_power_well_sync_hw,
1703	.enable = chv_pipe_power_well_enable,
1704	.disable = chv_pipe_power_well_disable,
1705	.is_enabled = chv_pipe_power_well_enabled,
1706};
1707
1708static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1709	.sync_hw = vlv_power_well_sync_hw,
1710	.enable = chv_dpio_cmn_power_well_enable,
1711	.disable = chv_dpio_cmn_power_well_disable,
1712	.is_enabled = vlv_power_well_enabled,
1713};
1714
1715static struct i915_power_well i9xx_always_on_power_well[] = {
1716	{
1717		.name = "always-on",
1718		.always_on = 1,
1719		.domains = POWER_DOMAIN_MASK,
1720		.ops = &i9xx_always_on_power_well_ops,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1721	},
1722};
1723
1724static const struct i915_power_well_ops hsw_power_well_ops = {
1725	.sync_hw = hsw_power_well_sync_hw,
1726	.enable = hsw_power_well_enable,
1727	.disable = hsw_power_well_disable,
1728	.is_enabled = hsw_power_well_enabled,
1729};
1730
1731static const struct i915_power_well_ops skl_power_well_ops = {
1732	.sync_hw = skl_power_well_sync_hw,
1733	.enable = skl_power_well_enable,
1734	.disable = skl_power_well_disable,
1735	.is_enabled = skl_power_well_enabled,
1736};
1737
1738static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1739	.sync_hw = gen9_dc_off_power_well_sync_hw,
1740	.enable = gen9_dc_off_power_well_enable,
1741	.disable = gen9_dc_off_power_well_disable,
1742	.is_enabled = gen9_dc_off_power_well_enabled,
1743};
1744
 
 
 
 
 
 
 
1745static struct i915_power_well hsw_power_wells[] = {
1746	{
1747		.name = "always-on",
1748		.always_on = 1,
1749		.domains = HSW_ALWAYS_ON_POWER_DOMAINS,
1750		.ops = &i9xx_always_on_power_well_ops,
 
1751	},
1752	{
1753		.name = "display",
1754		.domains = HSW_DISPLAY_POWER_DOMAINS,
1755		.ops = &hsw_power_well_ops,
 
 
 
 
1756	},
1757};
1758
1759static struct i915_power_well bdw_power_wells[] = {
1760	{
1761		.name = "always-on",
1762		.always_on = 1,
1763		.domains = BDW_ALWAYS_ON_POWER_DOMAINS,
1764		.ops = &i9xx_always_on_power_well_ops,
 
1765	},
1766	{
1767		.name = "display",
1768		.domains = BDW_DISPLAY_POWER_DOMAINS,
1769		.ops = &hsw_power_well_ops,
 
 
 
 
 
1770	},
1771};
1772
1773static const struct i915_power_well_ops vlv_display_power_well_ops = {
1774	.sync_hw = vlv_power_well_sync_hw,
1775	.enable = vlv_display_power_well_enable,
1776	.disable = vlv_display_power_well_disable,
1777	.is_enabled = vlv_power_well_enabled,
1778};
1779
1780static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1781	.sync_hw = vlv_power_well_sync_hw,
1782	.enable = vlv_dpio_cmn_power_well_enable,
1783	.disable = vlv_dpio_cmn_power_well_disable,
1784	.is_enabled = vlv_power_well_enabled,
1785};
1786
1787static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1788	.sync_hw = vlv_power_well_sync_hw,
1789	.enable = vlv_power_well_enable,
1790	.disable = vlv_power_well_disable,
1791	.is_enabled = vlv_power_well_enabled,
1792};
1793
1794static struct i915_power_well vlv_power_wells[] = {
1795	{
1796		.name = "always-on",
1797		.always_on = 1,
1798		.domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1799		.ops = &i9xx_always_on_power_well_ops,
1800		.data = PUNIT_POWER_WELL_ALWAYS_ON,
1801	},
1802	{
1803		.name = "display",
1804		.domains = VLV_DISPLAY_POWER_DOMAINS,
1805		.data = PUNIT_POWER_WELL_DISP2D,
1806		.ops = &vlv_display_power_well_ops,
1807	},
1808	{
1809		.name = "dpio-tx-b-01",
1810		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1811			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1812			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1813			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1814		.ops = &vlv_dpio_power_well_ops,
1815		.data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1816	},
1817	{
1818		.name = "dpio-tx-b-23",
1819		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1820			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1821			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1822			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1823		.ops = &vlv_dpio_power_well_ops,
1824		.data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1825	},
1826	{
1827		.name = "dpio-tx-c-01",
1828		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1829			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1830			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1831			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1832		.ops = &vlv_dpio_power_well_ops,
1833		.data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1834	},
1835	{
1836		.name = "dpio-tx-c-23",
1837		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1838			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1839			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1840			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1841		.ops = &vlv_dpio_power_well_ops,
1842		.data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1843	},
1844	{
1845		.name = "dpio-common",
1846		.domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1847		.data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1848		.ops = &vlv_dpio_cmn_power_well_ops,
1849	},
1850};
1851
1852static struct i915_power_well chv_power_wells[] = {
1853	{
1854		.name = "always-on",
1855		.always_on = 1,
1856		.domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1857		.ops = &i9xx_always_on_power_well_ops,
 
1858	},
1859	{
1860		.name = "display",
1861		/*
1862		 * Pipe A power well is the new disp2d well. Pipe B and C
1863		 * power wells don't actually exist. Pipe A power well is
1864		 * required for any pipe to work.
1865		 */
1866		.domains = VLV_DISPLAY_POWER_DOMAINS,
1867		.data = PIPE_A,
1868		.ops = &chv_pipe_power_well_ops,
1869	},
1870	{
1871		.name = "dpio-common-bc",
1872		.domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
1873		.data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1874		.ops = &chv_dpio_cmn_power_well_ops,
1875	},
1876	{
1877		.name = "dpio-common-d",
1878		.domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
1879		.data = PUNIT_POWER_WELL_DPIO_CMN_D,
1880		.ops = &chv_dpio_cmn_power_well_ops,
1881	},
1882};
1883
1884bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
1885				    int power_well_id)
1886{
1887	struct i915_power_well *power_well;
1888	bool ret;
1889
1890	power_well = lookup_power_well(dev_priv, power_well_id);
1891	ret = power_well->ops->is_enabled(dev_priv, power_well);
1892
1893	return ret;
1894}
1895
1896static struct i915_power_well skl_power_wells[] = {
1897	{
1898		.name = "always-on",
1899		.always_on = 1,
1900		.domains = SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1901		.ops = &i9xx_always_on_power_well_ops,
1902		.data = SKL_DISP_PW_ALWAYS_ON,
1903	},
1904	{
1905		.name = "power well 1",
1906		/* Handled by the DMC firmware */
1907		.domains = 0,
1908		.ops = &skl_power_well_ops,
1909		.data = SKL_DISP_PW_1,
 
 
 
1910	},
1911	{
1912		.name = "MISC IO power well",
1913		/* Handled by the DMC firmware */
1914		.domains = 0,
1915		.ops = &skl_power_well_ops,
1916		.data = SKL_DISP_PW_MISC_IO,
1917	},
1918	{
1919		.name = "DC off",
1920		.domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
1921		.ops = &gen9_dc_off_power_well_ops,
1922		.data = SKL_DISP_PW_DC_OFF,
1923	},
1924	{
1925		.name = "power well 2",
1926		.domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1927		.ops = &skl_power_well_ops,
1928		.data = SKL_DISP_PW_2,
 
 
 
 
 
1929	},
1930	{
1931		.name = "DDI A/E power well",
1932		.domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1933		.ops = &skl_power_well_ops,
1934		.data = SKL_DISP_PW_DDI_A_E,
1935	},
1936	{
1937		.name = "DDI B power well",
1938		.domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1939		.ops = &skl_power_well_ops,
1940		.data = SKL_DISP_PW_DDI_B,
1941	},
1942	{
1943		.name = "DDI C power well",
1944		.domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1945		.ops = &skl_power_well_ops,
1946		.data = SKL_DISP_PW_DDI_C,
1947	},
1948	{
1949		.name = "DDI D power well",
1950		.domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1951		.ops = &skl_power_well_ops,
1952		.data = SKL_DISP_PW_DDI_D,
1953	},
1954};
1955
1956void skl_pw1_misc_io_init(struct drm_i915_private *dev_priv)
1957{
1958	struct i915_power_well *well;
1959
1960	if (!(IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)))
1961		return;
1962
1963	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
1964	intel_power_well_enable(dev_priv, well);
1965
1966	well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
1967	intel_power_well_enable(dev_priv, well);
1968}
1969
1970void skl_pw1_misc_io_fini(struct drm_i915_private *dev_priv)
1971{
1972	struct i915_power_well *well;
1973
1974	if (!(IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)))
1975		return;
1976
1977	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
1978	intel_power_well_disable(dev_priv, well);
1979
1980	well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
1981	intel_power_well_disable(dev_priv, well);
1982}
1983
1984static struct i915_power_well bxt_power_wells[] = {
1985	{
1986		.name = "always-on",
1987		.always_on = 1,
1988		.domains = BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1989		.ops = &i9xx_always_on_power_well_ops,
 
1990	},
1991	{
1992		.name = "power well 1",
1993		.domains = BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS,
1994		.ops = &skl_power_well_ops,
1995		.data = SKL_DISP_PW_1,
 
 
 
1996	},
1997	{
1998		.name = "DC off",
1999		.domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
2000		.ops = &gen9_dc_off_power_well_ops,
2001		.data = SKL_DISP_PW_DC_OFF,
2002	},
2003	{
2004		.name = "power well 2",
2005		.domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2006		.ops = &skl_power_well_ops,
2007		.data = SKL_DISP_PW_2,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2008	},
2009};
2010
2011static int
2012sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
2013				   int disable_power_well)
2014{
2015	if (disable_power_well >= 0)
2016		return !!disable_power_well;
2017
2018	if (IS_BROXTON(dev_priv)) {
2019		DRM_DEBUG_KMS("Disabling display power well support\n");
2020		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2021	}
2022
2023	return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2024}
2025
2026#define set_power_wells(power_domains, __power_wells) ({		\
2027	(power_domains)->power_wells = (__power_wells);			\
2028	(power_domains)->power_well_count = ARRAY_SIZE(__power_wells);	\
2029})
2030
2031/**
2032 * intel_power_domains_init - initializes the power domain structures
2033 * @dev_priv: i915 device instance
2034 *
2035 * Initializes the power domain structures for @dev_priv depending upon the
2036 * supported platform.
2037 */
2038int intel_power_domains_init(struct drm_i915_private *dev_priv)
2039{
2040	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2041
2042	i915.disable_power_well = sanitize_disable_power_well_option(dev_priv,
2043						     i915.disable_power_well);
 
 
 
2044
2045	BUILD_BUG_ON(POWER_DOMAIN_NUM > 31);
2046
2047	mutex_init(&power_domains->lock);
2048
2049	/*
2050	 * The enabling order will be from lower to higher indexed wells,
2051	 * the disabling order is reversed.
2052	 */
2053	if (IS_HASWELL(dev_priv->dev)) {
2054		set_power_wells(power_domains, hsw_power_wells);
2055	} else if (IS_BROADWELL(dev_priv->dev)) {
2056		set_power_wells(power_domains, bdw_power_wells);
2057	} else if (IS_SKYLAKE(dev_priv->dev) || IS_KABYLAKE(dev_priv->dev)) {
2058		set_power_wells(power_domains, skl_power_wells);
2059	} else if (IS_BROXTON(dev_priv->dev)) {
 
 
 
 
 
 
 
 
 
 
 
 
2060		set_power_wells(power_domains, bxt_power_wells);
2061	} else if (IS_CHERRYVIEW(dev_priv->dev)) {
 
 
2062		set_power_wells(power_domains, chv_power_wells);
2063	} else if (IS_VALLEYVIEW(dev_priv->dev)) {
2064		set_power_wells(power_domains, vlv_power_wells);
 
 
2065	} else {
2066		set_power_wells(power_domains, i9xx_always_on_power_well);
2067	}
2068
 
 
2069	return 0;
2070}
2071
2072/**
2073 * intel_power_domains_fini - finalizes the power domain structures
2074 * @dev_priv: i915 device instance
2075 *
2076 * Finalizes the power domain structures for @dev_priv depending upon the
2077 * supported platform. This function also disables runtime pm and ensures that
2078 * the device stays powered up so that the driver can be reloaded.
2079 */
2080void intel_power_domains_fini(struct drm_i915_private *dev_priv)
2081{
2082	struct device *device = &dev_priv->dev->pdev->dev;
2083
2084	/*
2085	 * The i915.ko module is still not prepared to be loaded when
2086	 * the power well is not enabled, so just enable it in case
2087	 * we're going to unload/reload.
2088	 * The following also reacquires the RPM reference the core passed
2089	 * to the driver during loading, which is dropped in
2090	 * intel_runtime_pm_enable(). We have to hand back the control of the
2091	 * device to the core with this reference held.
2092	 */
2093	intel_display_set_init_power(dev_priv, true);
2094
2095	/* Remove the refcount we took to keep power well support disabled. */
2096	if (!i915.disable_power_well)
2097		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
2098
2099	/*
2100	 * Remove the refcount we took in intel_runtime_pm_enable() in case
2101	 * the platform doesn't support runtime PM.
2102	 */
2103	if (!HAS_RUNTIME_PM(dev_priv))
2104		pm_runtime_put(device);
2105}
2106
2107static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
2108{
2109	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2110	struct i915_power_well *power_well;
2111	int i;
2112
2113	mutex_lock(&power_domains->lock);
2114	for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
2115		power_well->ops->sync_hw(dev_priv, power_well);
2116		power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2117								     power_well);
2118	}
2119	mutex_unlock(&power_domains->lock);
2120}
2121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2122static void skl_display_core_init(struct drm_i915_private *dev_priv,
2123				  bool resume)
2124{
2125	struct i915_power_domains *power_domains = &dev_priv->power_domains;
 
2126	uint32_t val;
2127
2128	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2129
2130	/* enable PCH reset handshake */
2131	val = I915_READ(HSW_NDE_RSTWRN_OPT);
2132	I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2133
2134	/* enable PG1 and Misc I/O */
2135	mutex_lock(&power_domains->lock);
2136	skl_pw1_misc_io_init(dev_priv);
2137	mutex_unlock(&power_domains->lock);
2138
2139	if (!resume)
2140		return;
 
 
 
 
 
2141
2142	skl_init_cdclk(dev_priv);
2143
2144	if (dev_priv->csr.dmc_payload && intel_csr_load_program(dev_priv))
2145		gen9_set_dc_state_debugmask(dev_priv);
 
 
2146}
2147
2148static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2149{
2150	struct i915_power_domains *power_domains = &dev_priv->power_domains;
 
2151
2152	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2153
 
 
2154	skl_uninit_cdclk(dev_priv);
2155
2156	/* The spec doesn't call for removing the reset handshake flag */
2157	/* disable PG1 and Misc I/O */
 
2158	mutex_lock(&power_domains->lock);
2159	skl_pw1_misc_io_fini(dev_priv);
 
 
 
 
 
 
 
 
 
2160	mutex_unlock(&power_domains->lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2161}
2162
2163static void chv_phy_control_init(struct drm_i915_private *dev_priv)
2164{
2165	struct i915_power_well *cmn_bc =
2166		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2167	struct i915_power_well *cmn_d =
2168		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
2169
2170	/*
2171	 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
2172	 * workaround never ever read DISPLAY_PHY_CONTROL, and
2173	 * instead maintain a shadow copy ourselves. Use the actual
2174	 * power well state and lane status to reconstruct the
2175	 * expected initial value.
2176	 */
2177	dev_priv->chv_phy_control =
2178		PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
2179		PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
2180		PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
2181		PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
2182		PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
2183
2184	/*
2185	 * If all lanes are disabled we leave the override disabled
2186	 * with all power down bits cleared to match the state we
2187	 * would use after disabling the port. Otherwise enable the
2188	 * override and set the lane powerdown bits accding to the
2189	 * current lane status.
2190	 */
2191	if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
2192		uint32_t status = I915_READ(DPLL(PIPE_A));
2193		unsigned int mask;
2194
2195		mask = status & DPLL_PORTB_READY_MASK;
2196		if (mask == 0xf)
2197			mask = 0x0;
2198		else
2199			dev_priv->chv_phy_control |=
2200				PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
2201
2202		dev_priv->chv_phy_control |=
2203			PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
2204
2205		mask = (status & DPLL_PORTC_READY_MASK) >> 4;
2206		if (mask == 0xf)
2207			mask = 0x0;
2208		else
2209			dev_priv->chv_phy_control |=
2210				PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
2211
2212		dev_priv->chv_phy_control |=
2213			PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
2214
2215		dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
2216
2217		dev_priv->chv_phy_assert[DPIO_PHY0] = false;
2218	} else {
2219		dev_priv->chv_phy_assert[DPIO_PHY0] = true;
2220	}
2221
2222	if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
2223		uint32_t status = I915_READ(DPIO_PHY_STATUS);
2224		unsigned int mask;
2225
2226		mask = status & DPLL_PORTD_READY_MASK;
2227
2228		if (mask == 0xf)
2229			mask = 0x0;
2230		else
2231			dev_priv->chv_phy_control |=
2232				PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
2233
2234		dev_priv->chv_phy_control |=
2235			PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
2236
2237		dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
2238
2239		dev_priv->chv_phy_assert[DPIO_PHY1] = false;
2240	} else {
2241		dev_priv->chv_phy_assert[DPIO_PHY1] = true;
2242	}
2243
2244	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
2245
2246	DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
2247		      dev_priv->chv_phy_control);
2248}
2249
2250static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
2251{
2252	struct i915_power_well *cmn =
2253		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2254	struct i915_power_well *disp2d =
2255		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
2256
2257	/* If the display might be already active skip this */
2258	if (cmn->ops->is_enabled(dev_priv, cmn) &&
2259	    disp2d->ops->is_enabled(dev_priv, disp2d) &&
2260	    I915_READ(DPIO_CTL) & DPIO_CMNRST)
2261		return;
2262
2263	DRM_DEBUG_KMS("toggling display PHY side reset\n");
2264
2265	/* cmnlane needs DPLL registers */
2266	disp2d->ops->enable(dev_priv, disp2d);
2267
2268	/*
2269	 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
2270	 * Need to assert and de-assert PHY SB reset by gating the
2271	 * common lane power, then un-gating it.
2272	 * Simply ungating isn't enough to reset the PHY enough to get
2273	 * ports and lanes running.
2274	 */
2275	cmn->ops->disable(dev_priv, cmn);
2276}
2277
2278/**
2279 * intel_power_domains_init_hw - initialize hardware power domain state
2280 * @dev_priv: i915 device instance
 
2281 *
2282 * This function initializes the hardware power domain state and enables all
2283 * power domains using intel_display_set_init_power().
 
 
 
2284 */
2285void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
2286{
2287	struct drm_device *dev = dev_priv->dev;
2288	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2289
2290	power_domains->initializing = true;
2291
2292	if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
 
 
 
 
2293		skl_display_core_init(dev_priv, resume);
2294	} else if (IS_CHERRYVIEW(dev)) {
 
 
2295		mutex_lock(&power_domains->lock);
2296		chv_phy_control_init(dev_priv);
2297		mutex_unlock(&power_domains->lock);
2298	} else if (IS_VALLEYVIEW(dev)) {
2299		mutex_lock(&power_domains->lock);
2300		vlv_cmnlane_wa(dev_priv);
2301		mutex_unlock(&power_domains->lock);
2302	}
2303
2304	/* For now, we need the power well to be always enabled. */
2305	intel_display_set_init_power(dev_priv, true);
2306	/* Disable power support if the user asked so. */
2307	if (!i915.disable_power_well)
2308		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
2309	intel_power_domains_sync_hw(dev_priv);
2310	power_domains->initializing = false;
2311}
2312
2313/**
2314 * intel_power_domains_suspend - suspend power domain state
2315 * @dev_priv: i915 device instance
2316 *
2317 * This function prepares the hardware power domain state before entering
2318 * system suspend. It must be paired with intel_power_domains_init_hw().
2319 */
2320void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
2321{
2322	/*
2323	 * Even if power well support was disabled we still want to disable
2324	 * power wells while we are system suspended.
2325	 */
2326	if (!i915.disable_power_well)
2327		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
2328
2329	if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
 
 
 
 
2330		skl_display_core_uninit(dev_priv);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2331}
2332
2333/**
2334 * intel_runtime_pm_get - grab a runtime pm reference
2335 * @dev_priv: i915 device instance
2336 *
2337 * This function grabs a device-level runtime pm reference (mostly used for GEM
2338 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2339 *
2340 * Any runtime pm reference obtained by this function must have a symmetric
2341 * call to intel_runtime_pm_put() to release the reference again.
2342 */
2343void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2344{
2345	struct drm_device *dev = dev_priv->dev;
2346	struct device *device = &dev->pdev->dev;
 
2347
2348	pm_runtime_get_sync(device);
 
2349
2350	atomic_inc(&dev_priv->pm.wakeref_count);
2351	assert_rpm_wakelock_held(dev_priv);
2352}
2353
2354/**
2355 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
2356 * @dev_priv: i915 device instance
2357 *
2358 * This function grabs a device-level runtime pm reference if the device is
2359 * already in use and ensures that it is powered up.
 
2360 *
2361 * Any runtime pm reference obtained by this function must have a symmetric
2362 * call to intel_runtime_pm_put() to release the reference again.
 
 
2363 */
2364bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
2365{
2366	struct drm_device *dev = dev_priv->dev;
2367	struct device *device = &dev->pdev->dev;
2368
2369	if (IS_ENABLED(CONFIG_PM)) {
2370		int ret = pm_runtime_get_if_in_use(device);
 
2371
2372		/*
2373		 * In cases runtime PM is disabled by the RPM core and we get
2374		 * an -EINVAL return value we are not supposed to call this
2375		 * function, since the power state is undefined. This applies
2376		 * atm to the late/early system suspend/resume handlers.
2377		 */
2378		WARN_ON_ONCE(ret < 0);
2379		if (ret <= 0)
2380			return false;
2381	}
2382
2383	atomic_inc(&dev_priv->pm.wakeref_count);
2384	assert_rpm_wakelock_held(dev_priv);
2385
2386	return true;
2387}
2388
2389/**
2390 * intel_runtime_pm_get_noresume - grab a runtime pm reference
2391 * @dev_priv: i915 device instance
2392 *
2393 * This function grabs a device-level runtime pm reference (mostly used for GEM
2394 * code to ensure the GTT or GT is on).
2395 *
2396 * It will _not_ power up the device but instead only check that it's powered
2397 * on.  Therefore it is only valid to call this functions from contexts where
2398 * the device is known to be powered up and where trying to power it up would
2399 * result in hilarity and deadlocks. That pretty much means only the system
2400 * suspend/resume code where this is used to grab runtime pm references for
2401 * delayed setup down in work items.
2402 *
2403 * Any runtime pm reference obtained by this function must have a symmetric
2404 * call to intel_runtime_pm_put() to release the reference again.
2405 */
2406void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
2407{
2408	struct drm_device *dev = dev_priv->dev;
2409	struct device *device = &dev->pdev->dev;
2410
2411	assert_rpm_wakelock_held(dev_priv);
2412	pm_runtime_get_noresume(device);
2413
2414	atomic_inc(&dev_priv->pm.wakeref_count);
2415}
2416
2417/**
2418 * intel_runtime_pm_put - release a runtime pm reference
2419 * @dev_priv: i915 device instance
2420 *
2421 * This function drops the device-level runtime pm reference obtained by
2422 * intel_runtime_pm_get() and might power down the corresponding
2423 * hardware block right away if this is the last reference.
2424 */
2425void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
2426{
2427	struct drm_device *dev = dev_priv->dev;
2428	struct device *device = &dev->pdev->dev;
2429
2430	assert_rpm_wakelock_held(dev_priv);
2431	if (atomic_dec_and_test(&dev_priv->pm.wakeref_count))
2432		atomic_inc(&dev_priv->pm.atomic_seq);
2433
2434	pm_runtime_mark_last_busy(device);
2435	pm_runtime_put_autosuspend(device);
2436}
2437
2438/**
2439 * intel_runtime_pm_enable - enable runtime pm
2440 * @dev_priv: i915 device instance
2441 *
2442 * This function enables runtime pm at the end of the driver load sequence.
2443 *
2444 * Note that this function does currently not enable runtime pm for the
2445 * subordinate display power domains. That is only done on the first modeset
2446 * using intel_display_set_init_power().
2447 */
2448void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
2449{
2450	struct drm_device *dev = dev_priv->dev;
2451	struct device *device = &dev->pdev->dev;
2452
2453	pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
2454	pm_runtime_mark_last_busy(device);
2455
2456	/*
2457	 * Take a permanent reference to disable the RPM functionality and drop
2458	 * it only when unloading the driver. Use the low level get/put helpers,
2459	 * so the driver's own RPM reference tracking asserts also work on
2460	 * platforms without RPM support.
2461	 */
2462	if (!HAS_RUNTIME_PM(dev)) {
2463		pm_runtime_dont_use_autosuspend(device);
2464		pm_runtime_get_sync(device);
 
 
 
2465	} else {
2466		pm_runtime_use_autosuspend(device);
2467	}
2468
2469	/*
2470	 * The core calls the driver load handler with an RPM reference held.
2471	 * We drop that here and will reacquire it during unloading in
2472	 * intel_power_domains_fini().
2473	 */
2474	pm_runtime_put_autosuspend(device);
2475}
2476
v4.17
   1/*
   2 * Copyright © 2012-2014 Intel Corporation
   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 (including the next
  12 * paragraph) shall be included in all copies or substantial portions of the
  13 * Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21 * IN THE SOFTWARE.
  22 *
  23 * Authors:
  24 *    Eugeni Dodonov <eugeni.dodonov@intel.com>
  25 *    Daniel Vetter <daniel.vetter@ffwll.ch>
  26 *
  27 */
  28
  29#include <linux/pm_runtime.h>
  30#include <linux/vgaarb.h>
  31
  32#include "i915_drv.h"
  33#include "intel_drv.h"
  34
  35/**
  36 * DOC: runtime pm
  37 *
  38 * The i915 driver supports dynamic enabling and disabling of entire hardware
  39 * blocks at runtime. This is especially important on the display side where
  40 * software is supposed to control many power gates manually on recent hardware,
  41 * since on the GT side a lot of the power management is done by the hardware.
  42 * But even there some manual control at the device level is required.
  43 *
  44 * Since i915 supports a diverse set of platforms with a unified codebase and
  45 * hardware engineers just love to shuffle functionality around between power
  46 * domains there's a sizeable amount of indirection required. This file provides
  47 * generic functions to the driver for grabbing and releasing references for
  48 * abstract power domains. It then maps those to the actual power wells
  49 * present for a given platform.
  50 */
  51
 
 
 
 
 
 
 
 
 
 
 
 
 
  52bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
  53					 enum i915_power_well_id power_well_id);
  54
  55static struct i915_power_well *
  56lookup_power_well(struct drm_i915_private *dev_priv,
  57		  enum i915_power_well_id power_well_id);
  58
  59const char *
  60intel_display_power_domain_str(enum intel_display_power_domain domain)
  61{
  62	switch (domain) {
  63	case POWER_DOMAIN_PIPE_A:
  64		return "PIPE_A";
  65	case POWER_DOMAIN_PIPE_B:
  66		return "PIPE_B";
  67	case POWER_DOMAIN_PIPE_C:
  68		return "PIPE_C";
  69	case POWER_DOMAIN_PIPE_A_PANEL_FITTER:
  70		return "PIPE_A_PANEL_FITTER";
  71	case POWER_DOMAIN_PIPE_B_PANEL_FITTER:
  72		return "PIPE_B_PANEL_FITTER";
  73	case POWER_DOMAIN_PIPE_C_PANEL_FITTER:
  74		return "PIPE_C_PANEL_FITTER";
  75	case POWER_DOMAIN_TRANSCODER_A:
  76		return "TRANSCODER_A";
  77	case POWER_DOMAIN_TRANSCODER_B:
  78		return "TRANSCODER_B";
  79	case POWER_DOMAIN_TRANSCODER_C:
  80		return "TRANSCODER_C";
  81	case POWER_DOMAIN_TRANSCODER_EDP:
  82		return "TRANSCODER_EDP";
  83	case POWER_DOMAIN_TRANSCODER_DSI_A:
  84		return "TRANSCODER_DSI_A";
  85	case POWER_DOMAIN_TRANSCODER_DSI_C:
  86		return "TRANSCODER_DSI_C";
  87	case POWER_DOMAIN_PORT_DDI_A_LANES:
  88		return "PORT_DDI_A_LANES";
  89	case POWER_DOMAIN_PORT_DDI_B_LANES:
  90		return "PORT_DDI_B_LANES";
  91	case POWER_DOMAIN_PORT_DDI_C_LANES:
  92		return "PORT_DDI_C_LANES";
  93	case POWER_DOMAIN_PORT_DDI_D_LANES:
  94		return "PORT_DDI_D_LANES";
  95	case POWER_DOMAIN_PORT_DDI_E_LANES:
  96		return "PORT_DDI_E_LANES";
  97	case POWER_DOMAIN_PORT_DDI_F_LANES:
  98		return "PORT_DDI_F_LANES";
  99	case POWER_DOMAIN_PORT_DDI_A_IO:
 100		return "PORT_DDI_A_IO";
 101	case POWER_DOMAIN_PORT_DDI_B_IO:
 102		return "PORT_DDI_B_IO";
 103	case POWER_DOMAIN_PORT_DDI_C_IO:
 104		return "PORT_DDI_C_IO";
 105	case POWER_DOMAIN_PORT_DDI_D_IO:
 106		return "PORT_DDI_D_IO";
 107	case POWER_DOMAIN_PORT_DDI_E_IO:
 108		return "PORT_DDI_E_IO";
 109	case POWER_DOMAIN_PORT_DDI_F_IO:
 110		return "PORT_DDI_F_IO";
 111	case POWER_DOMAIN_PORT_DSI:
 112		return "PORT_DSI";
 113	case POWER_DOMAIN_PORT_CRT:
 114		return "PORT_CRT";
 115	case POWER_DOMAIN_PORT_OTHER:
 116		return "PORT_OTHER";
 117	case POWER_DOMAIN_VGA:
 118		return "VGA";
 119	case POWER_DOMAIN_AUDIO:
 120		return "AUDIO";
 121	case POWER_DOMAIN_PLLS:
 122		return "PLLS";
 123	case POWER_DOMAIN_AUX_A:
 124		return "AUX_A";
 125	case POWER_DOMAIN_AUX_B:
 126		return "AUX_B";
 127	case POWER_DOMAIN_AUX_C:
 128		return "AUX_C";
 129	case POWER_DOMAIN_AUX_D:
 130		return "AUX_D";
 131	case POWER_DOMAIN_AUX_F:
 132		return "AUX_F";
 133	case POWER_DOMAIN_AUX_IO_A:
 134		return "AUX_IO_A";
 135	case POWER_DOMAIN_GMBUS:
 136		return "GMBUS";
 137	case POWER_DOMAIN_INIT:
 138		return "INIT";
 139	case POWER_DOMAIN_MODESET:
 140		return "MODESET";
 141	case POWER_DOMAIN_GT_IRQ:
 142		return "GT_IRQ";
 143	default:
 144		MISSING_CASE(domain);
 145		return "?";
 146	}
 147}
 148
 149static void intel_power_well_enable(struct drm_i915_private *dev_priv,
 150				    struct i915_power_well *power_well)
 151{
 152	DRM_DEBUG_KMS("enabling %s\n", power_well->name);
 153	power_well->ops->enable(dev_priv, power_well);
 154	power_well->hw_enabled = true;
 155}
 156
 157static void intel_power_well_disable(struct drm_i915_private *dev_priv,
 158				     struct i915_power_well *power_well)
 159{
 160	DRM_DEBUG_KMS("disabling %s\n", power_well->name);
 161	power_well->hw_enabled = false;
 162	power_well->ops->disable(dev_priv, power_well);
 163}
 164
 165static void intel_power_well_get(struct drm_i915_private *dev_priv,
 166				 struct i915_power_well *power_well)
 
 
 
 
 
 167{
 168	if (!power_well->count++)
 169		intel_power_well_enable(dev_priv, power_well);
 170}
 171
 172static void intel_power_well_put(struct drm_i915_private *dev_priv,
 173				 struct i915_power_well *power_well)
 174{
 175	WARN(!power_well->count, "Use count on power well %s is already zero",
 176	     power_well->name);
 177
 178	if (!--power_well->count)
 179		intel_power_well_disable(dev_priv, power_well);
 180}
 181
 182/**
 183 * __intel_display_power_is_enabled - unlocked check for a power domain
 184 * @dev_priv: i915 device instance
 185 * @domain: power domain to check
 186 *
 187 * This is the unlocked version of intel_display_power_is_enabled() and should
 188 * only be used from error capture and recovery code where deadlocks are
 189 * possible.
 190 *
 191 * Returns:
 192 * True when the power domain is enabled, false otherwise.
 193 */
 194bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
 195				      enum intel_display_power_domain domain)
 196{
 
 197	struct i915_power_well *power_well;
 198	bool is_enabled;
 
 199
 200	if (dev_priv->runtime_pm.suspended)
 201		return false;
 202
 
 
 203	is_enabled = true;
 204
 205	for_each_power_domain_well_rev(dev_priv, power_well, BIT_ULL(domain)) {
 206		if (power_well->always_on)
 207			continue;
 208
 209		if (!power_well->hw_enabled) {
 210			is_enabled = false;
 211			break;
 212		}
 213	}
 214
 215	return is_enabled;
 216}
 217
 218/**
 219 * intel_display_power_is_enabled - check for a power domain
 220 * @dev_priv: i915 device instance
 221 * @domain: power domain to check
 222 *
 223 * This function can be used to check the hw power domain state. It is mostly
 224 * used in hardware state readout functions. Everywhere else code should rely
 225 * upon explicit power domain reference counting to ensure that the hardware
 226 * block is powered up before accessing it.
 227 *
 228 * Callers must hold the relevant modesetting locks to ensure that concurrent
 229 * threads can't disable the power well while the caller tries to read a few
 230 * registers.
 231 *
 232 * Returns:
 233 * True when the power domain is enabled, false otherwise.
 234 */
 235bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
 236				    enum intel_display_power_domain domain)
 237{
 238	struct i915_power_domains *power_domains;
 239	bool ret;
 240
 241	power_domains = &dev_priv->power_domains;
 242
 243	mutex_lock(&power_domains->lock);
 244	ret = __intel_display_power_is_enabled(dev_priv, domain);
 245	mutex_unlock(&power_domains->lock);
 246
 247	return ret;
 248}
 249
 250/**
 251 * intel_display_set_init_power - set the initial power domain state
 252 * @dev_priv: i915 device instance
 253 * @enable: whether to enable or disable the initial power domain state
 254 *
 255 * For simplicity our driver load/unload and system suspend/resume code assumes
 256 * that all power domains are always enabled. This functions controls the state
 257 * of this little hack. While the initial power domain state is enabled runtime
 258 * pm is effectively disabled.
 259 */
 260void intel_display_set_init_power(struct drm_i915_private *dev_priv,
 261				  bool enable)
 262{
 263	if (dev_priv->power_domains.init_power_on == enable)
 264		return;
 265
 266	if (enable)
 267		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
 268	else
 269		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
 270
 271	dev_priv->power_domains.init_power_on = enable;
 272}
 273
 274/*
 275 * Starting with Haswell, we have a "Power Down Well" that can be turned off
 276 * when not needed anymore. We have 4 registers that can request the power well
 277 * to be enabled, and it will only be disabled if none of the registers is
 278 * requesting it to be enabled.
 279 */
 280static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv,
 281				       u8 irq_pipe_mask, bool has_vga)
 282{
 283	struct pci_dev *pdev = dev_priv->drm.pdev;
 284
 285	/*
 286	 * After we re-enable the power well, if we touch VGA register 0x3d5
 287	 * we'll get unclaimed register interrupts. This stops after we write
 288	 * anything to the VGA MSR register. The vgacon module uses this
 289	 * register all the time, so if we unbind our driver and, as a
 290	 * consequence, bind vgacon, we'll get stuck in an infinite loop at
 291	 * console_unlock(). So make here we touch the VGA MSR register, making
 292	 * sure vgacon can keep working normally without triggering interrupts
 293	 * and error messages.
 294	 */
 295	if (has_vga) {
 296		vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
 297		outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
 298		vga_put(pdev, VGA_RSRC_LEGACY_IO);
 299	}
 300
 301	if (irq_pipe_mask)
 302		gen8_irq_power_well_post_enable(dev_priv, irq_pipe_mask);
 
 303}
 304
 305static void hsw_power_well_pre_disable(struct drm_i915_private *dev_priv,
 306				       u8 irq_pipe_mask)
 307{
 308	if (irq_pipe_mask)
 309		gen8_irq_power_well_pre_disable(dev_priv, irq_pipe_mask);
 
 310}
 311
 312
 313static void hsw_wait_for_power_well_enable(struct drm_i915_private *dev_priv,
 314					   struct i915_power_well *power_well)
 315{
 316	enum i915_power_well_id id = power_well->id;
 317
 318	/* Timeout for PW1:10 us, AUX:not specified, other PWs:20 us. */
 319	WARN_ON(intel_wait_for_register(dev_priv,
 320					HSW_PWR_WELL_CTL_DRIVER(id),
 321					HSW_PWR_WELL_CTL_STATE(id),
 322					HSW_PWR_WELL_CTL_STATE(id),
 323					1));
 324}
 325
 326static u32 hsw_power_well_requesters(struct drm_i915_private *dev_priv,
 327				     enum i915_power_well_id id)
 328{
 329	u32 req_mask = HSW_PWR_WELL_CTL_REQ(id);
 330	u32 ret;
 331
 332	ret = I915_READ(HSW_PWR_WELL_CTL_BIOS(id)) & req_mask ? 1 : 0;
 333	ret |= I915_READ(HSW_PWR_WELL_CTL_DRIVER(id)) & req_mask ? 2 : 0;
 334	ret |= I915_READ(HSW_PWR_WELL_CTL_KVMR) & req_mask ? 4 : 0;
 335	ret |= I915_READ(HSW_PWR_WELL_CTL_DEBUG(id)) & req_mask ? 8 : 0;
 336
 337	return ret;
 
 
 338}
 339
 340static void hsw_wait_for_power_well_disable(struct drm_i915_private *dev_priv,
 341					    struct i915_power_well *power_well)
 342{
 343	enum i915_power_well_id id = power_well->id;
 344	bool disabled;
 345	u32 reqs;
 346
 347	/*
 348	 * Bspec doesn't require waiting for PWs to get disabled, but still do
 349	 * this for paranoia. The known cases where a PW will be forced on:
 350	 * - a KVMR request on any power well via the KVMR request register
 351	 * - a DMC request on PW1 and MISC_IO power wells via the BIOS and
 352	 *   DEBUG request registers
 353	 * Skip the wait in case any of the request bits are set and print a
 354	 * diagnostic message.
 355	 */
 356	wait_for((disabled = !(I915_READ(HSW_PWR_WELL_CTL_DRIVER(id)) &
 357			       HSW_PWR_WELL_CTL_STATE(id))) ||
 358		 (reqs = hsw_power_well_requesters(dev_priv, id)), 1);
 359	if (disabled)
 360		return;
 361
 362	DRM_DEBUG_KMS("%s forced on (bios:%d driver:%d kvmr:%d debug:%d)\n",
 363		      power_well->name,
 364		      !!(reqs & 1), !!(reqs & 2), !!(reqs & 4), !!(reqs & 8));
 365}
 366
 367static void gen9_wait_for_power_well_fuses(struct drm_i915_private *dev_priv,
 368					   enum skl_power_gate pg)
 369{
 370	/* Timeout 5us for PG#0, for other PGs 1us */
 371	WARN_ON(intel_wait_for_register(dev_priv, SKL_FUSE_STATUS,
 372					SKL_FUSE_PG_DIST_STATUS(pg),
 373					SKL_FUSE_PG_DIST_STATUS(pg), 1));
 374}
 375
 376static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
 377				  struct i915_power_well *power_well)
 378{
 379	enum i915_power_well_id id = power_well->id;
 380	bool wait_fuses = power_well->hsw.has_fuses;
 381	enum skl_power_gate uninitialized_var(pg);
 382	u32 val;
 383
 384	if (wait_fuses) {
 385		pg = SKL_PW_TO_PG(id);
 386		/*
 387		 * For PW1 we have to wait both for the PW0/PG0 fuse state
 388		 * before enabling the power well and PW1/PG1's own fuse
 389		 * state after the enabling. For all other power wells with
 390		 * fuses we only have to wait for that PW/PG's fuse state
 391		 * after the enabling.
 392		 */
 393		if (pg == SKL_PG1)
 394			gen9_wait_for_power_well_fuses(dev_priv, SKL_PG0);
 395	}
 
 
 
 
 396
 397	val = I915_READ(HSW_PWR_WELL_CTL_DRIVER(id));
 398	I915_WRITE(HSW_PWR_WELL_CTL_DRIVER(id), val | HSW_PWR_WELL_CTL_REQ(id));
 399	hsw_wait_for_power_well_enable(dev_priv, power_well);
 400
 401	/* Display WA #1178: cnl */
 402	if (IS_CANNONLAKE(dev_priv) &&
 403	    (id == CNL_DISP_PW_AUX_B || id == CNL_DISP_PW_AUX_C ||
 404	     id == CNL_DISP_PW_AUX_D || id == CNL_DISP_PW_AUX_F)) {
 405		val = I915_READ(CNL_AUX_ANAOVRD1(id));
 406		val |= CNL_AUX_ANAOVRD1_ENABLE | CNL_AUX_ANAOVRD1_LDO_BYPASS;
 407		I915_WRITE(CNL_AUX_ANAOVRD1(id), val);
 408	}
 409
 410	if (wait_fuses)
 411		gen9_wait_for_power_well_fuses(dev_priv, pg);
 412
 413	hsw_power_well_post_enable(dev_priv, power_well->hsw.irq_pipe_mask,
 414				   power_well->hsw.has_vga);
 415}
 416
 417static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
 418				   struct i915_power_well *power_well)
 419{
 420	enum i915_power_well_id id = power_well->id;
 421	u32 val;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 422
 423	hsw_power_well_pre_disable(dev_priv, power_well->hsw.irq_pipe_mask);
 424
 425	val = I915_READ(HSW_PWR_WELL_CTL_DRIVER(id));
 426	I915_WRITE(HSW_PWR_WELL_CTL_DRIVER(id),
 427		   val & ~HSW_PWR_WELL_CTL_REQ(id));
 428	hsw_wait_for_power_well_disable(dev_priv, power_well);
 429}
 430
 431/*
 432 * We should only use the power well if we explicitly asked the hardware to
 433 * enable it, so check if it's enabled and also check if we've requested it to
 434 * be enabled.
 435 */
 436static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
 437				   struct i915_power_well *power_well)
 438{
 439	enum i915_power_well_id id = power_well->id;
 440	u32 mask = HSW_PWR_WELL_CTL_REQ(id) | HSW_PWR_WELL_CTL_STATE(id);
 441
 442	return (I915_READ(HSW_PWR_WELL_CTL_DRIVER(id)) & mask) == mask;
 443}
 
 
 
 
 
 
 
 
 
 
 
 
 
 444
 445static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
 446{
 447	enum i915_power_well_id id = SKL_DISP_PW_2;
 448
 449	WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
 450		  "DC9 already programmed to be enabled.\n");
 451	WARN_ONCE(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
 452		  "DC5 still not disabled to enable DC9.\n");
 453	WARN_ONCE(I915_READ(HSW_PWR_WELL_CTL_DRIVER(id)) &
 454		  HSW_PWR_WELL_CTL_REQ(id),
 455		  "Power well 2 on.\n");
 456	WARN_ONCE(intel_irqs_enabled(dev_priv),
 457		  "Interrupts not disabled yet.\n");
 458
 459	 /*
 460	  * TODO: check for the following to verify the conditions to enter DC9
 461	  * state are satisfied:
 462	  * 1] Check relevant display engine registers to verify if mode set
 463	  * disable sequence was followed.
 464	  * 2] Check if display uninitialize sequence is initialized.
 465	  */
 466}
 467
 468static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
 469{
 470	WARN_ONCE(intel_irqs_enabled(dev_priv),
 471		  "Interrupts not disabled yet.\n");
 472	WARN_ONCE(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
 473		  "DC5 still not disabled.\n");
 
 474
 475	 /*
 476	  * TODO: check for the following to verify DC9 state was indeed
 477	  * entered before programming to disable it:
 478	  * 1] Check relevant display engine registers to verify if mode
 479	  *  set disable sequence was followed.
 480	  * 2] Check if display uninitialize sequence is initialized.
 481	  */
 482}
 483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 484static void gen9_write_dc_state(struct drm_i915_private *dev_priv,
 485				u32 state)
 486{
 487	int rewrites = 0;
 488	int rereads = 0;
 489	u32 v;
 490
 491	I915_WRITE(DC_STATE_EN, state);
 492
 493	/* It has been observed that disabling the dc6 state sometimes
 494	 * doesn't stick and dmc keeps returning old value. Make sure
 495	 * the write really sticks enough times and also force rewrite until
 496	 * we are confident that state is exactly what we want.
 497	 */
 498	do  {
 499		v = I915_READ(DC_STATE_EN);
 500
 501		if (v != state) {
 502			I915_WRITE(DC_STATE_EN, state);
 503			rewrites++;
 504			rereads = 0;
 505		} else if (rereads++ > 5) {
 506			break;
 507		}
 508
 509	} while (rewrites < 100);
 510
 511	if (v != state)
 512		DRM_ERROR("Writing dc state to 0x%x failed, now 0x%x\n",
 513			  state, v);
 514
 515	/* Most of the times we need one retry, avoid spam */
 516	if (rewrites > 1)
 517		DRM_DEBUG_KMS("Rewrote dc state to 0x%x %d times\n",
 518			      state, rewrites);
 519}
 520
 521static u32 gen9_dc_mask(struct drm_i915_private *dev_priv)
 522{
 523	u32 mask;
 
 524
 525	mask = DC_STATE_EN_UPTO_DC5;
 526	if (IS_GEN9_LP(dev_priv))
 527		mask |= DC_STATE_EN_DC9;
 528	else
 529		mask |= DC_STATE_EN_UPTO_DC6;
 530
 531	return mask;
 532}
 533
 534void gen9_sanitize_dc_state(struct drm_i915_private *dev_priv)
 535{
 536	u32 val;
 537
 538	val = I915_READ(DC_STATE_EN) & gen9_dc_mask(dev_priv);
 539
 540	DRM_DEBUG_KMS("Resetting DC state tracking from %02x to %02x\n",
 541		      dev_priv->csr.dc_state, val);
 542	dev_priv->csr.dc_state = val;
 543}
 544
 545static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
 546{
 547	uint32_t val;
 548	uint32_t mask;
 549
 550	if (WARN_ON_ONCE(state & ~dev_priv->csr.allowed_dc_mask))
 551		state &= dev_priv->csr.allowed_dc_mask;
 552
 553	val = I915_READ(DC_STATE_EN);
 554	mask = gen9_dc_mask(dev_priv);
 555	DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
 556		      val & mask, state);
 557
 558	/* Check if DMC is ignoring our DC state requests */
 559	if ((val & mask) != dev_priv->csr.dc_state)
 560		DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
 561			  dev_priv->csr.dc_state, val & mask);
 562
 563	val &= ~mask;
 564	val |= state;
 565
 566	gen9_write_dc_state(dev_priv, val);
 567
 568	dev_priv->csr.dc_state = val & mask;
 569}
 570
 571void bxt_enable_dc9(struct drm_i915_private *dev_priv)
 572{
 573	assert_can_enable_dc9(dev_priv);
 574
 575	DRM_DEBUG_KMS("Enabling DC9\n");
 576
 577	intel_power_sequencer_reset(dev_priv);
 578	gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
 579}
 580
 581void bxt_disable_dc9(struct drm_i915_private *dev_priv)
 582{
 583	assert_can_disable_dc9(dev_priv);
 584
 585	DRM_DEBUG_KMS("Disabling DC9\n");
 586
 587	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 588
 589	intel_pps_unlock_regs_wa(dev_priv);
 590}
 591
 592static void assert_csr_loaded(struct drm_i915_private *dev_priv)
 593{
 594	WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
 595		  "CSR program storage start is NULL\n");
 596	WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
 597	WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
 598}
 599
 600static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
 601{
 
 602	bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
 603					SKL_DISP_PW_2);
 604
 
 
 
 605	WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
 606
 607	WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
 608		  "DC5 already programmed to be enabled.\n");
 609	assert_rpm_wakelock_held(dev_priv);
 610
 611	assert_csr_loaded(dev_priv);
 612}
 613
 614void gen9_enable_dc5(struct drm_i915_private *dev_priv)
 
 
 
 
 
 
 
 
 
 
 
 
 615{
 616	assert_can_enable_dc5(dev_priv);
 617
 618	DRM_DEBUG_KMS("Enabling DC5\n");
 619
 620	/* Wa Display #1183: skl,kbl,cfl */
 621	if (IS_GEN9_BC(dev_priv))
 622		I915_WRITE(GEN8_CHICKEN_DCPR_1, I915_READ(GEN8_CHICKEN_DCPR_1) |
 623			   SKL_SELECT_ALTERNATE_DC_EXIT);
 624
 625	gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
 626}
 627
 628static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
 629{
 
 
 
 
 
 630	WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
 631		  "Backlight is not disabled.\n");
 632	WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
 633		  "DC6 already programmed to be enabled.\n");
 634
 635	assert_csr_loaded(dev_priv);
 636}
 637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 638void skl_enable_dc6(struct drm_i915_private *dev_priv)
 639{
 640	assert_can_enable_dc6(dev_priv);
 641
 642	DRM_DEBUG_KMS("Enabling DC6\n");
 643
 644	/* Wa Display #1183: skl,kbl,cfl */
 645	if (IS_GEN9_BC(dev_priv))
 646		I915_WRITE(GEN8_CHICKEN_DCPR_1, I915_READ(GEN8_CHICKEN_DCPR_1) |
 647			   SKL_SELECT_ALTERNATE_DC_EXIT);
 648
 649	gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
 650}
 651
 652void skl_disable_dc6(struct drm_i915_private *dev_priv)
 653{
 
 
 654	DRM_DEBUG_KMS("Disabling DC6\n");
 655
 656	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 657}
 658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 659static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
 660				   struct i915_power_well *power_well)
 661{
 662	enum i915_power_well_id id = power_well->id;
 663	u32 mask = HSW_PWR_WELL_CTL_REQ(id);
 664	u32 bios_req = I915_READ(HSW_PWR_WELL_CTL_BIOS(id));
 665
 666	/* Take over the request bit if set by BIOS. */
 667	if (bios_req & mask) {
 668		u32 drv_req = I915_READ(HSW_PWR_WELL_CTL_DRIVER(id));
 
 
 
 
 669
 670		if (!(drv_req & mask))
 671			I915_WRITE(HSW_PWR_WELL_CTL_DRIVER(id), drv_req | mask);
 672		I915_WRITE(HSW_PWR_WELL_CTL_BIOS(id), bios_req & ~mask);
 673	}
 674}
 675
 676static void bxt_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
 677					   struct i915_power_well *power_well)
 678{
 679	bxt_ddi_phy_init(dev_priv, power_well->bxt.phy);
 680}
 681
 682static void bxt_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
 683					    struct i915_power_well *power_well)
 684{
 685	bxt_ddi_phy_uninit(dev_priv, power_well->bxt.phy);
 
 
 
 686}
 687
 688static bool bxt_dpio_cmn_power_well_enabled(struct drm_i915_private *dev_priv,
 689					    struct i915_power_well *power_well)
 690{
 691	return bxt_ddi_phy_is_enabled(dev_priv, power_well->bxt.phy);
 
 
 
 692}
 693
 694static void bxt_verify_ddi_phy_power_wells(struct drm_i915_private *dev_priv)
 
 695{
 696	struct i915_power_well *power_well;
 
 697
 698	power_well = lookup_power_well(dev_priv, BXT_DPIO_CMN_A);
 699	if (power_well->count > 0)
 700		bxt_ddi_phy_verify_state(dev_priv, power_well->bxt.phy);
 701
 702	power_well = lookup_power_well(dev_priv, BXT_DPIO_CMN_BC);
 703	if (power_well->count > 0)
 704		bxt_ddi_phy_verify_state(dev_priv, power_well->bxt.phy);
 705
 706	if (IS_GEMINILAKE(dev_priv)) {
 707		power_well = lookup_power_well(dev_priv, GLK_DPIO_CMN_C);
 708		if (power_well->count > 0)
 709			bxt_ddi_phy_verify_state(dev_priv, power_well->bxt.phy);
 710	}
 711}
 712
 713static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
 714					   struct i915_power_well *power_well)
 715{
 716	return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
 717}
 718
 719static void gen9_assert_dbuf_enabled(struct drm_i915_private *dev_priv)
 720{
 721	u32 tmp = I915_READ(DBUF_CTL);
 722
 723	WARN((tmp & (DBUF_POWER_STATE | DBUF_POWER_REQUEST)) !=
 724	     (DBUF_POWER_STATE | DBUF_POWER_REQUEST),
 725	     "Unexpected DBuf power power state (0x%08x)\n", tmp);
 726}
 727
 728static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
 729					  struct i915_power_well *power_well)
 730{
 731	struct intel_cdclk_state cdclk_state = {};
 732
 733	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
 734
 735	dev_priv->display.get_cdclk(dev_priv, &cdclk_state);
 736	/* Can't read out voltage_level so can't use intel_cdclk_changed() */
 737	WARN_ON(intel_cdclk_needs_modeset(&dev_priv->cdclk.hw, &cdclk_state));
 738
 739	gen9_assert_dbuf_enabled(dev_priv);
 740
 741	if (IS_GEN9_LP(dev_priv))
 742		bxt_verify_ddi_phy_power_wells(dev_priv);
 743}
 744
 745static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
 746					   struct i915_power_well *power_well)
 747{
 748	if (!dev_priv->csr.dmc_payload)
 749		return;
 750
 751	if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC6)
 752		skl_enable_dc6(dev_priv);
 753	else if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5)
 754		gen9_enable_dc5(dev_priv);
 755}
 756
 757static void i9xx_power_well_sync_hw_noop(struct drm_i915_private *dev_priv,
 758					 struct i915_power_well *power_well)
 759{
 
 
 
 
 
 
 
 
 
 
 760}
 761
 762static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
 763					   struct i915_power_well *power_well)
 764{
 765}
 766
 767static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
 768					     struct i915_power_well *power_well)
 769{
 770	return true;
 771}
 772
 773static void i830_pipes_power_well_enable(struct drm_i915_private *dev_priv,
 774					 struct i915_power_well *power_well)
 775{
 776	if ((I915_READ(PIPECONF(PIPE_A)) & PIPECONF_ENABLE) == 0)
 777		i830_enable_pipe(dev_priv, PIPE_A);
 778	if ((I915_READ(PIPECONF(PIPE_B)) & PIPECONF_ENABLE) == 0)
 779		i830_enable_pipe(dev_priv, PIPE_B);
 780}
 781
 782static void i830_pipes_power_well_disable(struct drm_i915_private *dev_priv,
 783					  struct i915_power_well *power_well)
 784{
 785	i830_disable_pipe(dev_priv, PIPE_B);
 786	i830_disable_pipe(dev_priv, PIPE_A);
 787}
 788
 789static bool i830_pipes_power_well_enabled(struct drm_i915_private *dev_priv,
 790					  struct i915_power_well *power_well)
 791{
 792	return I915_READ(PIPECONF(PIPE_A)) & PIPECONF_ENABLE &&
 793		I915_READ(PIPECONF(PIPE_B)) & PIPECONF_ENABLE;
 794}
 795
 796static void i830_pipes_power_well_sync_hw(struct drm_i915_private *dev_priv,
 797					  struct i915_power_well *power_well)
 798{
 799	if (power_well->count > 0)
 800		i830_pipes_power_well_enable(dev_priv, power_well);
 801	else
 802		i830_pipes_power_well_disable(dev_priv, power_well);
 803}
 804
 805static void vlv_set_power_well(struct drm_i915_private *dev_priv,
 806			       struct i915_power_well *power_well, bool enable)
 807{
 808	enum i915_power_well_id power_well_id = power_well->id;
 809	u32 mask;
 810	u32 state;
 811	u32 ctrl;
 812
 813	mask = PUNIT_PWRGT_MASK(power_well_id);
 814	state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
 815			 PUNIT_PWRGT_PWR_GATE(power_well_id);
 816
 817	mutex_lock(&dev_priv->pcu_lock);
 818
 819#define COND \
 820	((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
 821
 822	if (COND)
 823		goto out;
 824
 825	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
 826	ctrl &= ~mask;
 827	ctrl |= state;
 828	vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
 829
 830	if (wait_for(COND, 100))
 831		DRM_ERROR("timeout setting power well state %08x (%08x)\n",
 832			  state,
 833			  vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
 834
 835#undef COND
 836
 837out:
 838	mutex_unlock(&dev_priv->pcu_lock);
 
 
 
 
 
 
 839}
 840
 841static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
 842				  struct i915_power_well *power_well)
 843{
 844	vlv_set_power_well(dev_priv, power_well, true);
 845}
 846
 847static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
 848				   struct i915_power_well *power_well)
 849{
 850	vlv_set_power_well(dev_priv, power_well, false);
 851}
 852
 853static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
 854				   struct i915_power_well *power_well)
 855{
 856	enum i915_power_well_id power_well_id = power_well->id;
 857	bool enabled = false;
 858	u32 mask;
 859	u32 state;
 860	u32 ctrl;
 861
 862	mask = PUNIT_PWRGT_MASK(power_well_id);
 863	ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
 864
 865	mutex_lock(&dev_priv->pcu_lock);
 866
 867	state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
 868	/*
 869	 * We only ever set the power-on and power-gate states, anything
 870	 * else is unexpected.
 871	 */
 872	WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
 873		state != PUNIT_PWRGT_PWR_GATE(power_well_id));
 874	if (state == ctrl)
 875		enabled = true;
 876
 877	/*
 878	 * A transient state at this point would mean some unexpected party
 879	 * is poking at the power controls too.
 880	 */
 881	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
 882	WARN_ON(ctrl != state);
 883
 884	mutex_unlock(&dev_priv->pcu_lock);
 885
 886	return enabled;
 887}
 888
 889static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
 890{
 891	u32 val;
 892
 893	/*
 894	 * On driver load, a pipe may be active and driving a DSI display.
 895	 * Preserve DPOUNIT_CLOCK_GATE_DISABLE to avoid the pipe getting stuck
 896	 * (and never recovering) in this case. intel_dsi_post_disable() will
 897	 * clear it when we turn off the display.
 898	 */
 899	val = I915_READ(DSPCLK_GATE_D);
 900	val &= DPOUNIT_CLOCK_GATE_DISABLE;
 901	val |= VRHUNIT_CLOCK_GATE_DISABLE;
 902	I915_WRITE(DSPCLK_GATE_D, val);
 903
 904	/*
 905	 * Disable trickle feed and enable pnd deadline calculation
 906	 */
 907	I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
 908	I915_WRITE(CBR1_VLV, 0);
 909
 910	WARN_ON(dev_priv->rawclk_freq == 0);
 911
 912	I915_WRITE(RAWCLK_FREQ_VLV,
 913		   DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 1000));
 914}
 915
 916static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
 917{
 918	struct intel_encoder *encoder;
 919	enum pipe pipe;
 920
 921	/*
 922	 * Enable the CRI clock source so we can get at the
 923	 * display and the reference clock for VGA
 924	 * hotplug / manual detection. Supposedly DSI also
 925	 * needs the ref clock up and running.
 926	 *
 927	 * CHV DPLL B/C have some issues if VGA mode is enabled.
 928	 */
 929	for_each_pipe(dev_priv, pipe) {
 930		u32 val = I915_READ(DPLL(pipe));
 931
 932		val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
 933		if (pipe != PIPE_A)
 934			val |= DPLL_INTEGRATED_CRI_CLK_VLV;
 935
 936		I915_WRITE(DPLL(pipe), val);
 937	}
 938
 939	vlv_init_display_clock_gating(dev_priv);
 940
 941	spin_lock_irq(&dev_priv->irq_lock);
 942	valleyview_enable_display_irqs(dev_priv);
 943	spin_unlock_irq(&dev_priv->irq_lock);
 944
 945	/*
 946	 * During driver initialization/resume we can avoid restoring the
 947	 * part of the HW/SW state that will be inited anyway explicitly.
 948	 */
 949	if (dev_priv->power_domains.initializing)
 950		return;
 951
 952	intel_hpd_init(dev_priv);
 953
 954	/* Re-enable the ADPA, if we have one */
 955	for_each_intel_encoder(&dev_priv->drm, encoder) {
 956		if (encoder->type == INTEL_OUTPUT_ANALOG)
 957			intel_crt_reset(&encoder->base);
 958	}
 959
 960	i915_redisable_vga_power_on(dev_priv);
 961
 962	intel_pps_unlock_regs_wa(dev_priv);
 963}
 964
 965static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
 966{
 967	spin_lock_irq(&dev_priv->irq_lock);
 968	valleyview_disable_display_irqs(dev_priv);
 969	spin_unlock_irq(&dev_priv->irq_lock);
 970
 971	/* make sure we're done processing display irqs */
 972	synchronize_irq(dev_priv->drm.irq);
 973
 974	intel_power_sequencer_reset(dev_priv);
 975
 976	/* Prevent us from re-enabling polling on accident in late suspend */
 977	if (!dev_priv->drm.dev->power.is_suspended)
 978		intel_hpd_poll_init(dev_priv);
 979}
 980
 981static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
 982					  struct i915_power_well *power_well)
 983{
 984	WARN_ON_ONCE(power_well->id != PUNIT_POWER_WELL_DISP2D);
 985
 986	vlv_set_power_well(dev_priv, power_well, true);
 987
 988	vlv_display_power_well_init(dev_priv);
 989}
 990
 991static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
 992					   struct i915_power_well *power_well)
 993{
 994	WARN_ON_ONCE(power_well->id != PUNIT_POWER_WELL_DISP2D);
 995
 996	vlv_display_power_well_deinit(dev_priv);
 997
 998	vlv_set_power_well(dev_priv, power_well, false);
 999}
1000
1001static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1002					   struct i915_power_well *power_well)
1003{
1004	WARN_ON_ONCE(power_well->id != PUNIT_POWER_WELL_DPIO_CMN_BC);
1005
1006	/* since ref/cri clock was enabled */
1007	udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1008
1009	vlv_set_power_well(dev_priv, power_well, true);
1010
1011	/*
1012	 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1013	 *  6.	De-assert cmn_reset/side_reset. Same as VLV X0.
1014	 *   a.	GUnit 0x2110 bit[0] set to 1 (def 0)
1015	 *   b.	The other bits such as sfr settings / modesel may all
1016	 *	be set to 0.
1017	 *
1018	 * This should only be done on init and resume from S3 with
1019	 * both PLLs disabled, or we risk losing DPIO and PLL
1020	 * synchronization.
1021	 */
1022	I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1023}
1024
1025static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1026					    struct i915_power_well *power_well)
1027{
1028	enum pipe pipe;
1029
1030	WARN_ON_ONCE(power_well->id != PUNIT_POWER_WELL_DPIO_CMN_BC);
1031
1032	for_each_pipe(dev_priv, pipe)
1033		assert_pll_disabled(dev_priv, pipe);
1034
1035	/* Assert common reset */
1036	I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1037
1038	vlv_set_power_well(dev_priv, power_well, false);
1039}
1040
1041#define POWER_DOMAIN_MASK (GENMASK_ULL(POWER_DOMAIN_NUM - 1, 0))
1042
1043static struct i915_power_well *
1044lookup_power_well(struct drm_i915_private *dev_priv,
1045		  enum i915_power_well_id power_well_id)
1046{
1047	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1048	int i;
1049
1050	for (i = 0; i < power_domains->power_well_count; i++) {
1051		struct i915_power_well *power_well;
1052
1053		power_well = &power_domains->power_wells[i];
1054		if (power_well->id == power_well_id)
1055			return power_well;
1056	}
1057
1058	return NULL;
1059}
1060
1061#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1062
1063static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1064{
1065	struct i915_power_well *cmn_bc =
1066		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1067	struct i915_power_well *cmn_d =
1068		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1069	u32 phy_control = dev_priv->chv_phy_control;
1070	u32 phy_status = 0;
1071	u32 phy_status_mask = 0xffffffff;
 
1072
1073	/*
1074	 * The BIOS can leave the PHY is some weird state
1075	 * where it doesn't fully power down some parts.
1076	 * Disable the asserts until the PHY has been fully
1077	 * reset (ie. the power well has been disabled at
1078	 * least once).
1079	 */
1080	if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1081		phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1082				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1083				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1084				     PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1085				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1086				     PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1087
1088	if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1089		phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1090				     PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1091				     PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1092
1093	if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1094		phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1095
1096		/* this assumes override is only used to enable lanes */
1097		if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1098			phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1099
1100		if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1101			phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1102
1103		/* CL1 is on whenever anything is on in either channel */
1104		if (BITS_SET(phy_control,
1105			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1106			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1107			phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1108
1109		/*
1110		 * The DPLLB check accounts for the pipe B + port A usage
1111		 * with CL2 powered up but all the lanes in the second channel
1112		 * powered down.
1113		 */
1114		if (BITS_SET(phy_control,
1115			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1116		    (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1117			phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1118
1119		if (BITS_SET(phy_control,
1120			     PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1121			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1122		if (BITS_SET(phy_control,
1123			     PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1124			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1125
1126		if (BITS_SET(phy_control,
1127			     PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1128			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1129		if (BITS_SET(phy_control,
1130			     PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1131			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1132	}
1133
1134	if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1135		phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1136
1137		/* this assumes override is only used to enable lanes */
1138		if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1139			phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1140
1141		if (BITS_SET(phy_control,
1142			     PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1143			phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1144
1145		if (BITS_SET(phy_control,
1146			     PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1147			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1148		if (BITS_SET(phy_control,
1149			     PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1150			phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1151	}
1152
1153	phy_status &= phy_status_mask;
1154
1155	/*
1156	 * The PHY may be busy with some initial calibration and whatnot,
1157	 * so the power state can take a while to actually change.
1158	 */
1159	if (intel_wait_for_register(dev_priv,
1160				    DISPLAY_PHY_STATUS,
1161				    phy_status_mask,
1162				    phy_status,
1163				    10))
1164		DRM_ERROR("Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1165			  I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask,
1166			   phy_status, dev_priv->chv_phy_control);
1167}
1168
1169#undef BITS_SET
1170
1171static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1172					   struct i915_power_well *power_well)
1173{
1174	enum dpio_phy phy;
1175	enum pipe pipe;
1176	uint32_t tmp;
1177
1178	WARN_ON_ONCE(power_well->id != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1179		     power_well->id != PUNIT_POWER_WELL_DPIO_CMN_D);
1180
1181	if (power_well->id == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1182		pipe = PIPE_A;
1183		phy = DPIO_PHY0;
1184	} else {
1185		pipe = PIPE_C;
1186		phy = DPIO_PHY1;
1187	}
1188
1189	/* since ref/cri clock was enabled */
1190	udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1191	vlv_set_power_well(dev_priv, power_well, true);
1192
1193	/* Poll for phypwrgood signal */
1194	if (intel_wait_for_register(dev_priv,
1195				    DISPLAY_PHY_STATUS,
1196				    PHY_POWERGOOD(phy),
1197				    PHY_POWERGOOD(phy),
1198				    1))
1199		DRM_ERROR("Display PHY %d is not power up\n", phy);
1200
1201	mutex_lock(&dev_priv->sb_lock);
1202
1203	/* Enable dynamic power down */
1204	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
1205	tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1206		DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
1207	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1208
1209	if (power_well->id == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1210		tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1211		tmp |= DPIO_DYNPWRDOWNEN_CH1;
1212		vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
1213	} else {
1214		/*
1215		 * Force the non-existing CL2 off. BXT does this
1216		 * too, so maybe it saves some power even though
1217		 * CL2 doesn't exist?
1218		 */
1219		tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1220		tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1221		vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
1222	}
1223
1224	mutex_unlock(&dev_priv->sb_lock);
1225
1226	dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1227	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1228
1229	DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1230		      phy, dev_priv->chv_phy_control);
1231
1232	assert_chv_phy_status(dev_priv);
1233}
1234
1235static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1236					    struct i915_power_well *power_well)
1237{
1238	enum dpio_phy phy;
1239
1240	WARN_ON_ONCE(power_well->id != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1241		     power_well->id != PUNIT_POWER_WELL_DPIO_CMN_D);
1242
1243	if (power_well->id == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1244		phy = DPIO_PHY0;
1245		assert_pll_disabled(dev_priv, PIPE_A);
1246		assert_pll_disabled(dev_priv, PIPE_B);
1247	} else {
1248		phy = DPIO_PHY1;
1249		assert_pll_disabled(dev_priv, PIPE_C);
1250	}
1251
1252	dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1253	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1254
1255	vlv_set_power_well(dev_priv, power_well, false);
1256
1257	DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1258		      phy, dev_priv->chv_phy_control);
1259
1260	/* PHY is fully reset now, so we can enable the PHY state asserts */
1261	dev_priv->chv_phy_assert[phy] = true;
1262
1263	assert_chv_phy_status(dev_priv);
1264}
1265
1266static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1267				     enum dpio_channel ch, bool override, unsigned int mask)
1268{
1269	enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1270	u32 reg, val, expected, actual;
1271
1272	/*
1273	 * The BIOS can leave the PHY is some weird state
1274	 * where it doesn't fully power down some parts.
1275	 * Disable the asserts until the PHY has been fully
1276	 * reset (ie. the power well has been disabled at
1277	 * least once).
1278	 */
1279	if (!dev_priv->chv_phy_assert[phy])
1280		return;
1281
1282	if (ch == DPIO_CH0)
1283		reg = _CHV_CMN_DW0_CH0;
1284	else
1285		reg = _CHV_CMN_DW6_CH1;
1286
1287	mutex_lock(&dev_priv->sb_lock);
1288	val = vlv_dpio_read(dev_priv, pipe, reg);
1289	mutex_unlock(&dev_priv->sb_lock);
1290
1291	/*
1292	 * This assumes !override is only used when the port is disabled.
1293	 * All lanes should power down even without the override when
1294	 * the port is disabled.
1295	 */
1296	if (!override || mask == 0xf) {
1297		expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1298		/*
1299		 * If CH1 common lane is not active anymore
1300		 * (eg. for pipe B DPLL) the entire channel will
1301		 * shut down, which causes the common lane registers
1302		 * to read as 0. That means we can't actually check
1303		 * the lane power down status bits, but as the entire
1304		 * register reads as 0 it's a good indication that the
1305		 * channel is indeed entirely powered down.
1306		 */
1307		if (ch == DPIO_CH1 && val == 0)
1308			expected = 0;
1309	} else if (mask != 0x0) {
1310		expected = DPIO_ANYDL_POWERDOWN;
1311	} else {
1312		expected = 0;
1313	}
1314
1315	if (ch == DPIO_CH0)
1316		actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1317	else
1318		actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1319	actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1320
1321	WARN(actual != expected,
1322	     "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1323	     !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1324	     !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1325	     reg, val);
1326}
1327
1328bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1329			  enum dpio_channel ch, bool override)
1330{
1331	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1332	bool was_override;
1333
1334	mutex_lock(&power_domains->lock);
1335
1336	was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1337
1338	if (override == was_override)
1339		goto out;
1340
1341	if (override)
1342		dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1343	else
1344		dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1345
1346	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1347
1348	DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1349		      phy, ch, dev_priv->chv_phy_control);
1350
1351	assert_chv_phy_status(dev_priv);
1352
1353out:
1354	mutex_unlock(&power_domains->lock);
1355
1356	return was_override;
1357}
1358
1359void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1360			     bool override, unsigned int mask)
1361{
1362	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1363	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1364	enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1365	enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1366
1367	mutex_lock(&power_domains->lock);
1368
1369	dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1370	dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1371
1372	if (override)
1373		dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1374	else
1375		dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1376
1377	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1378
1379	DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1380		      phy, ch, mask, dev_priv->chv_phy_control);
1381
1382	assert_chv_phy_status(dev_priv);
1383
1384	assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1385
1386	mutex_unlock(&power_domains->lock);
1387}
1388
1389static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1390					struct i915_power_well *power_well)
1391{
1392	enum pipe pipe = PIPE_A;
1393	bool enabled;
1394	u32 state, ctrl;
1395
1396	mutex_lock(&dev_priv->pcu_lock);
1397
1398	state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1399	/*
1400	 * We only ever set the power-on and power-gate states, anything
1401	 * else is unexpected.
1402	 */
1403	WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1404	enabled = state == DP_SSS_PWR_ON(pipe);
1405
1406	/*
1407	 * A transient state at this point would mean some unexpected party
1408	 * is poking at the power controls too.
1409	 */
1410	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1411	WARN_ON(ctrl << 16 != state);
1412
1413	mutex_unlock(&dev_priv->pcu_lock);
1414
1415	return enabled;
1416}
1417
1418static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1419				    struct i915_power_well *power_well,
1420				    bool enable)
1421{
1422	enum pipe pipe = PIPE_A;
1423	u32 state;
1424	u32 ctrl;
1425
1426	state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1427
1428	mutex_lock(&dev_priv->pcu_lock);
1429
1430#define COND \
1431	((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1432
1433	if (COND)
1434		goto out;
1435
1436	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1437	ctrl &= ~DP_SSC_MASK(pipe);
1438	ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1439	vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1440
1441	if (wait_for(COND, 100))
1442		DRM_ERROR("timeout setting power well state %08x (%08x)\n",
1443			  state,
1444			  vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1445
1446#undef COND
1447
1448out:
1449	mutex_unlock(&dev_priv->pcu_lock);
 
 
 
 
 
 
 
 
1450}
1451
1452static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1453				       struct i915_power_well *power_well)
1454{
1455	WARN_ON_ONCE(power_well->id != CHV_DISP_PW_PIPE_A);
1456
1457	chv_set_pipe_power_well(dev_priv, power_well, true);
1458
1459	vlv_display_power_well_init(dev_priv);
1460}
1461
1462static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1463					struct i915_power_well *power_well)
1464{
1465	WARN_ON_ONCE(power_well->id != CHV_DISP_PW_PIPE_A);
1466
1467	vlv_display_power_well_deinit(dev_priv);
1468
1469	chv_set_pipe_power_well(dev_priv, power_well, false);
1470}
1471
1472static void
1473__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1474				 enum intel_display_power_domain domain)
1475{
1476	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1477	struct i915_power_well *power_well;
 
1478
1479	for_each_power_domain_well(dev_priv, power_well, BIT_ULL(domain))
1480		intel_power_well_get(dev_priv, power_well);
 
 
1481
1482	power_domains->domain_use_count[domain]++;
1483}
1484
1485/**
1486 * intel_display_power_get - grab a power domain reference
1487 * @dev_priv: i915 device instance
1488 * @domain: power domain to reference
1489 *
1490 * This function grabs a power domain reference for @domain and ensures that the
1491 * power domain and all its parents are powered up. Therefore users should only
1492 * grab a reference to the innermost power domain they need.
1493 *
1494 * Any power domain reference obtained by this function must have a symmetric
1495 * call to intel_display_power_put() to release the reference again.
1496 */
1497void intel_display_power_get(struct drm_i915_private *dev_priv,
1498			     enum intel_display_power_domain domain)
1499{
1500	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1501
1502	intel_runtime_pm_get(dev_priv);
1503
1504	mutex_lock(&power_domains->lock);
1505
1506	__intel_display_power_get_domain(dev_priv, domain);
1507
1508	mutex_unlock(&power_domains->lock);
1509}
1510
1511/**
1512 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
1513 * @dev_priv: i915 device instance
1514 * @domain: power domain to reference
1515 *
1516 * This function grabs a power domain reference for @domain and ensures that the
1517 * power domain and all its parents are powered up. Therefore users should only
1518 * grab a reference to the innermost power domain they need.
1519 *
1520 * Any power domain reference obtained by this function must have a symmetric
1521 * call to intel_display_power_put() to release the reference again.
1522 */
1523bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1524					enum intel_display_power_domain domain)
1525{
1526	struct i915_power_domains *power_domains = &dev_priv->power_domains;
1527	bool is_enabled;
1528
1529	if (!intel_runtime_pm_get_if_in_use(dev_priv))
1530		return false;
1531
1532	mutex_lock(&power_domains->lock);
1533
1534	if (__intel_display_power_is_enabled(dev_priv, domain)) {
1535		__intel_display_power_get_domain(dev_priv, domain);
1536		is_enabled = true;
1537	} else {
1538		is_enabled = false;
1539	}
1540
1541	mutex_unlock(&power_domains->lock);
1542
1543	if (!is_enabled)
1544		intel_runtime_pm_put(dev_priv);
1545
1546	return is_enabled;
1547}
1548
1549/**
1550 * intel_display_power_put - release a power domain reference
1551 * @dev_priv: i915 device instance
1552 * @domain: power domain to reference
1553 *
1554 * This function drops the power domain reference obtained by
1555 * intel_display_power_get() and might power down the corresponding hardware
1556 * block right away if this is the last reference.
1557 */
1558void intel_display_power_put(struct drm_i915_private *dev_priv,
1559			     enum intel_display_power_domain domain)
1560{
1561	struct i915_power_domains *power_domains;
1562	struct i915_power_well *power_well;
 
1563
1564	power_domains = &dev_priv->power_domains;
1565
1566	mutex_lock(&power_domains->lock);
1567
1568	WARN(!power_domains->domain_use_count[domain],
1569	     "Use count on domain %s is already zero\n",
1570	     intel_display_power_domain_str(domain));
1571	power_domains->domain_use_count[domain]--;
1572
1573	for_each_power_domain_well_rev(dev_priv, power_well, BIT_ULL(domain))
1574		intel_power_well_put(dev_priv, power_well);
 
 
 
 
 
 
1575
1576	mutex_unlock(&power_domains->lock);
1577
1578	intel_runtime_pm_put(dev_priv);
1579}
1580
1581#define I830_PIPES_POWER_DOMAINS (		\
1582	BIT_ULL(POWER_DOMAIN_PIPE_A) |		\
1583	BIT_ULL(POWER_DOMAIN_PIPE_B) |		\
1584	BIT_ULL(POWER_DOMAIN_PIPE_A_PANEL_FITTER) |	\
1585	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |	\
1586	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |	\
1587	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |	\
1588	BIT_ULL(POWER_DOMAIN_INIT))
1589
1590#define VLV_DISPLAY_POWER_DOMAINS (		\
1591	BIT_ULL(POWER_DOMAIN_PIPE_A) |		\
1592	BIT_ULL(POWER_DOMAIN_PIPE_B) |		\
1593	BIT_ULL(POWER_DOMAIN_PIPE_A_PANEL_FITTER) |	\
1594	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |	\
1595	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |	\
1596	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |	\
1597	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1598	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1599	BIT_ULL(POWER_DOMAIN_PORT_DSI) |		\
1600	BIT_ULL(POWER_DOMAIN_PORT_CRT) |		\
1601	BIT_ULL(POWER_DOMAIN_VGA) |			\
1602	BIT_ULL(POWER_DOMAIN_AUDIO) |		\
1603	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1604	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1605	BIT_ULL(POWER_DOMAIN_GMBUS) |		\
1606	BIT_ULL(POWER_DOMAIN_INIT))
 
 
1607
1608#define VLV_DPIO_CMN_BC_POWER_DOMAINS (		\
1609	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1610	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1611	BIT_ULL(POWER_DOMAIN_PORT_CRT) |		\
1612	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1613	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1614	BIT_ULL(POWER_DOMAIN_INIT))
1615
1616#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS (	\
1617	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1618	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1619	BIT_ULL(POWER_DOMAIN_INIT))
1620
1621#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS (	\
1622	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1623	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1624	BIT_ULL(POWER_DOMAIN_INIT))
1625
1626#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS (	\
1627	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1628	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1629	BIT_ULL(POWER_DOMAIN_INIT))
1630
1631#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS (	\
1632	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1633	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1634	BIT_ULL(POWER_DOMAIN_INIT))
1635
1636#define CHV_DISPLAY_POWER_DOMAINS (		\
1637	BIT_ULL(POWER_DOMAIN_PIPE_A) |		\
1638	BIT_ULL(POWER_DOMAIN_PIPE_B) |		\
1639	BIT_ULL(POWER_DOMAIN_PIPE_C) |		\
1640	BIT_ULL(POWER_DOMAIN_PIPE_A_PANEL_FITTER) |	\
1641	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |	\
1642	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |	\
1643	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |	\
1644	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |	\
1645	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |	\
1646	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1647	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1648	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_LANES) |	\
1649	BIT_ULL(POWER_DOMAIN_PORT_DSI) |		\
1650	BIT_ULL(POWER_DOMAIN_VGA) |			\
1651	BIT_ULL(POWER_DOMAIN_AUDIO) |		\
1652	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1653	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1654	BIT_ULL(POWER_DOMAIN_AUX_D) |		\
1655	BIT_ULL(POWER_DOMAIN_GMBUS) |		\
1656	BIT_ULL(POWER_DOMAIN_INIT))
1657
1658#define CHV_DPIO_CMN_BC_POWER_DOMAINS (		\
1659	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |	\
1660	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |	\
1661	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1662	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1663	BIT_ULL(POWER_DOMAIN_INIT))
1664
1665#define CHV_DPIO_CMN_D_POWER_DOMAINS (		\
1666	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_LANES) |	\
1667	BIT_ULL(POWER_DOMAIN_AUX_D) |		\
1668	BIT_ULL(POWER_DOMAIN_INIT))
1669
1670#define HSW_DISPLAY_POWER_DOMAINS (			\
1671	BIT_ULL(POWER_DOMAIN_PIPE_B) |			\
1672	BIT_ULL(POWER_DOMAIN_PIPE_C) |			\
1673	BIT_ULL(POWER_DOMAIN_PIPE_A_PANEL_FITTER) |		\
1674	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
1675	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
1676	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |		\
1677	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |		\
1678	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |		\
1679	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1680	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1681	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
1682	BIT_ULL(POWER_DOMAIN_PORT_CRT) | /* DDI E */	\
1683	BIT_ULL(POWER_DOMAIN_VGA) |				\
1684	BIT_ULL(POWER_DOMAIN_AUDIO) |			\
1685	BIT_ULL(POWER_DOMAIN_INIT))
1686
1687#define BDW_DISPLAY_POWER_DOMAINS (			\
1688	BIT_ULL(POWER_DOMAIN_PIPE_B) |			\
1689	BIT_ULL(POWER_DOMAIN_PIPE_C) |			\
1690	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
1691	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
1692	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |		\
1693	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |		\
1694	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |		\
1695	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1696	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1697	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
1698	BIT_ULL(POWER_DOMAIN_PORT_CRT) | /* DDI E */	\
1699	BIT_ULL(POWER_DOMAIN_VGA) |				\
1700	BIT_ULL(POWER_DOMAIN_AUDIO) |			\
1701	BIT_ULL(POWER_DOMAIN_INIT))
1702
1703#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS (		\
1704	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |		\
1705	BIT_ULL(POWER_DOMAIN_PIPE_B) |			\
1706	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |		\
1707	BIT_ULL(POWER_DOMAIN_PIPE_C) |			\
1708	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |		\
1709	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
1710	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
1711	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1712	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1713	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
1714	BIT_ULL(POWER_DOMAIN_PORT_DDI_E_LANES) |		\
1715	BIT_ULL(POWER_DOMAIN_AUX_B) |                       \
1716	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1717	BIT_ULL(POWER_DOMAIN_AUX_D) |			\
1718	BIT_ULL(POWER_DOMAIN_AUDIO) |			\
1719	BIT_ULL(POWER_DOMAIN_VGA) |				\
1720	BIT_ULL(POWER_DOMAIN_INIT))
1721#define SKL_DISPLAY_DDI_IO_A_E_POWER_DOMAINS (		\
1722	BIT_ULL(POWER_DOMAIN_PORT_DDI_A_IO) |		\
1723	BIT_ULL(POWER_DOMAIN_PORT_DDI_E_IO) |		\
1724	BIT_ULL(POWER_DOMAIN_INIT))
1725#define SKL_DISPLAY_DDI_IO_B_POWER_DOMAINS (		\
1726	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_IO) |		\
1727	BIT_ULL(POWER_DOMAIN_INIT))
1728#define SKL_DISPLAY_DDI_IO_C_POWER_DOMAINS (		\
1729	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_IO) |		\
1730	BIT_ULL(POWER_DOMAIN_INIT))
1731#define SKL_DISPLAY_DDI_IO_D_POWER_DOMAINS (		\
1732	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_IO) |		\
1733	BIT_ULL(POWER_DOMAIN_INIT))
1734#define SKL_DISPLAY_DC_OFF_POWER_DOMAINS (		\
1735	SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
1736	BIT_ULL(POWER_DOMAIN_GT_IRQ) |			\
1737	BIT_ULL(POWER_DOMAIN_MODESET) |			\
1738	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1739	BIT_ULL(POWER_DOMAIN_INIT))
1740
1741#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS (		\
1742	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |		\
1743	BIT_ULL(POWER_DOMAIN_PIPE_B) |			\
1744	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |		\
1745	BIT_ULL(POWER_DOMAIN_PIPE_C) |			\
1746	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |		\
1747	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
1748	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
1749	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1750	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1751	BIT_ULL(POWER_DOMAIN_AUX_B) |			\
1752	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1753	BIT_ULL(POWER_DOMAIN_AUDIO) |			\
1754	BIT_ULL(POWER_DOMAIN_VGA) |				\
1755	BIT_ULL(POWER_DOMAIN_INIT))
1756#define BXT_DISPLAY_DC_OFF_POWER_DOMAINS (		\
1757	BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
1758	BIT_ULL(POWER_DOMAIN_GT_IRQ) |			\
1759	BIT_ULL(POWER_DOMAIN_MODESET) |			\
1760	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1761	BIT_ULL(POWER_DOMAIN_GMBUS) |			\
1762	BIT_ULL(POWER_DOMAIN_INIT))
1763#define BXT_DPIO_CMN_A_POWER_DOMAINS (			\
1764	BIT_ULL(POWER_DOMAIN_PORT_DDI_A_LANES) |		\
1765	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1766	BIT_ULL(POWER_DOMAIN_INIT))
1767#define BXT_DPIO_CMN_BC_POWER_DOMAINS (			\
1768	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1769	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1770	BIT_ULL(POWER_DOMAIN_AUX_B) |			\
1771	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1772	BIT_ULL(POWER_DOMAIN_INIT))
1773
1774#define GLK_DISPLAY_POWERWELL_2_POWER_DOMAINS (		\
1775	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |		\
1776	BIT_ULL(POWER_DOMAIN_PIPE_B) |			\
1777	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |		\
1778	BIT_ULL(POWER_DOMAIN_PIPE_C) |			\
1779	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |		\
1780	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
1781	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
1782	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1783	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1784	BIT_ULL(POWER_DOMAIN_AUX_B) |                       \
1785	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1786	BIT_ULL(POWER_DOMAIN_AUDIO) |			\
1787	BIT_ULL(POWER_DOMAIN_VGA) |				\
1788	BIT_ULL(POWER_DOMAIN_INIT))
1789#define GLK_DISPLAY_DDI_IO_A_POWER_DOMAINS (		\
1790	BIT_ULL(POWER_DOMAIN_PORT_DDI_A_IO))
1791#define GLK_DISPLAY_DDI_IO_B_POWER_DOMAINS (		\
1792	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_IO))
1793#define GLK_DISPLAY_DDI_IO_C_POWER_DOMAINS (		\
1794	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_IO))
1795#define GLK_DPIO_CMN_A_POWER_DOMAINS (			\
1796	BIT_ULL(POWER_DOMAIN_PORT_DDI_A_LANES) |		\
1797	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1798	BIT_ULL(POWER_DOMAIN_INIT))
1799#define GLK_DPIO_CMN_B_POWER_DOMAINS (			\
1800	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1801	BIT_ULL(POWER_DOMAIN_AUX_B) |			\
1802	BIT_ULL(POWER_DOMAIN_INIT))
1803#define GLK_DPIO_CMN_C_POWER_DOMAINS (			\
1804	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1805	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1806	BIT_ULL(POWER_DOMAIN_INIT))
1807#define GLK_DISPLAY_AUX_A_POWER_DOMAINS (		\
1808	BIT_ULL(POWER_DOMAIN_AUX_A) |		\
1809	BIT_ULL(POWER_DOMAIN_INIT))
1810#define GLK_DISPLAY_AUX_B_POWER_DOMAINS (		\
1811	BIT_ULL(POWER_DOMAIN_AUX_B) |		\
1812	BIT_ULL(POWER_DOMAIN_INIT))
1813#define GLK_DISPLAY_AUX_C_POWER_DOMAINS (		\
1814	BIT_ULL(POWER_DOMAIN_AUX_C) |		\
1815	BIT_ULL(POWER_DOMAIN_INIT))
1816#define GLK_DISPLAY_DC_OFF_POWER_DOMAINS (		\
1817	GLK_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
1818	BIT_ULL(POWER_DOMAIN_GT_IRQ) |			\
1819	BIT_ULL(POWER_DOMAIN_MODESET) |			\
1820	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1821	BIT_ULL(POWER_DOMAIN_GMBUS) |			\
1822	BIT_ULL(POWER_DOMAIN_INIT))
1823
1824#define CNL_DISPLAY_POWERWELL_2_POWER_DOMAINS (		\
1825	BIT_ULL(POWER_DOMAIN_TRANSCODER_A) |		\
1826	BIT_ULL(POWER_DOMAIN_PIPE_B) |			\
1827	BIT_ULL(POWER_DOMAIN_TRANSCODER_B) |		\
1828	BIT_ULL(POWER_DOMAIN_PIPE_C) |			\
1829	BIT_ULL(POWER_DOMAIN_TRANSCODER_C) |		\
1830	BIT_ULL(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |		\
1831	BIT_ULL(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |		\
1832	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_LANES) |		\
1833	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_LANES) |		\
1834	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_LANES) |		\
1835	BIT_ULL(POWER_DOMAIN_PORT_DDI_F_LANES) |		\
1836	BIT_ULL(POWER_DOMAIN_AUX_B) |                       \
1837	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1838	BIT_ULL(POWER_DOMAIN_AUX_D) |			\
1839	BIT_ULL(POWER_DOMAIN_AUX_F) |			\
1840	BIT_ULL(POWER_DOMAIN_AUDIO) |			\
1841	BIT_ULL(POWER_DOMAIN_VGA) |				\
1842	BIT_ULL(POWER_DOMAIN_INIT))
1843#define CNL_DISPLAY_DDI_A_IO_POWER_DOMAINS (		\
1844	BIT_ULL(POWER_DOMAIN_PORT_DDI_A_IO) |		\
1845	BIT_ULL(POWER_DOMAIN_INIT))
1846#define CNL_DISPLAY_DDI_B_IO_POWER_DOMAINS (		\
1847	BIT_ULL(POWER_DOMAIN_PORT_DDI_B_IO) |		\
1848	BIT_ULL(POWER_DOMAIN_INIT))
1849#define CNL_DISPLAY_DDI_C_IO_POWER_DOMAINS (		\
1850	BIT_ULL(POWER_DOMAIN_PORT_DDI_C_IO) |		\
1851	BIT_ULL(POWER_DOMAIN_INIT))
1852#define CNL_DISPLAY_DDI_D_IO_POWER_DOMAINS (		\
1853	BIT_ULL(POWER_DOMAIN_PORT_DDI_D_IO) |		\
1854	BIT_ULL(POWER_DOMAIN_INIT))
1855#define CNL_DISPLAY_AUX_A_POWER_DOMAINS (		\
1856	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1857	BIT_ULL(POWER_DOMAIN_AUX_IO_A) |		\
1858	BIT_ULL(POWER_DOMAIN_INIT))
1859#define CNL_DISPLAY_AUX_B_POWER_DOMAINS (		\
1860	BIT_ULL(POWER_DOMAIN_AUX_B) |			\
1861	BIT_ULL(POWER_DOMAIN_INIT))
1862#define CNL_DISPLAY_AUX_C_POWER_DOMAINS (		\
1863	BIT_ULL(POWER_DOMAIN_AUX_C) |			\
1864	BIT_ULL(POWER_DOMAIN_INIT))
1865#define CNL_DISPLAY_AUX_D_POWER_DOMAINS (		\
1866	BIT_ULL(POWER_DOMAIN_AUX_D) |			\
1867	BIT_ULL(POWER_DOMAIN_INIT))
1868#define CNL_DISPLAY_AUX_F_POWER_DOMAINS (		\
1869	BIT_ULL(POWER_DOMAIN_AUX_F) |			\
1870	BIT_ULL(POWER_DOMAIN_INIT))
1871#define CNL_DISPLAY_DDI_F_IO_POWER_DOMAINS (		\
1872	BIT_ULL(POWER_DOMAIN_PORT_DDI_F_IO) |		\
1873	BIT_ULL(POWER_DOMAIN_INIT))
1874#define CNL_DISPLAY_DC_OFF_POWER_DOMAINS (		\
1875	CNL_DISPLAY_POWERWELL_2_POWER_DOMAINS |		\
1876	BIT_ULL(POWER_DOMAIN_GT_IRQ) |			\
1877	BIT_ULL(POWER_DOMAIN_MODESET) |			\
1878	BIT_ULL(POWER_DOMAIN_AUX_A) |			\
1879	BIT_ULL(POWER_DOMAIN_INIT))
1880
1881static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1882	.sync_hw = i9xx_power_well_sync_hw_noop,
1883	.enable = i9xx_always_on_power_well_noop,
1884	.disable = i9xx_always_on_power_well_noop,
1885	.is_enabled = i9xx_always_on_power_well_enabled,
1886};
1887
1888static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1889	.sync_hw = i9xx_power_well_sync_hw_noop,
1890	.enable = chv_pipe_power_well_enable,
1891	.disable = chv_pipe_power_well_disable,
1892	.is_enabled = chv_pipe_power_well_enabled,
1893};
1894
1895static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1896	.sync_hw = i9xx_power_well_sync_hw_noop,
1897	.enable = chv_dpio_cmn_power_well_enable,
1898	.disable = chv_dpio_cmn_power_well_disable,
1899	.is_enabled = vlv_power_well_enabled,
1900};
1901
1902static struct i915_power_well i9xx_always_on_power_well[] = {
1903	{
1904		.name = "always-on",
1905		.always_on = 1,
1906		.domains = POWER_DOMAIN_MASK,
1907		.ops = &i9xx_always_on_power_well_ops,
1908		.id = I915_DISP_PW_ALWAYS_ON,
1909	},
1910};
1911
1912static const struct i915_power_well_ops i830_pipes_power_well_ops = {
1913	.sync_hw = i830_pipes_power_well_sync_hw,
1914	.enable = i830_pipes_power_well_enable,
1915	.disable = i830_pipes_power_well_disable,
1916	.is_enabled = i830_pipes_power_well_enabled,
1917};
1918
1919static struct i915_power_well i830_power_wells[] = {
1920	{
1921		.name = "always-on",
1922		.always_on = 1,
1923		.domains = POWER_DOMAIN_MASK,
1924		.ops = &i9xx_always_on_power_well_ops,
1925		.id = I915_DISP_PW_ALWAYS_ON,
1926	},
1927	{
1928		.name = "pipes",
1929		.domains = I830_PIPES_POWER_DOMAINS,
1930		.ops = &i830_pipes_power_well_ops,
1931		.id = I830_DISP_PW_PIPES,
1932	},
1933};
1934
1935static const struct i915_power_well_ops hsw_power_well_ops = {
1936	.sync_hw = hsw_power_well_sync_hw,
1937	.enable = hsw_power_well_enable,
1938	.disable = hsw_power_well_disable,
1939	.is_enabled = hsw_power_well_enabled,
1940};
1941
 
 
 
 
 
 
 
1942static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1943	.sync_hw = i9xx_power_well_sync_hw_noop,
1944	.enable = gen9_dc_off_power_well_enable,
1945	.disable = gen9_dc_off_power_well_disable,
1946	.is_enabled = gen9_dc_off_power_well_enabled,
1947};
1948
1949static const struct i915_power_well_ops bxt_dpio_cmn_power_well_ops = {
1950	.sync_hw = i9xx_power_well_sync_hw_noop,
1951	.enable = bxt_dpio_cmn_power_well_enable,
1952	.disable = bxt_dpio_cmn_power_well_disable,
1953	.is_enabled = bxt_dpio_cmn_power_well_enabled,
1954};
1955
1956static struct i915_power_well hsw_power_wells[] = {
1957	{
1958		.name = "always-on",
1959		.always_on = 1,
1960		.domains = POWER_DOMAIN_MASK,
1961		.ops = &i9xx_always_on_power_well_ops,
1962		.id = I915_DISP_PW_ALWAYS_ON,
1963	},
1964	{
1965		.name = "display",
1966		.domains = HSW_DISPLAY_POWER_DOMAINS,
1967		.ops = &hsw_power_well_ops,
1968		.id = HSW_DISP_PW_GLOBAL,
1969		{
1970			.hsw.has_vga = true,
1971		},
1972	},
1973};
1974
1975static struct i915_power_well bdw_power_wells[] = {
1976	{
1977		.name = "always-on",
1978		.always_on = 1,
1979		.domains = POWER_DOMAIN_MASK,
1980		.ops = &i9xx_always_on_power_well_ops,
1981		.id = I915_DISP_PW_ALWAYS_ON,
1982	},
1983	{
1984		.name = "display",
1985		.domains = BDW_DISPLAY_POWER_DOMAINS,
1986		.ops = &hsw_power_well_ops,
1987		.id = HSW_DISP_PW_GLOBAL,
1988		{
1989			.hsw.irq_pipe_mask = BIT(PIPE_B) | BIT(PIPE_C),
1990			.hsw.has_vga = true,
1991		},
1992	},
1993};
1994
1995static const struct i915_power_well_ops vlv_display_power_well_ops = {
1996	.sync_hw = i9xx_power_well_sync_hw_noop,
1997	.enable = vlv_display_power_well_enable,
1998	.disable = vlv_display_power_well_disable,
1999	.is_enabled = vlv_power_well_enabled,
2000};
2001
2002static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
2003	.sync_hw = i9xx_power_well_sync_hw_noop,
2004	.enable = vlv_dpio_cmn_power_well_enable,
2005	.disable = vlv_dpio_cmn_power_well_disable,
2006	.is_enabled = vlv_power_well_enabled,
2007};
2008
2009static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
2010	.sync_hw = i9xx_power_well_sync_hw_noop,
2011	.enable = vlv_power_well_enable,
2012	.disable = vlv_power_well_disable,
2013	.is_enabled = vlv_power_well_enabled,
2014};
2015
2016static struct i915_power_well vlv_power_wells[] = {
2017	{
2018		.name = "always-on",
2019		.always_on = 1,
2020		.domains = POWER_DOMAIN_MASK,
2021		.ops = &i9xx_always_on_power_well_ops,
2022		.id = I915_DISP_PW_ALWAYS_ON,
2023	},
2024	{
2025		.name = "display",
2026		.domains = VLV_DISPLAY_POWER_DOMAINS,
2027		.id = PUNIT_POWER_WELL_DISP2D,
2028		.ops = &vlv_display_power_well_ops,
2029	},
2030	{
2031		.name = "dpio-tx-b-01",
2032		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
2033			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
2034			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
2035			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
2036		.ops = &vlv_dpio_power_well_ops,
2037		.id = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
2038	},
2039	{
2040		.name = "dpio-tx-b-23",
2041		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
2042			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
2043			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
2044			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
2045		.ops = &vlv_dpio_power_well_ops,
2046		.id = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
2047	},
2048	{
2049		.name = "dpio-tx-c-01",
2050		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
2051			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
2052			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
2053			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
2054		.ops = &vlv_dpio_power_well_ops,
2055		.id = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
2056	},
2057	{
2058		.name = "dpio-tx-c-23",
2059		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
2060			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
2061			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
2062			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
2063		.ops = &vlv_dpio_power_well_ops,
2064		.id = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
2065	},
2066	{
2067		.name = "dpio-common",
2068		.domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
2069		.id = PUNIT_POWER_WELL_DPIO_CMN_BC,
2070		.ops = &vlv_dpio_cmn_power_well_ops,
2071	},
2072};
2073
2074static struct i915_power_well chv_power_wells[] = {
2075	{
2076		.name = "always-on",
2077		.always_on = 1,
2078		.domains = POWER_DOMAIN_MASK,
2079		.ops = &i9xx_always_on_power_well_ops,
2080		.id = I915_DISP_PW_ALWAYS_ON,
2081	},
2082	{
2083		.name = "display",
2084		/*
2085		 * Pipe A power well is the new disp2d well. Pipe B and C
2086		 * power wells don't actually exist. Pipe A power well is
2087		 * required for any pipe to work.
2088		 */
2089		.domains = CHV_DISPLAY_POWER_DOMAINS,
2090		.id = CHV_DISP_PW_PIPE_A,
2091		.ops = &chv_pipe_power_well_ops,
2092	},
2093	{
2094		.name = "dpio-common-bc",
2095		.domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
2096		.id = PUNIT_POWER_WELL_DPIO_CMN_BC,
2097		.ops = &chv_dpio_cmn_power_well_ops,
2098	},
2099	{
2100		.name = "dpio-common-d",
2101		.domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
2102		.id = PUNIT_POWER_WELL_DPIO_CMN_D,
2103		.ops = &chv_dpio_cmn_power_well_ops,
2104	},
2105};
2106
2107bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
2108					 enum i915_power_well_id power_well_id)
2109{
2110	struct i915_power_well *power_well;
2111	bool ret;
2112
2113	power_well = lookup_power_well(dev_priv, power_well_id);
2114	ret = power_well->ops->is_enabled(dev_priv, power_well);
2115
2116	return ret;
2117}
2118
2119static struct i915_power_well skl_power_wells[] = {
2120	{
2121		.name = "always-on",
2122		.always_on = 1,
2123		.domains = POWER_DOMAIN_MASK,
2124		.ops = &i9xx_always_on_power_well_ops,
2125		.id = I915_DISP_PW_ALWAYS_ON,
2126	},
2127	{
2128		.name = "power well 1",
2129		/* Handled by the DMC firmware */
2130		.domains = 0,
2131		.ops = &hsw_power_well_ops,
2132		.id = SKL_DISP_PW_1,
2133		{
2134			.hsw.has_fuses = true,
2135		},
2136	},
2137	{
2138		.name = "MISC IO power well",
2139		/* Handled by the DMC firmware */
2140		.domains = 0,
2141		.ops = &hsw_power_well_ops,
2142		.id = SKL_DISP_PW_MISC_IO,
2143	},
2144	{
2145		.name = "DC off",
2146		.domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
2147		.ops = &gen9_dc_off_power_well_ops,
2148		.id = SKL_DISP_PW_DC_OFF,
2149	},
2150	{
2151		.name = "power well 2",
2152		.domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2153		.ops = &hsw_power_well_ops,
2154		.id = SKL_DISP_PW_2,
2155		{
2156			.hsw.irq_pipe_mask = BIT(PIPE_B) | BIT(PIPE_C),
2157			.hsw.has_vga = true,
2158			.hsw.has_fuses = true,
2159		},
2160	},
2161	{
2162		.name = "DDI A/E IO power well",
2163		.domains = SKL_DISPLAY_DDI_IO_A_E_POWER_DOMAINS,
2164		.ops = &hsw_power_well_ops,
2165		.id = SKL_DISP_PW_DDI_A_E,
2166	},
2167	{
2168		.name = "DDI B IO power well",
2169		.domains = SKL_DISPLAY_DDI_IO_B_POWER_DOMAINS,
2170		.ops = &hsw_power_well_ops,
2171		.id = SKL_DISP_PW_DDI_B,
2172	},
2173	{
2174		.name = "DDI C IO power well",
2175		.domains = SKL_DISPLAY_DDI_IO_C_POWER_DOMAINS,
2176		.ops = &hsw_power_well_ops,
2177		.id = SKL_DISP_PW_DDI_C,
2178	},
2179	{
2180		.name = "DDI D IO power well",
2181		.domains = SKL_DISPLAY_DDI_IO_D_POWER_DOMAINS,
2182		.ops = &hsw_power_well_ops,
2183		.id = SKL_DISP_PW_DDI_D,
2184	},
2185};
2186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2187static struct i915_power_well bxt_power_wells[] = {
2188	{
2189		.name = "always-on",
2190		.always_on = 1,
2191		.domains = POWER_DOMAIN_MASK,
2192		.ops = &i9xx_always_on_power_well_ops,
2193		.id = I915_DISP_PW_ALWAYS_ON,
2194	},
2195	{
2196		.name = "power well 1",
2197		.domains = 0,
2198		.ops = &hsw_power_well_ops,
2199		.id = SKL_DISP_PW_1,
2200		{
2201			.hsw.has_fuses = true,
2202		},
2203	},
2204	{
2205		.name = "DC off",
2206		.domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
2207		.ops = &gen9_dc_off_power_well_ops,
2208		.id = SKL_DISP_PW_DC_OFF,
2209	},
2210	{
2211		.name = "power well 2",
2212		.domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2213		.ops = &hsw_power_well_ops,
2214		.id = SKL_DISP_PW_2,
2215		{
2216			.hsw.irq_pipe_mask = BIT(PIPE_B) | BIT(PIPE_C),
2217			.hsw.has_vga = true,
2218			.hsw.has_fuses = true,
2219		},
2220	},
2221	{
2222		.name = "dpio-common-a",
2223		.domains = BXT_DPIO_CMN_A_POWER_DOMAINS,
2224		.ops = &bxt_dpio_cmn_power_well_ops,
2225		.id = BXT_DPIO_CMN_A,
2226		{
2227			.bxt.phy = DPIO_PHY1,
2228		},
2229	},
2230	{
2231		.name = "dpio-common-bc",
2232		.domains = BXT_DPIO_CMN_BC_POWER_DOMAINS,
2233		.ops = &bxt_dpio_cmn_power_well_ops,
2234		.id = BXT_DPIO_CMN_BC,
2235		{
2236			.bxt.phy = DPIO_PHY0,
2237		},
2238	},
2239};
2240
2241static struct i915_power_well glk_power_wells[] = {
2242	{
2243		.name = "always-on",
2244		.always_on = 1,
2245		.domains = POWER_DOMAIN_MASK,
2246		.ops = &i9xx_always_on_power_well_ops,
2247		.id = I915_DISP_PW_ALWAYS_ON,
2248	},
2249	{
2250		.name = "power well 1",
2251		/* Handled by the DMC firmware */
2252		.domains = 0,
2253		.ops = &hsw_power_well_ops,
2254		.id = SKL_DISP_PW_1,
2255		{
2256			.hsw.has_fuses = true,
2257		},
2258	},
2259	{
2260		.name = "DC off",
2261		.domains = GLK_DISPLAY_DC_OFF_POWER_DOMAINS,
2262		.ops = &gen9_dc_off_power_well_ops,
2263		.id = SKL_DISP_PW_DC_OFF,
2264	},
2265	{
2266		.name = "power well 2",
2267		.domains = GLK_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2268		.ops = &hsw_power_well_ops,
2269		.id = SKL_DISP_PW_2,
2270		{
2271			.hsw.irq_pipe_mask = BIT(PIPE_B) | BIT(PIPE_C),
2272			.hsw.has_vga = true,
2273			.hsw.has_fuses = true,
2274		},
2275	},
2276	{
2277		.name = "dpio-common-a",
2278		.domains = GLK_DPIO_CMN_A_POWER_DOMAINS,
2279		.ops = &bxt_dpio_cmn_power_well_ops,
2280		.id = BXT_DPIO_CMN_A,
2281		{
2282			.bxt.phy = DPIO_PHY1,
2283		},
2284	},
2285	{
2286		.name = "dpio-common-b",
2287		.domains = GLK_DPIO_CMN_B_POWER_DOMAINS,
2288		.ops = &bxt_dpio_cmn_power_well_ops,
2289		.id = BXT_DPIO_CMN_BC,
2290		{
2291			.bxt.phy = DPIO_PHY0,
2292		},
2293	},
2294	{
2295		.name = "dpio-common-c",
2296		.domains = GLK_DPIO_CMN_C_POWER_DOMAINS,
2297		.ops = &bxt_dpio_cmn_power_well_ops,
2298		.id = GLK_DPIO_CMN_C,
2299		{
2300			.bxt.phy = DPIO_PHY2,
2301		},
2302	},
2303	{
2304		.name = "AUX A",
2305		.domains = GLK_DISPLAY_AUX_A_POWER_DOMAINS,
2306		.ops = &hsw_power_well_ops,
2307		.id = GLK_DISP_PW_AUX_A,
2308	},
2309	{
2310		.name = "AUX B",
2311		.domains = GLK_DISPLAY_AUX_B_POWER_DOMAINS,
2312		.ops = &hsw_power_well_ops,
2313		.id = GLK_DISP_PW_AUX_B,
2314	},
2315	{
2316		.name = "AUX C",
2317		.domains = GLK_DISPLAY_AUX_C_POWER_DOMAINS,
2318		.ops = &hsw_power_well_ops,
2319		.id = GLK_DISP_PW_AUX_C,
2320	},
2321	{
2322		.name = "DDI A IO power well",
2323		.domains = GLK_DISPLAY_DDI_IO_A_POWER_DOMAINS,
2324		.ops = &hsw_power_well_ops,
2325		.id = GLK_DISP_PW_DDI_A,
2326	},
2327	{
2328		.name = "DDI B IO power well",
2329		.domains = GLK_DISPLAY_DDI_IO_B_POWER_DOMAINS,
2330		.ops = &hsw_power_well_ops,
2331		.id = SKL_DISP_PW_DDI_B,
2332	},
2333	{
2334		.name = "DDI C IO power well",
2335		.domains = GLK_DISPLAY_DDI_IO_C_POWER_DOMAINS,
2336		.ops = &hsw_power_well_ops,
2337		.id = SKL_DISP_PW_DDI_C,
2338	},
2339};
2340
2341static struct i915_power_well cnl_power_wells[] = {
2342	{
2343		.name = "always-on",
2344		.always_on = 1,
2345		.domains = POWER_DOMAIN_MASK,
2346		.ops = &i9xx_always_on_power_well_ops,
2347		.id = I915_DISP_PW_ALWAYS_ON,
2348	},
2349	{
2350		.name = "power well 1",
2351		/* Handled by the DMC firmware */
2352		.domains = 0,
2353		.ops = &hsw_power_well_ops,
2354		.id = SKL_DISP_PW_1,
2355		{
2356			.hsw.has_fuses = true,
2357		},
2358	},
2359	{
2360		.name = "AUX A",
2361		.domains = CNL_DISPLAY_AUX_A_POWER_DOMAINS,
2362		.ops = &hsw_power_well_ops,
2363		.id = CNL_DISP_PW_AUX_A,
2364	},
2365	{
2366		.name = "AUX B",
2367		.domains = CNL_DISPLAY_AUX_B_POWER_DOMAINS,
2368		.ops = &hsw_power_well_ops,
2369		.id = CNL_DISP_PW_AUX_B,
2370	},
2371	{
2372		.name = "AUX C",
2373		.domains = CNL_DISPLAY_AUX_C_POWER_DOMAINS,
2374		.ops = &hsw_power_well_ops,
2375		.id = CNL_DISP_PW_AUX_C,
2376	},
2377	{
2378		.name = "AUX D",
2379		.domains = CNL_DISPLAY_AUX_D_POWER_DOMAINS,
2380		.ops = &hsw_power_well_ops,
2381		.id = CNL_DISP_PW_AUX_D,
2382	},
2383	{
2384		.name = "DC off",
2385		.domains = CNL_DISPLAY_DC_OFF_POWER_DOMAINS,
2386		.ops = &gen9_dc_off_power_well_ops,
2387		.id = SKL_DISP_PW_DC_OFF,
2388	},
2389	{
2390		.name = "power well 2",
2391		.domains = CNL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2392		.ops = &hsw_power_well_ops,
2393		.id = SKL_DISP_PW_2,
2394		{
2395			.hsw.irq_pipe_mask = BIT(PIPE_B) | BIT(PIPE_C),
2396			.hsw.has_vga = true,
2397			.hsw.has_fuses = true,
2398		},
2399	},
2400	{
2401		.name = "DDI A IO power well",
2402		.domains = CNL_DISPLAY_DDI_A_IO_POWER_DOMAINS,
2403		.ops = &hsw_power_well_ops,
2404		.id = CNL_DISP_PW_DDI_A,
2405	},
2406	{
2407		.name = "DDI B IO power well",
2408		.domains = CNL_DISPLAY_DDI_B_IO_POWER_DOMAINS,
2409		.ops = &hsw_power_well_ops,
2410		.id = SKL_DISP_PW_DDI_B,
2411	},
2412	{
2413		.name = "DDI C IO power well",
2414		.domains = CNL_DISPLAY_DDI_C_IO_POWER_DOMAINS,
2415		.ops = &hsw_power_well_ops,
2416		.id = SKL_DISP_PW_DDI_C,
2417	},
2418	{
2419		.name = "DDI D IO power well",
2420		.domains = CNL_DISPLAY_DDI_D_IO_POWER_DOMAINS,
2421		.ops = &hsw_power_well_ops,
2422		.id = SKL_DISP_PW_DDI_D,
2423	},
2424	{
2425		.name = "DDI F IO power well",
2426		.domains = CNL_DISPLAY_DDI_F_IO_POWER_DOMAINS,
2427		.ops = &hsw_power_well_ops,
2428		.id = CNL_DISP_PW_DDI_F,
2429	},
2430	{
2431		.name = "AUX F",
2432		.domains = CNL_DISPLAY_AUX_F_POWER_DOMAINS,
2433		.ops = &hsw_power_well_ops,
2434		.id = CNL_DISP_PW_AUX_F,
2435	},
2436};
2437
2438static int
2439sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
2440				   int disable_power_well)
2441{
2442	if (disable_power_well >= 0)
2443		return !!disable_power_well;
2444
2445	return 1;
2446}
2447
2448static uint32_t get_allowed_dc_mask(const struct drm_i915_private *dev_priv,
2449				    int enable_dc)
2450{
2451	uint32_t mask;
2452	int requested_dc;
2453	int max_dc;
2454
2455	if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
2456		max_dc = 2;
2457		mask = 0;
2458	} else if (IS_GEN9_LP(dev_priv)) {
2459		max_dc = 1;
2460		/*
2461		 * DC9 has a separate HW flow from the rest of the DC states,
2462		 * not depending on the DMC firmware. It's needed by system
2463		 * suspend/resume, so allow it unconditionally.
2464		 */
2465		mask = DC_STATE_EN_DC9;
2466	} else {
2467		max_dc = 0;
2468		mask = 0;
2469	}
2470
2471	if (!i915_modparams.disable_power_well)
2472		max_dc = 0;
2473
2474	if (enable_dc >= 0 && enable_dc <= max_dc) {
2475		requested_dc = enable_dc;
2476	} else if (enable_dc == -1) {
2477		requested_dc = max_dc;
2478	} else if (enable_dc > max_dc && enable_dc <= 2) {
2479		DRM_DEBUG_KMS("Adjusting requested max DC state (%d->%d)\n",
2480			      enable_dc, max_dc);
2481		requested_dc = max_dc;
2482	} else {
2483		DRM_ERROR("Unexpected value for enable_dc (%d)\n", enable_dc);
2484		requested_dc = max_dc;
2485	}
2486
2487	if (requested_dc > 1)
2488		mask |= DC_STATE_EN_UPTO_DC6;
2489	if (requested_dc > 0)
2490		mask |= DC_STATE_EN_UPTO_DC5;
2491
2492	DRM_DEBUG_KMS("Allowed DC state mask %02x\n", mask);
2493
2494	return mask;
2495}
2496
2497static void assert_power_well_ids_unique(struct drm_i915_private *dev_priv)
2498{
2499	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2500	u64 power_well_ids;
2501	int i;
2502
2503	power_well_ids = 0;
2504	for (i = 0; i < power_domains->power_well_count; i++) {
2505		enum i915_power_well_id id = power_domains->power_wells[i].id;
2506
2507		WARN_ON(id >= sizeof(power_well_ids) * 8);
2508		WARN_ON(power_well_ids & BIT_ULL(id));
2509		power_well_ids |= BIT_ULL(id);
2510	}
2511}
2512
2513#define set_power_wells(power_domains, __power_wells) ({		\
2514	(power_domains)->power_wells = (__power_wells);			\
2515	(power_domains)->power_well_count = ARRAY_SIZE(__power_wells);	\
2516})
2517
2518/**
2519 * intel_power_domains_init - initializes the power domain structures
2520 * @dev_priv: i915 device instance
2521 *
2522 * Initializes the power domain structures for @dev_priv depending upon the
2523 * supported platform.
2524 */
2525int intel_power_domains_init(struct drm_i915_private *dev_priv)
2526{
2527	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2528
2529	i915_modparams.disable_power_well =
2530		sanitize_disable_power_well_option(dev_priv,
2531						   i915_modparams.disable_power_well);
2532	dev_priv->csr.allowed_dc_mask =
2533		get_allowed_dc_mask(dev_priv, i915_modparams.enable_dc);
2534
2535	BUILD_BUG_ON(POWER_DOMAIN_NUM > 64);
2536
2537	mutex_init(&power_domains->lock);
2538
2539	/*
2540	 * The enabling order will be from lower to higher indexed wells,
2541	 * the disabling order is reversed.
2542	 */
2543	if (IS_HASWELL(dev_priv)) {
2544		set_power_wells(power_domains, hsw_power_wells);
2545	} else if (IS_BROADWELL(dev_priv)) {
2546		set_power_wells(power_domains, bdw_power_wells);
2547	} else if (IS_GEN9_BC(dev_priv)) {
2548		set_power_wells(power_domains, skl_power_wells);
2549	} else if (IS_CANNONLAKE(dev_priv)) {
2550		set_power_wells(power_domains, cnl_power_wells);
2551
2552		/*
2553		 * DDI and Aux IO are getting enabled for all ports
2554		 * regardless the presence or use. So, in order to avoid
2555		 * timeouts, lets remove them from the list
2556		 * for the SKUs without port F.
2557		 */
2558		if (!IS_CNL_WITH_PORT_F(dev_priv))
2559			power_domains->power_well_count -= 2;
2560
2561	} else if (IS_BROXTON(dev_priv)) {
2562		set_power_wells(power_domains, bxt_power_wells);
2563	} else if (IS_GEMINILAKE(dev_priv)) {
2564		set_power_wells(power_domains, glk_power_wells);
2565	} else if (IS_CHERRYVIEW(dev_priv)) {
2566		set_power_wells(power_domains, chv_power_wells);
2567	} else if (IS_VALLEYVIEW(dev_priv)) {
2568		set_power_wells(power_domains, vlv_power_wells);
2569	} else if (IS_I830(dev_priv)) {
2570		set_power_wells(power_domains, i830_power_wells);
2571	} else {
2572		set_power_wells(power_domains, i9xx_always_on_power_well);
2573	}
2574
2575	assert_power_well_ids_unique(dev_priv);
2576
2577	return 0;
2578}
2579
2580/**
2581 * intel_power_domains_fini - finalizes the power domain structures
2582 * @dev_priv: i915 device instance
2583 *
2584 * Finalizes the power domain structures for @dev_priv depending upon the
2585 * supported platform. This function also disables runtime pm and ensures that
2586 * the device stays powered up so that the driver can be reloaded.
2587 */
2588void intel_power_domains_fini(struct drm_i915_private *dev_priv)
2589{
2590	struct device *kdev = &dev_priv->drm.pdev->dev;
2591
2592	/*
2593	 * The i915.ko module is still not prepared to be loaded when
2594	 * the power well is not enabled, so just enable it in case
2595	 * we're going to unload/reload.
2596	 * The following also reacquires the RPM reference the core passed
2597	 * to the driver during loading, which is dropped in
2598	 * intel_runtime_pm_enable(). We have to hand back the control of the
2599	 * device to the core with this reference held.
2600	 */
2601	intel_display_set_init_power(dev_priv, true);
2602
2603	/* Remove the refcount we took to keep power well support disabled. */
2604	if (!i915_modparams.disable_power_well)
2605		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
2606
2607	/*
2608	 * Remove the refcount we took in intel_runtime_pm_enable() in case
2609	 * the platform doesn't support runtime PM.
2610	 */
2611	if (!HAS_RUNTIME_PM(dev_priv))
2612		pm_runtime_put(kdev);
2613}
2614
2615static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
2616{
2617	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2618	struct i915_power_well *power_well;
 
2619
2620	mutex_lock(&power_domains->lock);
2621	for_each_power_well(dev_priv, power_well) {
2622		power_well->ops->sync_hw(dev_priv, power_well);
2623		power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2624								     power_well);
2625	}
2626	mutex_unlock(&power_domains->lock);
2627}
2628
2629static void gen9_dbuf_enable(struct drm_i915_private *dev_priv)
2630{
2631	I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
2632	POSTING_READ(DBUF_CTL);
2633
2634	udelay(10);
2635
2636	if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
2637		DRM_ERROR("DBuf power enable timeout\n");
2638}
2639
2640static void gen9_dbuf_disable(struct drm_i915_private *dev_priv)
2641{
2642	I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
2643	POSTING_READ(DBUF_CTL);
2644
2645	udelay(10);
2646
2647	if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
2648		DRM_ERROR("DBuf power disable timeout!\n");
2649}
2650
2651/*
2652 * TODO: we shouldn't always enable DBUF_CTL_S2, we should only enable it when
2653 * needed and keep it disabled as much as possible.
2654 */
2655static void icl_dbuf_enable(struct drm_i915_private *dev_priv)
2656{
2657	I915_WRITE(DBUF_CTL_S1, I915_READ(DBUF_CTL_S1) | DBUF_POWER_REQUEST);
2658	I915_WRITE(DBUF_CTL_S2, I915_READ(DBUF_CTL_S2) | DBUF_POWER_REQUEST);
2659	POSTING_READ(DBUF_CTL_S2);
2660
2661	udelay(10);
2662
2663	if (!(I915_READ(DBUF_CTL_S1) & DBUF_POWER_STATE) ||
2664	    !(I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE))
2665		DRM_ERROR("DBuf power enable timeout\n");
2666}
2667
2668static void icl_dbuf_disable(struct drm_i915_private *dev_priv)
2669{
2670	I915_WRITE(DBUF_CTL_S1, I915_READ(DBUF_CTL_S1) & ~DBUF_POWER_REQUEST);
2671	I915_WRITE(DBUF_CTL_S2, I915_READ(DBUF_CTL_S2) & ~DBUF_POWER_REQUEST);
2672	POSTING_READ(DBUF_CTL_S2);
2673
2674	udelay(10);
2675
2676	if ((I915_READ(DBUF_CTL_S1) & DBUF_POWER_STATE) ||
2677	    (I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE))
2678		DRM_ERROR("DBuf power disable timeout!\n");
2679}
2680
2681static void icl_mbus_init(struct drm_i915_private *dev_priv)
2682{
2683	uint32_t val;
2684
2685	val = MBUS_ABOX_BT_CREDIT_POOL1(16) |
2686	      MBUS_ABOX_BT_CREDIT_POOL2(16) |
2687	      MBUS_ABOX_B_CREDIT(1) |
2688	      MBUS_ABOX_BW_CREDIT(1);
2689
2690	I915_WRITE(MBUS_ABOX_CTL, val);
2691}
2692
2693static void skl_display_core_init(struct drm_i915_private *dev_priv,
2694				   bool resume)
2695{
2696	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2697	struct i915_power_well *well;
2698	uint32_t val;
2699
2700	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2701
2702	/* enable PCH reset handshake */
2703	val = I915_READ(HSW_NDE_RSTWRN_OPT);
2704	I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2705
2706	/* enable PG1 and Misc I/O */
2707	mutex_lock(&power_domains->lock);
 
 
2708
2709	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2710	intel_power_well_enable(dev_priv, well);
2711
2712	well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2713	intel_power_well_enable(dev_priv, well);
2714
2715	mutex_unlock(&power_domains->lock);
2716
2717	skl_init_cdclk(dev_priv);
2718
2719	gen9_dbuf_enable(dev_priv);
2720
2721	if (resume && dev_priv->csr.dmc_payload)
2722		intel_csr_load_program(dev_priv);
2723}
2724
2725static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2726{
2727	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2728	struct i915_power_well *well;
2729
2730	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2731
2732	gen9_dbuf_disable(dev_priv);
2733
2734	skl_uninit_cdclk(dev_priv);
2735
2736	/* The spec doesn't call for removing the reset handshake flag */
2737	/* disable PG1 and Misc I/O */
2738
2739	mutex_lock(&power_domains->lock);
2740
2741	/*
2742	 * BSpec says to keep the MISC IO power well enabled here, only
2743	 * remove our request for power well 1.
2744	 * Note that even though the driver's request is removed power well 1
2745	 * may stay enabled after this due to DMC's own request on it.
2746	 */
2747	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2748	intel_power_well_disable(dev_priv, well);
2749
2750	mutex_unlock(&power_domains->lock);
2751
2752	usleep_range(10, 30);		/* 10 us delay per Bspec */
2753}
2754
2755void bxt_display_core_init(struct drm_i915_private *dev_priv,
2756			   bool resume)
2757{
2758	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2759	struct i915_power_well *well;
2760	uint32_t val;
2761
2762	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2763
2764	/*
2765	 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
2766	 * or else the reset will hang because there is no PCH to respond.
2767	 * Move the handshake programming to initialization sequence.
2768	 * Previously was left up to BIOS.
2769	 */
2770	val = I915_READ(HSW_NDE_RSTWRN_OPT);
2771	val &= ~RESET_PCH_HANDSHAKE_ENABLE;
2772	I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
2773
2774	/* Enable PG1 */
2775	mutex_lock(&power_domains->lock);
2776
2777	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2778	intel_power_well_enable(dev_priv, well);
2779
2780	mutex_unlock(&power_domains->lock);
2781
2782	bxt_init_cdclk(dev_priv);
2783
2784	gen9_dbuf_enable(dev_priv);
2785
2786	if (resume && dev_priv->csr.dmc_payload)
2787		intel_csr_load_program(dev_priv);
2788}
2789
2790void bxt_display_core_uninit(struct drm_i915_private *dev_priv)
2791{
2792	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2793	struct i915_power_well *well;
2794
2795	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2796
2797	gen9_dbuf_disable(dev_priv);
2798
2799	bxt_uninit_cdclk(dev_priv);
2800
2801	/* The spec doesn't call for removing the reset handshake flag */
2802
2803	/*
2804	 * Disable PW1 (PG1).
2805	 * Note that even though the driver's request is removed power well 1
2806	 * may stay enabled after this due to DMC's own request on it.
2807	 */
2808	mutex_lock(&power_domains->lock);
2809
2810	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2811	intel_power_well_disable(dev_priv, well);
2812
2813	mutex_unlock(&power_domains->lock);
2814
2815	usleep_range(10, 30);		/* 10 us delay per Bspec */
2816}
2817
2818enum {
2819	PROCMON_0_85V_DOT_0,
2820	PROCMON_0_95V_DOT_0,
2821	PROCMON_0_95V_DOT_1,
2822	PROCMON_1_05V_DOT_0,
2823	PROCMON_1_05V_DOT_1,
2824};
2825
2826static const struct cnl_procmon {
2827	u32 dw1, dw9, dw10;
2828} cnl_procmon_values[] = {
2829	[PROCMON_0_85V_DOT_0] =
2830		{ .dw1 = 0x00000000, .dw9 = 0x62AB67BB, .dw10 = 0x51914F96, },
2831	[PROCMON_0_95V_DOT_0] =
2832		{ .dw1 = 0x00000000, .dw9 = 0x86E172C7, .dw10 = 0x77CA5EAB, },
2833	[PROCMON_0_95V_DOT_1] =
2834		{ .dw1 = 0x00000000, .dw9 = 0x93F87FE1, .dw10 = 0x8AE871C5, },
2835	[PROCMON_1_05V_DOT_0] =
2836		{ .dw1 = 0x00000000, .dw9 = 0x98FA82DD, .dw10 = 0x89E46DC1, },
2837	[PROCMON_1_05V_DOT_1] =
2838		{ .dw1 = 0x00440000, .dw9 = 0x9A00AB25, .dw10 = 0x8AE38FF1, },
2839};
2840
2841/*
2842 * CNL has just one set of registers, while ICL has two sets: one for port A and
2843 * the other for port B. The CNL registers are equivalent to the ICL port A
2844 * registers, that's why we call the ICL macros even though the function has CNL
2845 * on its name.
2846 */
2847static void cnl_set_procmon_ref_values(struct drm_i915_private *dev_priv,
2848				       enum port port)
2849{
2850	const struct cnl_procmon *procmon;
2851	u32 val;
2852
2853	val = I915_READ(ICL_PORT_COMP_DW3(port));
2854	switch (val & (PROCESS_INFO_MASK | VOLTAGE_INFO_MASK)) {
2855	default:
2856		MISSING_CASE(val);
2857	case VOLTAGE_INFO_0_85V | PROCESS_INFO_DOT_0:
2858		procmon = &cnl_procmon_values[PROCMON_0_85V_DOT_0];
2859		break;
2860	case VOLTAGE_INFO_0_95V | PROCESS_INFO_DOT_0:
2861		procmon = &cnl_procmon_values[PROCMON_0_95V_DOT_0];
2862		break;
2863	case VOLTAGE_INFO_0_95V | PROCESS_INFO_DOT_1:
2864		procmon = &cnl_procmon_values[PROCMON_0_95V_DOT_1];
2865		break;
2866	case VOLTAGE_INFO_1_05V | PROCESS_INFO_DOT_0:
2867		procmon = &cnl_procmon_values[PROCMON_1_05V_DOT_0];
2868		break;
2869	case VOLTAGE_INFO_1_05V | PROCESS_INFO_DOT_1:
2870		procmon = &cnl_procmon_values[PROCMON_1_05V_DOT_1];
2871		break;
2872	}
2873
2874	val = I915_READ(ICL_PORT_COMP_DW1(port));
2875	val &= ~((0xff << 16) | 0xff);
2876	val |= procmon->dw1;
2877	I915_WRITE(ICL_PORT_COMP_DW1(port), val);
2878
2879	I915_WRITE(ICL_PORT_COMP_DW9(port), procmon->dw9);
2880	I915_WRITE(ICL_PORT_COMP_DW10(port), procmon->dw10);
2881}
2882
2883static void cnl_display_core_init(struct drm_i915_private *dev_priv, bool resume)
2884{
2885	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2886	struct i915_power_well *well;
2887	u32 val;
2888
2889	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2890
2891	/* 1. Enable PCH Reset Handshake */
2892	val = I915_READ(HSW_NDE_RSTWRN_OPT);
2893	val |= RESET_PCH_HANDSHAKE_ENABLE;
2894	I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
2895
2896	/* 2. Enable Comp */
2897	val = I915_READ(CHICKEN_MISC_2);
2898	val &= ~CNL_COMP_PWR_DOWN;
2899	I915_WRITE(CHICKEN_MISC_2, val);
2900
2901	/* Dummy PORT_A to get the correct CNL register from the ICL macro */
2902	cnl_set_procmon_ref_values(dev_priv, PORT_A);
2903
2904	val = I915_READ(CNL_PORT_COMP_DW0);
2905	val |= COMP_INIT;
2906	I915_WRITE(CNL_PORT_COMP_DW0, val);
2907
2908	/* 3. */
2909	val = I915_READ(CNL_PORT_CL1CM_DW5);
2910	val |= CL_POWER_DOWN_ENABLE;
2911	I915_WRITE(CNL_PORT_CL1CM_DW5, val);
2912
2913	/*
2914	 * 4. Enable Power Well 1 (PG1).
2915	 *    The AUX IO power wells will be enabled on demand.
2916	 */
2917	mutex_lock(&power_domains->lock);
2918	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2919	intel_power_well_enable(dev_priv, well);
2920	mutex_unlock(&power_domains->lock);
2921
2922	/* 5. Enable CD clock */
2923	cnl_init_cdclk(dev_priv);
2924
2925	/* 6. Enable DBUF */
2926	gen9_dbuf_enable(dev_priv);
2927
2928	if (resume && dev_priv->csr.dmc_payload)
2929		intel_csr_load_program(dev_priv);
2930}
2931
2932static void cnl_display_core_uninit(struct drm_i915_private *dev_priv)
2933{
2934	struct i915_power_domains *power_domains = &dev_priv->power_domains;
2935	struct i915_power_well *well;
2936	u32 val;
2937
2938	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2939
2940	/* 1. Disable all display engine functions -> aready done */
2941
2942	/* 2. Disable DBUF */
2943	gen9_dbuf_disable(dev_priv);
2944
2945	/* 3. Disable CD clock */
2946	cnl_uninit_cdclk(dev_priv);
2947
2948	/*
2949	 * 4. Disable Power Well 1 (PG1).
2950	 *    The AUX IO power wells are toggled on demand, so they are already
2951	 *    disabled at this point.
2952	 */
2953	mutex_lock(&power_domains->lock);
2954	well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2955	intel_power_well_disable(dev_priv, well);
2956	mutex_unlock(&power_domains->lock);
2957
2958	usleep_range(10, 30);		/* 10 us delay per Bspec */
2959
2960	/* 5. Disable Comp */
2961	val = I915_READ(CHICKEN_MISC_2);
2962	val |= CNL_COMP_PWR_DOWN;
2963	I915_WRITE(CHICKEN_MISC_2, val);
2964}
2965
2966static void icl_display_core_init(struct drm_i915_private *dev_priv,
2967				  bool resume)
2968{
2969	enum port port;
2970	u32 val;
2971
2972	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2973
2974	/* 1. Enable PCH reset handshake. */
2975	val = I915_READ(HSW_NDE_RSTWRN_OPT);
2976	val |= RESET_PCH_HANDSHAKE_ENABLE;
2977	I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
2978
2979	for (port = PORT_A; port <= PORT_B; port++) {
2980		/* 2. Enable DDI combo PHY comp. */
2981		val = I915_READ(ICL_PHY_MISC(port));
2982		val &= ~ICL_PHY_MISC_DE_IO_COMP_PWR_DOWN;
2983		I915_WRITE(ICL_PHY_MISC(port), val);
2984
2985		cnl_set_procmon_ref_values(dev_priv, port);
2986
2987		val = I915_READ(ICL_PORT_COMP_DW0(port));
2988		val |= COMP_INIT;
2989		I915_WRITE(ICL_PORT_COMP_DW0(port), val);
2990
2991		/* 3. Set power down enable. */
2992		val = I915_READ(ICL_PORT_CL_DW5(port));
2993		val |= CL_POWER_DOWN_ENABLE;
2994		I915_WRITE(ICL_PORT_CL_DW5(port), val);
2995	}
2996
2997	/* 4. Enable power well 1 (PG1) and aux IO power. */
2998	/* FIXME: ICL power wells code not here yet. */
2999
3000	/* 5. Enable CDCLK. */
3001	icl_init_cdclk(dev_priv);
3002
3003	/* 6. Enable DBUF. */
3004	icl_dbuf_enable(dev_priv);
3005
3006	/* 7. Setup MBUS. */
3007	icl_mbus_init(dev_priv);
3008
3009	/* 8. CHICKEN_DCPR_1 */
3010	I915_WRITE(GEN8_CHICKEN_DCPR_1, I915_READ(GEN8_CHICKEN_DCPR_1) |
3011					CNL_DDI_CLOCK_REG_ACCESS_ON);
3012}
3013
3014static void icl_display_core_uninit(struct drm_i915_private *dev_priv)
3015{
3016	enum port port;
3017	u32 val;
3018
3019	gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
3020
3021	/* 1. Disable all display engine functions -> aready done */
3022
3023	/* 2. Disable DBUF */
3024	icl_dbuf_disable(dev_priv);
3025
3026	/* 3. Disable CD clock */
3027	icl_uninit_cdclk(dev_priv);
3028
3029	/* 4. Disable Power Well 1 (PG1) and Aux IO Power */
3030	/* FIXME: ICL power wells code not here yet. */
3031
3032	/* 5. Disable Comp */
3033	for (port = PORT_A; port <= PORT_B; port++) {
3034		val = I915_READ(ICL_PHY_MISC(port));
3035		val |= ICL_PHY_MISC_DE_IO_COMP_PWR_DOWN;
3036		I915_WRITE(ICL_PHY_MISC(port), val);
3037	}
3038}
3039
3040static void chv_phy_control_init(struct drm_i915_private *dev_priv)
3041{
3042	struct i915_power_well *cmn_bc =
3043		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
3044	struct i915_power_well *cmn_d =
3045		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
3046
3047	/*
3048	 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
3049	 * workaround never ever read DISPLAY_PHY_CONTROL, and
3050	 * instead maintain a shadow copy ourselves. Use the actual
3051	 * power well state and lane status to reconstruct the
3052	 * expected initial value.
3053	 */
3054	dev_priv->chv_phy_control =
3055		PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
3056		PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
3057		PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
3058		PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
3059		PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
3060
3061	/*
3062	 * If all lanes are disabled we leave the override disabled
3063	 * with all power down bits cleared to match the state we
3064	 * would use after disabling the port. Otherwise enable the
3065	 * override and set the lane powerdown bits accding to the
3066	 * current lane status.
3067	 */
3068	if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
3069		uint32_t status = I915_READ(DPLL(PIPE_A));
3070		unsigned int mask;
3071
3072		mask = status & DPLL_PORTB_READY_MASK;
3073		if (mask == 0xf)
3074			mask = 0x0;
3075		else
3076			dev_priv->chv_phy_control |=
3077				PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
3078
3079		dev_priv->chv_phy_control |=
3080			PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
3081
3082		mask = (status & DPLL_PORTC_READY_MASK) >> 4;
3083		if (mask == 0xf)
3084			mask = 0x0;
3085		else
3086			dev_priv->chv_phy_control |=
3087				PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
3088
3089		dev_priv->chv_phy_control |=
3090			PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
3091
3092		dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
3093
3094		dev_priv->chv_phy_assert[DPIO_PHY0] = false;
3095	} else {
3096		dev_priv->chv_phy_assert[DPIO_PHY0] = true;
3097	}
3098
3099	if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
3100		uint32_t status = I915_READ(DPIO_PHY_STATUS);
3101		unsigned int mask;
3102
3103		mask = status & DPLL_PORTD_READY_MASK;
3104
3105		if (mask == 0xf)
3106			mask = 0x0;
3107		else
3108			dev_priv->chv_phy_control |=
3109				PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
3110
3111		dev_priv->chv_phy_control |=
3112			PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
3113
3114		dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
3115
3116		dev_priv->chv_phy_assert[DPIO_PHY1] = false;
3117	} else {
3118		dev_priv->chv_phy_assert[DPIO_PHY1] = true;
3119	}
3120
3121	I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
3122
3123	DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
3124		      dev_priv->chv_phy_control);
3125}
3126
3127static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
3128{
3129	struct i915_power_well *cmn =
3130		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
3131	struct i915_power_well *disp2d =
3132		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
3133
3134	/* If the display might be already active skip this */
3135	if (cmn->ops->is_enabled(dev_priv, cmn) &&
3136	    disp2d->ops->is_enabled(dev_priv, disp2d) &&
3137	    I915_READ(DPIO_CTL) & DPIO_CMNRST)
3138		return;
3139
3140	DRM_DEBUG_KMS("toggling display PHY side reset\n");
3141
3142	/* cmnlane needs DPLL registers */
3143	disp2d->ops->enable(dev_priv, disp2d);
3144
3145	/*
3146	 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
3147	 * Need to assert and de-assert PHY SB reset by gating the
3148	 * common lane power, then un-gating it.
3149	 * Simply ungating isn't enough to reset the PHY enough to get
3150	 * ports and lanes running.
3151	 */
3152	cmn->ops->disable(dev_priv, cmn);
3153}
3154
3155/**
3156 * intel_power_domains_init_hw - initialize hardware power domain state
3157 * @dev_priv: i915 device instance
3158 * @resume: Called from resume code paths or not
3159 *
3160 * This function initializes the hardware power domain state and enables all
3161 * power wells belonging to the INIT power domain. Power wells in other
3162 * domains (and not in the INIT domain) are referenced or disabled during the
3163 * modeset state HW readout. After that the reference count of each power well
3164 * must match its HW enabled state, see intel_power_domains_verify_state().
3165 */
3166void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
3167{
 
3168	struct i915_power_domains *power_domains = &dev_priv->power_domains;
3169
3170	power_domains->initializing = true;
3171
3172	if (IS_ICELAKE(dev_priv)) {
3173		icl_display_core_init(dev_priv, resume);
3174	} else if (IS_CANNONLAKE(dev_priv)) {
3175		cnl_display_core_init(dev_priv, resume);
3176	} else if (IS_GEN9_BC(dev_priv)) {
3177		skl_display_core_init(dev_priv, resume);
3178	} else if (IS_GEN9_LP(dev_priv)) {
3179		bxt_display_core_init(dev_priv, resume);
3180	} else if (IS_CHERRYVIEW(dev_priv)) {
3181		mutex_lock(&power_domains->lock);
3182		chv_phy_control_init(dev_priv);
3183		mutex_unlock(&power_domains->lock);
3184	} else if (IS_VALLEYVIEW(dev_priv)) {
3185		mutex_lock(&power_domains->lock);
3186		vlv_cmnlane_wa(dev_priv);
3187		mutex_unlock(&power_domains->lock);
3188	}
3189
3190	/* For now, we need the power well to be always enabled. */
3191	intel_display_set_init_power(dev_priv, true);
3192	/* Disable power support if the user asked so. */
3193	if (!i915_modparams.disable_power_well)
3194		intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
3195	intel_power_domains_sync_hw(dev_priv);
3196	power_domains->initializing = false;
3197}
3198
3199/**
3200 * intel_power_domains_suspend - suspend power domain state
3201 * @dev_priv: i915 device instance
3202 *
3203 * This function prepares the hardware power domain state before entering
3204 * system suspend. It must be paired with intel_power_domains_init_hw().
3205 */
3206void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
3207{
3208	/*
3209	 * Even if power well support was disabled we still want to disable
3210	 * power wells while we are system suspended.
3211	 */
3212	if (!i915_modparams.disable_power_well)
3213		intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
3214
3215	if (IS_ICELAKE(dev_priv))
3216		icl_display_core_uninit(dev_priv);
3217	else if (IS_CANNONLAKE(dev_priv))
3218		cnl_display_core_uninit(dev_priv);
3219	else if (IS_GEN9_BC(dev_priv))
3220		skl_display_core_uninit(dev_priv);
3221	else if (IS_GEN9_LP(dev_priv))
3222		bxt_display_core_uninit(dev_priv);
3223}
3224
3225static void intel_power_domains_dump_info(struct drm_i915_private *dev_priv)
3226{
3227	struct i915_power_domains *power_domains = &dev_priv->power_domains;
3228	struct i915_power_well *power_well;
3229
3230	for_each_power_well(dev_priv, power_well) {
3231		enum intel_display_power_domain domain;
3232
3233		DRM_DEBUG_DRIVER("%-25s %d\n",
3234				 power_well->name, power_well->count);
3235
3236		for_each_power_domain(domain, power_well->domains)
3237			DRM_DEBUG_DRIVER("  %-23s %d\n",
3238					 intel_display_power_domain_str(domain),
3239					 power_domains->domain_use_count[domain]);
3240	}
3241}
3242
3243/**
3244 * intel_power_domains_verify_state - verify the HW/SW state for all power wells
3245 * @dev_priv: i915 device instance
3246 *
3247 * Verify if the reference count of each power well matches its HW enabled
3248 * state and the total refcount of the domains it belongs to. This must be
3249 * called after modeset HW state sanitization, which is responsible for
3250 * acquiring reference counts for any power wells in use and disabling the
3251 * ones left on by BIOS but not required by any active output.
3252 */
3253void intel_power_domains_verify_state(struct drm_i915_private *dev_priv)
3254{
3255	struct i915_power_domains *power_domains = &dev_priv->power_domains;
3256	struct i915_power_well *power_well;
3257	bool dump_domain_info;
3258
3259	mutex_lock(&power_domains->lock);
3260
3261	dump_domain_info = false;
3262	for_each_power_well(dev_priv, power_well) {
3263		enum intel_display_power_domain domain;
3264		int domains_count;
3265		bool enabled;
3266
3267		/*
3268		 * Power wells not belonging to any domain (like the MISC_IO
3269		 * and PW1 power wells) are under FW control, so ignore them,
3270		 * since their state can change asynchronously.
3271		 */
3272		if (!power_well->domains)
3273			continue;
3274
3275		enabled = power_well->ops->is_enabled(dev_priv, power_well);
3276		if ((power_well->count || power_well->always_on) != enabled)
3277			DRM_ERROR("power well %s state mismatch (refcount %d/enabled %d)",
3278				  power_well->name, power_well->count, enabled);
3279
3280		domains_count = 0;
3281		for_each_power_domain(domain, power_well->domains)
3282			domains_count += power_domains->domain_use_count[domain];
3283
3284		if (power_well->count != domains_count) {
3285			DRM_ERROR("power well %s refcount/domain refcount mismatch "
3286				  "(refcount %d/domains refcount %d)\n",
3287				  power_well->name, power_well->count,
3288				  domains_count);
3289			dump_domain_info = true;
3290		}
3291	}
3292
3293	if (dump_domain_info) {
3294		static bool dumped;
3295
3296		if (!dumped) {
3297			intel_power_domains_dump_info(dev_priv);
3298			dumped = true;
3299		}
3300	}
3301
3302	mutex_unlock(&power_domains->lock);
3303}
3304
3305/**
3306 * intel_runtime_pm_get - grab a runtime pm reference
3307 * @dev_priv: i915 device instance
3308 *
3309 * This function grabs a device-level runtime pm reference (mostly used for GEM
3310 * code to ensure the GTT or GT is on) and ensures that it is powered up.
3311 *
3312 * Any runtime pm reference obtained by this function must have a symmetric
3313 * call to intel_runtime_pm_put() to release the reference again.
3314 */
3315void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
3316{
3317	struct pci_dev *pdev = dev_priv->drm.pdev;
3318	struct device *kdev = &pdev->dev;
3319	int ret;
3320
3321	ret = pm_runtime_get_sync(kdev);
3322	WARN_ONCE(ret < 0, "pm_runtime_get_sync() failed: %d\n", ret);
3323
3324	atomic_inc(&dev_priv->runtime_pm.wakeref_count);
3325	assert_rpm_wakelock_held(dev_priv);
3326}
3327
3328/**
3329 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
3330 * @dev_priv: i915 device instance
3331 *
3332 * This function grabs a device-level runtime pm reference if the device is
3333 * already in use and ensures that it is powered up. It is illegal to try
3334 * and access the HW should intel_runtime_pm_get_if_in_use() report failure.
3335 *
3336 * Any runtime pm reference obtained by this function must have a symmetric
3337 * call to intel_runtime_pm_put() to release the reference again.
3338 *
3339 * Returns: True if the wakeref was acquired, or False otherwise.
3340 */
3341bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
3342{
 
 
 
3343	if (IS_ENABLED(CONFIG_PM)) {
3344		struct pci_dev *pdev = dev_priv->drm.pdev;
3345		struct device *kdev = &pdev->dev;
3346
3347		/*
3348		 * In cases runtime PM is disabled by the RPM core and we get
3349		 * an -EINVAL return value we are not supposed to call this
3350		 * function, since the power state is undefined. This applies
3351		 * atm to the late/early system suspend/resume handlers.
3352		 */
3353		if (pm_runtime_get_if_in_use(kdev) <= 0)
 
3354			return false;
3355	}
3356
3357	atomic_inc(&dev_priv->runtime_pm.wakeref_count);
3358	assert_rpm_wakelock_held(dev_priv);
3359
3360	return true;
3361}
3362
3363/**
3364 * intel_runtime_pm_get_noresume - grab a runtime pm reference
3365 * @dev_priv: i915 device instance
3366 *
3367 * This function grabs a device-level runtime pm reference (mostly used for GEM
3368 * code to ensure the GTT or GT is on).
3369 *
3370 * It will _not_ power up the device but instead only check that it's powered
3371 * on.  Therefore it is only valid to call this functions from contexts where
3372 * the device is known to be powered up and where trying to power it up would
3373 * result in hilarity and deadlocks. That pretty much means only the system
3374 * suspend/resume code where this is used to grab runtime pm references for
3375 * delayed setup down in work items.
3376 *
3377 * Any runtime pm reference obtained by this function must have a symmetric
3378 * call to intel_runtime_pm_put() to release the reference again.
3379 */
3380void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
3381{
3382	struct pci_dev *pdev = dev_priv->drm.pdev;
3383	struct device *kdev = &pdev->dev;
3384
3385	assert_rpm_wakelock_held(dev_priv);
3386	pm_runtime_get_noresume(kdev);
3387
3388	atomic_inc(&dev_priv->runtime_pm.wakeref_count);
3389}
3390
3391/**
3392 * intel_runtime_pm_put - release a runtime pm reference
3393 * @dev_priv: i915 device instance
3394 *
3395 * This function drops the device-level runtime pm reference obtained by
3396 * intel_runtime_pm_get() and might power down the corresponding
3397 * hardware block right away if this is the last reference.
3398 */
3399void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
3400{
3401	struct pci_dev *pdev = dev_priv->drm.pdev;
3402	struct device *kdev = &pdev->dev;
3403
3404	assert_rpm_wakelock_held(dev_priv);
3405	atomic_dec(&dev_priv->runtime_pm.wakeref_count);
 
3406
3407	pm_runtime_mark_last_busy(kdev);
3408	pm_runtime_put_autosuspend(kdev);
3409}
3410
3411/**
3412 * intel_runtime_pm_enable - enable runtime pm
3413 * @dev_priv: i915 device instance
3414 *
3415 * This function enables runtime pm at the end of the driver load sequence.
3416 *
3417 * Note that this function does currently not enable runtime pm for the
3418 * subordinate display power domains. That is only done on the first modeset
3419 * using intel_display_set_init_power().
3420 */
3421void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
3422{
3423	struct pci_dev *pdev = dev_priv->drm.pdev;
3424	struct device *kdev = &pdev->dev;
3425
3426	pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */
3427	pm_runtime_mark_last_busy(kdev);
3428
3429	/*
3430	 * Take a permanent reference to disable the RPM functionality and drop
3431	 * it only when unloading the driver. Use the low level get/put helpers,
3432	 * so the driver's own RPM reference tracking asserts also work on
3433	 * platforms without RPM support.
3434	 */
3435	if (!HAS_RUNTIME_PM(dev_priv)) {
3436		int ret;
3437
3438		pm_runtime_dont_use_autosuspend(kdev);
3439		ret = pm_runtime_get_sync(kdev);
3440		WARN(ret < 0, "pm_runtime_get_sync() failed: %d\n", ret);
3441	} else {
3442		pm_runtime_use_autosuspend(kdev);
3443	}
3444
3445	/*
3446	 * The core calls the driver load handler with an RPM reference held.
3447	 * We drop that here and will reacquire it during unloading in
3448	 * intel_power_domains_fini().
3449	 */
3450	pm_runtime_put_autosuspend(kdev);
3451}