blob: ec4faae49b3f6a21208c1e87ca617950e6c06201 [file] [log] [blame]
Daniel Vetter9c065a72014-09-30 10:56:38 +02001/*
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"
Daniel Vetter9c065a72014-09-30 10:56:38 +020034
Daniel Vettere4e76842014-09-30 10:56:42 +020035/**
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
Daniel Vetter9c065a72014-09-30 10:56:38 +020052#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++) \
Jani Nikula95150bd2015-11-24 21:21:56 +020057 for_each_if ((power_well)->domains & (domain_mask))
Daniel Vetter9c065a72014-09-30 10:56:38 +020058
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--) \
Jani Nikula95150bd2015-11-24 21:21:56 +020063 for_each_if ((power_well)->domains & (domain_mask))
Daniel Vetter9c065a72014-09-30 10:56:38 +020064
Suketu Shah5aefb232015-04-16 14:22:10 +053065bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
66 int power_well_id);
67
Daniel Stone9895ad02015-11-20 15:55:33 +000068const 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
Damien Lespiaue8ca9322015-07-30 18:20:26 -0300134static 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
Damien Lespiaudcddab32015-07-30 18:20:27 -0300142static 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
Daniel Vettere4e76842014-09-30 10:56:42 +0200150/*
Daniel Vetter9c065a72014-09-30 10:56:38 +0200151 * 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
Daniel Vettere4e76842014-09-30 10:56:42 +0200162/**
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 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200174bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
175 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200176{
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
Daniel Vettere4e76842014-09-30 10:56:42 +0200202/**
Damien Lespiauf61ccae2014-11-25 13:45:41 +0000203 * intel_display_power_is_enabled - check for a power domain
Daniel Vettere4e76842014-09-30 10:56:42 +0200204 * @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 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200219bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
220 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200221{
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);
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200228 ret = __intel_display_power_is_enabled(dev_priv, domain);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200229 mutex_unlock(&power_domains->lock);
230
231 return ret;
232}
233
Daniel Vettere4e76842014-09-30 10:56:42 +0200234/**
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 */
Daniel Vetterd9bc89d92014-09-30 10:56:40 +0200244void 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
Daniel Vetter9c065a72014-09-30 10:56:38 +0200258/*
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
Damien Lespiau25400392015-03-06 18:50:52 +0000282 if (IS_BROADWELL(dev))
Damien Lespiau4c6c03b2015-03-06 18:50:48 +0000283 gen8_irq_power_well_post_enable(dev_priv,
284 1 << PIPE_C | 1 << PIPE_B);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200285}
286
Damien Lespiaud14c0342015-03-06 18:50:51 +0000287static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
288 struct i915_power_well *power_well)
289{
290 struct drm_device *dev = dev_priv->dev;
291
292 /*
293 * After we re-enable the power well, if we touch VGA register 0x3d5
294 * we'll get unclaimed register interrupts. This stops after we write
295 * anything to the VGA MSR register. The vgacon module uses this
296 * register all the time, so if we unbind our driver and, as a
297 * consequence, bind vgacon, we'll get stuck in an infinite loop at
298 * console_unlock(). So make here we touch the VGA MSR register, making
299 * sure vgacon can keep working normally without triggering interrupts
300 * and error messages.
301 */
302 if (power_well->data == SKL_DISP_PW_2) {
303 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
304 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
305 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
306
307 gen8_irq_power_well_post_enable(dev_priv,
308 1 << PIPE_C | 1 << PIPE_B);
309 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000310}
311
Daniel Vetter9c065a72014-09-30 10:56:38 +0200312static void hsw_set_power_well(struct drm_i915_private *dev_priv,
313 struct i915_power_well *power_well, bool enable)
314{
315 bool is_enabled, enable_requested;
316 uint32_t tmp;
317
318 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
319 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
320 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
321
322 if (enable) {
323 if (!enable_requested)
324 I915_WRITE(HSW_PWR_WELL_DRIVER,
325 HSW_PWR_WELL_ENABLE_REQUEST);
326
327 if (!is_enabled) {
328 DRM_DEBUG_KMS("Enabling power well\n");
329 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
330 HSW_PWR_WELL_STATE_ENABLED), 20))
331 DRM_ERROR("Timeout enabling power well\n");
Paulo Zanoni6d729bf2014-10-07 16:11:11 -0300332 hsw_power_well_post_enable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200333 }
334
Daniel Vetter9c065a72014-09-30 10:56:38 +0200335 } else {
336 if (enable_requested) {
337 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
338 POSTING_READ(HSW_PWR_WELL_DRIVER);
339 DRM_DEBUG_KMS("Requesting to disable the power well\n");
340 }
341 }
342}
343
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000344#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
345 BIT(POWER_DOMAIN_TRANSCODER_A) | \
346 BIT(POWER_DOMAIN_PIPE_B) | \
347 BIT(POWER_DOMAIN_TRANSCODER_B) | \
348 BIT(POWER_DOMAIN_PIPE_C) | \
349 BIT(POWER_DOMAIN_TRANSCODER_C) | \
350 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
351 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100352 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
353 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
354 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
355 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000356 BIT(POWER_DOMAIN_AUX_B) | \
357 BIT(POWER_DOMAIN_AUX_C) | \
358 BIT(POWER_DOMAIN_AUX_D) | \
359 BIT(POWER_DOMAIN_AUDIO) | \
360 BIT(POWER_DOMAIN_VGA) | \
361 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000362#define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100363 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
364 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000365 BIT(POWER_DOMAIN_INIT))
366#define SKL_DISPLAY_DDI_B_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100367 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000368 BIT(POWER_DOMAIN_INIT))
369#define SKL_DISPLAY_DDI_C_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100370 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000371 BIT(POWER_DOMAIN_INIT))
372#define SKL_DISPLAY_DDI_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100373 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000374 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100375#define SKL_DISPLAY_DC_OFF_POWER_DOMAINS ( \
376 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
377 BIT(POWER_DOMAIN_MODESET) | \
378 BIT(POWER_DOMAIN_AUX_A) | \
379 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000380#define SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
Imre Deak4a76f292015-11-04 19:24:15 +0200381 (POWER_DOMAIN_MASK & ~( \
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100382 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
383 SKL_DISPLAY_DC_OFF_POWER_DOMAINS)) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000384 BIT(POWER_DOMAIN_INIT))
385
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530386#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
387 BIT(POWER_DOMAIN_TRANSCODER_A) | \
388 BIT(POWER_DOMAIN_PIPE_B) | \
389 BIT(POWER_DOMAIN_TRANSCODER_B) | \
390 BIT(POWER_DOMAIN_PIPE_C) | \
391 BIT(POWER_DOMAIN_TRANSCODER_C) | \
392 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
393 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100394 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
395 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530396 BIT(POWER_DOMAIN_AUX_B) | \
397 BIT(POWER_DOMAIN_AUX_C) | \
398 BIT(POWER_DOMAIN_AUDIO) | \
399 BIT(POWER_DOMAIN_VGA) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100400 BIT(POWER_DOMAIN_GMBUS) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530401 BIT(POWER_DOMAIN_INIT))
402#define BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS ( \
403 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
404 BIT(POWER_DOMAIN_PIPE_A) | \
405 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
406 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100407 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530408 BIT(POWER_DOMAIN_AUX_A) | \
409 BIT(POWER_DOMAIN_PLLS) | \
410 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100411#define BXT_DISPLAY_DC_OFF_POWER_DOMAINS ( \
412 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
413 BIT(POWER_DOMAIN_MODESET) | \
414 BIT(POWER_DOMAIN_AUX_A) | \
415 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530416#define BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
417 (POWER_DOMAIN_MASK & ~(BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
418 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS)) | \
419 BIT(POWER_DOMAIN_INIT))
420
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530421static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
422{
423 struct drm_device *dev = dev_priv->dev;
424
425 WARN(!IS_BROXTON(dev), "Platform doesn't support DC9.\n");
426 WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
427 "DC9 already programmed to be enabled.\n");
428 WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
429 "DC5 still not disabled to enable DC9.\n");
430 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
431 WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
432
433 /*
434 * TODO: check for the following to verify the conditions to enter DC9
435 * state are satisfied:
436 * 1] Check relevant display engine registers to verify if mode set
437 * disable sequence was followed.
438 * 2] Check if display uninitialize sequence is initialized.
439 */
440}
441
442static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
443{
444 WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
445 WARN(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
446 "DC9 already programmed to be disabled.\n");
447 WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
448 "DC5 still not disabled.\n");
449
450 /*
451 * TODO: check for the following to verify DC9 state was indeed
452 * entered before programming to disable it:
453 * 1] Check relevant display engine registers to verify if mode
454 * set disable sequence was followed.
455 * 2] Check if display uninitialize sequence is initialized.
456 */
457}
458
Mika Kuoppala5b076882016-02-19 12:26:04 +0200459static void gen9_set_dc_state_debugmask(struct drm_i915_private *dev_priv)
Patrik Jakobsson4deccbb2015-11-09 16:48:17 +0100460{
Mika Kuoppala5b076882016-02-19 12:26:04 +0200461 uint32_t val, mask;
462
463 mask = DC_STATE_DEBUG_MASK_MEMORY_UP;
464
465 if (IS_BROXTON(dev_priv))
466 mask |= DC_STATE_DEBUG_MASK_CORES;
Patrik Jakobsson4deccbb2015-11-09 16:48:17 +0100467
468 /* The below bit doesn't need to be cleared ever afterwards */
469 val = I915_READ(DC_STATE_DEBUG);
Mika Kuoppala5b076882016-02-19 12:26:04 +0200470 if ((val & mask) != mask) {
471 val |= mask;
Patrik Jakobsson4deccbb2015-11-09 16:48:17 +0100472 I915_WRITE(DC_STATE_DEBUG, val);
473 POSTING_READ(DC_STATE_DEBUG);
474 }
475}
476
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200477static void gen9_write_dc_state(struct drm_i915_private *dev_priv,
478 u32 state)
479{
480 int rewrites = 0;
481 int rereads = 0;
482 u32 v;
483
484 I915_WRITE(DC_STATE_EN, state);
485
486 /* It has been observed that disabling the dc6 state sometimes
487 * doesn't stick and dmc keeps returning old value. Make sure
488 * the write really sticks enough times and also force rewrite until
489 * we are confident that state is exactly what we want.
490 */
491 do {
492 v = I915_READ(DC_STATE_EN);
493
494 if (v != state) {
495 I915_WRITE(DC_STATE_EN, state);
496 rewrites++;
497 rereads = 0;
498 } else if (rereads++ > 5) {
499 break;
500 }
501
502 } while (rewrites < 100);
503
504 if (v != state)
505 DRM_ERROR("Writing dc state to 0x%x failed, now 0x%x\n",
506 state, v);
507
508 /* Most of the times we need one retry, avoid spam */
509 if (rewrites > 1)
510 DRM_DEBUG_KMS("Rewrote dc state to 0x%x %d times\n",
511 state, rewrites);
512}
513
Imre Deak13ae3a02015-11-04 19:24:16 +0200514static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530515{
516 uint32_t val;
Imre Deak13ae3a02015-11-04 19:24:16 +0200517 uint32_t mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530518
Imre Deak13ae3a02015-11-04 19:24:16 +0200519 mask = DC_STATE_EN_UPTO_DC5;
520 if (IS_BROXTON(dev_priv))
521 mask |= DC_STATE_EN_DC9;
522 else
523 mask |= DC_STATE_EN_UPTO_DC6;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530524
Imre Deak13ae3a02015-11-04 19:24:16 +0200525 WARN_ON_ONCE(state & ~mask);
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530526
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100527 if (i915.enable_dc == 0)
528 state = DC_STATE_DISABLE;
529 else if (i915.enable_dc == 1 && state > DC_STATE_EN_UPTO_DC5)
530 state = DC_STATE_EN_UPTO_DC5;
531
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530532 val = I915_READ(DC_STATE_EN);
Imre Deak13ae3a02015-11-04 19:24:16 +0200533 DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
534 val & mask, state);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200535
536 /* Check if DMC is ignoring our DC state requests */
537 if ((val & mask) != dev_priv->csr.dc_state)
538 DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
539 dev_priv->csr.dc_state, val & mask);
540
Imre Deak13ae3a02015-11-04 19:24:16 +0200541 val &= ~mask;
542 val |= state;
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200543
544 gen9_write_dc_state(dev_priv, val);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200545
546 dev_priv->csr.dc_state = val & mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530547}
548
Imre Deak13ae3a02015-11-04 19:24:16 +0200549void bxt_enable_dc9(struct drm_i915_private *dev_priv)
550{
551 assert_can_enable_dc9(dev_priv);
552
553 DRM_DEBUG_KMS("Enabling DC9\n");
554
555 gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
556}
557
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530558void bxt_disable_dc9(struct drm_i915_private *dev_priv)
559{
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530560 assert_can_disable_dc9(dev_priv);
561
562 DRM_DEBUG_KMS("Disabling DC9\n");
563
Imre Deak13ae3a02015-11-04 19:24:16 +0200564 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530565}
566
Daniel Vetteraf5fead2015-10-28 23:58:57 +0200567static void assert_csr_loaded(struct drm_i915_private *dev_priv)
568{
569 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
570 "CSR program storage start is NULL\n");
571 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
572 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
573}
574
Suketu Shah5aefb232015-04-16 14:22:10 +0530575static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shahdc174302015-04-17 19:46:16 +0530576{
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530577 struct drm_device *dev = dev_priv->dev;
Suketu Shah5aefb232015-04-16 14:22:10 +0530578 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
579 SKL_DISP_PW_2);
580
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800581 WARN_ONCE(!IS_SKYLAKE(dev) && !IS_KABYLAKE(dev),
582 "Platform doesn't support DC5.\n");
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700583 WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
584 WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530585
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700586 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
587 "DC5 already programmed to be enabled.\n");
Imre Deakc9b88462015-12-15 20:10:34 +0200588 assert_rpm_wakelock_held(dev_priv);
Suketu Shah5aefb232015-04-16 14:22:10 +0530589
590 assert_csr_loaded(dev_priv);
591}
592
593static void assert_can_disable_dc5(struct drm_i915_private *dev_priv)
594{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530595 /*
596 * During initialization, the firmware may not be loaded yet.
597 * We still want to make sure that the DC enabling flag is cleared.
598 */
599 if (dev_priv->power_domains.initializing)
600 return;
Suketu Shah5aefb232015-04-16 14:22:10 +0530601
Imre Deakc9b88462015-12-15 20:10:34 +0200602 assert_rpm_wakelock_held(dev_priv);
Suketu Shah5aefb232015-04-16 14:22:10 +0530603}
604
605static void gen9_enable_dc5(struct drm_i915_private *dev_priv)
606{
Suketu Shah5aefb232015-04-16 14:22:10 +0530607 assert_can_enable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530608
609 DRM_DEBUG_KMS("Enabling DC5\n");
610
Imre Deak13ae3a02015-11-04 19:24:16 +0200611 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
Suketu Shahdc174302015-04-17 19:46:16 +0530612}
613
Suketu Shah93c7cb62015-04-16 14:22:13 +0530614static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530615{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530616 struct drm_device *dev = dev_priv->dev;
Suketu Shah93c7cb62015-04-16 14:22:13 +0530617
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800618 WARN_ONCE(!IS_SKYLAKE(dev) && !IS_KABYLAKE(dev),
619 "Platform doesn't support DC6.\n");
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700620 WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
621 WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
622 "Backlight is not disabled.\n");
623 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
624 "DC6 already programmed to be enabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530625
626 assert_csr_loaded(dev_priv);
627}
628
629static void assert_can_disable_dc6(struct drm_i915_private *dev_priv)
630{
631 /*
632 * During initialization, the firmware may not be loaded yet.
633 * We still want to make sure that the DC enabling flag is cleared.
634 */
635 if (dev_priv->power_domains.initializing)
636 return;
637
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700638 WARN_ONCE(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
639 "DC6 already programmed to be disabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530640}
641
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100642static void gen9_disable_dc5_dc6(struct drm_i915_private *dev_priv)
643{
644 assert_can_disable_dc5(dev_priv);
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100645
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800646 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
647 i915.enable_dc != 0 && i915.enable_dc != 1)
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100648 assert_can_disable_dc6(dev_priv);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100649
650 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
651}
652
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530653void skl_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shah93c7cb62015-04-16 14:22:13 +0530654{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530655 assert_can_enable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530656
657 DRM_DEBUG_KMS("Enabling DC6\n");
658
Imre Deak13ae3a02015-11-04 19:24:16 +0200659 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
660
Suketu Shahf75a1982015-04-16 14:22:11 +0530661}
662
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530663void skl_disable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530664{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530665 assert_can_disable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530666
667 DRM_DEBUG_KMS("Disabling DC6\n");
668
Imre Deak13ae3a02015-11-04 19:24:16 +0200669 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Suketu Shahf75a1982015-04-16 14:22:11 +0530670}
671
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000672static void skl_set_power_well(struct drm_i915_private *dev_priv,
673 struct i915_power_well *power_well, bool enable)
674{
675 uint32_t tmp, fuse_status;
676 uint32_t req_mask, state_mask;
Damien Lespiau2a518352015-03-06 18:50:49 +0000677 bool is_enabled, enable_requested, check_fuse_status = false;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000678
679 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
680 fuse_status = I915_READ(SKL_FUSE_STATUS);
681
682 switch (power_well->data) {
683 case SKL_DISP_PW_1:
684 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
685 SKL_FUSE_PG0_DIST_STATUS), 1)) {
686 DRM_ERROR("PG0 not enabled\n");
687 return;
688 }
689 break;
690 case SKL_DISP_PW_2:
691 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
692 DRM_ERROR("PG1 in disabled state\n");
693 return;
694 }
695 break;
696 case SKL_DISP_PW_DDI_A_E:
697 case SKL_DISP_PW_DDI_B:
698 case SKL_DISP_PW_DDI_C:
699 case SKL_DISP_PW_DDI_D:
700 case SKL_DISP_PW_MISC_IO:
701 break;
702 default:
703 WARN(1, "Unknown power well %lu\n", power_well->data);
704 return;
705 }
706
707 req_mask = SKL_POWER_WELL_REQ(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000708 enable_requested = tmp & req_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000709 state_mask = SKL_POWER_WELL_STATE(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000710 is_enabled = tmp & state_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000711
712 if (enable) {
Damien Lespiau2a518352015-03-06 18:50:49 +0000713 if (!enable_requested) {
Suketu Shahdc174302015-04-17 19:46:16 +0530714 WARN((tmp & state_mask) &&
715 !I915_READ(HSW_PWR_WELL_BIOS),
716 "Invalid for power well status to be enabled, unless done by the BIOS, \
717 when request is to disable!\n");
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000718 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000719 }
720
Damien Lespiau2a518352015-03-06 18:50:49 +0000721 if (!is_enabled) {
Damien Lespiau510e6fd2015-03-06 18:50:50 +0000722 DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000723 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
724 state_mask), 1))
725 DRM_ERROR("%s enable timeout\n",
726 power_well->name);
727 check_fuse_status = true;
728 }
729 } else {
Damien Lespiau2a518352015-03-06 18:50:49 +0000730 if (enable_requested) {
Imre Deak4a76f292015-11-04 19:24:15 +0200731 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
732 POSTING_READ(HSW_PWR_WELL_DRIVER);
733 DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000734 }
735 }
736
737 if (check_fuse_status) {
738 if (power_well->data == SKL_DISP_PW_1) {
739 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
740 SKL_FUSE_PG1_DIST_STATUS), 1))
741 DRM_ERROR("PG1 distributing status timeout\n");
742 } else if (power_well->data == SKL_DISP_PW_2) {
743 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
744 SKL_FUSE_PG2_DIST_STATUS), 1))
745 DRM_ERROR("PG2 distributing status timeout\n");
746 }
747 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000748
749 if (enable && !is_enabled)
750 skl_power_well_post_enable(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000751}
752
Daniel Vetter9c065a72014-09-30 10:56:38 +0200753static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
754 struct i915_power_well *power_well)
755{
756 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
757
758 /*
759 * We're taking over the BIOS, so clear any requests made by it since
760 * the driver is in charge now.
761 */
762 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
763 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
764}
765
766static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
767 struct i915_power_well *power_well)
768{
769 hsw_set_power_well(dev_priv, power_well, true);
770}
771
772static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
773 struct i915_power_well *power_well)
774{
775 hsw_set_power_well(dev_priv, power_well, false);
776}
777
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000778static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
779 struct i915_power_well *power_well)
780{
781 uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
782 SKL_POWER_WELL_STATE(power_well->data);
783
784 return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
785}
786
787static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
788 struct i915_power_well *power_well)
789{
790 skl_set_power_well(dev_priv, power_well, power_well->count > 0);
791
792 /* Clear any request made by BIOS as driver is taking over */
793 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
794}
795
796static void skl_power_well_enable(struct drm_i915_private *dev_priv,
797 struct i915_power_well *power_well)
798{
799 skl_set_power_well(dev_priv, power_well, true);
800}
801
802static void skl_power_well_disable(struct drm_i915_private *dev_priv,
803 struct i915_power_well *power_well)
804{
805 skl_set_power_well(dev_priv, power_well, false);
806}
807
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100808static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
809 struct i915_power_well *power_well)
810{
811 return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
812}
813
814static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
815 struct i915_power_well *power_well)
816{
817 gen9_disable_dc5_dc6(dev_priv);
818}
819
820static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
821 struct i915_power_well *power_well)
822{
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800823 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
824 i915.enable_dc != 0 && i915.enable_dc != 1)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100825 skl_enable_dc6(dev_priv);
826 else
827 gen9_enable_dc5(dev_priv);
828}
829
830static void gen9_dc_off_power_well_sync_hw(struct drm_i915_private *dev_priv,
831 struct i915_power_well *power_well)
832{
833 if (power_well->count > 0) {
834 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
835 } else {
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800836 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
837 i915.enable_dc != 0 &&
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100838 i915.enable_dc != 1)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100839 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
840 else
841 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
842 }
843}
844
Daniel Vetter9c065a72014-09-30 10:56:38 +0200845static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
846 struct i915_power_well *power_well)
847{
848}
849
850static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
851 struct i915_power_well *power_well)
852{
853 return true;
854}
855
856static void vlv_set_power_well(struct drm_i915_private *dev_priv,
857 struct i915_power_well *power_well, bool enable)
858{
859 enum punit_power_well power_well_id = power_well->data;
860 u32 mask;
861 u32 state;
862 u32 ctrl;
863
864 mask = PUNIT_PWRGT_MASK(power_well_id);
865 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
866 PUNIT_PWRGT_PWR_GATE(power_well_id);
867
868 mutex_lock(&dev_priv->rps.hw_lock);
869
870#define COND \
871 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
872
873 if (COND)
874 goto out;
875
876 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
877 ctrl &= ~mask;
878 ctrl |= state;
879 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
880
881 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +0900882 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +0200883 state,
884 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
885
886#undef COND
887
888out:
889 mutex_unlock(&dev_priv->rps.hw_lock);
890}
891
892static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
893 struct i915_power_well *power_well)
894{
895 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
896}
897
898static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
899 struct i915_power_well *power_well)
900{
901 vlv_set_power_well(dev_priv, power_well, true);
902}
903
904static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
905 struct i915_power_well *power_well)
906{
907 vlv_set_power_well(dev_priv, power_well, false);
908}
909
910static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
911 struct i915_power_well *power_well)
912{
913 int power_well_id = power_well->data;
914 bool enabled = false;
915 u32 mask;
916 u32 state;
917 u32 ctrl;
918
919 mask = PUNIT_PWRGT_MASK(power_well_id);
920 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
921
922 mutex_lock(&dev_priv->rps.hw_lock);
923
924 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
925 /*
926 * We only ever set the power-on and power-gate states, anything
927 * else is unexpected.
928 */
929 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
930 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
931 if (state == ctrl)
932 enabled = true;
933
934 /*
935 * A transient state at this point would mean some unexpected party
936 * is poking at the power controls too.
937 */
938 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
939 WARN_ON(ctrl != state);
940
941 mutex_unlock(&dev_priv->rps.hw_lock);
942
943 return enabled;
944}
945
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300946static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200947{
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300948 enum pipe pipe;
949
950 /*
951 * Enable the CRI clock source so we can get at the
952 * display and the reference clock for VGA
953 * hotplug / manual detection. Supposedly DSI also
954 * needs the ref clock up and running.
955 *
956 * CHV DPLL B/C have some issues if VGA mode is enabled.
957 */
958 for_each_pipe(dev_priv->dev, pipe) {
959 u32 val = I915_READ(DPLL(pipe));
960
961 val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
962 if (pipe != PIPE_A)
963 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
964
965 I915_WRITE(DPLL(pipe), val);
966 }
Daniel Vetter9c065a72014-09-30 10:56:38 +0200967
968 spin_lock_irq(&dev_priv->irq_lock);
969 valleyview_enable_display_irqs(dev_priv);
970 spin_unlock_irq(&dev_priv->irq_lock);
971
972 /*
973 * During driver initialization/resume we can avoid restoring the
974 * part of the HW/SW state that will be inited anyway explicitly.
975 */
976 if (dev_priv->power_domains.initializing)
977 return;
978
Daniel Vetterb9632912014-09-30 10:56:44 +0200979 intel_hpd_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200980
981 i915_redisable_vga_power_on(dev_priv->dev);
982}
983
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300984static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
985{
986 spin_lock_irq(&dev_priv->irq_lock);
987 valleyview_disable_display_irqs(dev_priv);
988 spin_unlock_irq(&dev_priv->irq_lock);
989
Ville Syrjälä2230fde2016-02-19 18:41:52 +0200990 /* make sure we're done processing display irqs */
991 synchronize_irq(dev_priv->dev->irq);
992
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300993 vlv_power_sequencer_reset(dev_priv);
994}
995
996static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
997 struct i915_power_well *power_well)
998{
999 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1000
1001 vlv_set_power_well(dev_priv, power_well, true);
1002
1003 vlv_display_power_well_init(dev_priv);
1004}
1005
Daniel Vetter9c065a72014-09-30 10:56:38 +02001006static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
1007 struct i915_power_well *power_well)
1008{
1009 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1010
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001011 vlv_display_power_well_deinit(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001012
1013 vlv_set_power_well(dev_priv, power_well, false);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001014}
1015
1016static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1017 struct i915_power_well *power_well)
1018{
1019 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1020
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001021 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001022 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1023
1024 vlv_set_power_well(dev_priv, power_well, true);
1025
1026 /*
1027 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1028 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1029 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1030 * b. The other bits such as sfr settings / modesel may all
1031 * be set to 0.
1032 *
1033 * This should only be done on init and resume from S3 with
1034 * both PLLs disabled, or we risk losing DPIO and PLL
1035 * synchronization.
1036 */
1037 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1038}
1039
1040static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1041 struct i915_power_well *power_well)
1042{
1043 enum pipe pipe;
1044
1045 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1046
1047 for_each_pipe(dev_priv, pipe)
1048 assert_pll_disabled(dev_priv, pipe);
1049
1050 /* Assert common reset */
1051 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1052
1053 vlv_set_power_well(dev_priv, power_well, false);
1054}
1055
Ville Syrjälä30142272015-07-08 23:46:01 +03001056#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
1057
1058static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1059 int power_well_id)
1060{
1061 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Ville Syrjälä30142272015-07-08 23:46:01 +03001062 int i;
1063
Imre Deakfc17f222015-11-04 19:24:11 +02001064 for (i = 0; i < power_domains->power_well_count; i++) {
1065 struct i915_power_well *power_well;
1066
1067 power_well = &power_domains->power_wells[i];
Ville Syrjälä30142272015-07-08 23:46:01 +03001068 if (power_well->data == power_well_id)
1069 return power_well;
1070 }
1071
1072 return NULL;
1073}
1074
1075#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1076
1077static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1078{
1079 struct i915_power_well *cmn_bc =
1080 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1081 struct i915_power_well *cmn_d =
1082 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1083 u32 phy_control = dev_priv->chv_phy_control;
1084 u32 phy_status = 0;
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001085 u32 phy_status_mask = 0xffffffff;
Ville Syrjälä30142272015-07-08 23:46:01 +03001086 u32 tmp;
1087
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001088 /*
1089 * The BIOS can leave the PHY is some weird state
1090 * where it doesn't fully power down some parts.
1091 * Disable the asserts until the PHY has been fully
1092 * reset (ie. the power well has been disabled at
1093 * least once).
1094 */
1095 if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1096 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1097 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1098 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1099 PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1100 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1101 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1102
1103 if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1104 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1105 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1106 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1107
Ville Syrjälä30142272015-07-08 23:46:01 +03001108 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1109 phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1110
1111 /* this assumes override is only used to enable lanes */
1112 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1113 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1114
1115 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1116 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1117
1118 /* CL1 is on whenever anything is on in either channel */
1119 if (BITS_SET(phy_control,
1120 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1121 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1122 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1123
1124 /*
1125 * The DPLLB check accounts for the pipe B + port A usage
1126 * with CL2 powered up but all the lanes in the second channel
1127 * powered down.
1128 */
1129 if (BITS_SET(phy_control,
1130 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1131 (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1132 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1133
1134 if (BITS_SET(phy_control,
1135 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1136 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1137 if (BITS_SET(phy_control,
1138 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1139 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1140
1141 if (BITS_SET(phy_control,
1142 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1143 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1144 if (BITS_SET(phy_control,
1145 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1146 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1147 }
1148
1149 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1150 phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1151
1152 /* this assumes override is only used to enable lanes */
1153 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1154 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1155
1156 if (BITS_SET(phy_control,
1157 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1158 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1159
1160 if (BITS_SET(phy_control,
1161 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1162 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1163 if (BITS_SET(phy_control,
1164 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1165 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1166 }
1167
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001168 phy_status &= phy_status_mask;
1169
Ville Syrjälä30142272015-07-08 23:46:01 +03001170 /*
1171 * The PHY may be busy with some initial calibration and whatnot,
1172 * so the power state can take a while to actually change.
1173 */
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001174 if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
Ville Syrjälä30142272015-07-08 23:46:01 +03001175 WARN(phy_status != tmp,
1176 "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1177 tmp, phy_status, dev_priv->chv_phy_control);
1178}
1179
1180#undef BITS_SET
1181
Daniel Vetter9c065a72014-09-30 10:56:38 +02001182static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1183 struct i915_power_well *power_well)
1184{
1185 enum dpio_phy phy;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001186 enum pipe pipe;
1187 uint32_t tmp;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001188
1189 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1190 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1191
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001192 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1193 pipe = PIPE_A;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001194 phy = DPIO_PHY0;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001195 } else {
1196 pipe = PIPE_C;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001197 phy = DPIO_PHY1;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001198 }
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001199
1200 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001201 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1202 vlv_set_power_well(dev_priv, power_well, true);
1203
1204 /* Poll for phypwrgood signal */
1205 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
1206 DRM_ERROR("Display PHY %d is not power up\n", phy);
1207
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001208 mutex_lock(&dev_priv->sb_lock);
1209
1210 /* Enable dynamic power down */
1211 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
Ville Syrjäläee279212015-07-08 23:45:57 +03001212 tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1213 DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001214 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1215
1216 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1217 tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1218 tmp |= DPIO_DYNPWRDOWNEN_CH1;
1219 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
Ville Syrjälä3e288782015-07-08 23:45:58 +03001220 } else {
1221 /*
1222 * Force the non-existing CL2 off. BXT does this
1223 * too, so maybe it saves some power even though
1224 * CL2 doesn't exist?
1225 */
1226 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1227 tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1228 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001229 }
1230
1231 mutex_unlock(&dev_priv->sb_lock);
1232
Ville Syrjälä70722462015-04-10 18:21:28 +03001233 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1234 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001235
1236 DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1237 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001238
1239 assert_chv_phy_status(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001240}
1241
1242static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1243 struct i915_power_well *power_well)
1244{
1245 enum dpio_phy phy;
1246
1247 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1248 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1249
1250 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1251 phy = DPIO_PHY0;
1252 assert_pll_disabled(dev_priv, PIPE_A);
1253 assert_pll_disabled(dev_priv, PIPE_B);
1254 } else {
1255 phy = DPIO_PHY1;
1256 assert_pll_disabled(dev_priv, PIPE_C);
1257 }
1258
Ville Syrjälä70722462015-04-10 18:21:28 +03001259 dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1260 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001261
1262 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001263
1264 DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1265 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001266
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001267 /* PHY is fully reset now, so we can enable the PHY state asserts */
1268 dev_priv->chv_phy_assert[phy] = true;
1269
Ville Syrjälä30142272015-07-08 23:46:01 +03001270 assert_chv_phy_status(dev_priv);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001271}
1272
Ville Syrjälä6669e392015-07-08 23:46:00 +03001273static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1274 enum dpio_channel ch, bool override, unsigned int mask)
1275{
1276 enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1277 u32 reg, val, expected, actual;
1278
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001279 /*
1280 * The BIOS can leave the PHY is some weird state
1281 * where it doesn't fully power down some parts.
1282 * Disable the asserts until the PHY has been fully
1283 * reset (ie. the power well has been disabled at
1284 * least once).
1285 */
1286 if (!dev_priv->chv_phy_assert[phy])
1287 return;
1288
Ville Syrjälä6669e392015-07-08 23:46:00 +03001289 if (ch == DPIO_CH0)
1290 reg = _CHV_CMN_DW0_CH0;
1291 else
1292 reg = _CHV_CMN_DW6_CH1;
1293
1294 mutex_lock(&dev_priv->sb_lock);
1295 val = vlv_dpio_read(dev_priv, pipe, reg);
1296 mutex_unlock(&dev_priv->sb_lock);
1297
1298 /*
1299 * This assumes !override is only used when the port is disabled.
1300 * All lanes should power down even without the override when
1301 * the port is disabled.
1302 */
1303 if (!override || mask == 0xf) {
1304 expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1305 /*
1306 * If CH1 common lane is not active anymore
1307 * (eg. for pipe B DPLL) the entire channel will
1308 * shut down, which causes the common lane registers
1309 * to read as 0. That means we can't actually check
1310 * the lane power down status bits, but as the entire
1311 * register reads as 0 it's a good indication that the
1312 * channel is indeed entirely powered down.
1313 */
1314 if (ch == DPIO_CH1 && val == 0)
1315 expected = 0;
1316 } else if (mask != 0x0) {
1317 expected = DPIO_ANYDL_POWERDOWN;
1318 } else {
1319 expected = 0;
1320 }
1321
1322 if (ch == DPIO_CH0)
1323 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1324 else
1325 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1326 actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1327
1328 WARN(actual != expected,
1329 "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1330 !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1331 !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1332 reg, val);
1333}
1334
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001335bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1336 enum dpio_channel ch, bool override)
1337{
1338 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1339 bool was_override;
1340
1341 mutex_lock(&power_domains->lock);
1342
1343 was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1344
1345 if (override == was_override)
1346 goto out;
1347
1348 if (override)
1349 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1350 else
1351 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1352
1353 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1354
1355 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1356 phy, ch, dev_priv->chv_phy_control);
1357
Ville Syrjälä30142272015-07-08 23:46:01 +03001358 assert_chv_phy_status(dev_priv);
1359
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001360out:
1361 mutex_unlock(&power_domains->lock);
1362
1363 return was_override;
1364}
1365
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001366void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1367 bool override, unsigned int mask)
1368{
1369 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1370 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1371 enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1372 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1373
1374 mutex_lock(&power_domains->lock);
1375
1376 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1377 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1378
1379 if (override)
1380 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1381 else
1382 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1383
1384 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1385
1386 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1387 phy, ch, mask, dev_priv->chv_phy_control);
1388
Ville Syrjälä30142272015-07-08 23:46:01 +03001389 assert_chv_phy_status(dev_priv);
1390
Ville Syrjälä6669e392015-07-08 23:46:00 +03001391 assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1392
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001393 mutex_unlock(&power_domains->lock);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001394}
1395
1396static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1397 struct i915_power_well *power_well)
1398{
1399 enum pipe pipe = power_well->data;
1400 bool enabled;
1401 u32 state, ctrl;
1402
1403 mutex_lock(&dev_priv->rps.hw_lock);
1404
1405 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1406 /*
1407 * We only ever set the power-on and power-gate states, anything
1408 * else is unexpected.
1409 */
1410 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1411 enabled = state == DP_SSS_PWR_ON(pipe);
1412
1413 /*
1414 * A transient state at this point would mean some unexpected party
1415 * is poking at the power controls too.
1416 */
1417 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1418 WARN_ON(ctrl << 16 != state);
1419
1420 mutex_unlock(&dev_priv->rps.hw_lock);
1421
1422 return enabled;
1423}
1424
1425static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1426 struct i915_power_well *power_well,
1427 bool enable)
1428{
1429 enum pipe pipe = power_well->data;
1430 u32 state;
1431 u32 ctrl;
1432
1433 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1434
1435 mutex_lock(&dev_priv->rps.hw_lock);
1436
1437#define COND \
1438 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1439
1440 if (COND)
1441 goto out;
1442
1443 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1444 ctrl &= ~DP_SSC_MASK(pipe);
1445 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1446 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1447
1448 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +09001449 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001450 state,
1451 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1452
1453#undef COND
1454
1455out:
1456 mutex_unlock(&dev_priv->rps.hw_lock);
1457}
1458
1459static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1460 struct i915_power_well *power_well)
1461{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001462 WARN_ON_ONCE(power_well->data != PIPE_A);
1463
Daniel Vetter9c065a72014-09-30 10:56:38 +02001464 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1465}
1466
1467static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1468 struct i915_power_well *power_well)
1469{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001470 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001471
1472 chv_set_pipe_power_well(dev_priv, power_well, true);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001473
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001474 vlv_display_power_well_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001475}
1476
1477static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1478 struct i915_power_well *power_well)
1479{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001480 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001481
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001482 vlv_display_power_well_deinit(dev_priv);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001483
Daniel Vetter9c065a72014-09-30 10:56:38 +02001484 chv_set_pipe_power_well(dev_priv, power_well, false);
1485}
1486
Imre Deak09731282016-02-17 14:17:42 +02001487static void
1488__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1489 enum intel_display_power_domain domain)
1490{
1491 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1492 struct i915_power_well *power_well;
1493 int i;
1494
1495 for_each_power_well(i, power_well, BIT(domain), power_domains) {
1496 if (!power_well->count++)
1497 intel_power_well_enable(dev_priv, power_well);
1498 }
1499
1500 power_domains->domain_use_count[domain]++;
1501}
1502
Daniel Vettere4e76842014-09-30 10:56:42 +02001503/**
1504 * intel_display_power_get - grab a power domain reference
1505 * @dev_priv: i915 device instance
1506 * @domain: power domain to reference
1507 *
1508 * This function grabs a power domain reference for @domain and ensures that the
1509 * power domain and all its parents are powered up. Therefore users should only
1510 * grab a reference to the innermost power domain they need.
1511 *
1512 * Any power domain reference obtained by this function must have a symmetric
1513 * call to intel_display_power_put() to release the reference again.
1514 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001515void intel_display_power_get(struct drm_i915_private *dev_priv,
1516 enum intel_display_power_domain domain)
1517{
Imre Deak09731282016-02-17 14:17:42 +02001518 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001519
1520 intel_runtime_pm_get(dev_priv);
1521
Imre Deak09731282016-02-17 14:17:42 +02001522 mutex_lock(&power_domains->lock);
1523
1524 __intel_display_power_get_domain(dev_priv, domain);
1525
1526 mutex_unlock(&power_domains->lock);
1527}
1528
1529/**
1530 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
1531 * @dev_priv: i915 device instance
1532 * @domain: power domain to reference
1533 *
1534 * This function grabs a power domain reference for @domain and ensures that the
1535 * power domain and all its parents are powered up. Therefore users should only
1536 * grab a reference to the innermost power domain they need.
1537 *
1538 * Any power domain reference obtained by this function must have a symmetric
1539 * call to intel_display_power_put() to release the reference again.
1540 */
1541bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1542 enum intel_display_power_domain domain)
1543{
1544 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1545 bool is_enabled;
1546
1547 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1548 return false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001549
1550 mutex_lock(&power_domains->lock);
1551
Imre Deak09731282016-02-17 14:17:42 +02001552 if (__intel_display_power_is_enabled(dev_priv, domain)) {
1553 __intel_display_power_get_domain(dev_priv, domain);
1554 is_enabled = true;
1555 } else {
1556 is_enabled = false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001557 }
1558
Daniel Vetter9c065a72014-09-30 10:56:38 +02001559 mutex_unlock(&power_domains->lock);
Imre Deak09731282016-02-17 14:17:42 +02001560
1561 if (!is_enabled)
1562 intel_runtime_pm_put(dev_priv);
1563
1564 return is_enabled;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001565}
1566
Daniel Vettere4e76842014-09-30 10:56:42 +02001567/**
1568 * intel_display_power_put - release a power domain reference
1569 * @dev_priv: i915 device instance
1570 * @domain: power domain to reference
1571 *
1572 * This function drops the power domain reference obtained by
1573 * intel_display_power_get() and might power down the corresponding hardware
1574 * block right away if this is the last reference.
1575 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001576void intel_display_power_put(struct drm_i915_private *dev_priv,
1577 enum intel_display_power_domain domain)
1578{
1579 struct i915_power_domains *power_domains;
1580 struct i915_power_well *power_well;
1581 int i;
1582
1583 power_domains = &dev_priv->power_domains;
1584
1585 mutex_lock(&power_domains->lock);
1586
Daniel Stone11c86db2015-11-20 15:55:34 +00001587 WARN(!power_domains->domain_use_count[domain],
1588 "Use count on domain %s is already zero\n",
1589 intel_display_power_domain_str(domain));
Daniel Vetter9c065a72014-09-30 10:56:38 +02001590 power_domains->domain_use_count[domain]--;
1591
1592 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Daniel Stone11c86db2015-11-20 15:55:34 +00001593 WARN(!power_well->count,
1594 "Use count on power well %s is already zero",
1595 power_well->name);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001596
Imre Deakd314cd42015-11-17 17:44:23 +02001597 if (!--power_well->count)
Damien Lespiaudcddab32015-07-30 18:20:27 -03001598 intel_power_well_disable(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001599 }
1600
1601 mutex_unlock(&power_domains->lock);
1602
1603 intel_runtime_pm_put(dev_priv);
1604}
1605
Daniel Vetter9c065a72014-09-30 10:56:38 +02001606#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
1607 BIT(POWER_DOMAIN_PIPE_A) | \
1608 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001609 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
1610 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1611 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1612 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001613 BIT(POWER_DOMAIN_PORT_CRT) | \
1614 BIT(POWER_DOMAIN_PLLS) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001615 BIT(POWER_DOMAIN_AUX_A) | \
1616 BIT(POWER_DOMAIN_AUX_B) | \
1617 BIT(POWER_DOMAIN_AUX_C) | \
1618 BIT(POWER_DOMAIN_AUX_D) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01001619 BIT(POWER_DOMAIN_GMBUS) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001620 BIT(POWER_DOMAIN_INIT))
1621#define HSW_DISPLAY_POWER_DOMAINS ( \
1622 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
1623 BIT(POWER_DOMAIN_INIT))
1624
1625#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
1626 HSW_ALWAYS_ON_POWER_DOMAINS | \
1627 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
1628#define BDW_DISPLAY_POWER_DOMAINS ( \
1629 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
1630 BIT(POWER_DOMAIN_INIT))
1631
1632#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
1633#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
1634
1635#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001636 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1637 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001638 BIT(POWER_DOMAIN_PORT_CRT) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001639 BIT(POWER_DOMAIN_AUX_B) | \
1640 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001641 BIT(POWER_DOMAIN_INIT))
1642
1643#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001644 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001645 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001646 BIT(POWER_DOMAIN_INIT))
1647
1648#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001649 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001650 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001651 BIT(POWER_DOMAIN_INIT))
1652
1653#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001654 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001655 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001656 BIT(POWER_DOMAIN_INIT))
1657
1658#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001659 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001660 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001661 BIT(POWER_DOMAIN_INIT))
1662
Daniel Vetter9c065a72014-09-30 10:56:38 +02001663#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001664 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1665 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001666 BIT(POWER_DOMAIN_AUX_B) | \
1667 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001668 BIT(POWER_DOMAIN_INIT))
1669
1670#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001671 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001672 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001673 BIT(POWER_DOMAIN_INIT))
1674
Daniel Vetter9c065a72014-09-30 10:56:38 +02001675static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1676 .sync_hw = i9xx_always_on_power_well_noop,
1677 .enable = i9xx_always_on_power_well_noop,
1678 .disable = i9xx_always_on_power_well_noop,
1679 .is_enabled = i9xx_always_on_power_well_enabled,
1680};
1681
1682static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1683 .sync_hw = chv_pipe_power_well_sync_hw,
1684 .enable = chv_pipe_power_well_enable,
1685 .disable = chv_pipe_power_well_disable,
1686 .is_enabled = chv_pipe_power_well_enabled,
1687};
1688
1689static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1690 .sync_hw = vlv_power_well_sync_hw,
1691 .enable = chv_dpio_cmn_power_well_enable,
1692 .disable = chv_dpio_cmn_power_well_disable,
1693 .is_enabled = vlv_power_well_enabled,
1694};
1695
1696static struct i915_power_well i9xx_always_on_power_well[] = {
1697 {
1698 .name = "always-on",
1699 .always_on = 1,
1700 .domains = POWER_DOMAIN_MASK,
1701 .ops = &i9xx_always_on_power_well_ops,
1702 },
1703};
1704
1705static const struct i915_power_well_ops hsw_power_well_ops = {
1706 .sync_hw = hsw_power_well_sync_hw,
1707 .enable = hsw_power_well_enable,
1708 .disable = hsw_power_well_disable,
1709 .is_enabled = hsw_power_well_enabled,
1710};
1711
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001712static const struct i915_power_well_ops skl_power_well_ops = {
1713 .sync_hw = skl_power_well_sync_hw,
1714 .enable = skl_power_well_enable,
1715 .disable = skl_power_well_disable,
1716 .is_enabled = skl_power_well_enabled,
1717};
1718
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001719static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1720 .sync_hw = gen9_dc_off_power_well_sync_hw,
1721 .enable = gen9_dc_off_power_well_enable,
1722 .disable = gen9_dc_off_power_well_disable,
1723 .is_enabled = gen9_dc_off_power_well_enabled,
1724};
1725
Daniel Vetter9c065a72014-09-30 10:56:38 +02001726static struct i915_power_well hsw_power_wells[] = {
1727 {
1728 .name = "always-on",
1729 .always_on = 1,
1730 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
1731 .ops = &i9xx_always_on_power_well_ops,
1732 },
1733 {
1734 .name = "display",
1735 .domains = HSW_DISPLAY_POWER_DOMAINS,
1736 .ops = &hsw_power_well_ops,
1737 },
1738};
1739
1740static struct i915_power_well bdw_power_wells[] = {
1741 {
1742 .name = "always-on",
1743 .always_on = 1,
1744 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
1745 .ops = &i9xx_always_on_power_well_ops,
1746 },
1747 {
1748 .name = "display",
1749 .domains = BDW_DISPLAY_POWER_DOMAINS,
1750 .ops = &hsw_power_well_ops,
1751 },
1752};
1753
1754static const struct i915_power_well_ops vlv_display_power_well_ops = {
1755 .sync_hw = vlv_power_well_sync_hw,
1756 .enable = vlv_display_power_well_enable,
1757 .disable = vlv_display_power_well_disable,
1758 .is_enabled = vlv_power_well_enabled,
1759};
1760
1761static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1762 .sync_hw = vlv_power_well_sync_hw,
1763 .enable = vlv_dpio_cmn_power_well_enable,
1764 .disable = vlv_dpio_cmn_power_well_disable,
1765 .is_enabled = vlv_power_well_enabled,
1766};
1767
1768static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1769 .sync_hw = vlv_power_well_sync_hw,
1770 .enable = vlv_power_well_enable,
1771 .disable = vlv_power_well_disable,
1772 .is_enabled = vlv_power_well_enabled,
1773};
1774
1775static struct i915_power_well vlv_power_wells[] = {
1776 {
1777 .name = "always-on",
1778 .always_on = 1,
1779 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1780 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001781 .data = PUNIT_POWER_WELL_ALWAYS_ON,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001782 },
1783 {
1784 .name = "display",
1785 .domains = VLV_DISPLAY_POWER_DOMAINS,
1786 .data = PUNIT_POWER_WELL_DISP2D,
1787 .ops = &vlv_display_power_well_ops,
1788 },
1789 {
1790 .name = "dpio-tx-b-01",
1791 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1792 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1793 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1794 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1795 .ops = &vlv_dpio_power_well_ops,
1796 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1797 },
1798 {
1799 .name = "dpio-tx-b-23",
1800 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1801 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1802 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1803 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1804 .ops = &vlv_dpio_power_well_ops,
1805 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1806 },
1807 {
1808 .name = "dpio-tx-c-01",
1809 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1810 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1811 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1812 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1813 .ops = &vlv_dpio_power_well_ops,
1814 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1815 },
1816 {
1817 .name = "dpio-tx-c-23",
1818 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1819 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1820 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1821 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1822 .ops = &vlv_dpio_power_well_ops,
1823 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1824 },
1825 {
1826 .name = "dpio-common",
1827 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1828 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1829 .ops = &vlv_dpio_cmn_power_well_ops,
1830 },
1831};
1832
1833static struct i915_power_well chv_power_wells[] = {
1834 {
1835 .name = "always-on",
1836 .always_on = 1,
1837 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1838 .ops = &i9xx_always_on_power_well_ops,
1839 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001840 {
1841 .name = "display",
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001842 /*
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001843 * Pipe A power well is the new disp2d well. Pipe B and C
1844 * power wells don't actually exist. Pipe A power well is
1845 * required for any pipe to work.
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001846 */
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001847 .domains = VLV_DISPLAY_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001848 .data = PIPE_A,
1849 .ops = &chv_pipe_power_well_ops,
1850 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001851 {
1852 .name = "dpio-common-bc",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001853 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001854 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1855 .ops = &chv_dpio_cmn_power_well_ops,
1856 },
1857 {
1858 .name = "dpio-common-d",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001859 .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001860 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
1861 .ops = &chv_dpio_cmn_power_well_ops,
1862 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001863};
1864
Suketu Shah5aefb232015-04-16 14:22:10 +05301865bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
1866 int power_well_id)
1867{
1868 struct i915_power_well *power_well;
1869 bool ret;
1870
1871 power_well = lookup_power_well(dev_priv, power_well_id);
1872 ret = power_well->ops->is_enabled(dev_priv, power_well);
1873
1874 return ret;
1875}
1876
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001877static struct i915_power_well skl_power_wells[] = {
1878 {
1879 .name = "always-on",
1880 .always_on = 1,
1881 .domains = SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1882 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001883 .data = SKL_DISP_PW_ALWAYS_ON,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001884 },
1885 {
1886 .name = "power well 1",
Imre Deak4a76f292015-11-04 19:24:15 +02001887 /* Handled by the DMC firmware */
1888 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001889 .ops = &skl_power_well_ops,
1890 .data = SKL_DISP_PW_1,
1891 },
1892 {
1893 .name = "MISC IO power well",
Imre Deak4a76f292015-11-04 19:24:15 +02001894 /* Handled by the DMC firmware */
1895 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001896 .ops = &skl_power_well_ops,
1897 .data = SKL_DISP_PW_MISC_IO,
1898 },
1899 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001900 .name = "DC off",
1901 .domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
1902 .ops = &gen9_dc_off_power_well_ops,
1903 .data = SKL_DISP_PW_DC_OFF,
1904 },
1905 {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001906 .name = "power well 2",
1907 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1908 .ops = &skl_power_well_ops,
1909 .data = SKL_DISP_PW_2,
1910 },
1911 {
1912 .name = "DDI A/E power well",
1913 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1914 .ops = &skl_power_well_ops,
1915 .data = SKL_DISP_PW_DDI_A_E,
1916 },
1917 {
1918 .name = "DDI B power well",
1919 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1920 .ops = &skl_power_well_ops,
1921 .data = SKL_DISP_PW_DDI_B,
1922 },
1923 {
1924 .name = "DDI C power well",
1925 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1926 .ops = &skl_power_well_ops,
1927 .data = SKL_DISP_PW_DDI_C,
1928 },
1929 {
1930 .name = "DDI D power well",
1931 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1932 .ops = &skl_power_well_ops,
1933 .data = SKL_DISP_PW_DDI_D,
1934 },
1935};
1936
Damien Lespiau2f693e22015-11-04 19:24:12 +02001937void skl_pw1_misc_io_init(struct drm_i915_private *dev_priv)
1938{
1939 struct i915_power_well *well;
1940
Michel Thierry16fbc292016-01-06 12:08:36 +00001941 if (!(IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)))
Damien Lespiau2f693e22015-11-04 19:24:12 +02001942 return;
1943
1944 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
1945 intel_power_well_enable(dev_priv, well);
1946
1947 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
1948 intel_power_well_enable(dev_priv, well);
1949}
1950
1951void skl_pw1_misc_io_fini(struct drm_i915_private *dev_priv)
1952{
1953 struct i915_power_well *well;
1954
Michel Thierry16fbc292016-01-06 12:08:36 +00001955 if (!(IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)))
Damien Lespiau2f693e22015-11-04 19:24:12 +02001956 return;
1957
1958 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
1959 intel_power_well_disable(dev_priv, well);
1960
1961 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
1962 intel_power_well_disable(dev_priv, well);
1963}
1964
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301965static struct i915_power_well bxt_power_wells[] = {
1966 {
1967 .name = "always-on",
1968 .always_on = 1,
1969 .domains = BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1970 .ops = &i9xx_always_on_power_well_ops,
1971 },
1972 {
1973 .name = "power well 1",
1974 .domains = BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS,
1975 .ops = &skl_power_well_ops,
1976 .data = SKL_DISP_PW_1,
1977 },
1978 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001979 .name = "DC off",
1980 .domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
1981 .ops = &gen9_dc_off_power_well_ops,
1982 .data = SKL_DISP_PW_DC_OFF,
1983 },
1984 {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301985 .name = "power well 2",
1986 .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1987 .ops = &skl_power_well_ops,
1988 .data = SKL_DISP_PW_2,
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001989 },
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301990};
1991
Imre Deak1b0e3a02015-11-05 23:04:11 +02001992static int
1993sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
1994 int disable_power_well)
1995{
1996 if (disable_power_well >= 0)
1997 return !!disable_power_well;
1998
Matt Roper18024192015-12-01 09:26:58 -08001999 if (IS_BROXTON(dev_priv)) {
2000 DRM_DEBUG_KMS("Disabling display power well support\n");
2001 return 0;
2002 }
2003
Imre Deak1b0e3a02015-11-05 23:04:11 +02002004 return 1;
2005}
2006
Daniel Vetter9c065a72014-09-30 10:56:38 +02002007#define set_power_wells(power_domains, __power_wells) ({ \
2008 (power_domains)->power_wells = (__power_wells); \
2009 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
2010})
2011
Daniel Vettere4e76842014-09-30 10:56:42 +02002012/**
2013 * intel_power_domains_init - initializes the power domain structures
2014 * @dev_priv: i915 device instance
2015 *
2016 * Initializes the power domain structures for @dev_priv depending upon the
2017 * supported platform.
2018 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002019int intel_power_domains_init(struct drm_i915_private *dev_priv)
2020{
2021 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2022
Imre Deak1b0e3a02015-11-05 23:04:11 +02002023 i915.disable_power_well = sanitize_disable_power_well_option(dev_priv,
2024 i915.disable_power_well);
2025
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01002026 BUILD_BUG_ON(POWER_DOMAIN_NUM > 31);
2027
Daniel Vetter9c065a72014-09-30 10:56:38 +02002028 mutex_init(&power_domains->lock);
2029
2030 /*
2031 * The enabling order will be from lower to higher indexed wells,
2032 * the disabling order is reversed.
2033 */
2034 if (IS_HASWELL(dev_priv->dev)) {
2035 set_power_wells(power_domains, hsw_power_wells);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002036 } else if (IS_BROADWELL(dev_priv->dev)) {
2037 set_power_wells(power_domains, bdw_power_wells);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07002038 } else if (IS_SKYLAKE(dev_priv->dev) || IS_KABYLAKE(dev_priv->dev)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002039 set_power_wells(power_domains, skl_power_wells);
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302040 } else if (IS_BROXTON(dev_priv->dev)) {
2041 set_power_wells(power_domains, bxt_power_wells);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002042 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
2043 set_power_wells(power_domains, chv_power_wells);
2044 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
2045 set_power_wells(power_domains, vlv_power_wells);
2046 } else {
2047 set_power_wells(power_domains, i9xx_always_on_power_well);
2048 }
2049
2050 return 0;
2051}
2052
Daniel Vettere4e76842014-09-30 10:56:42 +02002053/**
2054 * intel_power_domains_fini - finalizes the power domain structures
2055 * @dev_priv: i915 device instance
2056 *
2057 * Finalizes the power domain structures for @dev_priv depending upon the
2058 * supported platform. This function also disables runtime pm and ensures that
2059 * the device stays powered up so that the driver can be reloaded.
2060 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002061void intel_power_domains_fini(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002062{
Imre Deak25b181b2015-12-17 13:44:56 +02002063 struct device *device = &dev_priv->dev->pdev->dev;
2064
Imre Deakaabee1b2015-12-15 20:10:29 +02002065 /*
2066 * The i915.ko module is still not prepared to be loaded when
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002067 * the power well is not enabled, so just enable it in case
Imre Deakaabee1b2015-12-15 20:10:29 +02002068 * we're going to unload/reload.
2069 * The following also reacquires the RPM reference the core passed
2070 * to the driver during loading, which is dropped in
2071 * intel_runtime_pm_enable(). We have to hand back the control of the
2072 * device to the core with this reference held.
2073 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002074 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002075
2076 /* Remove the refcount we took to keep power well support disabled. */
2077 if (!i915.disable_power_well)
2078 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak25b181b2015-12-17 13:44:56 +02002079
2080 /*
2081 * Remove the refcount we took in intel_runtime_pm_enable() in case
2082 * the platform doesn't support runtime PM.
2083 */
2084 if (!HAS_RUNTIME_PM(dev_priv))
2085 pm_runtime_put(device);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002086}
2087
Imre Deak30eade12015-11-04 19:24:13 +02002088static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002089{
2090 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2091 struct i915_power_well *power_well;
2092 int i;
2093
2094 mutex_lock(&power_domains->lock);
2095 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
2096 power_well->ops->sync_hw(dev_priv, power_well);
2097 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2098 power_well);
2099 }
2100 mutex_unlock(&power_domains->lock);
2101}
2102
Imre Deak73dfc222015-11-17 17:33:53 +02002103static void skl_display_core_init(struct drm_i915_private *dev_priv,
2104 bool resume)
2105{
2106 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2107 uint32_t val;
2108
Imre Deakd26fa1d2015-11-04 19:24:17 +02002109 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2110
Imre Deak73dfc222015-11-17 17:33:53 +02002111 /* enable PCH reset handshake */
2112 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2113 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2114
2115 /* enable PG1 and Misc I/O */
2116 mutex_lock(&power_domains->lock);
2117 skl_pw1_misc_io_init(dev_priv);
2118 mutex_unlock(&power_domains->lock);
2119
2120 if (!resume)
2121 return;
2122
2123 skl_init_cdclk(dev_priv);
2124
Mika Kuoppala1e657ad2016-02-18 17:21:14 +02002125 if (dev_priv->csr.dmc_payload && intel_csr_load_program(dev_priv))
2126 gen9_set_dc_state_debugmask(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002127}
2128
2129static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2130{
2131 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2132
Imre Deakd26fa1d2015-11-04 19:24:17 +02002133 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2134
Imre Deak73dfc222015-11-17 17:33:53 +02002135 skl_uninit_cdclk(dev_priv);
2136
2137 /* The spec doesn't call for removing the reset handshake flag */
2138 /* disable PG1 and Misc I/O */
2139 mutex_lock(&power_domains->lock);
2140 skl_pw1_misc_io_fini(dev_priv);
2141 mutex_unlock(&power_domains->lock);
2142}
2143
Ville Syrjälä70722462015-04-10 18:21:28 +03002144static void chv_phy_control_init(struct drm_i915_private *dev_priv)
2145{
2146 struct i915_power_well *cmn_bc =
2147 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2148 struct i915_power_well *cmn_d =
2149 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
2150
2151 /*
2152 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
2153 * workaround never ever read DISPLAY_PHY_CONTROL, and
2154 * instead maintain a shadow copy ourselves. Use the actual
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002155 * power well state and lane status to reconstruct the
2156 * expected initial value.
Ville Syrjälä70722462015-04-10 18:21:28 +03002157 */
2158 dev_priv->chv_phy_control =
Ville Syrjäläbc284542015-05-26 20:22:38 +03002159 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
2160 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002161 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
2162 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
2163 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
2164
2165 /*
2166 * If all lanes are disabled we leave the override disabled
2167 * with all power down bits cleared to match the state we
2168 * would use after disabling the port. Otherwise enable the
2169 * override and set the lane powerdown bits accding to the
2170 * current lane status.
2171 */
2172 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
2173 uint32_t status = I915_READ(DPLL(PIPE_A));
2174 unsigned int mask;
2175
2176 mask = status & DPLL_PORTB_READY_MASK;
2177 if (mask == 0xf)
2178 mask = 0x0;
2179 else
2180 dev_priv->chv_phy_control |=
2181 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
2182
2183 dev_priv->chv_phy_control |=
2184 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
2185
2186 mask = (status & DPLL_PORTC_READY_MASK) >> 4;
2187 if (mask == 0xf)
2188 mask = 0x0;
2189 else
2190 dev_priv->chv_phy_control |=
2191 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
2192
2193 dev_priv->chv_phy_control |=
2194 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
2195
Ville Syrjälä70722462015-04-10 18:21:28 +03002196 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002197
2198 dev_priv->chv_phy_assert[DPIO_PHY0] = false;
2199 } else {
2200 dev_priv->chv_phy_assert[DPIO_PHY0] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002201 }
2202
2203 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
2204 uint32_t status = I915_READ(DPIO_PHY_STATUS);
2205 unsigned int mask;
2206
2207 mask = status & DPLL_PORTD_READY_MASK;
2208
2209 if (mask == 0xf)
2210 mask = 0x0;
2211 else
2212 dev_priv->chv_phy_control |=
2213 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
2214
2215 dev_priv->chv_phy_control |=
2216 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
2217
Ville Syrjälä70722462015-04-10 18:21:28 +03002218 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002219
2220 dev_priv->chv_phy_assert[DPIO_PHY1] = false;
2221 } else {
2222 dev_priv->chv_phy_assert[DPIO_PHY1] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002223 }
2224
2225 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
2226
2227 DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
2228 dev_priv->chv_phy_control);
Ville Syrjälä70722462015-04-10 18:21:28 +03002229}
2230
Daniel Vetter9c065a72014-09-30 10:56:38 +02002231static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
2232{
2233 struct i915_power_well *cmn =
2234 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2235 struct i915_power_well *disp2d =
2236 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
2237
Daniel Vetter9c065a72014-09-30 10:56:38 +02002238 /* If the display might be already active skip this */
Ville Syrjälä5d93a6e2014-10-16 20:52:33 +03002239 if (cmn->ops->is_enabled(dev_priv, cmn) &&
2240 disp2d->ops->is_enabled(dev_priv, disp2d) &&
Daniel Vetter9c065a72014-09-30 10:56:38 +02002241 I915_READ(DPIO_CTL) & DPIO_CMNRST)
2242 return;
2243
2244 DRM_DEBUG_KMS("toggling display PHY side reset\n");
2245
2246 /* cmnlane needs DPLL registers */
2247 disp2d->ops->enable(dev_priv, disp2d);
2248
2249 /*
2250 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
2251 * Need to assert and de-assert PHY SB reset by gating the
2252 * common lane power, then un-gating it.
2253 * Simply ungating isn't enough to reset the PHY enough to get
2254 * ports and lanes running.
2255 */
2256 cmn->ops->disable(dev_priv, cmn);
2257}
2258
Daniel Vettere4e76842014-09-30 10:56:42 +02002259/**
2260 * intel_power_domains_init_hw - initialize hardware power domain state
2261 * @dev_priv: i915 device instance
2262 *
2263 * This function initializes the hardware power domain state and enables all
2264 * power domains using intel_display_set_init_power().
2265 */
Imre Deak73dfc222015-11-17 17:33:53 +02002266void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002267{
2268 struct drm_device *dev = dev_priv->dev;
2269 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2270
2271 power_domains->initializing = true;
2272
Imre Deak73dfc222015-11-17 17:33:53 +02002273 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
2274 skl_display_core_init(dev_priv, resume);
2275 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä770effb2015-07-08 23:45:51 +03002276 mutex_lock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002277 chv_phy_control_init(dev_priv);
Ville Syrjälä770effb2015-07-08 23:45:51 +03002278 mutex_unlock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002279 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002280 mutex_lock(&power_domains->lock);
2281 vlv_cmnlane_wa(dev_priv);
2282 mutex_unlock(&power_domains->lock);
2283 }
2284
2285 /* For now, we need the power well to be always enabled. */
2286 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002287 /* Disable power support if the user asked so. */
2288 if (!i915.disable_power_well)
2289 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
Imre Deak30eade12015-11-04 19:24:13 +02002290 intel_power_domains_sync_hw(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002291 power_domains->initializing = false;
2292}
2293
Daniel Vettere4e76842014-09-30 10:56:42 +02002294/**
Imre Deak73dfc222015-11-17 17:33:53 +02002295 * intel_power_domains_suspend - suspend power domain state
2296 * @dev_priv: i915 device instance
2297 *
2298 * This function prepares the hardware power domain state before entering
2299 * system suspend. It must be paired with intel_power_domains_init_hw().
2300 */
2301void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
2302{
2303 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
2304 skl_display_core_uninit(dev_priv);
Imre Deakd314cd42015-11-17 17:44:23 +02002305
2306 /*
2307 * Even if power well support was disabled we still want to disable
2308 * power wells while we are system suspended.
2309 */
2310 if (!i915.disable_power_well)
2311 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak73dfc222015-11-17 17:33:53 +02002312}
2313
2314/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002315 * intel_runtime_pm_get - grab a runtime pm reference
2316 * @dev_priv: i915 device instance
2317 *
2318 * This function grabs a device-level runtime pm reference (mostly used for GEM
2319 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2320 *
2321 * Any runtime pm reference obtained by this function must have a symmetric
2322 * call to intel_runtime_pm_put() to release the reference again.
2323 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002324void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2325{
2326 struct drm_device *dev = dev_priv->dev;
2327 struct device *device = &dev->pdev->dev;
2328
Daniel Vetter9c065a72014-09-30 10:56:38 +02002329 pm_runtime_get_sync(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002330
2331 atomic_inc(&dev_priv->pm.wakeref_count);
Imre Deakc9b88462015-12-15 20:10:34 +02002332 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002333}
2334
Daniel Vettere4e76842014-09-30 10:56:42 +02002335/**
Imre Deak09731282016-02-17 14:17:42 +02002336 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
2337 * @dev_priv: i915 device instance
2338 *
2339 * This function grabs a device-level runtime pm reference if the device is
2340 * already in use and ensures that it is powered up.
2341 *
2342 * Any runtime pm reference obtained by this function must have a symmetric
2343 * call to intel_runtime_pm_put() to release the reference again.
2344 */
2345bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
2346{
2347 struct drm_device *dev = dev_priv->dev;
2348 struct device *device = &dev->pdev->dev;
2349 int ret;
2350
2351 if (!IS_ENABLED(CONFIG_PM))
2352 return true;
2353
2354 ret = pm_runtime_get_if_in_use(device);
2355
2356 /*
2357 * In cases runtime PM is disabled by the RPM core and we get an
2358 * -EINVAL return value we are not supposed to call this function,
2359 * since the power state is undefined. This applies atm to the
2360 * late/early system suspend/resume handlers.
2361 */
2362 WARN_ON_ONCE(ret < 0);
2363 if (ret <= 0)
2364 return false;
2365
2366 atomic_inc(&dev_priv->pm.wakeref_count);
2367 assert_rpm_wakelock_held(dev_priv);
2368
2369 return true;
2370}
2371
2372/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002373 * intel_runtime_pm_get_noresume - grab a runtime pm reference
2374 * @dev_priv: i915 device instance
2375 *
2376 * This function grabs a device-level runtime pm reference (mostly used for GEM
2377 * code to ensure the GTT or GT is on).
2378 *
2379 * It will _not_ power up the device but instead only check that it's powered
2380 * on. Therefore it is only valid to call this functions from contexts where
2381 * the device is known to be powered up and where trying to power it up would
2382 * result in hilarity and deadlocks. That pretty much means only the system
2383 * suspend/resume code where this is used to grab runtime pm references for
2384 * delayed setup down in work items.
2385 *
2386 * Any runtime pm reference obtained by this function must have a symmetric
2387 * call to intel_runtime_pm_put() to release the reference again.
2388 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002389void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
2390{
2391 struct drm_device *dev = dev_priv->dev;
2392 struct device *device = &dev->pdev->dev;
2393
Imre Deakc9b88462015-12-15 20:10:34 +02002394 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002395 pm_runtime_get_noresume(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002396
2397 atomic_inc(&dev_priv->pm.wakeref_count);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002398}
2399
Daniel Vettere4e76842014-09-30 10:56:42 +02002400/**
2401 * intel_runtime_pm_put - release a runtime pm reference
2402 * @dev_priv: i915 device instance
2403 *
2404 * This function drops the device-level runtime pm reference obtained by
2405 * intel_runtime_pm_get() and might power down the corresponding
2406 * hardware block right away if this is the last reference.
2407 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002408void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
2409{
2410 struct drm_device *dev = dev_priv->dev;
2411 struct device *device = &dev->pdev->dev;
2412
Imre Deak542db3c2015-12-15 20:10:36 +02002413 assert_rpm_wakelock_held(dev_priv);
Imre Deak2b19efe2015-12-15 20:10:37 +02002414 if (atomic_dec_and_test(&dev_priv->pm.wakeref_count))
2415 atomic_inc(&dev_priv->pm.atomic_seq);
Imre Deak1f814da2015-12-16 02:52:19 +02002416
Daniel Vetter9c065a72014-09-30 10:56:38 +02002417 pm_runtime_mark_last_busy(device);
2418 pm_runtime_put_autosuspend(device);
2419}
2420
Daniel Vettere4e76842014-09-30 10:56:42 +02002421/**
2422 * intel_runtime_pm_enable - enable runtime pm
2423 * @dev_priv: i915 device instance
2424 *
2425 * This function enables runtime pm at the end of the driver load sequence.
2426 *
2427 * Note that this function does currently not enable runtime pm for the
2428 * subordinate display power domains. That is only done on the first modeset
2429 * using intel_display_set_init_power().
2430 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002431void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002432{
2433 struct drm_device *dev = dev_priv->dev;
2434 struct device *device = &dev->pdev->dev;
2435
Imre Deakcbc68dc2015-12-17 19:04:33 +02002436 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
2437 pm_runtime_mark_last_busy(device);
2438
Imre Deak25b181b2015-12-17 13:44:56 +02002439 /*
2440 * Take a permanent reference to disable the RPM functionality and drop
2441 * it only when unloading the driver. Use the low level get/put helpers,
2442 * so the driver's own RPM reference tracking asserts also work on
2443 * platforms without RPM support.
2444 */
Imre Deakcbc68dc2015-12-17 19:04:33 +02002445 if (!HAS_RUNTIME_PM(dev)) {
2446 pm_runtime_dont_use_autosuspend(device);
Imre Deak25b181b2015-12-17 13:44:56 +02002447 pm_runtime_get_sync(device);
Imre Deakcbc68dc2015-12-17 19:04:33 +02002448 } else {
2449 pm_runtime_use_autosuspend(device);
2450 }
Daniel Vetter9c065a72014-09-30 10:56:38 +02002451
Imre Deakaabee1b2015-12-15 20:10:29 +02002452 /*
2453 * The core calls the driver load handler with an RPM reference held.
2454 * We drop that here and will reacquire it during unloading in
2455 * intel_power_domains_fini().
2456 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002457 pm_runtime_put_autosuspend(device);
2458}
2459