blob: 2e88a5e068848afcdb3f991220d037209718bbb8 [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
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200287static void hsw_power_well_pre_disable(struct drm_i915_private *dev_priv)
288{
289 if (IS_BROADWELL(dev_priv))
290 gen8_irq_power_well_pre_disable(dev_priv,
291 1 << PIPE_C | 1 << PIPE_B);
292}
293
Damien Lespiaud14c0342015-03-06 18:50:51 +0000294static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
295 struct i915_power_well *power_well)
296{
297 struct drm_device *dev = dev_priv->dev;
298
299 /*
300 * After we re-enable the power well, if we touch VGA register 0x3d5
301 * we'll get unclaimed register interrupts. This stops after we write
302 * anything to the VGA MSR register. The vgacon module uses this
303 * register all the time, so if we unbind our driver and, as a
304 * consequence, bind vgacon, we'll get stuck in an infinite loop at
305 * console_unlock(). So make here we touch the VGA MSR register, making
306 * sure vgacon can keep working normally without triggering interrupts
307 * and error messages.
308 */
309 if (power_well->data == SKL_DISP_PW_2) {
310 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
311 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
312 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
313
314 gen8_irq_power_well_post_enable(dev_priv,
315 1 << PIPE_C | 1 << PIPE_B);
316 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000317}
318
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200319static void skl_power_well_pre_disable(struct drm_i915_private *dev_priv,
320 struct i915_power_well *power_well)
321{
322 if (power_well->data == SKL_DISP_PW_2)
323 gen8_irq_power_well_pre_disable(dev_priv,
324 1 << PIPE_C | 1 << PIPE_B);
325}
326
Daniel Vetter9c065a72014-09-30 10:56:38 +0200327static void hsw_set_power_well(struct drm_i915_private *dev_priv,
328 struct i915_power_well *power_well, bool enable)
329{
330 bool is_enabled, enable_requested;
331 uint32_t tmp;
332
333 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
334 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
335 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
336
337 if (enable) {
338 if (!enable_requested)
339 I915_WRITE(HSW_PWR_WELL_DRIVER,
340 HSW_PWR_WELL_ENABLE_REQUEST);
341
342 if (!is_enabled) {
343 DRM_DEBUG_KMS("Enabling power well\n");
344 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
345 HSW_PWR_WELL_STATE_ENABLED), 20))
346 DRM_ERROR("Timeout enabling power well\n");
Paulo Zanoni6d729bf2014-10-07 16:11:11 -0300347 hsw_power_well_post_enable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200348 }
349
Daniel Vetter9c065a72014-09-30 10:56:38 +0200350 } else {
351 if (enable_requested) {
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200352 hsw_power_well_pre_disable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200353 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
354 POSTING_READ(HSW_PWR_WELL_DRIVER);
355 DRM_DEBUG_KMS("Requesting to disable the power well\n");
356 }
357 }
358}
359
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000360#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
361 BIT(POWER_DOMAIN_TRANSCODER_A) | \
362 BIT(POWER_DOMAIN_PIPE_B) | \
363 BIT(POWER_DOMAIN_TRANSCODER_B) | \
364 BIT(POWER_DOMAIN_PIPE_C) | \
365 BIT(POWER_DOMAIN_TRANSCODER_C) | \
366 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
367 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100368 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
369 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
370 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
371 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000372 BIT(POWER_DOMAIN_AUX_B) | \
373 BIT(POWER_DOMAIN_AUX_C) | \
374 BIT(POWER_DOMAIN_AUX_D) | \
375 BIT(POWER_DOMAIN_AUDIO) | \
376 BIT(POWER_DOMAIN_VGA) | \
377 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000378#define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100379 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
380 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000381 BIT(POWER_DOMAIN_INIT))
382#define SKL_DISPLAY_DDI_B_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100383 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000384 BIT(POWER_DOMAIN_INIT))
385#define SKL_DISPLAY_DDI_C_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100386 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000387 BIT(POWER_DOMAIN_INIT))
388#define SKL_DISPLAY_DDI_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100389 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000390 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100391#define SKL_DISPLAY_DC_OFF_POWER_DOMAINS ( \
392 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
393 BIT(POWER_DOMAIN_MODESET) | \
394 BIT(POWER_DOMAIN_AUX_A) | \
395 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000396#define SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
Imre Deak4a76f292015-11-04 19:24:15 +0200397 (POWER_DOMAIN_MASK & ~( \
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100398 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
399 SKL_DISPLAY_DC_OFF_POWER_DOMAINS)) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000400 BIT(POWER_DOMAIN_INIT))
401
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530402#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
403 BIT(POWER_DOMAIN_TRANSCODER_A) | \
404 BIT(POWER_DOMAIN_PIPE_B) | \
405 BIT(POWER_DOMAIN_TRANSCODER_B) | \
406 BIT(POWER_DOMAIN_PIPE_C) | \
407 BIT(POWER_DOMAIN_TRANSCODER_C) | \
408 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
409 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100410 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
411 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530412 BIT(POWER_DOMAIN_AUX_B) | \
413 BIT(POWER_DOMAIN_AUX_C) | \
414 BIT(POWER_DOMAIN_AUDIO) | \
415 BIT(POWER_DOMAIN_VGA) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100416 BIT(POWER_DOMAIN_GMBUS) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530417 BIT(POWER_DOMAIN_INIT))
418#define BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS ( \
419 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
420 BIT(POWER_DOMAIN_PIPE_A) | \
421 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
422 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100423 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
Jani Nikulaacad8892016-03-08 21:00:56 +0200424 BIT(POWER_DOMAIN_PORT_DSI) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530425 BIT(POWER_DOMAIN_AUX_A) | \
426 BIT(POWER_DOMAIN_PLLS) | \
427 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100428#define BXT_DISPLAY_DC_OFF_POWER_DOMAINS ( \
429 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
430 BIT(POWER_DOMAIN_MODESET) | \
431 BIT(POWER_DOMAIN_AUX_A) | \
432 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530433#define BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
434 (POWER_DOMAIN_MASK & ~(BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
435 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS)) | \
436 BIT(POWER_DOMAIN_INIT))
437
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530438static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
439{
440 struct drm_device *dev = dev_priv->dev;
441
442 WARN(!IS_BROXTON(dev), "Platform doesn't support DC9.\n");
443 WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
444 "DC9 already programmed to be enabled.\n");
445 WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
446 "DC5 still not disabled to enable DC9.\n");
447 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
448 WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
449
450 /*
451 * TODO: check for the following to verify the conditions to enter DC9
452 * state are satisfied:
453 * 1] Check relevant display engine registers to verify if mode set
454 * disable sequence was followed.
455 * 2] Check if display uninitialize sequence is initialized.
456 */
457}
458
459static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
460{
461 WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530462 WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
463 "DC5 still not disabled.\n");
464
465 /*
466 * TODO: check for the following to verify DC9 state was indeed
467 * entered before programming to disable it:
468 * 1] Check relevant display engine registers to verify if mode
469 * set disable sequence was followed.
470 * 2] Check if display uninitialize sequence is initialized.
471 */
472}
473
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200474static void gen9_write_dc_state(struct drm_i915_private *dev_priv,
475 u32 state)
476{
477 int rewrites = 0;
478 int rereads = 0;
479 u32 v;
480
481 I915_WRITE(DC_STATE_EN, state);
482
483 /* It has been observed that disabling the dc6 state sometimes
484 * doesn't stick and dmc keeps returning old value. Make sure
485 * the write really sticks enough times and also force rewrite until
486 * we are confident that state is exactly what we want.
487 */
488 do {
489 v = I915_READ(DC_STATE_EN);
490
491 if (v != state) {
492 I915_WRITE(DC_STATE_EN, state);
493 rewrites++;
494 rereads = 0;
495 } else if (rereads++ > 5) {
496 break;
497 }
498
499 } while (rewrites < 100);
500
501 if (v != state)
502 DRM_ERROR("Writing dc state to 0x%x failed, now 0x%x\n",
503 state, v);
504
505 /* Most of the times we need one retry, avoid spam */
506 if (rewrites > 1)
507 DRM_DEBUG_KMS("Rewrote dc state to 0x%x %d times\n",
508 state, rewrites);
509}
510
Imre Deak13ae3a02015-11-04 19:24:16 +0200511static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530512{
513 uint32_t val;
Imre Deak13ae3a02015-11-04 19:24:16 +0200514 uint32_t mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530515
Imre Deak13ae3a02015-11-04 19:24:16 +0200516 mask = DC_STATE_EN_UPTO_DC5;
517 if (IS_BROXTON(dev_priv))
518 mask |= DC_STATE_EN_DC9;
519 else
520 mask |= DC_STATE_EN_UPTO_DC6;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530521
Imre Deaka37baf32016-02-29 22:49:03 +0200522 if (WARN_ON_ONCE(state & ~dev_priv->csr.allowed_dc_mask))
523 state &= dev_priv->csr.allowed_dc_mask;
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100524
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530525 val = I915_READ(DC_STATE_EN);
Imre Deak13ae3a02015-11-04 19:24:16 +0200526 DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
527 val & mask, state);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200528
529 /* Check if DMC is ignoring our DC state requests */
530 if ((val & mask) != dev_priv->csr.dc_state)
531 DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
532 dev_priv->csr.dc_state, val & mask);
533
Imre Deak13ae3a02015-11-04 19:24:16 +0200534 val &= ~mask;
535 val |= state;
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200536
537 gen9_write_dc_state(dev_priv, val);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200538
539 dev_priv->csr.dc_state = val & mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530540}
541
Imre Deak13ae3a02015-11-04 19:24:16 +0200542void bxt_enable_dc9(struct drm_i915_private *dev_priv)
543{
544 assert_can_enable_dc9(dev_priv);
545
546 DRM_DEBUG_KMS("Enabling DC9\n");
547
548 gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
549}
550
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530551void bxt_disable_dc9(struct drm_i915_private *dev_priv)
552{
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530553 assert_can_disable_dc9(dev_priv);
554
555 DRM_DEBUG_KMS("Disabling DC9\n");
556
Imre Deak13ae3a02015-11-04 19:24:16 +0200557 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530558}
559
Daniel Vetteraf5fead2015-10-28 23:58:57 +0200560static void assert_csr_loaded(struct drm_i915_private *dev_priv)
561{
562 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
563 "CSR program storage start is NULL\n");
564 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
565 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
566}
567
Suketu Shah5aefb232015-04-16 14:22:10 +0530568static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shahdc174302015-04-17 19:46:16 +0530569{
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530570 struct drm_device *dev = dev_priv->dev;
Suketu Shah5aefb232015-04-16 14:22:10 +0530571 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
572 SKL_DISP_PW_2);
573
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800574 WARN_ONCE(!IS_SKYLAKE(dev) && !IS_KABYLAKE(dev),
575 "Platform doesn't support DC5.\n");
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700576 WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
577 WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530578
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700579 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
580 "DC5 already programmed to be enabled.\n");
Imre Deakc9b88462015-12-15 20:10:34 +0200581 assert_rpm_wakelock_held(dev_priv);
Suketu Shah5aefb232015-04-16 14:22:10 +0530582
583 assert_csr_loaded(dev_priv);
584}
585
Suketu Shah5aefb232015-04-16 14:22:10 +0530586static void gen9_enable_dc5(struct drm_i915_private *dev_priv)
587{
Suketu Shah5aefb232015-04-16 14:22:10 +0530588 assert_can_enable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530589
590 DRM_DEBUG_KMS("Enabling DC5\n");
591
Imre Deak13ae3a02015-11-04 19:24:16 +0200592 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
Suketu Shahdc174302015-04-17 19:46:16 +0530593}
594
Suketu Shah93c7cb62015-04-16 14:22:13 +0530595static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530596{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530597 struct drm_device *dev = dev_priv->dev;
Suketu Shah93c7cb62015-04-16 14:22:13 +0530598
Rodrigo Vivi8d7a1c42016-01-07 16:49:39 -0800599 WARN_ONCE(!IS_SKYLAKE(dev) && !IS_KABYLAKE(dev),
600 "Platform doesn't support DC6.\n");
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700601 WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
602 WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
603 "Backlight is not disabled.\n");
604 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
605 "DC6 already programmed to be enabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530606
607 assert_csr_loaded(dev_priv);
608}
609
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530610void skl_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shah93c7cb62015-04-16 14:22:13 +0530611{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530612 assert_can_enable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530613
614 DRM_DEBUG_KMS("Enabling DC6\n");
615
Imre Deak13ae3a02015-11-04 19:24:16 +0200616 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
617
Suketu Shahf75a1982015-04-16 14:22:11 +0530618}
619
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530620void skl_disable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530621{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530622 DRM_DEBUG_KMS("Disabling DC6\n");
623
Imre Deak13ae3a02015-11-04 19:24:16 +0200624 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Suketu Shahf75a1982015-04-16 14:22:11 +0530625}
626
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000627static void skl_set_power_well(struct drm_i915_private *dev_priv,
628 struct i915_power_well *power_well, bool enable)
629{
630 uint32_t tmp, fuse_status;
631 uint32_t req_mask, state_mask;
Damien Lespiau2a518352015-03-06 18:50:49 +0000632 bool is_enabled, enable_requested, check_fuse_status = false;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000633
634 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
635 fuse_status = I915_READ(SKL_FUSE_STATUS);
636
637 switch (power_well->data) {
638 case SKL_DISP_PW_1:
639 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
640 SKL_FUSE_PG0_DIST_STATUS), 1)) {
641 DRM_ERROR("PG0 not enabled\n");
642 return;
643 }
644 break;
645 case SKL_DISP_PW_2:
646 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
647 DRM_ERROR("PG1 in disabled state\n");
648 return;
649 }
650 break;
651 case SKL_DISP_PW_DDI_A_E:
652 case SKL_DISP_PW_DDI_B:
653 case SKL_DISP_PW_DDI_C:
654 case SKL_DISP_PW_DDI_D:
655 case SKL_DISP_PW_MISC_IO:
656 break;
657 default:
658 WARN(1, "Unknown power well %lu\n", power_well->data);
659 return;
660 }
661
662 req_mask = SKL_POWER_WELL_REQ(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000663 enable_requested = tmp & req_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000664 state_mask = SKL_POWER_WELL_STATE(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000665 is_enabled = tmp & state_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000666
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200667 if (!enable && enable_requested)
668 skl_power_well_pre_disable(dev_priv, power_well);
669
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000670 if (enable) {
Damien Lespiau2a518352015-03-06 18:50:49 +0000671 if (!enable_requested) {
Suketu Shahdc174302015-04-17 19:46:16 +0530672 WARN((tmp & state_mask) &&
673 !I915_READ(HSW_PWR_WELL_BIOS),
674 "Invalid for power well status to be enabled, unless done by the BIOS, \
675 when request is to disable!\n");
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000676 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000677 }
678
Damien Lespiau2a518352015-03-06 18:50:49 +0000679 if (!is_enabled) {
Damien Lespiau510e6fd2015-03-06 18:50:50 +0000680 DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000681 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
682 state_mask), 1))
683 DRM_ERROR("%s enable timeout\n",
684 power_well->name);
685 check_fuse_status = true;
686 }
687 } else {
Damien Lespiau2a518352015-03-06 18:50:49 +0000688 if (enable_requested) {
Imre Deak4a76f292015-11-04 19:24:15 +0200689 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
690 POSTING_READ(HSW_PWR_WELL_DRIVER);
691 DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000692 }
693 }
694
695 if (check_fuse_status) {
696 if (power_well->data == SKL_DISP_PW_1) {
697 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
698 SKL_FUSE_PG1_DIST_STATUS), 1))
699 DRM_ERROR("PG1 distributing status timeout\n");
700 } else if (power_well->data == SKL_DISP_PW_2) {
701 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
702 SKL_FUSE_PG2_DIST_STATUS), 1))
703 DRM_ERROR("PG2 distributing status timeout\n");
704 }
705 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000706
707 if (enable && !is_enabled)
708 skl_power_well_post_enable(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000709}
710
Daniel Vetter9c065a72014-09-30 10:56:38 +0200711static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
712 struct i915_power_well *power_well)
713{
714 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
715
716 /*
717 * We're taking over the BIOS, so clear any requests made by it since
718 * the driver is in charge now.
719 */
720 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
721 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
722}
723
724static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
725 struct i915_power_well *power_well)
726{
727 hsw_set_power_well(dev_priv, power_well, true);
728}
729
730static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
731 struct i915_power_well *power_well)
732{
733 hsw_set_power_well(dev_priv, power_well, false);
734}
735
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000736static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
737 struct i915_power_well *power_well)
738{
739 uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
740 SKL_POWER_WELL_STATE(power_well->data);
741
742 return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
743}
744
745static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
746 struct i915_power_well *power_well)
747{
748 skl_set_power_well(dev_priv, power_well, power_well->count > 0);
749
750 /* Clear any request made by BIOS as driver is taking over */
751 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
752}
753
754static void skl_power_well_enable(struct drm_i915_private *dev_priv,
755 struct i915_power_well *power_well)
756{
757 skl_set_power_well(dev_priv, power_well, true);
758}
759
760static void skl_power_well_disable(struct drm_i915_private *dev_priv,
761 struct i915_power_well *power_well)
762{
763 skl_set_power_well(dev_priv, power_well, false);
764}
765
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100766static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
767 struct i915_power_well *power_well)
768{
769 return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
770}
771
772static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
773 struct i915_power_well *power_well)
774{
Imre Deak5b773eb2016-02-29 22:49:05 +0200775 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100776}
777
778static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
779 struct i915_power_well *power_well)
780{
Imre Deaka37baf32016-02-29 22:49:03 +0200781 if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC6)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100782 skl_enable_dc6(dev_priv);
Imre Deaka37baf32016-02-29 22:49:03 +0200783 else if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100784 gen9_enable_dc5(dev_priv);
785}
786
787static void gen9_dc_off_power_well_sync_hw(struct drm_i915_private *dev_priv,
788 struct i915_power_well *power_well)
789{
Imre Deaka37baf32016-02-29 22:49:03 +0200790 if (power_well->count > 0)
791 gen9_dc_off_power_well_enable(dev_priv, power_well);
792 else
793 gen9_dc_off_power_well_disable(dev_priv, power_well);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100794}
795
Daniel Vetter9c065a72014-09-30 10:56:38 +0200796static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
797 struct i915_power_well *power_well)
798{
799}
800
801static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
802 struct i915_power_well *power_well)
803{
804 return true;
805}
806
807static void vlv_set_power_well(struct drm_i915_private *dev_priv,
808 struct i915_power_well *power_well, bool enable)
809{
810 enum punit_power_well power_well_id = power_well->data;
811 u32 mask;
812 u32 state;
813 u32 ctrl;
814
815 mask = PUNIT_PWRGT_MASK(power_well_id);
816 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
817 PUNIT_PWRGT_PWR_GATE(power_well_id);
818
819 mutex_lock(&dev_priv->rps.hw_lock);
820
821#define COND \
822 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
823
824 if (COND)
825 goto out;
826
827 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
828 ctrl &= ~mask;
829 ctrl |= state;
830 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
831
832 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +0900833 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +0200834 state,
835 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
836
837#undef COND
838
839out:
840 mutex_unlock(&dev_priv->rps.hw_lock);
841}
842
843static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
844 struct i915_power_well *power_well)
845{
846 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
847}
848
849static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
850 struct i915_power_well *power_well)
851{
852 vlv_set_power_well(dev_priv, power_well, true);
853}
854
855static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
856 struct i915_power_well *power_well)
857{
858 vlv_set_power_well(dev_priv, power_well, false);
859}
860
861static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
862 struct i915_power_well *power_well)
863{
864 int power_well_id = power_well->data;
865 bool enabled = false;
866 u32 mask;
867 u32 state;
868 u32 ctrl;
869
870 mask = PUNIT_PWRGT_MASK(power_well_id);
871 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
872
873 mutex_lock(&dev_priv->rps.hw_lock);
874
875 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
876 /*
877 * We only ever set the power-on and power-gate states, anything
878 * else is unexpected.
879 */
880 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
881 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
882 if (state == ctrl)
883 enabled = true;
884
885 /*
886 * A transient state at this point would mean some unexpected party
887 * is poking at the power controls too.
888 */
889 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
890 WARN_ON(ctrl != state);
891
892 mutex_unlock(&dev_priv->rps.hw_lock);
893
894 return enabled;
895}
896
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300897static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200898{
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300899 enum pipe pipe;
900
901 /*
902 * Enable the CRI clock source so we can get at the
903 * display and the reference clock for VGA
904 * hotplug / manual detection. Supposedly DSI also
905 * needs the ref clock up and running.
906 *
907 * CHV DPLL B/C have some issues if VGA mode is enabled.
908 */
909 for_each_pipe(dev_priv->dev, pipe) {
910 u32 val = I915_READ(DPLL(pipe));
911
912 val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
913 if (pipe != PIPE_A)
914 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
915
916 I915_WRITE(DPLL(pipe), val);
917 }
Daniel Vetter9c065a72014-09-30 10:56:38 +0200918
919 spin_lock_irq(&dev_priv->irq_lock);
920 valleyview_enable_display_irqs(dev_priv);
921 spin_unlock_irq(&dev_priv->irq_lock);
922
923 /*
924 * During driver initialization/resume we can avoid restoring the
925 * part of the HW/SW state that will be inited anyway explicitly.
926 */
927 if (dev_priv->power_domains.initializing)
928 return;
929
Daniel Vetterb9632912014-09-30 10:56:44 +0200930 intel_hpd_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200931
932 i915_redisable_vga_power_on(dev_priv->dev);
933}
934
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300935static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
936{
937 spin_lock_irq(&dev_priv->irq_lock);
938 valleyview_disable_display_irqs(dev_priv);
939 spin_unlock_irq(&dev_priv->irq_lock);
940
Ville Syrjälä2230fde2016-02-19 18:41:52 +0200941 /* make sure we're done processing display irqs */
942 synchronize_irq(dev_priv->dev->irq);
943
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300944 vlv_power_sequencer_reset(dev_priv);
945}
946
947static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
948 struct i915_power_well *power_well)
949{
950 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
951
952 vlv_set_power_well(dev_priv, power_well, true);
953
954 vlv_display_power_well_init(dev_priv);
955}
956
Daniel Vetter9c065a72014-09-30 10:56:38 +0200957static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
958 struct i915_power_well *power_well)
959{
960 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
961
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300962 vlv_display_power_well_deinit(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200963
964 vlv_set_power_well(dev_priv, power_well, false);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200965}
966
967static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
968 struct i915_power_well *power_well)
969{
970 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
971
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300972 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +0200973 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
974
975 vlv_set_power_well(dev_priv, power_well, true);
976
977 /*
978 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
979 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
980 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
981 * b. The other bits such as sfr settings / modesel may all
982 * be set to 0.
983 *
984 * This should only be done on init and resume from S3 with
985 * both PLLs disabled, or we risk losing DPIO and PLL
986 * synchronization.
987 */
988 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
989}
990
991static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
992 struct i915_power_well *power_well)
993{
994 enum pipe pipe;
995
996 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
997
998 for_each_pipe(dev_priv, pipe)
999 assert_pll_disabled(dev_priv, pipe);
1000
1001 /* Assert common reset */
1002 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1003
1004 vlv_set_power_well(dev_priv, power_well, false);
1005}
1006
Ville Syrjälä30142272015-07-08 23:46:01 +03001007#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
1008
1009static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1010 int power_well_id)
1011{
1012 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Ville Syrjälä30142272015-07-08 23:46:01 +03001013 int i;
1014
Imre Deakfc17f222015-11-04 19:24:11 +02001015 for (i = 0; i < power_domains->power_well_count; i++) {
1016 struct i915_power_well *power_well;
1017
1018 power_well = &power_domains->power_wells[i];
Ville Syrjälä30142272015-07-08 23:46:01 +03001019 if (power_well->data == power_well_id)
1020 return power_well;
1021 }
1022
1023 return NULL;
1024}
1025
1026#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1027
1028static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1029{
1030 struct i915_power_well *cmn_bc =
1031 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1032 struct i915_power_well *cmn_d =
1033 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1034 u32 phy_control = dev_priv->chv_phy_control;
1035 u32 phy_status = 0;
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001036 u32 phy_status_mask = 0xffffffff;
Ville Syrjälä30142272015-07-08 23:46:01 +03001037 u32 tmp;
1038
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001039 /*
1040 * The BIOS can leave the PHY is some weird state
1041 * where it doesn't fully power down some parts.
1042 * Disable the asserts until the PHY has been fully
1043 * reset (ie. the power well has been disabled at
1044 * least once).
1045 */
1046 if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1047 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1048 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1049 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1050 PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1051 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1052 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1053
1054 if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1055 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1056 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1057 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1058
Ville Syrjälä30142272015-07-08 23:46:01 +03001059 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1060 phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1061
1062 /* this assumes override is only used to enable lanes */
1063 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1064 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1065
1066 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1067 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1068
1069 /* CL1 is on whenever anything is on in either channel */
1070 if (BITS_SET(phy_control,
1071 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1072 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1073 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1074
1075 /*
1076 * The DPLLB check accounts for the pipe B + port A usage
1077 * with CL2 powered up but all the lanes in the second channel
1078 * powered down.
1079 */
1080 if (BITS_SET(phy_control,
1081 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1082 (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1083 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1084
1085 if (BITS_SET(phy_control,
1086 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1087 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1088 if (BITS_SET(phy_control,
1089 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1090 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1091
1092 if (BITS_SET(phy_control,
1093 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1094 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1095 if (BITS_SET(phy_control,
1096 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1097 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1098 }
1099
1100 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1101 phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1102
1103 /* this assumes override is only used to enable lanes */
1104 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1105 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1106
1107 if (BITS_SET(phy_control,
1108 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1109 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1110
1111 if (BITS_SET(phy_control,
1112 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1113 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1114 if (BITS_SET(phy_control,
1115 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1116 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1117 }
1118
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001119 phy_status &= phy_status_mask;
1120
Ville Syrjälä30142272015-07-08 23:46:01 +03001121 /*
1122 * The PHY may be busy with some initial calibration and whatnot,
1123 * so the power state can take a while to actually change.
1124 */
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001125 if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
Ville Syrjälä30142272015-07-08 23:46:01 +03001126 WARN(phy_status != tmp,
1127 "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1128 tmp, phy_status, dev_priv->chv_phy_control);
1129}
1130
1131#undef BITS_SET
1132
Daniel Vetter9c065a72014-09-30 10:56:38 +02001133static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1134 struct i915_power_well *power_well)
1135{
1136 enum dpio_phy phy;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001137 enum pipe pipe;
1138 uint32_t tmp;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001139
1140 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1141 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1142
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001143 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1144 pipe = PIPE_A;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001145 phy = DPIO_PHY0;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001146 } else {
1147 pipe = PIPE_C;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001148 phy = DPIO_PHY1;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001149 }
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001150
1151 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001152 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1153 vlv_set_power_well(dev_priv, power_well, true);
1154
1155 /* Poll for phypwrgood signal */
1156 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
1157 DRM_ERROR("Display PHY %d is not power up\n", phy);
1158
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001159 mutex_lock(&dev_priv->sb_lock);
1160
1161 /* Enable dynamic power down */
1162 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
Ville Syrjäläee279212015-07-08 23:45:57 +03001163 tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1164 DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001165 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1166
1167 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1168 tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1169 tmp |= DPIO_DYNPWRDOWNEN_CH1;
1170 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
Ville Syrjälä3e288782015-07-08 23:45:58 +03001171 } else {
1172 /*
1173 * Force the non-existing CL2 off. BXT does this
1174 * too, so maybe it saves some power even though
1175 * CL2 doesn't exist?
1176 */
1177 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1178 tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1179 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001180 }
1181
1182 mutex_unlock(&dev_priv->sb_lock);
1183
Ville Syrjälä70722462015-04-10 18:21:28 +03001184 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1185 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001186
1187 DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1188 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001189
1190 assert_chv_phy_status(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001191}
1192
1193static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1194 struct i915_power_well *power_well)
1195{
1196 enum dpio_phy phy;
1197
1198 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1199 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1200
1201 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1202 phy = DPIO_PHY0;
1203 assert_pll_disabled(dev_priv, PIPE_A);
1204 assert_pll_disabled(dev_priv, PIPE_B);
1205 } else {
1206 phy = DPIO_PHY1;
1207 assert_pll_disabled(dev_priv, PIPE_C);
1208 }
1209
Ville Syrjälä70722462015-04-10 18:21:28 +03001210 dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1211 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001212
1213 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001214
1215 DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1216 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001217
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001218 /* PHY is fully reset now, so we can enable the PHY state asserts */
1219 dev_priv->chv_phy_assert[phy] = true;
1220
Ville Syrjälä30142272015-07-08 23:46:01 +03001221 assert_chv_phy_status(dev_priv);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001222}
1223
Ville Syrjälä6669e392015-07-08 23:46:00 +03001224static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1225 enum dpio_channel ch, bool override, unsigned int mask)
1226{
1227 enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1228 u32 reg, val, expected, actual;
1229
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001230 /*
1231 * The BIOS can leave the PHY is some weird state
1232 * where it doesn't fully power down some parts.
1233 * Disable the asserts until the PHY has been fully
1234 * reset (ie. the power well has been disabled at
1235 * least once).
1236 */
1237 if (!dev_priv->chv_phy_assert[phy])
1238 return;
1239
Ville Syrjälä6669e392015-07-08 23:46:00 +03001240 if (ch == DPIO_CH0)
1241 reg = _CHV_CMN_DW0_CH0;
1242 else
1243 reg = _CHV_CMN_DW6_CH1;
1244
1245 mutex_lock(&dev_priv->sb_lock);
1246 val = vlv_dpio_read(dev_priv, pipe, reg);
1247 mutex_unlock(&dev_priv->sb_lock);
1248
1249 /*
1250 * This assumes !override is only used when the port is disabled.
1251 * All lanes should power down even without the override when
1252 * the port is disabled.
1253 */
1254 if (!override || mask == 0xf) {
1255 expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1256 /*
1257 * If CH1 common lane is not active anymore
1258 * (eg. for pipe B DPLL) the entire channel will
1259 * shut down, which causes the common lane registers
1260 * to read as 0. That means we can't actually check
1261 * the lane power down status bits, but as the entire
1262 * register reads as 0 it's a good indication that the
1263 * channel is indeed entirely powered down.
1264 */
1265 if (ch == DPIO_CH1 && val == 0)
1266 expected = 0;
1267 } else if (mask != 0x0) {
1268 expected = DPIO_ANYDL_POWERDOWN;
1269 } else {
1270 expected = 0;
1271 }
1272
1273 if (ch == DPIO_CH0)
1274 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1275 else
1276 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1277 actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1278
1279 WARN(actual != expected,
1280 "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1281 !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1282 !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1283 reg, val);
1284}
1285
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001286bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1287 enum dpio_channel ch, bool override)
1288{
1289 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1290 bool was_override;
1291
1292 mutex_lock(&power_domains->lock);
1293
1294 was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1295
1296 if (override == was_override)
1297 goto out;
1298
1299 if (override)
1300 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1301 else
1302 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1303
1304 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1305
1306 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1307 phy, ch, dev_priv->chv_phy_control);
1308
Ville Syrjälä30142272015-07-08 23:46:01 +03001309 assert_chv_phy_status(dev_priv);
1310
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001311out:
1312 mutex_unlock(&power_domains->lock);
1313
1314 return was_override;
1315}
1316
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001317void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1318 bool override, unsigned int mask)
1319{
1320 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1321 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1322 enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1323 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1324
1325 mutex_lock(&power_domains->lock);
1326
1327 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1328 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1329
1330 if (override)
1331 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1332 else
1333 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1334
1335 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1336
1337 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1338 phy, ch, mask, dev_priv->chv_phy_control);
1339
Ville Syrjälä30142272015-07-08 23:46:01 +03001340 assert_chv_phy_status(dev_priv);
1341
Ville Syrjälä6669e392015-07-08 23:46:00 +03001342 assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1343
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001344 mutex_unlock(&power_domains->lock);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001345}
1346
1347static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1348 struct i915_power_well *power_well)
1349{
1350 enum pipe pipe = power_well->data;
1351 bool enabled;
1352 u32 state, ctrl;
1353
1354 mutex_lock(&dev_priv->rps.hw_lock);
1355
1356 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1357 /*
1358 * We only ever set the power-on and power-gate states, anything
1359 * else is unexpected.
1360 */
1361 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1362 enabled = state == DP_SSS_PWR_ON(pipe);
1363
1364 /*
1365 * A transient state at this point would mean some unexpected party
1366 * is poking at the power controls too.
1367 */
1368 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1369 WARN_ON(ctrl << 16 != state);
1370
1371 mutex_unlock(&dev_priv->rps.hw_lock);
1372
1373 return enabled;
1374}
1375
1376static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1377 struct i915_power_well *power_well,
1378 bool enable)
1379{
1380 enum pipe pipe = power_well->data;
1381 u32 state;
1382 u32 ctrl;
1383
1384 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1385
1386 mutex_lock(&dev_priv->rps.hw_lock);
1387
1388#define COND \
1389 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1390
1391 if (COND)
1392 goto out;
1393
1394 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1395 ctrl &= ~DP_SSC_MASK(pipe);
1396 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1397 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1398
1399 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +09001400 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001401 state,
1402 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1403
1404#undef COND
1405
1406out:
1407 mutex_unlock(&dev_priv->rps.hw_lock);
1408}
1409
1410static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1411 struct i915_power_well *power_well)
1412{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001413 WARN_ON_ONCE(power_well->data != PIPE_A);
1414
Daniel Vetter9c065a72014-09-30 10:56:38 +02001415 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1416}
1417
1418static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1419 struct i915_power_well *power_well)
1420{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001421 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001422
1423 chv_set_pipe_power_well(dev_priv, power_well, true);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001424
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001425 vlv_display_power_well_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001426}
1427
1428static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1429 struct i915_power_well *power_well)
1430{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001431 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001432
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001433 vlv_display_power_well_deinit(dev_priv);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001434
Daniel Vetter9c065a72014-09-30 10:56:38 +02001435 chv_set_pipe_power_well(dev_priv, power_well, false);
1436}
1437
Imre Deak09731282016-02-17 14:17:42 +02001438static void
1439__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1440 enum intel_display_power_domain domain)
1441{
1442 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1443 struct i915_power_well *power_well;
1444 int i;
1445
1446 for_each_power_well(i, power_well, BIT(domain), power_domains) {
1447 if (!power_well->count++)
1448 intel_power_well_enable(dev_priv, power_well);
1449 }
1450
1451 power_domains->domain_use_count[domain]++;
1452}
1453
Daniel Vettere4e76842014-09-30 10:56:42 +02001454/**
1455 * intel_display_power_get - grab a power domain reference
1456 * @dev_priv: i915 device instance
1457 * @domain: power domain to reference
1458 *
1459 * This function grabs a power domain reference for @domain and ensures that the
1460 * power domain and all its parents are powered up. Therefore users should only
1461 * grab a reference to the innermost power domain they need.
1462 *
1463 * Any power domain reference obtained by this function must have a symmetric
1464 * call to intel_display_power_put() to release the reference again.
1465 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001466void intel_display_power_get(struct drm_i915_private *dev_priv,
1467 enum intel_display_power_domain domain)
1468{
Imre Deak09731282016-02-17 14:17:42 +02001469 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001470
1471 intel_runtime_pm_get(dev_priv);
1472
Imre Deak09731282016-02-17 14:17:42 +02001473 mutex_lock(&power_domains->lock);
1474
1475 __intel_display_power_get_domain(dev_priv, domain);
1476
1477 mutex_unlock(&power_domains->lock);
1478}
1479
1480/**
1481 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
1482 * @dev_priv: i915 device instance
1483 * @domain: power domain to reference
1484 *
1485 * This function grabs a power domain reference for @domain and ensures that the
1486 * power domain and all its parents are powered up. Therefore users should only
1487 * grab a reference to the innermost power domain they need.
1488 *
1489 * Any power domain reference obtained by this function must have a symmetric
1490 * call to intel_display_power_put() to release the reference again.
1491 */
1492bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1493 enum intel_display_power_domain domain)
1494{
1495 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1496 bool is_enabled;
1497
1498 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1499 return false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001500
1501 mutex_lock(&power_domains->lock);
1502
Imre Deak09731282016-02-17 14:17:42 +02001503 if (__intel_display_power_is_enabled(dev_priv, domain)) {
1504 __intel_display_power_get_domain(dev_priv, domain);
1505 is_enabled = true;
1506 } else {
1507 is_enabled = false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001508 }
1509
Daniel Vetter9c065a72014-09-30 10:56:38 +02001510 mutex_unlock(&power_domains->lock);
Imre Deak09731282016-02-17 14:17:42 +02001511
1512 if (!is_enabled)
1513 intel_runtime_pm_put(dev_priv);
1514
1515 return is_enabled;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001516}
1517
Daniel Vettere4e76842014-09-30 10:56:42 +02001518/**
1519 * intel_display_power_put - release a power domain reference
1520 * @dev_priv: i915 device instance
1521 * @domain: power domain to reference
1522 *
1523 * This function drops the power domain reference obtained by
1524 * intel_display_power_get() and might power down the corresponding hardware
1525 * block right away if this is the last reference.
1526 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001527void intel_display_power_put(struct drm_i915_private *dev_priv,
1528 enum intel_display_power_domain domain)
1529{
1530 struct i915_power_domains *power_domains;
1531 struct i915_power_well *power_well;
1532 int i;
1533
1534 power_domains = &dev_priv->power_domains;
1535
1536 mutex_lock(&power_domains->lock);
1537
Daniel Stone11c86db2015-11-20 15:55:34 +00001538 WARN(!power_domains->domain_use_count[domain],
1539 "Use count on domain %s is already zero\n",
1540 intel_display_power_domain_str(domain));
Daniel Vetter9c065a72014-09-30 10:56:38 +02001541 power_domains->domain_use_count[domain]--;
1542
1543 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Daniel Stone11c86db2015-11-20 15:55:34 +00001544 WARN(!power_well->count,
1545 "Use count on power well %s is already zero",
1546 power_well->name);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001547
Imre Deakd314cd42015-11-17 17:44:23 +02001548 if (!--power_well->count)
Damien Lespiaudcddab32015-07-30 18:20:27 -03001549 intel_power_well_disable(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001550 }
1551
1552 mutex_unlock(&power_domains->lock);
1553
1554 intel_runtime_pm_put(dev_priv);
1555}
1556
Daniel Vetter9c065a72014-09-30 10:56:38 +02001557#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
1558 BIT(POWER_DOMAIN_PIPE_A) | \
1559 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001560 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
1561 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1562 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1563 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001564 BIT(POWER_DOMAIN_PORT_CRT) | \
1565 BIT(POWER_DOMAIN_PLLS) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001566 BIT(POWER_DOMAIN_AUX_A) | \
1567 BIT(POWER_DOMAIN_AUX_B) | \
1568 BIT(POWER_DOMAIN_AUX_C) | \
1569 BIT(POWER_DOMAIN_AUX_D) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01001570 BIT(POWER_DOMAIN_GMBUS) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001571 BIT(POWER_DOMAIN_INIT))
1572#define HSW_DISPLAY_POWER_DOMAINS ( \
1573 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
1574 BIT(POWER_DOMAIN_INIT))
1575
1576#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
1577 HSW_ALWAYS_ON_POWER_DOMAINS | \
1578 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
1579#define BDW_DISPLAY_POWER_DOMAINS ( \
1580 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
1581 BIT(POWER_DOMAIN_INIT))
1582
1583#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
1584#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
1585
1586#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001587 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1588 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001589 BIT(POWER_DOMAIN_PORT_CRT) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001590 BIT(POWER_DOMAIN_AUX_B) | \
1591 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001592 BIT(POWER_DOMAIN_INIT))
1593
1594#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001595 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001596 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001597 BIT(POWER_DOMAIN_INIT))
1598
1599#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001600 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001601 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001602 BIT(POWER_DOMAIN_INIT))
1603
1604#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001605 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001606 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001607 BIT(POWER_DOMAIN_INIT))
1608
1609#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001610 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001611 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001612 BIT(POWER_DOMAIN_INIT))
1613
Daniel Vetter9c065a72014-09-30 10:56:38 +02001614#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001615 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1616 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001617 BIT(POWER_DOMAIN_AUX_B) | \
1618 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001619 BIT(POWER_DOMAIN_INIT))
1620
1621#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001622 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001623 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001624 BIT(POWER_DOMAIN_INIT))
1625
Daniel Vetter9c065a72014-09-30 10:56:38 +02001626static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1627 .sync_hw = i9xx_always_on_power_well_noop,
1628 .enable = i9xx_always_on_power_well_noop,
1629 .disable = i9xx_always_on_power_well_noop,
1630 .is_enabled = i9xx_always_on_power_well_enabled,
1631};
1632
1633static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1634 .sync_hw = chv_pipe_power_well_sync_hw,
1635 .enable = chv_pipe_power_well_enable,
1636 .disable = chv_pipe_power_well_disable,
1637 .is_enabled = chv_pipe_power_well_enabled,
1638};
1639
1640static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1641 .sync_hw = vlv_power_well_sync_hw,
1642 .enable = chv_dpio_cmn_power_well_enable,
1643 .disable = chv_dpio_cmn_power_well_disable,
1644 .is_enabled = vlv_power_well_enabled,
1645};
1646
1647static struct i915_power_well i9xx_always_on_power_well[] = {
1648 {
1649 .name = "always-on",
1650 .always_on = 1,
1651 .domains = POWER_DOMAIN_MASK,
1652 .ops = &i9xx_always_on_power_well_ops,
1653 },
1654};
1655
1656static const struct i915_power_well_ops hsw_power_well_ops = {
1657 .sync_hw = hsw_power_well_sync_hw,
1658 .enable = hsw_power_well_enable,
1659 .disable = hsw_power_well_disable,
1660 .is_enabled = hsw_power_well_enabled,
1661};
1662
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001663static const struct i915_power_well_ops skl_power_well_ops = {
1664 .sync_hw = skl_power_well_sync_hw,
1665 .enable = skl_power_well_enable,
1666 .disable = skl_power_well_disable,
1667 .is_enabled = skl_power_well_enabled,
1668};
1669
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001670static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1671 .sync_hw = gen9_dc_off_power_well_sync_hw,
1672 .enable = gen9_dc_off_power_well_enable,
1673 .disable = gen9_dc_off_power_well_disable,
1674 .is_enabled = gen9_dc_off_power_well_enabled,
1675};
1676
Daniel Vetter9c065a72014-09-30 10:56:38 +02001677static struct i915_power_well hsw_power_wells[] = {
1678 {
1679 .name = "always-on",
1680 .always_on = 1,
1681 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
1682 .ops = &i9xx_always_on_power_well_ops,
1683 },
1684 {
1685 .name = "display",
1686 .domains = HSW_DISPLAY_POWER_DOMAINS,
1687 .ops = &hsw_power_well_ops,
1688 },
1689};
1690
1691static struct i915_power_well bdw_power_wells[] = {
1692 {
1693 .name = "always-on",
1694 .always_on = 1,
1695 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
1696 .ops = &i9xx_always_on_power_well_ops,
1697 },
1698 {
1699 .name = "display",
1700 .domains = BDW_DISPLAY_POWER_DOMAINS,
1701 .ops = &hsw_power_well_ops,
1702 },
1703};
1704
1705static const struct i915_power_well_ops vlv_display_power_well_ops = {
1706 .sync_hw = vlv_power_well_sync_hw,
1707 .enable = vlv_display_power_well_enable,
1708 .disable = vlv_display_power_well_disable,
1709 .is_enabled = vlv_power_well_enabled,
1710};
1711
1712static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1713 .sync_hw = vlv_power_well_sync_hw,
1714 .enable = vlv_dpio_cmn_power_well_enable,
1715 .disable = vlv_dpio_cmn_power_well_disable,
1716 .is_enabled = vlv_power_well_enabled,
1717};
1718
1719static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1720 .sync_hw = vlv_power_well_sync_hw,
1721 .enable = vlv_power_well_enable,
1722 .disable = vlv_power_well_disable,
1723 .is_enabled = vlv_power_well_enabled,
1724};
1725
1726static struct i915_power_well vlv_power_wells[] = {
1727 {
1728 .name = "always-on",
1729 .always_on = 1,
1730 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1731 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001732 .data = PUNIT_POWER_WELL_ALWAYS_ON,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001733 },
1734 {
1735 .name = "display",
1736 .domains = VLV_DISPLAY_POWER_DOMAINS,
1737 .data = PUNIT_POWER_WELL_DISP2D,
1738 .ops = &vlv_display_power_well_ops,
1739 },
1740 {
1741 .name = "dpio-tx-b-01",
1742 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1743 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1744 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1745 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1746 .ops = &vlv_dpio_power_well_ops,
1747 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1748 },
1749 {
1750 .name = "dpio-tx-b-23",
1751 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1752 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1753 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1754 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1755 .ops = &vlv_dpio_power_well_ops,
1756 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1757 },
1758 {
1759 .name = "dpio-tx-c-01",
1760 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1761 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1762 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1763 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1764 .ops = &vlv_dpio_power_well_ops,
1765 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1766 },
1767 {
1768 .name = "dpio-tx-c-23",
1769 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1770 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1771 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1772 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1773 .ops = &vlv_dpio_power_well_ops,
1774 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1775 },
1776 {
1777 .name = "dpio-common",
1778 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1779 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1780 .ops = &vlv_dpio_cmn_power_well_ops,
1781 },
1782};
1783
1784static struct i915_power_well chv_power_wells[] = {
1785 {
1786 .name = "always-on",
1787 .always_on = 1,
1788 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1789 .ops = &i9xx_always_on_power_well_ops,
1790 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001791 {
1792 .name = "display",
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001793 /*
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001794 * Pipe A power well is the new disp2d well. Pipe B and C
1795 * power wells don't actually exist. Pipe A power well is
1796 * required for any pipe to work.
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001797 */
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001798 .domains = VLV_DISPLAY_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001799 .data = PIPE_A,
1800 .ops = &chv_pipe_power_well_ops,
1801 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001802 {
1803 .name = "dpio-common-bc",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001804 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001805 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1806 .ops = &chv_dpio_cmn_power_well_ops,
1807 },
1808 {
1809 .name = "dpio-common-d",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001810 .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001811 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
1812 .ops = &chv_dpio_cmn_power_well_ops,
1813 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001814};
1815
Suketu Shah5aefb232015-04-16 14:22:10 +05301816bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
1817 int power_well_id)
1818{
1819 struct i915_power_well *power_well;
1820 bool ret;
1821
1822 power_well = lookup_power_well(dev_priv, power_well_id);
1823 ret = power_well->ops->is_enabled(dev_priv, power_well);
1824
1825 return ret;
1826}
1827
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001828static struct i915_power_well skl_power_wells[] = {
1829 {
1830 .name = "always-on",
1831 .always_on = 1,
1832 .domains = SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1833 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001834 .data = SKL_DISP_PW_ALWAYS_ON,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001835 },
1836 {
1837 .name = "power well 1",
Imre Deak4a76f292015-11-04 19:24:15 +02001838 /* Handled by the DMC firmware */
1839 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001840 .ops = &skl_power_well_ops,
1841 .data = SKL_DISP_PW_1,
1842 },
1843 {
1844 .name = "MISC IO power well",
Imre Deak4a76f292015-11-04 19:24:15 +02001845 /* Handled by the DMC firmware */
1846 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001847 .ops = &skl_power_well_ops,
1848 .data = SKL_DISP_PW_MISC_IO,
1849 },
1850 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001851 .name = "DC off",
1852 .domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
1853 .ops = &gen9_dc_off_power_well_ops,
1854 .data = SKL_DISP_PW_DC_OFF,
1855 },
1856 {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001857 .name = "power well 2",
1858 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1859 .ops = &skl_power_well_ops,
1860 .data = SKL_DISP_PW_2,
1861 },
1862 {
1863 .name = "DDI A/E power well",
1864 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1865 .ops = &skl_power_well_ops,
1866 .data = SKL_DISP_PW_DDI_A_E,
1867 },
1868 {
1869 .name = "DDI B power well",
1870 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1871 .ops = &skl_power_well_ops,
1872 .data = SKL_DISP_PW_DDI_B,
1873 },
1874 {
1875 .name = "DDI C power well",
1876 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1877 .ops = &skl_power_well_ops,
1878 .data = SKL_DISP_PW_DDI_C,
1879 },
1880 {
1881 .name = "DDI D power well",
1882 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1883 .ops = &skl_power_well_ops,
1884 .data = SKL_DISP_PW_DDI_D,
1885 },
1886};
1887
Damien Lespiau2f693e22015-11-04 19:24:12 +02001888void skl_pw1_misc_io_init(struct drm_i915_private *dev_priv)
1889{
1890 struct i915_power_well *well;
1891
Michel Thierry16fbc292016-01-06 12:08:36 +00001892 if (!(IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)))
Damien Lespiau2f693e22015-11-04 19:24:12 +02001893 return;
1894
1895 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
1896 intel_power_well_enable(dev_priv, well);
1897
1898 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
1899 intel_power_well_enable(dev_priv, well);
1900}
1901
1902void skl_pw1_misc_io_fini(struct drm_i915_private *dev_priv)
1903{
1904 struct i915_power_well *well;
1905
Michel Thierry16fbc292016-01-06 12:08:36 +00001906 if (!(IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)))
Damien Lespiau2f693e22015-11-04 19:24:12 +02001907 return;
1908
1909 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
1910 intel_power_well_disable(dev_priv, well);
1911
1912 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
1913 intel_power_well_disable(dev_priv, well);
1914}
1915
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301916static struct i915_power_well bxt_power_wells[] = {
1917 {
1918 .name = "always-on",
1919 .always_on = 1,
1920 .domains = BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1921 .ops = &i9xx_always_on_power_well_ops,
1922 },
1923 {
1924 .name = "power well 1",
1925 .domains = BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS,
1926 .ops = &skl_power_well_ops,
1927 .data = SKL_DISP_PW_1,
1928 },
1929 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001930 .name = "DC off",
1931 .domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
1932 .ops = &gen9_dc_off_power_well_ops,
1933 .data = SKL_DISP_PW_DC_OFF,
1934 },
1935 {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301936 .name = "power well 2",
1937 .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1938 .ops = &skl_power_well_ops,
1939 .data = SKL_DISP_PW_2,
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001940 },
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301941};
1942
Imre Deak1b0e3a02015-11-05 23:04:11 +02001943static int
1944sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
1945 int disable_power_well)
1946{
1947 if (disable_power_well >= 0)
1948 return !!disable_power_well;
1949
Matt Roper18024192015-12-01 09:26:58 -08001950 if (IS_BROXTON(dev_priv)) {
1951 DRM_DEBUG_KMS("Disabling display power well support\n");
1952 return 0;
1953 }
1954
Imre Deak1b0e3a02015-11-05 23:04:11 +02001955 return 1;
1956}
1957
Imre Deaka37baf32016-02-29 22:49:03 +02001958static uint32_t get_allowed_dc_mask(const struct drm_i915_private *dev_priv,
1959 int enable_dc)
1960{
1961 uint32_t mask;
1962 int requested_dc;
1963 int max_dc;
1964
1965 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
1966 max_dc = 2;
1967 mask = 0;
1968 } else if (IS_BROXTON(dev_priv)) {
1969 max_dc = 1;
1970 /*
1971 * DC9 has a separate HW flow from the rest of the DC states,
1972 * not depending on the DMC firmware. It's needed by system
1973 * suspend/resume, so allow it unconditionally.
1974 */
1975 mask = DC_STATE_EN_DC9;
1976 } else {
1977 max_dc = 0;
1978 mask = 0;
1979 }
1980
Imre Deak66e2c4c2016-02-29 22:49:04 +02001981 if (!i915.disable_power_well)
1982 max_dc = 0;
1983
Imre Deaka37baf32016-02-29 22:49:03 +02001984 if (enable_dc >= 0 && enable_dc <= max_dc) {
1985 requested_dc = enable_dc;
1986 } else if (enable_dc == -1) {
1987 requested_dc = max_dc;
1988 } else if (enable_dc > max_dc && enable_dc <= 2) {
1989 DRM_DEBUG_KMS("Adjusting requested max DC state (%d->%d)\n",
1990 enable_dc, max_dc);
1991 requested_dc = max_dc;
1992 } else {
1993 DRM_ERROR("Unexpected value for enable_dc (%d)\n", enable_dc);
1994 requested_dc = max_dc;
1995 }
1996
1997 if (requested_dc > 1)
1998 mask |= DC_STATE_EN_UPTO_DC6;
1999 if (requested_dc > 0)
2000 mask |= DC_STATE_EN_UPTO_DC5;
2001
2002 DRM_DEBUG_KMS("Allowed DC state mask %02x\n", mask);
2003
2004 return mask;
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);
Imre Deaka37baf32016-02-29 22:49:03 +02002025 dev_priv->csr.allowed_dc_mask = get_allowed_dc_mask(dev_priv,
2026 i915.enable_dc);
Imre Deak1b0e3a02015-11-05 23:04:11 +02002027
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01002028 BUILD_BUG_ON(POWER_DOMAIN_NUM > 31);
2029
Daniel Vetter9c065a72014-09-30 10:56:38 +02002030 mutex_init(&power_domains->lock);
2031
2032 /*
2033 * The enabling order will be from lower to higher indexed wells,
2034 * the disabling order is reversed.
2035 */
2036 if (IS_HASWELL(dev_priv->dev)) {
2037 set_power_wells(power_domains, hsw_power_wells);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002038 } else if (IS_BROADWELL(dev_priv->dev)) {
2039 set_power_wells(power_domains, bdw_power_wells);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07002040 } else if (IS_SKYLAKE(dev_priv->dev) || IS_KABYLAKE(dev_priv->dev)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002041 set_power_wells(power_domains, skl_power_wells);
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302042 } else if (IS_BROXTON(dev_priv->dev)) {
2043 set_power_wells(power_domains, bxt_power_wells);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002044 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
2045 set_power_wells(power_domains, chv_power_wells);
2046 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
2047 set_power_wells(power_domains, vlv_power_wells);
2048 } else {
2049 set_power_wells(power_domains, i9xx_always_on_power_well);
2050 }
2051
2052 return 0;
2053}
2054
Daniel Vettere4e76842014-09-30 10:56:42 +02002055/**
2056 * intel_power_domains_fini - finalizes the power domain structures
2057 * @dev_priv: i915 device instance
2058 *
2059 * Finalizes the power domain structures for @dev_priv depending upon the
2060 * supported platform. This function also disables runtime pm and ensures that
2061 * the device stays powered up so that the driver can be reloaded.
2062 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002063void intel_power_domains_fini(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002064{
Imre Deak25b181b2015-12-17 13:44:56 +02002065 struct device *device = &dev_priv->dev->pdev->dev;
2066
Imre Deakaabee1b2015-12-15 20:10:29 +02002067 /*
2068 * The i915.ko module is still not prepared to be loaded when
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002069 * the power well is not enabled, so just enable it in case
Imre Deakaabee1b2015-12-15 20:10:29 +02002070 * we're going to unload/reload.
2071 * The following also reacquires the RPM reference the core passed
2072 * to the driver during loading, which is dropped in
2073 * intel_runtime_pm_enable(). We have to hand back the control of the
2074 * device to the core with this reference held.
2075 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002076 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002077
2078 /* Remove the refcount we took to keep power well support disabled. */
2079 if (!i915.disable_power_well)
2080 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak25b181b2015-12-17 13:44:56 +02002081
2082 /*
2083 * Remove the refcount we took in intel_runtime_pm_enable() in case
2084 * the platform doesn't support runtime PM.
2085 */
2086 if (!HAS_RUNTIME_PM(dev_priv))
2087 pm_runtime_put(device);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002088}
2089
Imre Deak30eade12015-11-04 19:24:13 +02002090static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002091{
2092 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2093 struct i915_power_well *power_well;
2094 int i;
2095
2096 mutex_lock(&power_domains->lock);
2097 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
2098 power_well->ops->sync_hw(dev_priv, power_well);
2099 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2100 power_well);
2101 }
2102 mutex_unlock(&power_domains->lock);
2103}
2104
Imre Deak73dfc222015-11-17 17:33:53 +02002105static void skl_display_core_init(struct drm_i915_private *dev_priv,
2106 bool resume)
2107{
2108 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2109 uint32_t val;
2110
Imre Deakd26fa1d2015-11-04 19:24:17 +02002111 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2112
Imre Deak73dfc222015-11-17 17:33:53 +02002113 /* enable PCH reset handshake */
2114 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2115 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2116
2117 /* enable PG1 and Misc I/O */
2118 mutex_lock(&power_domains->lock);
2119 skl_pw1_misc_io_init(dev_priv);
2120 mutex_unlock(&power_domains->lock);
2121
2122 if (!resume)
2123 return;
2124
2125 skl_init_cdclk(dev_priv);
2126
Imre Deak2abc5252016-03-04 21:57:41 +02002127 if (dev_priv->csr.dmc_payload)
2128 intel_csr_load_program(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002129}
2130
2131static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2132{
2133 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2134
Imre Deakd26fa1d2015-11-04 19:24:17 +02002135 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2136
Imre Deak73dfc222015-11-17 17:33:53 +02002137 skl_uninit_cdclk(dev_priv);
2138
2139 /* The spec doesn't call for removing the reset handshake flag */
2140 /* disable PG1 and Misc I/O */
2141 mutex_lock(&power_domains->lock);
2142 skl_pw1_misc_io_fini(dev_priv);
2143 mutex_unlock(&power_domains->lock);
2144}
2145
Ville Syrjälä70722462015-04-10 18:21:28 +03002146static void chv_phy_control_init(struct drm_i915_private *dev_priv)
2147{
2148 struct i915_power_well *cmn_bc =
2149 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2150 struct i915_power_well *cmn_d =
2151 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
2152
2153 /*
2154 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
2155 * workaround never ever read DISPLAY_PHY_CONTROL, and
2156 * instead maintain a shadow copy ourselves. Use the actual
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002157 * power well state and lane status to reconstruct the
2158 * expected initial value.
Ville Syrjälä70722462015-04-10 18:21:28 +03002159 */
2160 dev_priv->chv_phy_control =
Ville Syrjäläbc284542015-05-26 20:22:38 +03002161 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
2162 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002163 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
2164 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
2165 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
2166
2167 /*
2168 * If all lanes are disabled we leave the override disabled
2169 * with all power down bits cleared to match the state we
2170 * would use after disabling the port. Otherwise enable the
2171 * override and set the lane powerdown bits accding to the
2172 * current lane status.
2173 */
2174 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
2175 uint32_t status = I915_READ(DPLL(PIPE_A));
2176 unsigned int mask;
2177
2178 mask = status & DPLL_PORTB_READY_MASK;
2179 if (mask == 0xf)
2180 mask = 0x0;
2181 else
2182 dev_priv->chv_phy_control |=
2183 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
2184
2185 dev_priv->chv_phy_control |=
2186 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
2187
2188 mask = (status & DPLL_PORTC_READY_MASK) >> 4;
2189 if (mask == 0xf)
2190 mask = 0x0;
2191 else
2192 dev_priv->chv_phy_control |=
2193 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
2194
2195 dev_priv->chv_phy_control |=
2196 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
2197
Ville Syrjälä70722462015-04-10 18:21:28 +03002198 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002199
2200 dev_priv->chv_phy_assert[DPIO_PHY0] = false;
2201 } else {
2202 dev_priv->chv_phy_assert[DPIO_PHY0] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002203 }
2204
2205 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
2206 uint32_t status = I915_READ(DPIO_PHY_STATUS);
2207 unsigned int mask;
2208
2209 mask = status & DPLL_PORTD_READY_MASK;
2210
2211 if (mask == 0xf)
2212 mask = 0x0;
2213 else
2214 dev_priv->chv_phy_control |=
2215 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
2216
2217 dev_priv->chv_phy_control |=
2218 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
2219
Ville Syrjälä70722462015-04-10 18:21:28 +03002220 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002221
2222 dev_priv->chv_phy_assert[DPIO_PHY1] = false;
2223 } else {
2224 dev_priv->chv_phy_assert[DPIO_PHY1] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002225 }
2226
2227 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
2228
2229 DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
2230 dev_priv->chv_phy_control);
Ville Syrjälä70722462015-04-10 18:21:28 +03002231}
2232
Daniel Vetter9c065a72014-09-30 10:56:38 +02002233static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
2234{
2235 struct i915_power_well *cmn =
2236 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2237 struct i915_power_well *disp2d =
2238 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
2239
Daniel Vetter9c065a72014-09-30 10:56:38 +02002240 /* If the display might be already active skip this */
Ville Syrjälä5d93a6e2014-10-16 20:52:33 +03002241 if (cmn->ops->is_enabled(dev_priv, cmn) &&
2242 disp2d->ops->is_enabled(dev_priv, disp2d) &&
Daniel Vetter9c065a72014-09-30 10:56:38 +02002243 I915_READ(DPIO_CTL) & DPIO_CMNRST)
2244 return;
2245
2246 DRM_DEBUG_KMS("toggling display PHY side reset\n");
2247
2248 /* cmnlane needs DPLL registers */
2249 disp2d->ops->enable(dev_priv, disp2d);
2250
2251 /*
2252 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
2253 * Need to assert and de-assert PHY SB reset by gating the
2254 * common lane power, then un-gating it.
2255 * Simply ungating isn't enough to reset the PHY enough to get
2256 * ports and lanes running.
2257 */
2258 cmn->ops->disable(dev_priv, cmn);
2259}
2260
Daniel Vettere4e76842014-09-30 10:56:42 +02002261/**
2262 * intel_power_domains_init_hw - initialize hardware power domain state
2263 * @dev_priv: i915 device instance
2264 *
2265 * This function initializes the hardware power domain state and enables all
2266 * power domains using intel_display_set_init_power().
2267 */
Imre Deak73dfc222015-11-17 17:33:53 +02002268void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002269{
2270 struct drm_device *dev = dev_priv->dev;
2271 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2272
2273 power_domains->initializing = true;
2274
Imre Deak73dfc222015-11-17 17:33:53 +02002275 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
2276 skl_display_core_init(dev_priv, resume);
2277 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä770effb2015-07-08 23:45:51 +03002278 mutex_lock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002279 chv_phy_control_init(dev_priv);
Ville Syrjälä770effb2015-07-08 23:45:51 +03002280 mutex_unlock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002281 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002282 mutex_lock(&power_domains->lock);
2283 vlv_cmnlane_wa(dev_priv);
2284 mutex_unlock(&power_domains->lock);
2285 }
2286
2287 /* For now, we need the power well to be always enabled. */
2288 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002289 /* Disable power support if the user asked so. */
2290 if (!i915.disable_power_well)
2291 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
Imre Deak30eade12015-11-04 19:24:13 +02002292 intel_power_domains_sync_hw(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002293 power_domains->initializing = false;
2294}
2295
Daniel Vettere4e76842014-09-30 10:56:42 +02002296/**
Imre Deak73dfc222015-11-17 17:33:53 +02002297 * intel_power_domains_suspend - suspend power domain state
2298 * @dev_priv: i915 device instance
2299 *
2300 * This function prepares the hardware power domain state before entering
2301 * system suspend. It must be paired with intel_power_domains_init_hw().
2302 */
2303void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
2304{
Imre Deakd314cd42015-11-17 17:44:23 +02002305 /*
2306 * Even if power well support was disabled we still want to disable
2307 * power wells while we are system suspended.
2308 */
2309 if (!i915.disable_power_well)
2310 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak2622d792016-02-29 22:49:02 +02002311
2312 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
2313 skl_display_core_uninit(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002314}
2315
2316/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002317 * intel_runtime_pm_get - grab a runtime pm reference
2318 * @dev_priv: i915 device instance
2319 *
2320 * This function grabs a device-level runtime pm reference (mostly used for GEM
2321 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2322 *
2323 * Any runtime pm reference obtained by this function must have a symmetric
2324 * call to intel_runtime_pm_put() to release the reference again.
2325 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002326void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2327{
2328 struct drm_device *dev = dev_priv->dev;
2329 struct device *device = &dev->pdev->dev;
2330
Daniel Vetter9c065a72014-09-30 10:56:38 +02002331 pm_runtime_get_sync(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002332
2333 atomic_inc(&dev_priv->pm.wakeref_count);
Imre Deakc9b88462015-12-15 20:10:34 +02002334 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002335}
2336
Daniel Vettere4e76842014-09-30 10:56:42 +02002337/**
Imre Deak09731282016-02-17 14:17:42 +02002338 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
2339 * @dev_priv: i915 device instance
2340 *
2341 * This function grabs a device-level runtime pm reference if the device is
2342 * already in use and ensures that it is powered up.
2343 *
2344 * Any runtime pm reference obtained by this function must have a symmetric
2345 * call to intel_runtime_pm_put() to release the reference again.
2346 */
2347bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
2348{
2349 struct drm_device *dev = dev_priv->dev;
2350 struct device *device = &dev->pdev->dev;
Imre Deak09731282016-02-17 14:17:42 +02002351
Chris Wilson135dc792016-02-25 21:10:28 +00002352 if (IS_ENABLED(CONFIG_PM)) {
2353 int ret = pm_runtime_get_if_in_use(device);
Imre Deak09731282016-02-17 14:17:42 +02002354
Chris Wilson135dc792016-02-25 21:10:28 +00002355 /*
2356 * In cases runtime PM is disabled by the RPM core and we get
2357 * an -EINVAL return value we are not supposed to call this
2358 * function, since the power state is undefined. This applies
2359 * atm to the late/early system suspend/resume handlers.
2360 */
2361 WARN_ON_ONCE(ret < 0);
2362 if (ret <= 0)
2363 return false;
2364 }
Imre Deak09731282016-02-17 14:17:42 +02002365
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