blob: a1741182f92468fc23b03ce78a77a1c63a60c216 [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";
Jani Nikula4d1de972016-03-18 17:05:42 +020092 case POWER_DOMAIN_TRANSCODER_DSI_A:
93 return "TRANSCODER_DSI_A";
94 case POWER_DOMAIN_TRANSCODER_DSI_C:
95 return "TRANSCODER_DSI_C";
Daniel Stone9895ad02015-11-20 15:55:33 +000096 case POWER_DOMAIN_PORT_DDI_A_LANES:
97 return "PORT_DDI_A_LANES";
98 case POWER_DOMAIN_PORT_DDI_B_LANES:
99 return "PORT_DDI_B_LANES";
100 case POWER_DOMAIN_PORT_DDI_C_LANES:
101 return "PORT_DDI_C_LANES";
102 case POWER_DOMAIN_PORT_DDI_D_LANES:
103 return "PORT_DDI_D_LANES";
104 case POWER_DOMAIN_PORT_DDI_E_LANES:
105 return "PORT_DDI_E_LANES";
106 case POWER_DOMAIN_PORT_DSI:
107 return "PORT_DSI";
108 case POWER_DOMAIN_PORT_CRT:
109 return "PORT_CRT";
110 case POWER_DOMAIN_PORT_OTHER:
111 return "PORT_OTHER";
112 case POWER_DOMAIN_VGA:
113 return "VGA";
114 case POWER_DOMAIN_AUDIO:
115 return "AUDIO";
116 case POWER_DOMAIN_PLLS:
117 return "PLLS";
118 case POWER_DOMAIN_AUX_A:
119 return "AUX_A";
120 case POWER_DOMAIN_AUX_B:
121 return "AUX_B";
122 case POWER_DOMAIN_AUX_C:
123 return "AUX_C";
124 case POWER_DOMAIN_AUX_D:
125 return "AUX_D";
126 case POWER_DOMAIN_GMBUS:
127 return "GMBUS";
128 case POWER_DOMAIN_INIT:
129 return "INIT";
130 case POWER_DOMAIN_MODESET:
131 return "MODESET";
132 default:
133 MISSING_CASE(domain);
134 return "?";
135 }
136}
137
Damien Lespiaue8ca9322015-07-30 18:20:26 -0300138static void intel_power_well_enable(struct drm_i915_private *dev_priv,
139 struct i915_power_well *power_well)
140{
141 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
142 power_well->ops->enable(dev_priv, power_well);
143 power_well->hw_enabled = true;
144}
145
Damien Lespiaudcddab32015-07-30 18:20:27 -0300146static void intel_power_well_disable(struct drm_i915_private *dev_priv,
147 struct i915_power_well *power_well)
148{
149 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
150 power_well->hw_enabled = false;
151 power_well->ops->disable(dev_priv, power_well);
152}
153
Daniel Vettere4e76842014-09-30 10:56:42 +0200154/*
Daniel Vetter9c065a72014-09-30 10:56:38 +0200155 * We should only use the power well if we explicitly asked the hardware to
156 * enable it, so check if it's enabled and also check if we've requested it to
157 * be enabled.
158 */
159static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
160 struct i915_power_well *power_well)
161{
162 return I915_READ(HSW_PWR_WELL_DRIVER) ==
163 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
164}
165
Daniel Vettere4e76842014-09-30 10:56:42 +0200166/**
167 * __intel_display_power_is_enabled - unlocked check for a power domain
168 * @dev_priv: i915 device instance
169 * @domain: power domain to check
170 *
171 * This is the unlocked version of intel_display_power_is_enabled() and should
172 * only be used from error capture and recovery code where deadlocks are
173 * possible.
174 *
175 * Returns:
176 * True when the power domain is enabled, false otherwise.
177 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200178bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
179 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200180{
181 struct i915_power_domains *power_domains;
182 struct i915_power_well *power_well;
183 bool is_enabled;
184 int i;
185
186 if (dev_priv->pm.suspended)
187 return false;
188
189 power_domains = &dev_priv->power_domains;
190
191 is_enabled = true;
192
193 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
194 if (power_well->always_on)
195 continue;
196
197 if (!power_well->hw_enabled) {
198 is_enabled = false;
199 break;
200 }
201 }
202
203 return is_enabled;
204}
205
Daniel Vettere4e76842014-09-30 10:56:42 +0200206/**
Damien Lespiauf61ccae2014-11-25 13:45:41 +0000207 * intel_display_power_is_enabled - check for a power domain
Daniel Vettere4e76842014-09-30 10:56:42 +0200208 * @dev_priv: i915 device instance
209 * @domain: power domain to check
210 *
211 * This function can be used to check the hw power domain state. It is mostly
212 * used in hardware state readout functions. Everywhere else code should rely
213 * upon explicit power domain reference counting to ensure that the hardware
214 * block is powered up before accessing it.
215 *
216 * Callers must hold the relevant modesetting locks to ensure that concurrent
217 * threads can't disable the power well while the caller tries to read a few
218 * registers.
219 *
220 * Returns:
221 * True when the power domain is enabled, false otherwise.
222 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200223bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
224 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200225{
226 struct i915_power_domains *power_domains;
227 bool ret;
228
229 power_domains = &dev_priv->power_domains;
230
231 mutex_lock(&power_domains->lock);
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200232 ret = __intel_display_power_is_enabled(dev_priv, domain);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200233 mutex_unlock(&power_domains->lock);
234
235 return ret;
236}
237
Daniel Vettere4e76842014-09-30 10:56:42 +0200238/**
239 * intel_display_set_init_power - set the initial power domain state
240 * @dev_priv: i915 device instance
241 * @enable: whether to enable or disable the initial power domain state
242 *
243 * For simplicity our driver load/unload and system suspend/resume code assumes
244 * that all power domains are always enabled. This functions controls the state
245 * of this little hack. While the initial power domain state is enabled runtime
246 * pm is effectively disabled.
247 */
Daniel Vetterd9bc89d92014-09-30 10:56:40 +0200248void intel_display_set_init_power(struct drm_i915_private *dev_priv,
249 bool enable)
250{
251 if (dev_priv->power_domains.init_power_on == enable)
252 return;
253
254 if (enable)
255 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
256 else
257 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
258
259 dev_priv->power_domains.init_power_on = enable;
260}
261
Daniel Vetter9c065a72014-09-30 10:56:38 +0200262/*
263 * Starting with Haswell, we have a "Power Down Well" that can be turned off
264 * when not needed anymore. We have 4 registers that can request the power well
265 * to be enabled, and it will only be disabled if none of the registers is
266 * requesting it to be enabled.
267 */
268static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
269{
270 struct drm_device *dev = dev_priv->dev;
271
272 /*
273 * After we re-enable the power well, if we touch VGA register 0x3d5
274 * we'll get unclaimed register interrupts. This stops after we write
275 * anything to the VGA MSR register. The vgacon module uses this
276 * register all the time, so if we unbind our driver and, as a
277 * consequence, bind vgacon, we'll get stuck in an infinite loop at
278 * console_unlock(). So make here we touch the VGA MSR register, making
279 * sure vgacon can keep working normally without triggering interrupts
280 * and error messages.
281 */
282 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
283 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
284 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
285
Damien Lespiau25400392015-03-06 18:50:52 +0000286 if (IS_BROADWELL(dev))
Damien Lespiau4c6c03b2015-03-06 18:50:48 +0000287 gen8_irq_power_well_post_enable(dev_priv,
288 1 << PIPE_C | 1 << PIPE_B);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200289}
290
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200291static void hsw_power_well_pre_disable(struct drm_i915_private *dev_priv)
292{
293 if (IS_BROADWELL(dev_priv))
294 gen8_irq_power_well_pre_disable(dev_priv,
295 1 << PIPE_C | 1 << PIPE_B);
296}
297
Damien Lespiaud14c0342015-03-06 18:50:51 +0000298static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
299 struct i915_power_well *power_well)
300{
301 struct drm_device *dev = dev_priv->dev;
302
303 /*
304 * After we re-enable the power well, if we touch VGA register 0x3d5
305 * we'll get unclaimed register interrupts. This stops after we write
306 * anything to the VGA MSR register. The vgacon module uses this
307 * register all the time, so if we unbind our driver and, as a
308 * consequence, bind vgacon, we'll get stuck in an infinite loop at
309 * console_unlock(). So make here we touch the VGA MSR register, making
310 * sure vgacon can keep working normally without triggering interrupts
311 * and error messages.
312 */
313 if (power_well->data == SKL_DISP_PW_2) {
314 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
315 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
316 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
317
318 gen8_irq_power_well_post_enable(dev_priv,
319 1 << PIPE_C | 1 << PIPE_B);
320 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000321}
322
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200323static void skl_power_well_pre_disable(struct drm_i915_private *dev_priv,
324 struct i915_power_well *power_well)
325{
326 if (power_well->data == SKL_DISP_PW_2)
327 gen8_irq_power_well_pre_disable(dev_priv,
328 1 << PIPE_C | 1 << PIPE_B);
329}
330
Daniel Vetter9c065a72014-09-30 10:56:38 +0200331static void hsw_set_power_well(struct drm_i915_private *dev_priv,
332 struct i915_power_well *power_well, bool enable)
333{
334 bool is_enabled, enable_requested;
335 uint32_t tmp;
336
337 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
338 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
339 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
340
341 if (enable) {
342 if (!enable_requested)
343 I915_WRITE(HSW_PWR_WELL_DRIVER,
344 HSW_PWR_WELL_ENABLE_REQUEST);
345
346 if (!is_enabled) {
347 DRM_DEBUG_KMS("Enabling power well\n");
348 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
349 HSW_PWR_WELL_STATE_ENABLED), 20))
350 DRM_ERROR("Timeout enabling power well\n");
Paulo Zanoni6d729bf2014-10-07 16:11:11 -0300351 hsw_power_well_post_enable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200352 }
353
Daniel Vetter9c065a72014-09-30 10:56:38 +0200354 } else {
355 if (enable_requested) {
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200356 hsw_power_well_pre_disable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200357 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
358 POSTING_READ(HSW_PWR_WELL_DRIVER);
359 DRM_DEBUG_KMS("Requesting to disable the power well\n");
360 }
361 }
362}
363
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000364#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
365 BIT(POWER_DOMAIN_TRANSCODER_A) | \
366 BIT(POWER_DOMAIN_PIPE_B) | \
367 BIT(POWER_DOMAIN_TRANSCODER_B) | \
368 BIT(POWER_DOMAIN_PIPE_C) | \
369 BIT(POWER_DOMAIN_TRANSCODER_C) | \
370 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
371 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100372 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
373 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
374 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
375 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000376 BIT(POWER_DOMAIN_AUX_B) | \
377 BIT(POWER_DOMAIN_AUX_C) | \
378 BIT(POWER_DOMAIN_AUX_D) | \
379 BIT(POWER_DOMAIN_AUDIO) | \
380 BIT(POWER_DOMAIN_VGA) | \
381 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000382#define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100383 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
384 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000385 BIT(POWER_DOMAIN_INIT))
386#define SKL_DISPLAY_DDI_B_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100387 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000388 BIT(POWER_DOMAIN_INIT))
389#define SKL_DISPLAY_DDI_C_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100390 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000391 BIT(POWER_DOMAIN_INIT))
392#define SKL_DISPLAY_DDI_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100393 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000394 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100395#define SKL_DISPLAY_DC_OFF_POWER_DOMAINS ( \
396 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
397 BIT(POWER_DOMAIN_MODESET) | \
398 BIT(POWER_DOMAIN_AUX_A) | \
399 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000400
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530401#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
402 BIT(POWER_DOMAIN_TRANSCODER_A) | \
403 BIT(POWER_DOMAIN_PIPE_B) | \
404 BIT(POWER_DOMAIN_TRANSCODER_B) | \
405 BIT(POWER_DOMAIN_PIPE_C) | \
406 BIT(POWER_DOMAIN_TRANSCODER_C) | \
407 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
408 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100409 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
410 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530411 BIT(POWER_DOMAIN_AUX_B) | \
412 BIT(POWER_DOMAIN_AUX_C) | \
413 BIT(POWER_DOMAIN_AUDIO) | \
414 BIT(POWER_DOMAIN_VGA) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100415 BIT(POWER_DOMAIN_GMBUS) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530416 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100417#define BXT_DISPLAY_DC_OFF_POWER_DOMAINS ( \
418 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
419 BIT(POWER_DOMAIN_MODESET) | \
420 BIT(POWER_DOMAIN_AUX_A) | \
421 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530422
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530423static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
424{
Imre Deakbfcdabe2016-04-01 16:02:37 +0300425 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
426 "DC9 already programmed to be enabled.\n");
427 WARN_ONCE(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
428 "DC5 still not disabled to enable DC9.\n");
429 WARN_ONCE(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
430 WARN_ONCE(intel_irqs_enabled(dev_priv),
431 "Interrupts not disabled yet.\n");
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530432
433 /*
434 * TODO: check for the following to verify the conditions to enter DC9
435 * state are satisfied:
436 * 1] Check relevant display engine registers to verify if mode set
437 * disable sequence was followed.
438 * 2] Check if display uninitialize sequence is initialized.
439 */
440}
441
442static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
443{
Imre Deakbfcdabe2016-04-01 16:02:37 +0300444 WARN_ONCE(intel_irqs_enabled(dev_priv),
445 "Interrupts not disabled yet.\n");
446 WARN_ONCE(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
447 "DC5 still not disabled.\n");
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530448
449 /*
450 * TODO: check for the following to verify DC9 state was indeed
451 * entered before programming to disable it:
452 * 1] Check relevant display engine registers to verify if mode
453 * set disable sequence was followed.
454 * 2] Check if display uninitialize sequence is initialized.
455 */
456}
457
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200458static void gen9_write_dc_state(struct drm_i915_private *dev_priv,
459 u32 state)
460{
461 int rewrites = 0;
462 int rereads = 0;
463 u32 v;
464
465 I915_WRITE(DC_STATE_EN, state);
466
467 /* It has been observed that disabling the dc6 state sometimes
468 * doesn't stick and dmc keeps returning old value. Make sure
469 * the write really sticks enough times and also force rewrite until
470 * we are confident that state is exactly what we want.
471 */
472 do {
473 v = I915_READ(DC_STATE_EN);
474
475 if (v != state) {
476 I915_WRITE(DC_STATE_EN, state);
477 rewrites++;
478 rereads = 0;
479 } else if (rereads++ > 5) {
480 break;
481 }
482
483 } while (rewrites < 100);
484
485 if (v != state)
486 DRM_ERROR("Writing dc state to 0x%x failed, now 0x%x\n",
487 state, v);
488
489 /* Most of the times we need one retry, avoid spam */
490 if (rewrites > 1)
491 DRM_DEBUG_KMS("Rewrote dc state to 0x%x %d times\n",
492 state, rewrites);
493}
494
Imre Deakda2f41d2016-04-20 20:27:56 +0300495static u32 gen9_dc_mask(struct drm_i915_private *dev_priv)
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530496{
Imre Deakda2f41d2016-04-20 20:27:56 +0300497 u32 mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530498
Imre Deak13ae3a02015-11-04 19:24:16 +0200499 mask = DC_STATE_EN_UPTO_DC5;
500 if (IS_BROXTON(dev_priv))
501 mask |= DC_STATE_EN_DC9;
502 else
503 mask |= DC_STATE_EN_UPTO_DC6;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530504
Imre Deakda2f41d2016-04-20 20:27:56 +0300505 return mask;
506}
507
508void gen9_sanitize_dc_state(struct drm_i915_private *dev_priv)
509{
510 u32 val;
511
512 val = I915_READ(DC_STATE_EN) & gen9_dc_mask(dev_priv);
513
514 DRM_DEBUG_KMS("Resetting DC state tracking from %02x to %02x\n",
515 dev_priv->csr.dc_state, val);
516 dev_priv->csr.dc_state = val;
517}
518
519static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
520{
521 uint32_t val;
522 uint32_t mask;
523
Imre Deaka37baf32016-02-29 22:49:03 +0200524 if (WARN_ON_ONCE(state & ~dev_priv->csr.allowed_dc_mask))
525 state &= dev_priv->csr.allowed_dc_mask;
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100526
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530527 val = I915_READ(DC_STATE_EN);
Imre Deakda2f41d2016-04-20 20:27:56 +0300528 mask = gen9_dc_mask(dev_priv);
Imre Deak13ae3a02015-11-04 19:24:16 +0200529 DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
530 val & mask, state);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200531
532 /* Check if DMC is ignoring our DC state requests */
533 if ((val & mask) != dev_priv->csr.dc_state)
534 DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
535 dev_priv->csr.dc_state, val & mask);
536
Imre Deak13ae3a02015-11-04 19:24:16 +0200537 val &= ~mask;
538 val |= state;
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200539
540 gen9_write_dc_state(dev_priv, val);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200541
542 dev_priv->csr.dc_state = val & mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530543}
544
Imre Deak13ae3a02015-11-04 19:24:16 +0200545void bxt_enable_dc9(struct drm_i915_private *dev_priv)
546{
547 assert_can_enable_dc9(dev_priv);
548
549 DRM_DEBUG_KMS("Enabling DC9\n");
550
551 gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
552}
553
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530554void bxt_disable_dc9(struct drm_i915_private *dev_priv)
555{
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530556 assert_can_disable_dc9(dev_priv);
557
558 DRM_DEBUG_KMS("Disabling DC9\n");
559
Imre Deak13ae3a02015-11-04 19:24:16 +0200560 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530561}
562
Daniel Vetteraf5fead2015-10-28 23:58:57 +0200563static void assert_csr_loaded(struct drm_i915_private *dev_priv)
564{
565 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
566 "CSR program storage start is NULL\n");
567 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
568 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
569}
570
Suketu Shah5aefb232015-04-16 14:22:10 +0530571static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shahdc174302015-04-17 19:46:16 +0530572{
Suketu Shah5aefb232015-04-16 14:22:10 +0530573 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
574 SKL_DISP_PW_2);
575
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700576 WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530577
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700578 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
579 "DC5 already programmed to be enabled.\n");
Imre Deakc9b88462015-12-15 20:10:34 +0200580 assert_rpm_wakelock_held(dev_priv);
Suketu Shah5aefb232015-04-16 14:22:10 +0530581
582 assert_csr_loaded(dev_priv);
583}
584
Imre Deakf62c79b2016-04-20 20:27:57 +0300585void gen9_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shah5aefb232015-04-16 14:22:10 +0530586{
Suketu Shah5aefb232015-04-16 14:22:10 +0530587 assert_can_enable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530588
589 DRM_DEBUG_KMS("Enabling DC5\n");
590
Imre Deak13ae3a02015-11-04 19:24:16 +0200591 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
Suketu Shahdc174302015-04-17 19:46:16 +0530592}
593
Suketu Shah93c7cb62015-04-16 14:22:13 +0530594static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530595{
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700596 WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
597 "Backlight is not disabled.\n");
598 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
599 "DC6 already programmed to be enabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530600
601 assert_csr_loaded(dev_priv);
602}
603
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530604void skl_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shah93c7cb62015-04-16 14:22:13 +0530605{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530606 assert_can_enable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530607
608 DRM_DEBUG_KMS("Enabling DC6\n");
609
Imre Deak13ae3a02015-11-04 19:24:16 +0200610 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
611
Suketu Shahf75a1982015-04-16 14:22:11 +0530612}
613
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530614void skl_disable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530615{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530616 DRM_DEBUG_KMS("Disabling DC6\n");
617
Imre Deak13ae3a02015-11-04 19:24:16 +0200618 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Suketu Shahf75a1982015-04-16 14:22:11 +0530619}
620
Imre Deakc6782b72016-04-05 13:26:05 +0300621static void
622gen9_sanitize_power_well_requests(struct drm_i915_private *dev_priv,
623 struct i915_power_well *power_well)
624{
625 enum skl_disp_power_wells power_well_id = power_well->data;
626 u32 val;
627 u32 mask;
628
629 mask = SKL_POWER_WELL_REQ(power_well_id);
630
631 val = I915_READ(HSW_PWR_WELL_KVMR);
632 if (WARN_ONCE(val & mask, "Clearing unexpected KVMR request for %s\n",
633 power_well->name))
634 I915_WRITE(HSW_PWR_WELL_KVMR, val & ~mask);
635
636 val = I915_READ(HSW_PWR_WELL_BIOS);
637 val |= I915_READ(HSW_PWR_WELL_DEBUG);
638
639 if (!(val & mask))
640 return;
641
642 /*
643 * DMC is known to force on the request bits for power well 1 on SKL
644 * and BXT and the misc IO power well on SKL but we don't expect any
645 * other request bits to be set, so WARN for those.
646 */
647 if (power_well_id == SKL_DISP_PW_1 ||
Imre Deak80dbe992016-04-19 13:00:36 +0300648 ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
649 power_well_id == SKL_DISP_PW_MISC_IO))
Imre Deakc6782b72016-04-05 13:26:05 +0300650 DRM_DEBUG_DRIVER("Clearing auxiliary requests for %s forced on "
651 "by DMC\n", power_well->name);
652 else
653 WARN_ONCE(1, "Clearing unexpected auxiliary requests for %s\n",
654 power_well->name);
655
656 I915_WRITE(HSW_PWR_WELL_BIOS, val & ~mask);
657 I915_WRITE(HSW_PWR_WELL_DEBUG, val & ~mask);
658}
659
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000660static void skl_set_power_well(struct drm_i915_private *dev_priv,
661 struct i915_power_well *power_well, bool enable)
662{
663 uint32_t tmp, fuse_status;
664 uint32_t req_mask, state_mask;
Damien Lespiau2a518352015-03-06 18:50:49 +0000665 bool is_enabled, enable_requested, check_fuse_status = false;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000666
667 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
668 fuse_status = I915_READ(SKL_FUSE_STATUS);
669
670 switch (power_well->data) {
671 case SKL_DISP_PW_1:
672 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
673 SKL_FUSE_PG0_DIST_STATUS), 1)) {
674 DRM_ERROR("PG0 not enabled\n");
675 return;
676 }
677 break;
678 case SKL_DISP_PW_2:
679 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
680 DRM_ERROR("PG1 in disabled state\n");
681 return;
682 }
683 break;
684 case SKL_DISP_PW_DDI_A_E:
685 case SKL_DISP_PW_DDI_B:
686 case SKL_DISP_PW_DDI_C:
687 case SKL_DISP_PW_DDI_D:
688 case SKL_DISP_PW_MISC_IO:
689 break;
690 default:
691 WARN(1, "Unknown power well %lu\n", power_well->data);
692 return;
693 }
694
695 req_mask = SKL_POWER_WELL_REQ(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000696 enable_requested = tmp & req_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000697 state_mask = SKL_POWER_WELL_STATE(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000698 is_enabled = tmp & state_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000699
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200700 if (!enable && enable_requested)
701 skl_power_well_pre_disable(dev_priv, power_well);
702
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000703 if (enable) {
Damien Lespiau2a518352015-03-06 18:50:49 +0000704 if (!enable_requested) {
Suketu Shahdc174302015-04-17 19:46:16 +0530705 WARN((tmp & state_mask) &&
706 !I915_READ(HSW_PWR_WELL_BIOS),
707 "Invalid for power well status to be enabled, unless done by the BIOS, \
708 when request is to disable!\n");
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000709 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000710 }
711
Damien Lespiau2a518352015-03-06 18:50:49 +0000712 if (!is_enabled) {
Damien Lespiau510e6fd2015-03-06 18:50:50 +0000713 DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000714 check_fuse_status = true;
715 }
716 } else {
Damien Lespiau2a518352015-03-06 18:50:49 +0000717 if (enable_requested) {
Imre Deak4a76f292015-11-04 19:24:15 +0200718 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
719 POSTING_READ(HSW_PWR_WELL_DRIVER);
720 DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000721 }
Imre Deakc6782b72016-04-05 13:26:05 +0300722
Imre Deak5f304c82016-04-15 22:32:58 +0300723 if (IS_GEN9(dev_priv))
Imre Deakc6782b72016-04-05 13:26:05 +0300724 gen9_sanitize_power_well_requests(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000725 }
726
Imre Deak1d963af2016-04-01 16:02:36 +0300727 if (wait_for(!!(I915_READ(HSW_PWR_WELL_DRIVER) & state_mask) == enable,
728 1))
729 DRM_ERROR("%s %s timeout\n",
730 power_well->name, enable ? "enable" : "disable");
731
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000732 if (check_fuse_status) {
733 if (power_well->data == SKL_DISP_PW_1) {
734 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
735 SKL_FUSE_PG1_DIST_STATUS), 1))
736 DRM_ERROR("PG1 distributing status timeout\n");
737 } else if (power_well->data == SKL_DISP_PW_2) {
738 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
739 SKL_FUSE_PG2_DIST_STATUS), 1))
740 DRM_ERROR("PG2 distributing status timeout\n");
741 }
742 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000743
744 if (enable && !is_enabled)
745 skl_power_well_post_enable(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000746}
747
Daniel Vetter9c065a72014-09-30 10:56:38 +0200748static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
749 struct i915_power_well *power_well)
750{
751 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
752
753 /*
754 * We're taking over the BIOS, so clear any requests made by it since
755 * the driver is in charge now.
756 */
757 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
758 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
759}
760
761static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
762 struct i915_power_well *power_well)
763{
764 hsw_set_power_well(dev_priv, power_well, true);
765}
766
767static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
768 struct i915_power_well *power_well)
769{
770 hsw_set_power_well(dev_priv, power_well, false);
771}
772
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000773static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
774 struct i915_power_well *power_well)
775{
776 uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
777 SKL_POWER_WELL_STATE(power_well->data);
778
779 return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
780}
781
782static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
783 struct i915_power_well *power_well)
784{
785 skl_set_power_well(dev_priv, power_well, power_well->count > 0);
786
787 /* Clear any request made by BIOS as driver is taking over */
788 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
789}
790
791static void skl_power_well_enable(struct drm_i915_private *dev_priv,
792 struct i915_power_well *power_well)
793{
794 skl_set_power_well(dev_priv, power_well, true);
795}
796
797static void skl_power_well_disable(struct drm_i915_private *dev_priv,
798 struct i915_power_well *power_well)
799{
800 skl_set_power_well(dev_priv, power_well, false);
801}
802
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100803static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
804 struct i915_power_well *power_well)
805{
806 return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
807}
808
809static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
810 struct i915_power_well *power_well)
811{
Imre Deak5b773eb2016-02-29 22:49:05 +0200812 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Imre Deakadc7f042016-04-04 17:27:10 +0300813
Ville Syrjälä342be922016-05-13 23:41:39 +0300814 WARN_ON(dev_priv->cdclk_freq !=
815 dev_priv->display.get_display_clock_speed(dev_priv->dev));
816
817 if (IS_BROXTON(dev_priv))
Imre Deakadc7f042016-04-04 17:27:10 +0300818 broxton_ddi_phy_verify_state(dev_priv);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100819}
820
821static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
822 struct i915_power_well *power_well)
823{
Imre Deakf74ed082016-04-18 14:48:21 +0300824 if (!dev_priv->csr.dmc_payload)
825 return;
826
Imre Deaka37baf32016-02-29 22:49:03 +0200827 if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC6)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100828 skl_enable_dc6(dev_priv);
Imre Deaka37baf32016-02-29 22:49:03 +0200829 else if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100830 gen9_enable_dc5(dev_priv);
831}
832
833static void gen9_dc_off_power_well_sync_hw(struct drm_i915_private *dev_priv,
834 struct i915_power_well *power_well)
835{
Imre Deaka37baf32016-02-29 22:49:03 +0200836 if (power_well->count > 0)
837 gen9_dc_off_power_well_enable(dev_priv, power_well);
838 else
839 gen9_dc_off_power_well_disable(dev_priv, power_well);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100840}
841
Daniel Vetter9c065a72014-09-30 10:56:38 +0200842static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
843 struct i915_power_well *power_well)
844{
845}
846
847static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
848 struct i915_power_well *power_well)
849{
850 return true;
851}
852
853static void vlv_set_power_well(struct drm_i915_private *dev_priv,
854 struct i915_power_well *power_well, bool enable)
855{
856 enum punit_power_well power_well_id = power_well->data;
857 u32 mask;
858 u32 state;
859 u32 ctrl;
860
861 mask = PUNIT_PWRGT_MASK(power_well_id);
862 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
863 PUNIT_PWRGT_PWR_GATE(power_well_id);
864
865 mutex_lock(&dev_priv->rps.hw_lock);
866
867#define COND \
868 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
869
870 if (COND)
871 goto out;
872
873 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
874 ctrl &= ~mask;
875 ctrl |= state;
876 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
877
878 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +0900879 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +0200880 state,
881 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
882
883#undef COND
884
885out:
886 mutex_unlock(&dev_priv->rps.hw_lock);
887}
888
889static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
890 struct i915_power_well *power_well)
891{
892 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
893}
894
895static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
896 struct i915_power_well *power_well)
897{
898 vlv_set_power_well(dev_priv, power_well, true);
899}
900
901static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
902 struct i915_power_well *power_well)
903{
904 vlv_set_power_well(dev_priv, power_well, false);
905}
906
907static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
908 struct i915_power_well *power_well)
909{
910 int power_well_id = power_well->data;
911 bool enabled = false;
912 u32 mask;
913 u32 state;
914 u32 ctrl;
915
916 mask = PUNIT_PWRGT_MASK(power_well_id);
917 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
918
919 mutex_lock(&dev_priv->rps.hw_lock);
920
921 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
922 /*
923 * We only ever set the power-on and power-gate states, anything
924 * else is unexpected.
925 */
926 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
927 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
928 if (state == ctrl)
929 enabled = true;
930
931 /*
932 * A transient state at this point would mean some unexpected party
933 * is poking at the power controls too.
934 */
935 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
936 WARN_ON(ctrl != state);
937
938 mutex_unlock(&dev_priv->rps.hw_lock);
939
940 return enabled;
941}
942
Ville Syrjälä766078d2016-04-11 16:56:30 +0300943static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
944{
945 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
946
947 /*
948 * Disable trickle feed and enable pnd deadline calculation
949 */
950 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
951 I915_WRITE(CBR1_VLV, 0);
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300952
953 WARN_ON(dev_priv->rawclk_freq == 0);
954
955 I915_WRITE(RAWCLK_FREQ_VLV,
956 DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 1000));
Ville Syrjälä766078d2016-04-11 16:56:30 +0300957}
958
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300959static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200960{
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300961 enum pipe pipe;
962
963 /*
964 * Enable the CRI clock source so we can get at the
965 * display and the reference clock for VGA
966 * hotplug / manual detection. Supposedly DSI also
967 * needs the ref clock up and running.
968 *
969 * CHV DPLL B/C have some issues if VGA mode is enabled.
970 */
971 for_each_pipe(dev_priv->dev, pipe) {
972 u32 val = I915_READ(DPLL(pipe));
973
974 val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
975 if (pipe != PIPE_A)
976 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
977
978 I915_WRITE(DPLL(pipe), val);
979 }
Daniel Vetter9c065a72014-09-30 10:56:38 +0200980
Ville Syrjälä766078d2016-04-11 16:56:30 +0300981 vlv_init_display_clock_gating(dev_priv);
982
Daniel Vetter9c065a72014-09-30 10:56:38 +0200983 spin_lock_irq(&dev_priv->irq_lock);
984 valleyview_enable_display_irqs(dev_priv);
985 spin_unlock_irq(&dev_priv->irq_lock);
986
987 /*
988 * During driver initialization/resume we can avoid restoring the
989 * part of the HW/SW state that will be inited anyway explicitly.
990 */
991 if (dev_priv->power_domains.initializing)
992 return;
993
Daniel Vetterb9632912014-09-30 10:56:44 +0200994 intel_hpd_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200995
996 i915_redisable_vga_power_on(dev_priv->dev);
997}
998
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300999static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
1000{
1001 spin_lock_irq(&dev_priv->irq_lock);
1002 valleyview_disable_display_irqs(dev_priv);
1003 spin_unlock_irq(&dev_priv->irq_lock);
1004
Ville Syrjälä2230fde2016-02-19 18:41:52 +02001005 /* make sure we're done processing display irqs */
1006 synchronize_irq(dev_priv->dev->irq);
1007
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001008 vlv_power_sequencer_reset(dev_priv);
1009}
1010
1011static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
1012 struct i915_power_well *power_well)
1013{
1014 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1015
1016 vlv_set_power_well(dev_priv, power_well, true);
1017
1018 vlv_display_power_well_init(dev_priv);
1019}
1020
Daniel Vetter9c065a72014-09-30 10:56:38 +02001021static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
1022 struct i915_power_well *power_well)
1023{
1024 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1025
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001026 vlv_display_power_well_deinit(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001027
1028 vlv_set_power_well(dev_priv, power_well, false);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001029}
1030
1031static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1032 struct i915_power_well *power_well)
1033{
1034 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1035
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001036 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001037 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1038
1039 vlv_set_power_well(dev_priv, power_well, true);
1040
1041 /*
1042 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1043 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1044 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1045 * b. The other bits such as sfr settings / modesel may all
1046 * be set to 0.
1047 *
1048 * This should only be done on init and resume from S3 with
1049 * both PLLs disabled, or we risk losing DPIO and PLL
1050 * synchronization.
1051 */
1052 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1053}
1054
1055static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1056 struct i915_power_well *power_well)
1057{
1058 enum pipe pipe;
1059
1060 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1061
1062 for_each_pipe(dev_priv, pipe)
1063 assert_pll_disabled(dev_priv, pipe);
1064
1065 /* Assert common reset */
1066 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1067
1068 vlv_set_power_well(dev_priv, power_well, false);
1069}
1070
Ville Syrjälä30142272015-07-08 23:46:01 +03001071#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
1072
1073static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1074 int power_well_id)
1075{
1076 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Ville Syrjälä30142272015-07-08 23:46:01 +03001077 int i;
1078
Imre Deakfc17f222015-11-04 19:24:11 +02001079 for (i = 0; i < power_domains->power_well_count; i++) {
1080 struct i915_power_well *power_well;
1081
1082 power_well = &power_domains->power_wells[i];
Ville Syrjälä30142272015-07-08 23:46:01 +03001083 if (power_well->data == power_well_id)
1084 return power_well;
1085 }
1086
1087 return NULL;
1088}
1089
1090#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1091
1092static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1093{
1094 struct i915_power_well *cmn_bc =
1095 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1096 struct i915_power_well *cmn_d =
1097 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1098 u32 phy_control = dev_priv->chv_phy_control;
1099 u32 phy_status = 0;
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001100 u32 phy_status_mask = 0xffffffff;
Ville Syrjälä30142272015-07-08 23:46:01 +03001101 u32 tmp;
1102
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001103 /*
1104 * The BIOS can leave the PHY is some weird state
1105 * where it doesn't fully power down some parts.
1106 * Disable the asserts until the PHY has been fully
1107 * reset (ie. the power well has been disabled at
1108 * least once).
1109 */
1110 if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1111 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1112 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1113 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1114 PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1115 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1116 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1117
1118 if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1119 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1120 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1121 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1122
Ville Syrjälä30142272015-07-08 23:46:01 +03001123 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1124 phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1125
1126 /* this assumes override is only used to enable lanes */
1127 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1128 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1129
1130 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1131 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1132
1133 /* CL1 is on whenever anything is on in either channel */
1134 if (BITS_SET(phy_control,
1135 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1136 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1137 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1138
1139 /*
1140 * The DPLLB check accounts for the pipe B + port A usage
1141 * with CL2 powered up but all the lanes in the second channel
1142 * powered down.
1143 */
1144 if (BITS_SET(phy_control,
1145 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1146 (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1147 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1148
1149 if (BITS_SET(phy_control,
1150 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1151 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1152 if (BITS_SET(phy_control,
1153 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1154 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1155
1156 if (BITS_SET(phy_control,
1157 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1158 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1159 if (BITS_SET(phy_control,
1160 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1161 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1162 }
1163
1164 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1165 phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1166
1167 /* this assumes override is only used to enable lanes */
1168 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1169 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1170
1171 if (BITS_SET(phy_control,
1172 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1173 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1174
1175 if (BITS_SET(phy_control,
1176 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1177 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1178 if (BITS_SET(phy_control,
1179 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1180 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1181 }
1182
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001183 phy_status &= phy_status_mask;
1184
Ville Syrjälä30142272015-07-08 23:46:01 +03001185 /*
1186 * The PHY may be busy with some initial calibration and whatnot,
1187 * so the power state can take a while to actually change.
1188 */
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001189 if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
Ville Syrjälä30142272015-07-08 23:46:01 +03001190 WARN(phy_status != tmp,
1191 "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1192 tmp, phy_status, dev_priv->chv_phy_control);
1193}
1194
1195#undef BITS_SET
1196
Daniel Vetter9c065a72014-09-30 10:56:38 +02001197static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1198 struct i915_power_well *power_well)
1199{
1200 enum dpio_phy phy;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001201 enum pipe pipe;
1202 uint32_t tmp;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001203
1204 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1205 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1206
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001207 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1208 pipe = PIPE_A;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001209 phy = DPIO_PHY0;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001210 } else {
1211 pipe = PIPE_C;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001212 phy = DPIO_PHY1;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001213 }
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001214
1215 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001216 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1217 vlv_set_power_well(dev_priv, power_well, true);
1218
1219 /* Poll for phypwrgood signal */
1220 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
1221 DRM_ERROR("Display PHY %d is not power up\n", phy);
1222
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001223 mutex_lock(&dev_priv->sb_lock);
1224
1225 /* Enable dynamic power down */
1226 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
Ville Syrjäläee279212015-07-08 23:45:57 +03001227 tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1228 DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001229 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1230
1231 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1232 tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1233 tmp |= DPIO_DYNPWRDOWNEN_CH1;
1234 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
Ville Syrjälä3e288782015-07-08 23:45:58 +03001235 } else {
1236 /*
1237 * Force the non-existing CL2 off. BXT does this
1238 * too, so maybe it saves some power even though
1239 * CL2 doesn't exist?
1240 */
1241 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1242 tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1243 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001244 }
1245
1246 mutex_unlock(&dev_priv->sb_lock);
1247
Ville Syrjälä70722462015-04-10 18:21:28 +03001248 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1249 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001250
1251 DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1252 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001253
1254 assert_chv_phy_status(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001255}
1256
1257static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1258 struct i915_power_well *power_well)
1259{
1260 enum dpio_phy phy;
1261
1262 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1263 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1264
1265 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1266 phy = DPIO_PHY0;
1267 assert_pll_disabled(dev_priv, PIPE_A);
1268 assert_pll_disabled(dev_priv, PIPE_B);
1269 } else {
1270 phy = DPIO_PHY1;
1271 assert_pll_disabled(dev_priv, PIPE_C);
1272 }
1273
Ville Syrjälä70722462015-04-10 18:21:28 +03001274 dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1275 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001276
1277 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001278
1279 DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1280 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001281
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001282 /* PHY is fully reset now, so we can enable the PHY state asserts */
1283 dev_priv->chv_phy_assert[phy] = true;
1284
Ville Syrjälä30142272015-07-08 23:46:01 +03001285 assert_chv_phy_status(dev_priv);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001286}
1287
Ville Syrjälä6669e392015-07-08 23:46:00 +03001288static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1289 enum dpio_channel ch, bool override, unsigned int mask)
1290{
1291 enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1292 u32 reg, val, expected, actual;
1293
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001294 /*
1295 * The BIOS can leave the PHY is some weird state
1296 * where it doesn't fully power down some parts.
1297 * Disable the asserts until the PHY has been fully
1298 * reset (ie. the power well has been disabled at
1299 * least once).
1300 */
1301 if (!dev_priv->chv_phy_assert[phy])
1302 return;
1303
Ville Syrjälä6669e392015-07-08 23:46:00 +03001304 if (ch == DPIO_CH0)
1305 reg = _CHV_CMN_DW0_CH0;
1306 else
1307 reg = _CHV_CMN_DW6_CH1;
1308
1309 mutex_lock(&dev_priv->sb_lock);
1310 val = vlv_dpio_read(dev_priv, pipe, reg);
1311 mutex_unlock(&dev_priv->sb_lock);
1312
1313 /*
1314 * This assumes !override is only used when the port is disabled.
1315 * All lanes should power down even without the override when
1316 * the port is disabled.
1317 */
1318 if (!override || mask == 0xf) {
1319 expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1320 /*
1321 * If CH1 common lane is not active anymore
1322 * (eg. for pipe B DPLL) the entire channel will
1323 * shut down, which causes the common lane registers
1324 * to read as 0. That means we can't actually check
1325 * the lane power down status bits, but as the entire
1326 * register reads as 0 it's a good indication that the
1327 * channel is indeed entirely powered down.
1328 */
1329 if (ch == DPIO_CH1 && val == 0)
1330 expected = 0;
1331 } else if (mask != 0x0) {
1332 expected = DPIO_ANYDL_POWERDOWN;
1333 } else {
1334 expected = 0;
1335 }
1336
1337 if (ch == DPIO_CH0)
1338 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1339 else
1340 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1341 actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1342
1343 WARN(actual != expected,
1344 "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1345 !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1346 !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1347 reg, val);
1348}
1349
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001350bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1351 enum dpio_channel ch, bool override)
1352{
1353 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1354 bool was_override;
1355
1356 mutex_lock(&power_domains->lock);
1357
1358 was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1359
1360 if (override == was_override)
1361 goto out;
1362
1363 if (override)
1364 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1365 else
1366 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1367
1368 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1369
1370 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1371 phy, ch, dev_priv->chv_phy_control);
1372
Ville Syrjälä30142272015-07-08 23:46:01 +03001373 assert_chv_phy_status(dev_priv);
1374
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001375out:
1376 mutex_unlock(&power_domains->lock);
1377
1378 return was_override;
1379}
1380
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001381void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1382 bool override, unsigned int mask)
1383{
1384 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1385 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1386 enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1387 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1388
1389 mutex_lock(&power_domains->lock);
1390
1391 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1392 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1393
1394 if (override)
1395 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1396 else
1397 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1398
1399 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1400
1401 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1402 phy, ch, mask, dev_priv->chv_phy_control);
1403
Ville Syrjälä30142272015-07-08 23:46:01 +03001404 assert_chv_phy_status(dev_priv);
1405
Ville Syrjälä6669e392015-07-08 23:46:00 +03001406 assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1407
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001408 mutex_unlock(&power_domains->lock);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001409}
1410
1411static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1412 struct i915_power_well *power_well)
1413{
1414 enum pipe pipe = power_well->data;
1415 bool enabled;
1416 u32 state, ctrl;
1417
1418 mutex_lock(&dev_priv->rps.hw_lock);
1419
1420 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1421 /*
1422 * We only ever set the power-on and power-gate states, anything
1423 * else is unexpected.
1424 */
1425 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1426 enabled = state == DP_SSS_PWR_ON(pipe);
1427
1428 /*
1429 * A transient state at this point would mean some unexpected party
1430 * is poking at the power controls too.
1431 */
1432 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1433 WARN_ON(ctrl << 16 != state);
1434
1435 mutex_unlock(&dev_priv->rps.hw_lock);
1436
1437 return enabled;
1438}
1439
1440static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1441 struct i915_power_well *power_well,
1442 bool enable)
1443{
1444 enum pipe pipe = power_well->data;
1445 u32 state;
1446 u32 ctrl;
1447
1448 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1449
1450 mutex_lock(&dev_priv->rps.hw_lock);
1451
1452#define COND \
1453 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1454
1455 if (COND)
1456 goto out;
1457
1458 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1459 ctrl &= ~DP_SSC_MASK(pipe);
1460 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1461 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1462
1463 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +09001464 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001465 state,
1466 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1467
1468#undef COND
1469
1470out:
1471 mutex_unlock(&dev_priv->rps.hw_lock);
1472}
1473
1474static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1475 struct i915_power_well *power_well)
1476{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001477 WARN_ON_ONCE(power_well->data != PIPE_A);
1478
Daniel Vetter9c065a72014-09-30 10:56:38 +02001479 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1480}
1481
1482static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1483 struct i915_power_well *power_well)
1484{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001485 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001486
1487 chv_set_pipe_power_well(dev_priv, power_well, true);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001488
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001489 vlv_display_power_well_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001490}
1491
1492static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1493 struct i915_power_well *power_well)
1494{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001495 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001496
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001497 vlv_display_power_well_deinit(dev_priv);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001498
Daniel Vetter9c065a72014-09-30 10:56:38 +02001499 chv_set_pipe_power_well(dev_priv, power_well, false);
1500}
1501
Imre Deak09731282016-02-17 14:17:42 +02001502static void
1503__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1504 enum intel_display_power_domain domain)
1505{
1506 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1507 struct i915_power_well *power_well;
1508 int i;
1509
1510 for_each_power_well(i, power_well, BIT(domain), power_domains) {
1511 if (!power_well->count++)
1512 intel_power_well_enable(dev_priv, power_well);
1513 }
1514
1515 power_domains->domain_use_count[domain]++;
1516}
1517
Daniel Vettere4e76842014-09-30 10:56:42 +02001518/**
1519 * intel_display_power_get - grab a power domain reference
1520 * @dev_priv: i915 device instance
1521 * @domain: power domain to reference
1522 *
1523 * This function grabs a power domain reference for @domain and ensures that the
1524 * power domain and all its parents are powered up. Therefore users should only
1525 * grab a reference to the innermost power domain they need.
1526 *
1527 * Any power domain reference obtained by this function must have a symmetric
1528 * call to intel_display_power_put() to release the reference again.
1529 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001530void intel_display_power_get(struct drm_i915_private *dev_priv,
1531 enum intel_display_power_domain domain)
1532{
Imre Deak09731282016-02-17 14:17:42 +02001533 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001534
1535 intel_runtime_pm_get(dev_priv);
1536
Imre Deak09731282016-02-17 14:17:42 +02001537 mutex_lock(&power_domains->lock);
1538
1539 __intel_display_power_get_domain(dev_priv, domain);
1540
1541 mutex_unlock(&power_domains->lock);
1542}
1543
1544/**
1545 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
1546 * @dev_priv: i915 device instance
1547 * @domain: power domain to reference
1548 *
1549 * This function grabs a power domain reference for @domain and ensures that the
1550 * power domain and all its parents are powered up. Therefore users should only
1551 * grab a reference to the innermost power domain they need.
1552 *
1553 * Any power domain reference obtained by this function must have a symmetric
1554 * call to intel_display_power_put() to release the reference again.
1555 */
1556bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1557 enum intel_display_power_domain domain)
1558{
1559 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1560 bool is_enabled;
1561
1562 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1563 return false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001564
1565 mutex_lock(&power_domains->lock);
1566
Imre Deak09731282016-02-17 14:17:42 +02001567 if (__intel_display_power_is_enabled(dev_priv, domain)) {
1568 __intel_display_power_get_domain(dev_priv, domain);
1569 is_enabled = true;
1570 } else {
1571 is_enabled = false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001572 }
1573
Daniel Vetter9c065a72014-09-30 10:56:38 +02001574 mutex_unlock(&power_domains->lock);
Imre Deak09731282016-02-17 14:17:42 +02001575
1576 if (!is_enabled)
1577 intel_runtime_pm_put(dev_priv);
1578
1579 return is_enabled;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001580}
1581
Daniel Vettere4e76842014-09-30 10:56:42 +02001582/**
1583 * intel_display_power_put - release a power domain reference
1584 * @dev_priv: i915 device instance
1585 * @domain: power domain to reference
1586 *
1587 * This function drops the power domain reference obtained by
1588 * intel_display_power_get() and might power down the corresponding hardware
1589 * block right away if this is the last reference.
1590 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001591void intel_display_power_put(struct drm_i915_private *dev_priv,
1592 enum intel_display_power_domain domain)
1593{
1594 struct i915_power_domains *power_domains;
1595 struct i915_power_well *power_well;
1596 int i;
1597
1598 power_domains = &dev_priv->power_domains;
1599
1600 mutex_lock(&power_domains->lock);
1601
Daniel Stone11c86db2015-11-20 15:55:34 +00001602 WARN(!power_domains->domain_use_count[domain],
1603 "Use count on domain %s is already zero\n",
1604 intel_display_power_domain_str(domain));
Daniel Vetter9c065a72014-09-30 10:56:38 +02001605 power_domains->domain_use_count[domain]--;
1606
1607 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Daniel Stone11c86db2015-11-20 15:55:34 +00001608 WARN(!power_well->count,
1609 "Use count on power well %s is already zero",
1610 power_well->name);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001611
Imre Deakd314cd42015-11-17 17:44:23 +02001612 if (!--power_well->count)
Damien Lespiaudcddab32015-07-30 18:20:27 -03001613 intel_power_well_disable(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001614 }
1615
1616 mutex_unlock(&power_domains->lock);
1617
1618 intel_runtime_pm_put(dev_priv);
1619}
1620
Ville Syrjälä9d0996b2016-04-18 14:02:28 +03001621#define HSW_DISPLAY_POWER_DOMAINS ( \
1622 BIT(POWER_DOMAIN_PIPE_B) | \
1623 BIT(POWER_DOMAIN_PIPE_C) | \
1624 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1625 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1626 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1627 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1628 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1629 BIT(POWER_DOMAIN_TRANSCODER_C) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001630 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1631 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1632 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Ville Syrjälä9d0996b2016-04-18 14:02:28 +03001633 BIT(POWER_DOMAIN_PORT_CRT) | /* DDI E */ \
1634 BIT(POWER_DOMAIN_VGA) | \
1635 BIT(POWER_DOMAIN_AUDIO) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001636 BIT(POWER_DOMAIN_INIT))
1637
Ville Syrjälä9d0996b2016-04-18 14:02:28 +03001638#define BDW_DISPLAY_POWER_DOMAINS ( \
1639 BIT(POWER_DOMAIN_PIPE_B) | \
1640 BIT(POWER_DOMAIN_PIPE_C) | \
1641 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1642 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1643 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1644 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1645 BIT(POWER_DOMAIN_TRANSCODER_C) | \
1646 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1647 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1648 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
1649 BIT(POWER_DOMAIN_PORT_CRT) | /* DDI E */ \
1650 BIT(POWER_DOMAIN_VGA) | \
1651 BIT(POWER_DOMAIN_AUDIO) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001652 BIT(POWER_DOMAIN_INIT))
1653
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001654#define VLV_DISPLAY_POWER_DOMAINS ( \
1655 BIT(POWER_DOMAIN_PIPE_A) | \
1656 BIT(POWER_DOMAIN_PIPE_B) | \
1657 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1658 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1659 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1660 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1661 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1662 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1663 BIT(POWER_DOMAIN_PORT_DSI) | \
1664 BIT(POWER_DOMAIN_PORT_CRT) | \
1665 BIT(POWER_DOMAIN_VGA) | \
1666 BIT(POWER_DOMAIN_AUDIO) | \
1667 BIT(POWER_DOMAIN_AUX_B) | \
1668 BIT(POWER_DOMAIN_AUX_C) | \
1669 BIT(POWER_DOMAIN_GMBUS) | \
1670 BIT(POWER_DOMAIN_INIT))
Daniel Vetter9c065a72014-09-30 10:56:38 +02001671
1672#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001673 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1674 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001675 BIT(POWER_DOMAIN_PORT_CRT) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001676 BIT(POWER_DOMAIN_AUX_B) | \
1677 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001678 BIT(POWER_DOMAIN_INIT))
1679
1680#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001681 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001682 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001683 BIT(POWER_DOMAIN_INIT))
1684
1685#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001686 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001687 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001688 BIT(POWER_DOMAIN_INIT))
1689
1690#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001691 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001692 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001693 BIT(POWER_DOMAIN_INIT))
1694
1695#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001696 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001697 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001698 BIT(POWER_DOMAIN_INIT))
1699
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001700#define CHV_DISPLAY_POWER_DOMAINS ( \
1701 BIT(POWER_DOMAIN_PIPE_A) | \
1702 BIT(POWER_DOMAIN_PIPE_B) | \
1703 BIT(POWER_DOMAIN_PIPE_C) | \
1704 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1705 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1706 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1707 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1708 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1709 BIT(POWER_DOMAIN_TRANSCODER_C) | \
1710 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1711 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1712 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
1713 BIT(POWER_DOMAIN_PORT_DSI) | \
1714 BIT(POWER_DOMAIN_VGA) | \
1715 BIT(POWER_DOMAIN_AUDIO) | \
1716 BIT(POWER_DOMAIN_AUX_B) | \
1717 BIT(POWER_DOMAIN_AUX_C) | \
1718 BIT(POWER_DOMAIN_AUX_D) | \
1719 BIT(POWER_DOMAIN_GMBUS) | \
1720 BIT(POWER_DOMAIN_INIT))
1721
Daniel Vetter9c065a72014-09-30 10:56:38 +02001722#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001723 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1724 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001725 BIT(POWER_DOMAIN_AUX_B) | \
1726 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001727 BIT(POWER_DOMAIN_INIT))
1728
1729#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001730 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001731 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001732 BIT(POWER_DOMAIN_INIT))
1733
Daniel Vetter9c065a72014-09-30 10:56:38 +02001734static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1735 .sync_hw = i9xx_always_on_power_well_noop,
1736 .enable = i9xx_always_on_power_well_noop,
1737 .disable = i9xx_always_on_power_well_noop,
1738 .is_enabled = i9xx_always_on_power_well_enabled,
1739};
1740
1741static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1742 .sync_hw = chv_pipe_power_well_sync_hw,
1743 .enable = chv_pipe_power_well_enable,
1744 .disable = chv_pipe_power_well_disable,
1745 .is_enabled = chv_pipe_power_well_enabled,
1746};
1747
1748static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1749 .sync_hw = vlv_power_well_sync_hw,
1750 .enable = chv_dpio_cmn_power_well_enable,
1751 .disable = chv_dpio_cmn_power_well_disable,
1752 .is_enabled = vlv_power_well_enabled,
1753};
1754
1755static struct i915_power_well i9xx_always_on_power_well[] = {
1756 {
1757 .name = "always-on",
1758 .always_on = 1,
1759 .domains = POWER_DOMAIN_MASK,
1760 .ops = &i9xx_always_on_power_well_ops,
1761 },
1762};
1763
1764static const struct i915_power_well_ops hsw_power_well_ops = {
1765 .sync_hw = hsw_power_well_sync_hw,
1766 .enable = hsw_power_well_enable,
1767 .disable = hsw_power_well_disable,
1768 .is_enabled = hsw_power_well_enabled,
1769};
1770
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001771static const struct i915_power_well_ops skl_power_well_ops = {
1772 .sync_hw = skl_power_well_sync_hw,
1773 .enable = skl_power_well_enable,
1774 .disable = skl_power_well_disable,
1775 .is_enabled = skl_power_well_enabled,
1776};
1777
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001778static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1779 .sync_hw = gen9_dc_off_power_well_sync_hw,
1780 .enable = gen9_dc_off_power_well_enable,
1781 .disable = gen9_dc_off_power_well_disable,
1782 .is_enabled = gen9_dc_off_power_well_enabled,
1783};
1784
Daniel Vetter9c065a72014-09-30 10:56:38 +02001785static struct i915_power_well hsw_power_wells[] = {
1786 {
1787 .name = "always-on",
1788 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001789 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001790 .ops = &i9xx_always_on_power_well_ops,
1791 },
1792 {
1793 .name = "display",
1794 .domains = HSW_DISPLAY_POWER_DOMAINS,
1795 .ops = &hsw_power_well_ops,
1796 },
1797};
1798
1799static struct i915_power_well bdw_power_wells[] = {
1800 {
1801 .name = "always-on",
1802 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001803 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001804 .ops = &i9xx_always_on_power_well_ops,
1805 },
1806 {
1807 .name = "display",
1808 .domains = BDW_DISPLAY_POWER_DOMAINS,
1809 .ops = &hsw_power_well_ops,
1810 },
1811};
1812
1813static const struct i915_power_well_ops vlv_display_power_well_ops = {
1814 .sync_hw = vlv_power_well_sync_hw,
1815 .enable = vlv_display_power_well_enable,
1816 .disable = vlv_display_power_well_disable,
1817 .is_enabled = vlv_power_well_enabled,
1818};
1819
1820static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1821 .sync_hw = vlv_power_well_sync_hw,
1822 .enable = vlv_dpio_cmn_power_well_enable,
1823 .disable = vlv_dpio_cmn_power_well_disable,
1824 .is_enabled = vlv_power_well_enabled,
1825};
1826
1827static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1828 .sync_hw = vlv_power_well_sync_hw,
1829 .enable = vlv_power_well_enable,
1830 .disable = vlv_power_well_disable,
1831 .is_enabled = vlv_power_well_enabled,
1832};
1833
1834static struct i915_power_well vlv_power_wells[] = {
1835 {
1836 .name = "always-on",
1837 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001838 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001839 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001840 .data = PUNIT_POWER_WELL_ALWAYS_ON,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001841 },
1842 {
1843 .name = "display",
1844 .domains = VLV_DISPLAY_POWER_DOMAINS,
1845 .data = PUNIT_POWER_WELL_DISP2D,
1846 .ops = &vlv_display_power_well_ops,
1847 },
1848 {
1849 .name = "dpio-tx-b-01",
1850 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1851 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1852 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1853 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1854 .ops = &vlv_dpio_power_well_ops,
1855 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1856 },
1857 {
1858 .name = "dpio-tx-b-23",
1859 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1860 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1861 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1862 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1863 .ops = &vlv_dpio_power_well_ops,
1864 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1865 },
1866 {
1867 .name = "dpio-tx-c-01",
1868 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1869 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1870 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1871 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1872 .ops = &vlv_dpio_power_well_ops,
1873 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1874 },
1875 {
1876 .name = "dpio-tx-c-23",
1877 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1878 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1879 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1880 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1881 .ops = &vlv_dpio_power_well_ops,
1882 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1883 },
1884 {
1885 .name = "dpio-common",
1886 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1887 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1888 .ops = &vlv_dpio_cmn_power_well_ops,
1889 },
1890};
1891
1892static struct i915_power_well chv_power_wells[] = {
1893 {
1894 .name = "always-on",
1895 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001896 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001897 .ops = &i9xx_always_on_power_well_ops,
1898 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001899 {
1900 .name = "display",
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001901 /*
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001902 * Pipe A power well is the new disp2d well. Pipe B and C
1903 * power wells don't actually exist. Pipe A power well is
1904 * required for any pipe to work.
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001905 */
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001906 .domains = CHV_DISPLAY_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001907 .data = PIPE_A,
1908 .ops = &chv_pipe_power_well_ops,
1909 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001910 {
1911 .name = "dpio-common-bc",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001912 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001913 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1914 .ops = &chv_dpio_cmn_power_well_ops,
1915 },
1916 {
1917 .name = "dpio-common-d",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001918 .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001919 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
1920 .ops = &chv_dpio_cmn_power_well_ops,
1921 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001922};
1923
Suketu Shah5aefb232015-04-16 14:22:10 +05301924bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
1925 int power_well_id)
1926{
1927 struct i915_power_well *power_well;
1928 bool ret;
1929
1930 power_well = lookup_power_well(dev_priv, power_well_id);
1931 ret = power_well->ops->is_enabled(dev_priv, power_well);
1932
1933 return ret;
1934}
1935
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001936static struct i915_power_well skl_power_wells[] = {
1937 {
1938 .name = "always-on",
1939 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001940 .domains = POWER_DOMAIN_MASK,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001941 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001942 .data = SKL_DISP_PW_ALWAYS_ON,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001943 },
1944 {
1945 .name = "power well 1",
Imre Deak4a76f292015-11-04 19:24:15 +02001946 /* Handled by the DMC firmware */
1947 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001948 .ops = &skl_power_well_ops,
1949 .data = SKL_DISP_PW_1,
1950 },
1951 {
1952 .name = "MISC IO power well",
Imre Deak4a76f292015-11-04 19:24:15 +02001953 /* Handled by the DMC firmware */
1954 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001955 .ops = &skl_power_well_ops,
1956 .data = SKL_DISP_PW_MISC_IO,
1957 },
1958 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001959 .name = "DC off",
1960 .domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
1961 .ops = &gen9_dc_off_power_well_ops,
1962 .data = SKL_DISP_PW_DC_OFF,
1963 },
1964 {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001965 .name = "power well 2",
1966 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1967 .ops = &skl_power_well_ops,
1968 .data = SKL_DISP_PW_2,
1969 },
1970 {
1971 .name = "DDI A/E power well",
1972 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1973 .ops = &skl_power_well_ops,
1974 .data = SKL_DISP_PW_DDI_A_E,
1975 },
1976 {
1977 .name = "DDI B power well",
1978 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1979 .ops = &skl_power_well_ops,
1980 .data = SKL_DISP_PW_DDI_B,
1981 },
1982 {
1983 .name = "DDI C power well",
1984 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1985 .ops = &skl_power_well_ops,
1986 .data = SKL_DISP_PW_DDI_C,
1987 },
1988 {
1989 .name = "DDI D power well",
1990 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1991 .ops = &skl_power_well_ops,
1992 .data = SKL_DISP_PW_DDI_D,
1993 },
1994};
1995
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301996static struct i915_power_well bxt_power_wells[] = {
1997 {
1998 .name = "always-on",
1999 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03002000 .domains = POWER_DOMAIN_MASK,
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302001 .ops = &i9xx_always_on_power_well_ops,
2002 },
2003 {
2004 .name = "power well 1",
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002005 .domains = 0,
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302006 .ops = &skl_power_well_ops,
2007 .data = SKL_DISP_PW_1,
2008 },
2009 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01002010 .name = "DC off",
2011 .domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
2012 .ops = &gen9_dc_off_power_well_ops,
2013 .data = SKL_DISP_PW_DC_OFF,
2014 },
2015 {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302016 .name = "power well 2",
2017 .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2018 .ops = &skl_power_well_ops,
2019 .data = SKL_DISP_PW_2,
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01002020 },
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302021};
2022
Imre Deak1b0e3a02015-11-05 23:04:11 +02002023static int
2024sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
2025 int disable_power_well)
2026{
2027 if (disable_power_well >= 0)
2028 return !!disable_power_well;
2029
Imre Deak1b0e3a02015-11-05 23:04:11 +02002030 return 1;
2031}
2032
Imre Deaka37baf32016-02-29 22:49:03 +02002033static uint32_t get_allowed_dc_mask(const struct drm_i915_private *dev_priv,
2034 int enable_dc)
2035{
2036 uint32_t mask;
2037 int requested_dc;
2038 int max_dc;
2039
2040 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
2041 max_dc = 2;
2042 mask = 0;
2043 } else if (IS_BROXTON(dev_priv)) {
2044 max_dc = 1;
2045 /*
2046 * DC9 has a separate HW flow from the rest of the DC states,
2047 * not depending on the DMC firmware. It's needed by system
2048 * suspend/resume, so allow it unconditionally.
2049 */
2050 mask = DC_STATE_EN_DC9;
2051 } else {
2052 max_dc = 0;
2053 mask = 0;
2054 }
2055
Imre Deak66e2c4c2016-02-29 22:49:04 +02002056 if (!i915.disable_power_well)
2057 max_dc = 0;
2058
Imre Deaka37baf32016-02-29 22:49:03 +02002059 if (enable_dc >= 0 && enable_dc <= max_dc) {
2060 requested_dc = enable_dc;
2061 } else if (enable_dc == -1) {
2062 requested_dc = max_dc;
2063 } else if (enable_dc > max_dc && enable_dc <= 2) {
2064 DRM_DEBUG_KMS("Adjusting requested max DC state (%d->%d)\n",
2065 enable_dc, max_dc);
2066 requested_dc = max_dc;
2067 } else {
2068 DRM_ERROR("Unexpected value for enable_dc (%d)\n", enable_dc);
2069 requested_dc = max_dc;
2070 }
2071
2072 if (requested_dc > 1)
2073 mask |= DC_STATE_EN_UPTO_DC6;
2074 if (requested_dc > 0)
2075 mask |= DC_STATE_EN_UPTO_DC5;
2076
2077 DRM_DEBUG_KMS("Allowed DC state mask %02x\n", mask);
2078
2079 return mask;
2080}
2081
Daniel Vetter9c065a72014-09-30 10:56:38 +02002082#define set_power_wells(power_domains, __power_wells) ({ \
2083 (power_domains)->power_wells = (__power_wells); \
2084 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
2085})
2086
Daniel Vettere4e76842014-09-30 10:56:42 +02002087/**
2088 * intel_power_domains_init - initializes the power domain structures
2089 * @dev_priv: i915 device instance
2090 *
2091 * Initializes the power domain structures for @dev_priv depending upon the
2092 * supported platform.
2093 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002094int intel_power_domains_init(struct drm_i915_private *dev_priv)
2095{
2096 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2097
Imre Deak1b0e3a02015-11-05 23:04:11 +02002098 i915.disable_power_well = sanitize_disable_power_well_option(dev_priv,
2099 i915.disable_power_well);
Imre Deaka37baf32016-02-29 22:49:03 +02002100 dev_priv->csr.allowed_dc_mask = get_allowed_dc_mask(dev_priv,
2101 i915.enable_dc);
Imre Deak1b0e3a02015-11-05 23:04:11 +02002102
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01002103 BUILD_BUG_ON(POWER_DOMAIN_NUM > 31);
2104
Daniel Vetter9c065a72014-09-30 10:56:38 +02002105 mutex_init(&power_domains->lock);
2106
2107 /*
2108 * The enabling order will be from lower to higher indexed wells,
2109 * the disabling order is reversed.
2110 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002111 if (IS_HASWELL(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002112 set_power_wells(power_domains, hsw_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002113 } else if (IS_BROADWELL(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002114 set_power_wells(power_domains, bdw_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002115 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002116 set_power_wells(power_domains, skl_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002117 } else if (IS_BROXTON(dev_priv)) {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302118 set_power_wells(power_domains, bxt_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002119 } else if (IS_CHERRYVIEW(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002120 set_power_wells(power_domains, chv_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002121 } else if (IS_VALLEYVIEW(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002122 set_power_wells(power_domains, vlv_power_wells);
2123 } else {
2124 set_power_wells(power_domains, i9xx_always_on_power_well);
2125 }
2126
2127 return 0;
2128}
2129
Daniel Vettere4e76842014-09-30 10:56:42 +02002130/**
2131 * intel_power_domains_fini - finalizes the power domain structures
2132 * @dev_priv: i915 device instance
2133 *
2134 * Finalizes the power domain structures for @dev_priv depending upon the
2135 * supported platform. This function also disables runtime pm and ensures that
2136 * the device stays powered up so that the driver can be reloaded.
2137 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002138void intel_power_domains_fini(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002139{
Imre Deak25b181b2015-12-17 13:44:56 +02002140 struct device *device = &dev_priv->dev->pdev->dev;
2141
Imre Deakaabee1b2015-12-15 20:10:29 +02002142 /*
2143 * The i915.ko module is still not prepared to be loaded when
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002144 * the power well is not enabled, so just enable it in case
Imre Deakaabee1b2015-12-15 20:10:29 +02002145 * we're going to unload/reload.
2146 * The following also reacquires the RPM reference the core passed
2147 * to the driver during loading, which is dropped in
2148 * intel_runtime_pm_enable(). We have to hand back the control of the
2149 * device to the core with this reference held.
2150 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002151 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002152
2153 /* Remove the refcount we took to keep power well support disabled. */
2154 if (!i915.disable_power_well)
2155 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak25b181b2015-12-17 13:44:56 +02002156
2157 /*
2158 * Remove the refcount we took in intel_runtime_pm_enable() in case
2159 * the platform doesn't support runtime PM.
2160 */
2161 if (!HAS_RUNTIME_PM(dev_priv))
2162 pm_runtime_put(device);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002163}
2164
Imre Deak30eade12015-11-04 19:24:13 +02002165static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002166{
2167 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2168 struct i915_power_well *power_well;
2169 int i;
2170
2171 mutex_lock(&power_domains->lock);
2172 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
2173 power_well->ops->sync_hw(dev_priv, power_well);
2174 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2175 power_well);
2176 }
2177 mutex_unlock(&power_domains->lock);
2178}
2179
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002180static void gen9_dbuf_enable(struct drm_i915_private *dev_priv)
2181{
2182 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
2183 POSTING_READ(DBUF_CTL);
2184
2185 udelay(10);
2186
2187 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
2188 DRM_ERROR("DBuf power enable timeout\n");
2189}
2190
2191static void gen9_dbuf_disable(struct drm_i915_private *dev_priv)
2192{
2193 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
2194 POSTING_READ(DBUF_CTL);
2195
2196 udelay(10);
2197
2198 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
2199 DRM_ERROR("DBuf power disable timeout!\n");
2200}
2201
Imre Deak73dfc222015-11-17 17:33:53 +02002202static void skl_display_core_init(struct drm_i915_private *dev_priv,
Imre Deak443a93a2016-04-04 15:42:57 +03002203 bool resume)
Imre Deak73dfc222015-11-17 17:33:53 +02002204{
2205 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deak443a93a2016-04-04 15:42:57 +03002206 struct i915_power_well *well;
Imre Deak73dfc222015-11-17 17:33:53 +02002207 uint32_t val;
2208
Imre Deakd26fa1d2015-11-04 19:24:17 +02002209 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2210
Imre Deak73dfc222015-11-17 17:33:53 +02002211 /* enable PCH reset handshake */
2212 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2213 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2214
2215 /* enable PG1 and Misc I/O */
2216 mutex_lock(&power_domains->lock);
Imre Deak443a93a2016-04-04 15:42:57 +03002217
2218 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2219 intel_power_well_enable(dev_priv, well);
2220
2221 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2222 intel_power_well_enable(dev_priv, well);
2223
Imre Deak73dfc222015-11-17 17:33:53 +02002224 mutex_unlock(&power_domains->lock);
2225
Imre Deak73dfc222015-11-17 17:33:53 +02002226 skl_init_cdclk(dev_priv);
2227
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002228 gen9_dbuf_enable(dev_priv);
2229
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03002230 if (resume && dev_priv->csr.dmc_payload)
Imre Deak2abc5252016-03-04 21:57:41 +02002231 intel_csr_load_program(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002232}
2233
2234static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2235{
2236 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deak443a93a2016-04-04 15:42:57 +03002237 struct i915_power_well *well;
Imre Deak73dfc222015-11-17 17:33:53 +02002238
Imre Deakd26fa1d2015-11-04 19:24:17 +02002239 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2240
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002241 gen9_dbuf_disable(dev_priv);
2242
Imre Deak73dfc222015-11-17 17:33:53 +02002243 skl_uninit_cdclk(dev_priv);
2244
2245 /* The spec doesn't call for removing the reset handshake flag */
2246 /* disable PG1 and Misc I/O */
Imre Deak443a93a2016-04-04 15:42:57 +03002247
Imre Deak73dfc222015-11-17 17:33:53 +02002248 mutex_lock(&power_domains->lock);
Imre Deak443a93a2016-04-04 15:42:57 +03002249
2250 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2251 intel_power_well_disable(dev_priv, well);
2252
2253 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2254 intel_power_well_disable(dev_priv, well);
2255
Imre Deak73dfc222015-11-17 17:33:53 +02002256 mutex_unlock(&power_domains->lock);
2257}
2258
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002259void bxt_display_core_init(struct drm_i915_private *dev_priv,
2260 bool resume)
2261{
2262 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2263 struct i915_power_well *well;
2264 uint32_t val;
2265
2266 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2267
2268 /*
2269 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
2270 * or else the reset will hang because there is no PCH to respond.
2271 * Move the handshake programming to initialization sequence.
2272 * Previously was left up to BIOS.
2273 */
2274 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2275 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
2276 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
2277
2278 /* Enable PG1 */
2279 mutex_lock(&power_domains->lock);
2280
2281 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2282 intel_power_well_enable(dev_priv, well);
2283
2284 mutex_unlock(&power_domains->lock);
2285
2286 broxton_init_cdclk(dev_priv);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002287
2288 gen9_dbuf_enable(dev_priv);
2289
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002290 broxton_ddi_phy_init(dev_priv);
2291
Imre Deakadc7f042016-04-04 17:27:10 +03002292 broxton_ddi_phy_verify_state(dev_priv);
2293
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002294 if (resume && dev_priv->csr.dmc_payload)
2295 intel_csr_load_program(dev_priv);
2296}
2297
2298void bxt_display_core_uninit(struct drm_i915_private *dev_priv)
2299{
2300 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2301 struct i915_power_well *well;
2302
2303 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2304
2305 broxton_ddi_phy_uninit(dev_priv);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002306
2307 gen9_dbuf_disable(dev_priv);
2308
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002309 broxton_uninit_cdclk(dev_priv);
2310
2311 /* The spec doesn't call for removing the reset handshake flag */
2312
2313 /* Disable PG1 */
2314 mutex_lock(&power_domains->lock);
2315
2316 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2317 intel_power_well_disable(dev_priv, well);
2318
2319 mutex_unlock(&power_domains->lock);
2320}
2321
Ville Syrjälä70722462015-04-10 18:21:28 +03002322static void chv_phy_control_init(struct drm_i915_private *dev_priv)
2323{
2324 struct i915_power_well *cmn_bc =
2325 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2326 struct i915_power_well *cmn_d =
2327 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
2328
2329 /*
2330 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
2331 * workaround never ever read DISPLAY_PHY_CONTROL, and
2332 * instead maintain a shadow copy ourselves. Use the actual
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002333 * power well state and lane status to reconstruct the
2334 * expected initial value.
Ville Syrjälä70722462015-04-10 18:21:28 +03002335 */
2336 dev_priv->chv_phy_control =
Ville Syrjäläbc284542015-05-26 20:22:38 +03002337 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
2338 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002339 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
2340 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
2341 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
2342
2343 /*
2344 * If all lanes are disabled we leave the override disabled
2345 * with all power down bits cleared to match the state we
2346 * would use after disabling the port. Otherwise enable the
2347 * override and set the lane powerdown bits accding to the
2348 * current lane status.
2349 */
2350 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
2351 uint32_t status = I915_READ(DPLL(PIPE_A));
2352 unsigned int mask;
2353
2354 mask = status & DPLL_PORTB_READY_MASK;
2355 if (mask == 0xf)
2356 mask = 0x0;
2357 else
2358 dev_priv->chv_phy_control |=
2359 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
2360
2361 dev_priv->chv_phy_control |=
2362 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
2363
2364 mask = (status & DPLL_PORTC_READY_MASK) >> 4;
2365 if (mask == 0xf)
2366 mask = 0x0;
2367 else
2368 dev_priv->chv_phy_control |=
2369 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
2370
2371 dev_priv->chv_phy_control |=
2372 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
2373
Ville Syrjälä70722462015-04-10 18:21:28 +03002374 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002375
2376 dev_priv->chv_phy_assert[DPIO_PHY0] = false;
2377 } else {
2378 dev_priv->chv_phy_assert[DPIO_PHY0] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002379 }
2380
2381 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
2382 uint32_t status = I915_READ(DPIO_PHY_STATUS);
2383 unsigned int mask;
2384
2385 mask = status & DPLL_PORTD_READY_MASK;
2386
2387 if (mask == 0xf)
2388 mask = 0x0;
2389 else
2390 dev_priv->chv_phy_control |=
2391 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
2392
2393 dev_priv->chv_phy_control |=
2394 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
2395
Ville Syrjälä70722462015-04-10 18:21:28 +03002396 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002397
2398 dev_priv->chv_phy_assert[DPIO_PHY1] = false;
2399 } else {
2400 dev_priv->chv_phy_assert[DPIO_PHY1] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002401 }
2402
2403 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
2404
2405 DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
2406 dev_priv->chv_phy_control);
Ville Syrjälä70722462015-04-10 18:21:28 +03002407}
2408
Daniel Vetter9c065a72014-09-30 10:56:38 +02002409static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
2410{
2411 struct i915_power_well *cmn =
2412 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2413 struct i915_power_well *disp2d =
2414 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
2415
Daniel Vetter9c065a72014-09-30 10:56:38 +02002416 /* If the display might be already active skip this */
Ville Syrjälä5d93a6e2014-10-16 20:52:33 +03002417 if (cmn->ops->is_enabled(dev_priv, cmn) &&
2418 disp2d->ops->is_enabled(dev_priv, disp2d) &&
Daniel Vetter9c065a72014-09-30 10:56:38 +02002419 I915_READ(DPIO_CTL) & DPIO_CMNRST)
2420 return;
2421
2422 DRM_DEBUG_KMS("toggling display PHY side reset\n");
2423
2424 /* cmnlane needs DPLL registers */
2425 disp2d->ops->enable(dev_priv, disp2d);
2426
2427 /*
2428 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
2429 * Need to assert and de-assert PHY SB reset by gating the
2430 * common lane power, then un-gating it.
2431 * Simply ungating isn't enough to reset the PHY enough to get
2432 * ports and lanes running.
2433 */
2434 cmn->ops->disable(dev_priv, cmn);
2435}
2436
Daniel Vettere4e76842014-09-30 10:56:42 +02002437/**
2438 * intel_power_domains_init_hw - initialize hardware power domain state
2439 * @dev_priv: i915 device instance
2440 *
2441 * This function initializes the hardware power domain state and enables all
2442 * power domains using intel_display_set_init_power().
2443 */
Imre Deak73dfc222015-11-17 17:33:53 +02002444void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002445{
2446 struct drm_device *dev = dev_priv->dev;
2447 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2448
2449 power_domains->initializing = true;
2450
Imre Deak73dfc222015-11-17 17:33:53 +02002451 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
2452 skl_display_core_init(dev_priv, resume);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002453 } else if (IS_BROXTON(dev)) {
2454 bxt_display_core_init(dev_priv, resume);
Imre Deak73dfc222015-11-17 17:33:53 +02002455 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä770effb2015-07-08 23:45:51 +03002456 mutex_lock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002457 chv_phy_control_init(dev_priv);
Ville Syrjälä770effb2015-07-08 23:45:51 +03002458 mutex_unlock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002459 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002460 mutex_lock(&power_domains->lock);
2461 vlv_cmnlane_wa(dev_priv);
2462 mutex_unlock(&power_domains->lock);
2463 }
2464
2465 /* For now, we need the power well to be always enabled. */
2466 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002467 /* Disable power support if the user asked so. */
2468 if (!i915.disable_power_well)
2469 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
Imre Deak30eade12015-11-04 19:24:13 +02002470 intel_power_domains_sync_hw(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002471 power_domains->initializing = false;
2472}
2473
Daniel Vettere4e76842014-09-30 10:56:42 +02002474/**
Imre Deak73dfc222015-11-17 17:33:53 +02002475 * intel_power_domains_suspend - suspend power domain state
2476 * @dev_priv: i915 device instance
2477 *
2478 * This function prepares the hardware power domain state before entering
2479 * system suspend. It must be paired with intel_power_domains_init_hw().
2480 */
2481void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
2482{
Imre Deakd314cd42015-11-17 17:44:23 +02002483 /*
2484 * Even if power well support was disabled we still want to disable
2485 * power wells while we are system suspended.
2486 */
2487 if (!i915.disable_power_well)
2488 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak2622d792016-02-29 22:49:02 +02002489
2490 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
2491 skl_display_core_uninit(dev_priv);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002492 else if (IS_BROXTON(dev_priv))
2493 bxt_display_core_uninit(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002494}
2495
2496/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002497 * intel_runtime_pm_get - grab a runtime pm reference
2498 * @dev_priv: i915 device instance
2499 *
2500 * This function grabs a device-level runtime pm reference (mostly used for GEM
2501 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2502 *
2503 * Any runtime pm reference obtained by this function must have a symmetric
2504 * call to intel_runtime_pm_put() to release the reference again.
2505 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002506void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2507{
2508 struct drm_device *dev = dev_priv->dev;
2509 struct device *device = &dev->pdev->dev;
2510
Daniel Vetter9c065a72014-09-30 10:56:38 +02002511 pm_runtime_get_sync(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002512
2513 atomic_inc(&dev_priv->pm.wakeref_count);
Imre Deakc9b88462015-12-15 20:10:34 +02002514 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002515}
2516
Daniel Vettere4e76842014-09-30 10:56:42 +02002517/**
Imre Deak09731282016-02-17 14:17:42 +02002518 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
2519 * @dev_priv: i915 device instance
2520 *
2521 * This function grabs a device-level runtime pm reference if the device is
2522 * already in use and ensures that it is powered up.
2523 *
2524 * Any runtime pm reference obtained by this function must have a symmetric
2525 * call to intel_runtime_pm_put() to release the reference again.
2526 */
2527bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
2528{
2529 struct drm_device *dev = dev_priv->dev;
2530 struct device *device = &dev->pdev->dev;
Imre Deak09731282016-02-17 14:17:42 +02002531
Chris Wilson135dc792016-02-25 21:10:28 +00002532 if (IS_ENABLED(CONFIG_PM)) {
2533 int ret = pm_runtime_get_if_in_use(device);
Imre Deak09731282016-02-17 14:17:42 +02002534
Chris Wilson135dc792016-02-25 21:10:28 +00002535 /*
2536 * In cases runtime PM is disabled by the RPM core and we get
2537 * an -EINVAL return value we are not supposed to call this
2538 * function, since the power state is undefined. This applies
2539 * atm to the late/early system suspend/resume handlers.
2540 */
2541 WARN_ON_ONCE(ret < 0);
2542 if (ret <= 0)
2543 return false;
2544 }
Imre Deak09731282016-02-17 14:17:42 +02002545
2546 atomic_inc(&dev_priv->pm.wakeref_count);
2547 assert_rpm_wakelock_held(dev_priv);
2548
2549 return true;
2550}
2551
2552/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002553 * intel_runtime_pm_get_noresume - grab a runtime pm reference
2554 * @dev_priv: i915 device instance
2555 *
2556 * This function grabs a device-level runtime pm reference (mostly used for GEM
2557 * code to ensure the GTT or GT is on).
2558 *
2559 * It will _not_ power up the device but instead only check that it's powered
2560 * on. Therefore it is only valid to call this functions from contexts where
2561 * the device is known to be powered up and where trying to power it up would
2562 * result in hilarity and deadlocks. That pretty much means only the system
2563 * suspend/resume code where this is used to grab runtime pm references for
2564 * delayed setup down in work items.
2565 *
2566 * Any runtime pm reference obtained by this function must have a symmetric
2567 * call to intel_runtime_pm_put() to release the reference again.
2568 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002569void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
2570{
2571 struct drm_device *dev = dev_priv->dev;
2572 struct device *device = &dev->pdev->dev;
2573
Imre Deakc9b88462015-12-15 20:10:34 +02002574 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002575 pm_runtime_get_noresume(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002576
2577 atomic_inc(&dev_priv->pm.wakeref_count);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002578}
2579
Daniel Vettere4e76842014-09-30 10:56:42 +02002580/**
2581 * intel_runtime_pm_put - release a runtime pm reference
2582 * @dev_priv: i915 device instance
2583 *
2584 * This function drops the device-level runtime pm reference obtained by
2585 * intel_runtime_pm_get() and might power down the corresponding
2586 * hardware block right away if this is the last reference.
2587 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002588void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
2589{
2590 struct drm_device *dev = dev_priv->dev;
2591 struct device *device = &dev->pdev->dev;
2592
Imre Deak542db3c2015-12-15 20:10:36 +02002593 assert_rpm_wakelock_held(dev_priv);
Imre Deak2b19efe2015-12-15 20:10:37 +02002594 if (atomic_dec_and_test(&dev_priv->pm.wakeref_count))
2595 atomic_inc(&dev_priv->pm.atomic_seq);
Imre Deak1f814da2015-12-16 02:52:19 +02002596
Daniel Vetter9c065a72014-09-30 10:56:38 +02002597 pm_runtime_mark_last_busy(device);
2598 pm_runtime_put_autosuspend(device);
2599}
2600
Daniel Vettere4e76842014-09-30 10:56:42 +02002601/**
2602 * intel_runtime_pm_enable - enable runtime pm
2603 * @dev_priv: i915 device instance
2604 *
2605 * This function enables runtime pm at the end of the driver load sequence.
2606 *
2607 * Note that this function does currently not enable runtime pm for the
2608 * subordinate display power domains. That is only done on the first modeset
2609 * using intel_display_set_init_power().
2610 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002611void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002612{
2613 struct drm_device *dev = dev_priv->dev;
2614 struct device *device = &dev->pdev->dev;
2615
Imre Deakcbc68dc2015-12-17 19:04:33 +02002616 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
2617 pm_runtime_mark_last_busy(device);
2618
Imre Deak25b181b2015-12-17 13:44:56 +02002619 /*
2620 * Take a permanent reference to disable the RPM functionality and drop
2621 * it only when unloading the driver. Use the low level get/put helpers,
2622 * so the driver's own RPM reference tracking asserts also work on
2623 * platforms without RPM support.
2624 */
Imre Deakcbc68dc2015-12-17 19:04:33 +02002625 if (!HAS_RUNTIME_PM(dev)) {
2626 pm_runtime_dont_use_autosuspend(device);
Imre Deak25b181b2015-12-17 13:44:56 +02002627 pm_runtime_get_sync(device);
Imre Deakcbc68dc2015-12-17 19:04:33 +02002628 } else {
2629 pm_runtime_use_autosuspend(device);
2630 }
Daniel Vetter9c065a72014-09-30 10:56:38 +02002631
Imre Deakaabee1b2015-12-15 20:10:29 +02002632 /*
2633 * The core calls the driver load handler with an RPM reference held.
2634 * We drop that here and will reacquire it during unloading in
2635 * intel_power_domains_fini().
2636 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002637 pm_runtime_put_autosuspend(device);
2638}
2639