blob: aae8545d75df14dd4c665a77af3e987869d0555f [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 Deak13ae3a02015-11-04 19:24:16 +0200495static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530496{
497 uint32_t val;
Imre Deak13ae3a02015-11-04 19:24:16 +0200498 uint32_t mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530499
Imre Deak13ae3a02015-11-04 19:24:16 +0200500 mask = DC_STATE_EN_UPTO_DC5;
501 if (IS_BROXTON(dev_priv))
502 mask |= DC_STATE_EN_DC9;
503 else
504 mask |= DC_STATE_EN_UPTO_DC6;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530505
Imre Deaka37baf32016-02-29 22:49:03 +0200506 if (WARN_ON_ONCE(state & ~dev_priv->csr.allowed_dc_mask))
507 state &= dev_priv->csr.allowed_dc_mask;
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100508
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530509 val = I915_READ(DC_STATE_EN);
Imre Deak13ae3a02015-11-04 19:24:16 +0200510 DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
511 val & mask, state);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200512
513 /* Check if DMC is ignoring our DC state requests */
514 if ((val & mask) != dev_priv->csr.dc_state)
515 DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
516 dev_priv->csr.dc_state, val & mask);
517
Imre Deak13ae3a02015-11-04 19:24:16 +0200518 val &= ~mask;
519 val |= state;
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200520
521 gen9_write_dc_state(dev_priv, val);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200522
523 dev_priv->csr.dc_state = val & mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530524}
525
Imre Deak13ae3a02015-11-04 19:24:16 +0200526void bxt_enable_dc9(struct drm_i915_private *dev_priv)
527{
528 assert_can_enable_dc9(dev_priv);
529
530 DRM_DEBUG_KMS("Enabling DC9\n");
531
532 gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
533}
534
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530535void bxt_disable_dc9(struct drm_i915_private *dev_priv)
536{
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530537 assert_can_disable_dc9(dev_priv);
538
539 DRM_DEBUG_KMS("Disabling DC9\n");
540
Imre Deak13ae3a02015-11-04 19:24:16 +0200541 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530542}
543
Daniel Vetteraf5fead2015-10-28 23:58:57 +0200544static void assert_csr_loaded(struct drm_i915_private *dev_priv)
545{
546 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
547 "CSR program storage start is NULL\n");
548 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
549 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
550}
551
Suketu Shah5aefb232015-04-16 14:22:10 +0530552static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shahdc174302015-04-17 19:46:16 +0530553{
Suketu Shah5aefb232015-04-16 14:22:10 +0530554 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
555 SKL_DISP_PW_2);
556
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700557 WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530558
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700559 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
560 "DC5 already programmed to be enabled.\n");
Imre Deakc9b88462015-12-15 20:10:34 +0200561 assert_rpm_wakelock_held(dev_priv);
Suketu Shah5aefb232015-04-16 14:22:10 +0530562
563 assert_csr_loaded(dev_priv);
564}
565
Suketu Shah5aefb232015-04-16 14:22:10 +0530566static void gen9_enable_dc5(struct drm_i915_private *dev_priv)
567{
Suketu Shah5aefb232015-04-16 14:22:10 +0530568 assert_can_enable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530569
570 DRM_DEBUG_KMS("Enabling DC5\n");
571
Imre Deak13ae3a02015-11-04 19:24:16 +0200572 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
Suketu Shahdc174302015-04-17 19:46:16 +0530573}
574
Suketu Shah93c7cb62015-04-16 14:22:13 +0530575static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530576{
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700577 WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
578 "Backlight is not disabled.\n");
579 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
580 "DC6 already programmed to be enabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530581
582 assert_csr_loaded(dev_priv);
583}
584
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530585void skl_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shah93c7cb62015-04-16 14:22:13 +0530586{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530587 assert_can_enable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530588
589 DRM_DEBUG_KMS("Enabling DC6\n");
590
Imre Deak13ae3a02015-11-04 19:24:16 +0200591 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
592
Suketu Shahf75a1982015-04-16 14:22:11 +0530593}
594
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530595void skl_disable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530596{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530597 DRM_DEBUG_KMS("Disabling DC6\n");
598
Imre Deak13ae3a02015-11-04 19:24:16 +0200599 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Suketu Shahf75a1982015-04-16 14:22:11 +0530600}
601
Imre Deakc6782b72016-04-05 13:26:05 +0300602static void
603gen9_sanitize_power_well_requests(struct drm_i915_private *dev_priv,
604 struct i915_power_well *power_well)
605{
606 enum skl_disp_power_wells power_well_id = power_well->data;
607 u32 val;
608 u32 mask;
609
610 mask = SKL_POWER_WELL_REQ(power_well_id);
611
612 val = I915_READ(HSW_PWR_WELL_KVMR);
613 if (WARN_ONCE(val & mask, "Clearing unexpected KVMR request for %s\n",
614 power_well->name))
615 I915_WRITE(HSW_PWR_WELL_KVMR, val & ~mask);
616
617 val = I915_READ(HSW_PWR_WELL_BIOS);
618 val |= I915_READ(HSW_PWR_WELL_DEBUG);
619
620 if (!(val & mask))
621 return;
622
623 /*
624 * DMC is known to force on the request bits for power well 1 on SKL
625 * and BXT and the misc IO power well on SKL but we don't expect any
626 * other request bits to be set, so WARN for those.
627 */
628 if (power_well_id == SKL_DISP_PW_1 ||
629 (IS_SKYLAKE(dev_priv) && power_well_id == SKL_DISP_PW_MISC_IO))
630 DRM_DEBUG_DRIVER("Clearing auxiliary requests for %s forced on "
631 "by DMC\n", power_well->name);
632 else
633 WARN_ONCE(1, "Clearing unexpected auxiliary requests for %s\n",
634 power_well->name);
635
636 I915_WRITE(HSW_PWR_WELL_BIOS, val & ~mask);
637 I915_WRITE(HSW_PWR_WELL_DEBUG, val & ~mask);
638}
639
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000640static void skl_set_power_well(struct drm_i915_private *dev_priv,
641 struct i915_power_well *power_well, bool enable)
642{
643 uint32_t tmp, fuse_status;
644 uint32_t req_mask, state_mask;
Damien Lespiau2a518352015-03-06 18:50:49 +0000645 bool is_enabled, enable_requested, check_fuse_status = false;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000646
647 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
648 fuse_status = I915_READ(SKL_FUSE_STATUS);
649
650 switch (power_well->data) {
651 case SKL_DISP_PW_1:
652 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
653 SKL_FUSE_PG0_DIST_STATUS), 1)) {
654 DRM_ERROR("PG0 not enabled\n");
655 return;
656 }
657 break;
658 case SKL_DISP_PW_2:
659 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
660 DRM_ERROR("PG1 in disabled state\n");
661 return;
662 }
663 break;
664 case SKL_DISP_PW_DDI_A_E:
665 case SKL_DISP_PW_DDI_B:
666 case SKL_DISP_PW_DDI_C:
667 case SKL_DISP_PW_DDI_D:
668 case SKL_DISP_PW_MISC_IO:
669 break;
670 default:
671 WARN(1, "Unknown power well %lu\n", power_well->data);
672 return;
673 }
674
675 req_mask = SKL_POWER_WELL_REQ(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000676 enable_requested = tmp & req_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000677 state_mask = SKL_POWER_WELL_STATE(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000678 is_enabled = tmp & state_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000679
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200680 if (!enable && enable_requested)
681 skl_power_well_pre_disable(dev_priv, power_well);
682
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000683 if (enable) {
Damien Lespiau2a518352015-03-06 18:50:49 +0000684 if (!enable_requested) {
Suketu Shahdc174302015-04-17 19:46:16 +0530685 WARN((tmp & state_mask) &&
686 !I915_READ(HSW_PWR_WELL_BIOS),
687 "Invalid for power well status to be enabled, unless done by the BIOS, \
688 when request is to disable!\n");
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000689 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000690 }
691
Damien Lespiau2a518352015-03-06 18:50:49 +0000692 if (!is_enabled) {
Damien Lespiau510e6fd2015-03-06 18:50:50 +0000693 DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000694 check_fuse_status = true;
695 }
696 } else {
Damien Lespiau2a518352015-03-06 18:50:49 +0000697 if (enable_requested) {
Imre Deak4a76f292015-11-04 19:24:15 +0200698 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
699 POSTING_READ(HSW_PWR_WELL_DRIVER);
700 DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000701 }
Imre Deakc6782b72016-04-05 13:26:05 +0300702
Imre Deak5f304c82016-04-15 22:32:58 +0300703 if (IS_GEN9(dev_priv))
Imre Deakc6782b72016-04-05 13:26:05 +0300704 gen9_sanitize_power_well_requests(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000705 }
706
Imre Deak1d963af2016-04-01 16:02:36 +0300707 if (wait_for(!!(I915_READ(HSW_PWR_WELL_DRIVER) & state_mask) == enable,
708 1))
709 DRM_ERROR("%s %s timeout\n",
710 power_well->name, enable ? "enable" : "disable");
711
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000712 if (check_fuse_status) {
713 if (power_well->data == SKL_DISP_PW_1) {
714 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
715 SKL_FUSE_PG1_DIST_STATUS), 1))
716 DRM_ERROR("PG1 distributing status timeout\n");
717 } else if (power_well->data == SKL_DISP_PW_2) {
718 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
719 SKL_FUSE_PG2_DIST_STATUS), 1))
720 DRM_ERROR("PG2 distributing status timeout\n");
721 }
722 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000723
724 if (enable && !is_enabled)
725 skl_power_well_post_enable(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000726}
727
Daniel Vetter9c065a72014-09-30 10:56:38 +0200728static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
729 struct i915_power_well *power_well)
730{
731 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
732
733 /*
734 * We're taking over the BIOS, so clear any requests made by it since
735 * the driver is in charge now.
736 */
737 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
738 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
739}
740
741static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
742 struct i915_power_well *power_well)
743{
744 hsw_set_power_well(dev_priv, power_well, true);
745}
746
747static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
748 struct i915_power_well *power_well)
749{
750 hsw_set_power_well(dev_priv, power_well, false);
751}
752
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000753static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
754 struct i915_power_well *power_well)
755{
756 uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
757 SKL_POWER_WELL_STATE(power_well->data);
758
759 return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
760}
761
762static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
763 struct i915_power_well *power_well)
764{
765 skl_set_power_well(dev_priv, power_well, power_well->count > 0);
766
767 /* Clear any request made by BIOS as driver is taking over */
768 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
769}
770
771static void skl_power_well_enable(struct drm_i915_private *dev_priv,
772 struct i915_power_well *power_well)
773{
774 skl_set_power_well(dev_priv, power_well, true);
775}
776
777static void skl_power_well_disable(struct drm_i915_private *dev_priv,
778 struct i915_power_well *power_well)
779{
780 skl_set_power_well(dev_priv, power_well, false);
781}
782
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100783static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
784 struct i915_power_well *power_well)
785{
786 return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
787}
788
789static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
790 struct i915_power_well *power_well)
791{
Imre Deak5b773eb2016-02-29 22:49:05 +0200792 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Imre Deakadc7f042016-04-04 17:27:10 +0300793
794 if (IS_BROXTON(dev_priv)) {
795 broxton_cdclk_verify_state(dev_priv);
796 broxton_ddi_phy_verify_state(dev_priv);
797 }
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100798}
799
800static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
801 struct i915_power_well *power_well)
802{
Imre Deakf74ed082016-04-18 14:48:21 +0300803 if (!dev_priv->csr.dmc_payload)
804 return;
805
Imre Deaka37baf32016-02-29 22:49:03 +0200806 if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC6)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100807 skl_enable_dc6(dev_priv);
Imre Deaka37baf32016-02-29 22:49:03 +0200808 else if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100809 gen9_enable_dc5(dev_priv);
810}
811
812static void gen9_dc_off_power_well_sync_hw(struct drm_i915_private *dev_priv,
813 struct i915_power_well *power_well)
814{
Imre Deaka37baf32016-02-29 22:49:03 +0200815 if (power_well->count > 0)
816 gen9_dc_off_power_well_enable(dev_priv, power_well);
817 else
818 gen9_dc_off_power_well_disable(dev_priv, power_well);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100819}
820
Daniel Vetter9c065a72014-09-30 10:56:38 +0200821static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
822 struct i915_power_well *power_well)
823{
824}
825
826static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
827 struct i915_power_well *power_well)
828{
829 return true;
830}
831
832static void vlv_set_power_well(struct drm_i915_private *dev_priv,
833 struct i915_power_well *power_well, bool enable)
834{
835 enum punit_power_well power_well_id = power_well->data;
836 u32 mask;
837 u32 state;
838 u32 ctrl;
839
840 mask = PUNIT_PWRGT_MASK(power_well_id);
841 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
842 PUNIT_PWRGT_PWR_GATE(power_well_id);
843
844 mutex_lock(&dev_priv->rps.hw_lock);
845
846#define COND \
847 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
848
849 if (COND)
850 goto out;
851
852 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
853 ctrl &= ~mask;
854 ctrl |= state;
855 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
856
857 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +0900858 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +0200859 state,
860 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
861
862#undef COND
863
864out:
865 mutex_unlock(&dev_priv->rps.hw_lock);
866}
867
868static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
869 struct i915_power_well *power_well)
870{
871 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
872}
873
874static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
875 struct i915_power_well *power_well)
876{
877 vlv_set_power_well(dev_priv, power_well, true);
878}
879
880static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
881 struct i915_power_well *power_well)
882{
883 vlv_set_power_well(dev_priv, power_well, false);
884}
885
886static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
887 struct i915_power_well *power_well)
888{
889 int power_well_id = power_well->data;
890 bool enabled = false;
891 u32 mask;
892 u32 state;
893 u32 ctrl;
894
895 mask = PUNIT_PWRGT_MASK(power_well_id);
896 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
897
898 mutex_lock(&dev_priv->rps.hw_lock);
899
900 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
901 /*
902 * We only ever set the power-on and power-gate states, anything
903 * else is unexpected.
904 */
905 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
906 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
907 if (state == ctrl)
908 enabled = true;
909
910 /*
911 * A transient state at this point would mean some unexpected party
912 * is poking at the power controls too.
913 */
914 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
915 WARN_ON(ctrl != state);
916
917 mutex_unlock(&dev_priv->rps.hw_lock);
918
919 return enabled;
920}
921
Ville Syrjälä766078d2016-04-11 16:56:30 +0300922static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
923{
924 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
925
926 /*
927 * Disable trickle feed and enable pnd deadline calculation
928 */
929 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
930 I915_WRITE(CBR1_VLV, 0);
931}
932
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300933static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200934{
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300935 enum pipe pipe;
936
937 /*
938 * Enable the CRI clock source so we can get at the
939 * display and the reference clock for VGA
940 * hotplug / manual detection. Supposedly DSI also
941 * needs the ref clock up and running.
942 *
943 * CHV DPLL B/C have some issues if VGA mode is enabled.
944 */
945 for_each_pipe(dev_priv->dev, pipe) {
946 u32 val = I915_READ(DPLL(pipe));
947
948 val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
949 if (pipe != PIPE_A)
950 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
951
952 I915_WRITE(DPLL(pipe), val);
953 }
Daniel Vetter9c065a72014-09-30 10:56:38 +0200954
Ville Syrjälä766078d2016-04-11 16:56:30 +0300955 vlv_init_display_clock_gating(dev_priv);
956
Daniel Vetter9c065a72014-09-30 10:56:38 +0200957 spin_lock_irq(&dev_priv->irq_lock);
958 valleyview_enable_display_irqs(dev_priv);
959 spin_unlock_irq(&dev_priv->irq_lock);
960
961 /*
962 * During driver initialization/resume we can avoid restoring the
963 * part of the HW/SW state that will be inited anyway explicitly.
964 */
965 if (dev_priv->power_domains.initializing)
966 return;
967
Daniel Vetterb9632912014-09-30 10:56:44 +0200968 intel_hpd_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200969
970 i915_redisable_vga_power_on(dev_priv->dev);
971}
972
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300973static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
974{
975 spin_lock_irq(&dev_priv->irq_lock);
976 valleyview_disable_display_irqs(dev_priv);
977 spin_unlock_irq(&dev_priv->irq_lock);
978
Ville Syrjälä2230fde2016-02-19 18:41:52 +0200979 /* make sure we're done processing display irqs */
980 synchronize_irq(dev_priv->dev->irq);
981
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300982 vlv_power_sequencer_reset(dev_priv);
983}
984
985static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
986 struct i915_power_well *power_well)
987{
988 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
989
990 vlv_set_power_well(dev_priv, power_well, true);
991
992 vlv_display_power_well_init(dev_priv);
993}
994
Daniel Vetter9c065a72014-09-30 10:56:38 +0200995static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
996 struct i915_power_well *power_well)
997{
998 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
999
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001000 vlv_display_power_well_deinit(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001001
1002 vlv_set_power_well(dev_priv, power_well, false);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001003}
1004
1005static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1006 struct i915_power_well *power_well)
1007{
1008 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1009
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001010 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001011 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1012
1013 vlv_set_power_well(dev_priv, power_well, true);
1014
1015 /*
1016 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1017 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1018 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1019 * b. The other bits such as sfr settings / modesel may all
1020 * be set to 0.
1021 *
1022 * This should only be done on init and resume from S3 with
1023 * both PLLs disabled, or we risk losing DPIO and PLL
1024 * synchronization.
1025 */
1026 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1027}
1028
1029static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1030 struct i915_power_well *power_well)
1031{
1032 enum pipe pipe;
1033
1034 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1035
1036 for_each_pipe(dev_priv, pipe)
1037 assert_pll_disabled(dev_priv, pipe);
1038
1039 /* Assert common reset */
1040 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1041
1042 vlv_set_power_well(dev_priv, power_well, false);
1043}
1044
Ville Syrjälä30142272015-07-08 23:46:01 +03001045#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
1046
1047static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1048 int power_well_id)
1049{
1050 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Ville Syrjälä30142272015-07-08 23:46:01 +03001051 int i;
1052
Imre Deakfc17f222015-11-04 19:24:11 +02001053 for (i = 0; i < power_domains->power_well_count; i++) {
1054 struct i915_power_well *power_well;
1055
1056 power_well = &power_domains->power_wells[i];
Ville Syrjälä30142272015-07-08 23:46:01 +03001057 if (power_well->data == power_well_id)
1058 return power_well;
1059 }
1060
1061 return NULL;
1062}
1063
1064#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1065
1066static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1067{
1068 struct i915_power_well *cmn_bc =
1069 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1070 struct i915_power_well *cmn_d =
1071 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1072 u32 phy_control = dev_priv->chv_phy_control;
1073 u32 phy_status = 0;
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001074 u32 phy_status_mask = 0xffffffff;
Ville Syrjälä30142272015-07-08 23:46:01 +03001075 u32 tmp;
1076
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001077 /*
1078 * The BIOS can leave the PHY is some weird state
1079 * where it doesn't fully power down some parts.
1080 * Disable the asserts until the PHY has been fully
1081 * reset (ie. the power well has been disabled at
1082 * least once).
1083 */
1084 if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1085 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1086 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1087 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1088 PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1089 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1090 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1091
1092 if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1093 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1094 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1095 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1096
Ville Syrjälä30142272015-07-08 23:46:01 +03001097 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1098 phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1099
1100 /* this assumes override is only used to enable lanes */
1101 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1102 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1103
1104 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1105 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1106
1107 /* CL1 is on whenever anything is on in either channel */
1108 if (BITS_SET(phy_control,
1109 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1110 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1111 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1112
1113 /*
1114 * The DPLLB check accounts for the pipe B + port A usage
1115 * with CL2 powered up but all the lanes in the second channel
1116 * powered down.
1117 */
1118 if (BITS_SET(phy_control,
1119 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1120 (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1121 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1122
1123 if (BITS_SET(phy_control,
1124 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1125 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1126 if (BITS_SET(phy_control,
1127 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1128 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1129
1130 if (BITS_SET(phy_control,
1131 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1132 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1133 if (BITS_SET(phy_control,
1134 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1135 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1136 }
1137
1138 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1139 phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1140
1141 /* this assumes override is only used to enable lanes */
1142 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1143 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1144
1145 if (BITS_SET(phy_control,
1146 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1147 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1148
1149 if (BITS_SET(phy_control,
1150 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1151 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1152 if (BITS_SET(phy_control,
1153 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1154 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1155 }
1156
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001157 phy_status &= phy_status_mask;
1158
Ville Syrjälä30142272015-07-08 23:46:01 +03001159 /*
1160 * The PHY may be busy with some initial calibration and whatnot,
1161 * so the power state can take a while to actually change.
1162 */
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001163 if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
Ville Syrjälä30142272015-07-08 23:46:01 +03001164 WARN(phy_status != tmp,
1165 "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1166 tmp, phy_status, dev_priv->chv_phy_control);
1167}
1168
1169#undef BITS_SET
1170
Daniel Vetter9c065a72014-09-30 10:56:38 +02001171static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1172 struct i915_power_well *power_well)
1173{
1174 enum dpio_phy phy;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001175 enum pipe pipe;
1176 uint32_t tmp;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001177
1178 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1179 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1180
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001181 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1182 pipe = PIPE_A;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001183 phy = DPIO_PHY0;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001184 } else {
1185 pipe = PIPE_C;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001186 phy = DPIO_PHY1;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001187 }
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001188
1189 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001190 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1191 vlv_set_power_well(dev_priv, power_well, true);
1192
1193 /* Poll for phypwrgood signal */
1194 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
1195 DRM_ERROR("Display PHY %d is not power up\n", phy);
1196
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001197 mutex_lock(&dev_priv->sb_lock);
1198
1199 /* Enable dynamic power down */
1200 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
Ville Syrjäläee279212015-07-08 23:45:57 +03001201 tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1202 DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001203 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1204
1205 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1206 tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1207 tmp |= DPIO_DYNPWRDOWNEN_CH1;
1208 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
Ville Syrjälä3e288782015-07-08 23:45:58 +03001209 } else {
1210 /*
1211 * Force the non-existing CL2 off. BXT does this
1212 * too, so maybe it saves some power even though
1213 * CL2 doesn't exist?
1214 */
1215 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1216 tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1217 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001218 }
1219
1220 mutex_unlock(&dev_priv->sb_lock);
1221
Ville Syrjälä70722462015-04-10 18:21:28 +03001222 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1223 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001224
1225 DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1226 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001227
1228 assert_chv_phy_status(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001229}
1230
1231static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1232 struct i915_power_well *power_well)
1233{
1234 enum dpio_phy phy;
1235
1236 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1237 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1238
1239 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1240 phy = DPIO_PHY0;
1241 assert_pll_disabled(dev_priv, PIPE_A);
1242 assert_pll_disabled(dev_priv, PIPE_B);
1243 } else {
1244 phy = DPIO_PHY1;
1245 assert_pll_disabled(dev_priv, PIPE_C);
1246 }
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);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001250
1251 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001252
1253 DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1254 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001255
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001256 /* PHY is fully reset now, so we can enable the PHY state asserts */
1257 dev_priv->chv_phy_assert[phy] = true;
1258
Ville Syrjälä30142272015-07-08 23:46:01 +03001259 assert_chv_phy_status(dev_priv);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001260}
1261
Ville Syrjälä6669e392015-07-08 23:46:00 +03001262static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1263 enum dpio_channel ch, bool override, unsigned int mask)
1264{
1265 enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1266 u32 reg, val, expected, actual;
1267
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001268 /*
1269 * The BIOS can leave the PHY is some weird state
1270 * where it doesn't fully power down some parts.
1271 * Disable the asserts until the PHY has been fully
1272 * reset (ie. the power well has been disabled at
1273 * least once).
1274 */
1275 if (!dev_priv->chv_phy_assert[phy])
1276 return;
1277
Ville Syrjälä6669e392015-07-08 23:46:00 +03001278 if (ch == DPIO_CH0)
1279 reg = _CHV_CMN_DW0_CH0;
1280 else
1281 reg = _CHV_CMN_DW6_CH1;
1282
1283 mutex_lock(&dev_priv->sb_lock);
1284 val = vlv_dpio_read(dev_priv, pipe, reg);
1285 mutex_unlock(&dev_priv->sb_lock);
1286
1287 /*
1288 * This assumes !override is only used when the port is disabled.
1289 * All lanes should power down even without the override when
1290 * the port is disabled.
1291 */
1292 if (!override || mask == 0xf) {
1293 expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1294 /*
1295 * If CH1 common lane is not active anymore
1296 * (eg. for pipe B DPLL) the entire channel will
1297 * shut down, which causes the common lane registers
1298 * to read as 0. That means we can't actually check
1299 * the lane power down status bits, but as the entire
1300 * register reads as 0 it's a good indication that the
1301 * channel is indeed entirely powered down.
1302 */
1303 if (ch == DPIO_CH1 && val == 0)
1304 expected = 0;
1305 } else if (mask != 0x0) {
1306 expected = DPIO_ANYDL_POWERDOWN;
1307 } else {
1308 expected = 0;
1309 }
1310
1311 if (ch == DPIO_CH0)
1312 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1313 else
1314 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1315 actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1316
1317 WARN(actual != expected,
1318 "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1319 !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1320 !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1321 reg, val);
1322}
1323
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001324bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1325 enum dpio_channel ch, bool override)
1326{
1327 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1328 bool was_override;
1329
1330 mutex_lock(&power_domains->lock);
1331
1332 was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1333
1334 if (override == was_override)
1335 goto out;
1336
1337 if (override)
1338 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1339 else
1340 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1341
1342 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1343
1344 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1345 phy, ch, dev_priv->chv_phy_control);
1346
Ville Syrjälä30142272015-07-08 23:46:01 +03001347 assert_chv_phy_status(dev_priv);
1348
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001349out:
1350 mutex_unlock(&power_domains->lock);
1351
1352 return was_override;
1353}
1354
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001355void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1356 bool override, unsigned int mask)
1357{
1358 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1359 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1360 enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1361 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1362
1363 mutex_lock(&power_domains->lock);
1364
1365 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1366 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1367
1368 if (override)
1369 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1370 else
1371 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1372
1373 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1374
1375 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1376 phy, ch, mask, dev_priv->chv_phy_control);
1377
Ville Syrjälä30142272015-07-08 23:46:01 +03001378 assert_chv_phy_status(dev_priv);
1379
Ville Syrjälä6669e392015-07-08 23:46:00 +03001380 assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1381
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001382 mutex_unlock(&power_domains->lock);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001383}
1384
1385static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1386 struct i915_power_well *power_well)
1387{
1388 enum pipe pipe = power_well->data;
1389 bool enabled;
1390 u32 state, ctrl;
1391
1392 mutex_lock(&dev_priv->rps.hw_lock);
1393
1394 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1395 /*
1396 * We only ever set the power-on and power-gate states, anything
1397 * else is unexpected.
1398 */
1399 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1400 enabled = state == DP_SSS_PWR_ON(pipe);
1401
1402 /*
1403 * A transient state at this point would mean some unexpected party
1404 * is poking at the power controls too.
1405 */
1406 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1407 WARN_ON(ctrl << 16 != state);
1408
1409 mutex_unlock(&dev_priv->rps.hw_lock);
1410
1411 return enabled;
1412}
1413
1414static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1415 struct i915_power_well *power_well,
1416 bool enable)
1417{
1418 enum pipe pipe = power_well->data;
1419 u32 state;
1420 u32 ctrl;
1421
1422 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1423
1424 mutex_lock(&dev_priv->rps.hw_lock);
1425
1426#define COND \
1427 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1428
1429 if (COND)
1430 goto out;
1431
1432 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1433 ctrl &= ~DP_SSC_MASK(pipe);
1434 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1435 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1436
1437 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +09001438 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001439 state,
1440 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1441
1442#undef COND
1443
1444out:
1445 mutex_unlock(&dev_priv->rps.hw_lock);
1446}
1447
1448static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1449 struct i915_power_well *power_well)
1450{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001451 WARN_ON_ONCE(power_well->data != PIPE_A);
1452
Daniel Vetter9c065a72014-09-30 10:56:38 +02001453 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1454}
1455
1456static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1457 struct i915_power_well *power_well)
1458{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001459 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001460
1461 chv_set_pipe_power_well(dev_priv, power_well, true);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001462
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001463 vlv_display_power_well_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001464}
1465
1466static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1467 struct i915_power_well *power_well)
1468{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001469 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001470
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001471 vlv_display_power_well_deinit(dev_priv);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001472
Daniel Vetter9c065a72014-09-30 10:56:38 +02001473 chv_set_pipe_power_well(dev_priv, power_well, false);
1474}
1475
Imre Deak09731282016-02-17 14:17:42 +02001476static void
1477__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1478 enum intel_display_power_domain domain)
1479{
1480 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1481 struct i915_power_well *power_well;
1482 int i;
1483
1484 for_each_power_well(i, power_well, BIT(domain), power_domains) {
1485 if (!power_well->count++)
1486 intel_power_well_enable(dev_priv, power_well);
1487 }
1488
1489 power_domains->domain_use_count[domain]++;
1490}
1491
Daniel Vettere4e76842014-09-30 10:56:42 +02001492/**
1493 * intel_display_power_get - grab a power domain reference
1494 * @dev_priv: i915 device instance
1495 * @domain: power domain to reference
1496 *
1497 * This function grabs a power domain reference for @domain and ensures that the
1498 * power domain and all its parents are powered up. Therefore users should only
1499 * grab a reference to the innermost power domain they need.
1500 *
1501 * Any power domain reference obtained by this function must have a symmetric
1502 * call to intel_display_power_put() to release the reference again.
1503 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001504void intel_display_power_get(struct drm_i915_private *dev_priv,
1505 enum intel_display_power_domain domain)
1506{
Imre Deak09731282016-02-17 14:17:42 +02001507 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001508
1509 intel_runtime_pm_get(dev_priv);
1510
Imre Deak09731282016-02-17 14:17:42 +02001511 mutex_lock(&power_domains->lock);
1512
1513 __intel_display_power_get_domain(dev_priv, domain);
1514
1515 mutex_unlock(&power_domains->lock);
1516}
1517
1518/**
1519 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
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 */
1530bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1531 enum intel_display_power_domain domain)
1532{
1533 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1534 bool is_enabled;
1535
1536 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1537 return false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001538
1539 mutex_lock(&power_domains->lock);
1540
Imre Deak09731282016-02-17 14:17:42 +02001541 if (__intel_display_power_is_enabled(dev_priv, domain)) {
1542 __intel_display_power_get_domain(dev_priv, domain);
1543 is_enabled = true;
1544 } else {
1545 is_enabled = false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001546 }
1547
Daniel Vetter9c065a72014-09-30 10:56:38 +02001548 mutex_unlock(&power_domains->lock);
Imre Deak09731282016-02-17 14:17:42 +02001549
1550 if (!is_enabled)
1551 intel_runtime_pm_put(dev_priv);
1552
1553 return is_enabled;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001554}
1555
Daniel Vettere4e76842014-09-30 10:56:42 +02001556/**
1557 * intel_display_power_put - release a power domain reference
1558 * @dev_priv: i915 device instance
1559 * @domain: power domain to reference
1560 *
1561 * This function drops the power domain reference obtained by
1562 * intel_display_power_get() and might power down the corresponding hardware
1563 * block right away if this is the last reference.
1564 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001565void intel_display_power_put(struct drm_i915_private *dev_priv,
1566 enum intel_display_power_domain domain)
1567{
1568 struct i915_power_domains *power_domains;
1569 struct i915_power_well *power_well;
1570 int i;
1571
1572 power_domains = &dev_priv->power_domains;
1573
1574 mutex_lock(&power_domains->lock);
1575
Daniel Stone11c86db2015-11-20 15:55:34 +00001576 WARN(!power_domains->domain_use_count[domain],
1577 "Use count on domain %s is already zero\n",
1578 intel_display_power_domain_str(domain));
Daniel Vetter9c065a72014-09-30 10:56:38 +02001579 power_domains->domain_use_count[domain]--;
1580
1581 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Daniel Stone11c86db2015-11-20 15:55:34 +00001582 WARN(!power_well->count,
1583 "Use count on power well %s is already zero",
1584 power_well->name);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001585
Imre Deakd314cd42015-11-17 17:44:23 +02001586 if (!--power_well->count)
Damien Lespiaudcddab32015-07-30 18:20:27 -03001587 intel_power_well_disable(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001588 }
1589
1590 mutex_unlock(&power_domains->lock);
1591
1592 intel_runtime_pm_put(dev_priv);
1593}
1594
Daniel Vetter9c065a72014-09-30 10:56:38 +02001595#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
1596 BIT(POWER_DOMAIN_PIPE_A) | \
1597 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001598 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
1599 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1600 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1601 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001602 BIT(POWER_DOMAIN_PORT_CRT) | \
1603 BIT(POWER_DOMAIN_PLLS) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001604 BIT(POWER_DOMAIN_AUX_A) | \
1605 BIT(POWER_DOMAIN_AUX_B) | \
1606 BIT(POWER_DOMAIN_AUX_C) | \
1607 BIT(POWER_DOMAIN_AUX_D) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01001608 BIT(POWER_DOMAIN_GMBUS) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001609 BIT(POWER_DOMAIN_INIT))
1610#define HSW_DISPLAY_POWER_DOMAINS ( \
1611 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
1612 BIT(POWER_DOMAIN_INIT))
1613
1614#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
1615 HSW_ALWAYS_ON_POWER_DOMAINS | \
1616 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
1617#define BDW_DISPLAY_POWER_DOMAINS ( \
1618 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
1619 BIT(POWER_DOMAIN_INIT))
1620
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001621#define VLV_DISPLAY_POWER_DOMAINS ( \
1622 BIT(POWER_DOMAIN_PIPE_A) | \
1623 BIT(POWER_DOMAIN_PIPE_B) | \
1624 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1625 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1626 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1627 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1628 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1629 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1630 BIT(POWER_DOMAIN_PORT_DSI) | \
1631 BIT(POWER_DOMAIN_PORT_CRT) | \
1632 BIT(POWER_DOMAIN_VGA) | \
1633 BIT(POWER_DOMAIN_AUDIO) | \
1634 BIT(POWER_DOMAIN_AUX_B) | \
1635 BIT(POWER_DOMAIN_AUX_C) | \
1636 BIT(POWER_DOMAIN_GMBUS) | \
1637 BIT(POWER_DOMAIN_INIT))
Daniel Vetter9c065a72014-09-30 10:56:38 +02001638
1639#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001640 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1641 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001642 BIT(POWER_DOMAIN_PORT_CRT) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001643 BIT(POWER_DOMAIN_AUX_B) | \
1644 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001645 BIT(POWER_DOMAIN_INIT))
1646
1647#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001648 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001649 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001650 BIT(POWER_DOMAIN_INIT))
1651
1652#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001653 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001654 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001655 BIT(POWER_DOMAIN_INIT))
1656
1657#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001658 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001659 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001660 BIT(POWER_DOMAIN_INIT))
1661
1662#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001663 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001664 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001665 BIT(POWER_DOMAIN_INIT))
1666
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001667#define CHV_DISPLAY_POWER_DOMAINS ( \
1668 BIT(POWER_DOMAIN_PIPE_A) | \
1669 BIT(POWER_DOMAIN_PIPE_B) | \
1670 BIT(POWER_DOMAIN_PIPE_C) | \
1671 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1672 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1673 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1674 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1675 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1676 BIT(POWER_DOMAIN_TRANSCODER_C) | \
1677 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1678 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1679 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
1680 BIT(POWER_DOMAIN_PORT_DSI) | \
1681 BIT(POWER_DOMAIN_VGA) | \
1682 BIT(POWER_DOMAIN_AUDIO) | \
1683 BIT(POWER_DOMAIN_AUX_B) | \
1684 BIT(POWER_DOMAIN_AUX_C) | \
1685 BIT(POWER_DOMAIN_AUX_D) | \
1686 BIT(POWER_DOMAIN_GMBUS) | \
1687 BIT(POWER_DOMAIN_INIT))
1688
Daniel Vetter9c065a72014-09-30 10:56:38 +02001689#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001690 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1691 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001692 BIT(POWER_DOMAIN_AUX_B) | \
1693 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001694 BIT(POWER_DOMAIN_INIT))
1695
1696#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001697 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001698 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001699 BIT(POWER_DOMAIN_INIT))
1700
Daniel Vetter9c065a72014-09-30 10:56:38 +02001701static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1702 .sync_hw = i9xx_always_on_power_well_noop,
1703 .enable = i9xx_always_on_power_well_noop,
1704 .disable = i9xx_always_on_power_well_noop,
1705 .is_enabled = i9xx_always_on_power_well_enabled,
1706};
1707
1708static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1709 .sync_hw = chv_pipe_power_well_sync_hw,
1710 .enable = chv_pipe_power_well_enable,
1711 .disable = chv_pipe_power_well_disable,
1712 .is_enabled = chv_pipe_power_well_enabled,
1713};
1714
1715static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1716 .sync_hw = vlv_power_well_sync_hw,
1717 .enable = chv_dpio_cmn_power_well_enable,
1718 .disable = chv_dpio_cmn_power_well_disable,
1719 .is_enabled = vlv_power_well_enabled,
1720};
1721
1722static struct i915_power_well i9xx_always_on_power_well[] = {
1723 {
1724 .name = "always-on",
1725 .always_on = 1,
1726 .domains = POWER_DOMAIN_MASK,
1727 .ops = &i9xx_always_on_power_well_ops,
1728 },
1729};
1730
1731static const struct i915_power_well_ops hsw_power_well_ops = {
1732 .sync_hw = hsw_power_well_sync_hw,
1733 .enable = hsw_power_well_enable,
1734 .disable = hsw_power_well_disable,
1735 .is_enabled = hsw_power_well_enabled,
1736};
1737
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001738static const struct i915_power_well_ops skl_power_well_ops = {
1739 .sync_hw = skl_power_well_sync_hw,
1740 .enable = skl_power_well_enable,
1741 .disable = skl_power_well_disable,
1742 .is_enabled = skl_power_well_enabled,
1743};
1744
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001745static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1746 .sync_hw = gen9_dc_off_power_well_sync_hw,
1747 .enable = gen9_dc_off_power_well_enable,
1748 .disable = gen9_dc_off_power_well_disable,
1749 .is_enabled = gen9_dc_off_power_well_enabled,
1750};
1751
Daniel Vetter9c065a72014-09-30 10:56:38 +02001752static struct i915_power_well hsw_power_wells[] = {
1753 {
1754 .name = "always-on",
1755 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001756 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001757 .ops = &i9xx_always_on_power_well_ops,
1758 },
1759 {
1760 .name = "display",
1761 .domains = HSW_DISPLAY_POWER_DOMAINS,
1762 .ops = &hsw_power_well_ops,
1763 },
1764};
1765
1766static struct i915_power_well bdw_power_wells[] = {
1767 {
1768 .name = "always-on",
1769 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001770 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001771 .ops = &i9xx_always_on_power_well_ops,
1772 },
1773 {
1774 .name = "display",
1775 .domains = BDW_DISPLAY_POWER_DOMAINS,
1776 .ops = &hsw_power_well_ops,
1777 },
1778};
1779
1780static const struct i915_power_well_ops vlv_display_power_well_ops = {
1781 .sync_hw = vlv_power_well_sync_hw,
1782 .enable = vlv_display_power_well_enable,
1783 .disable = vlv_display_power_well_disable,
1784 .is_enabled = vlv_power_well_enabled,
1785};
1786
1787static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1788 .sync_hw = vlv_power_well_sync_hw,
1789 .enable = vlv_dpio_cmn_power_well_enable,
1790 .disable = vlv_dpio_cmn_power_well_disable,
1791 .is_enabled = vlv_power_well_enabled,
1792};
1793
1794static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1795 .sync_hw = vlv_power_well_sync_hw,
1796 .enable = vlv_power_well_enable,
1797 .disable = vlv_power_well_disable,
1798 .is_enabled = vlv_power_well_enabled,
1799};
1800
1801static struct i915_power_well vlv_power_wells[] = {
1802 {
1803 .name = "always-on",
1804 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001805 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001806 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001807 .data = PUNIT_POWER_WELL_ALWAYS_ON,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001808 },
1809 {
1810 .name = "display",
1811 .domains = VLV_DISPLAY_POWER_DOMAINS,
1812 .data = PUNIT_POWER_WELL_DISP2D,
1813 .ops = &vlv_display_power_well_ops,
1814 },
1815 {
1816 .name = "dpio-tx-b-01",
1817 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1818 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1819 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1820 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1821 .ops = &vlv_dpio_power_well_ops,
1822 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1823 },
1824 {
1825 .name = "dpio-tx-b-23",
1826 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1827 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1828 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1829 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1830 .ops = &vlv_dpio_power_well_ops,
1831 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1832 },
1833 {
1834 .name = "dpio-tx-c-01",
1835 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1836 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1837 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1838 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1839 .ops = &vlv_dpio_power_well_ops,
1840 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1841 },
1842 {
1843 .name = "dpio-tx-c-23",
1844 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1845 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1846 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1847 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1848 .ops = &vlv_dpio_power_well_ops,
1849 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1850 },
1851 {
1852 .name = "dpio-common",
1853 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1854 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1855 .ops = &vlv_dpio_cmn_power_well_ops,
1856 },
1857};
1858
1859static struct i915_power_well chv_power_wells[] = {
1860 {
1861 .name = "always-on",
1862 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001863 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001864 .ops = &i9xx_always_on_power_well_ops,
1865 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001866 {
1867 .name = "display",
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001868 /*
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001869 * Pipe A power well is the new disp2d well. Pipe B and C
1870 * power wells don't actually exist. Pipe A power well is
1871 * required for any pipe to work.
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001872 */
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001873 .domains = CHV_DISPLAY_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001874 .data = PIPE_A,
1875 .ops = &chv_pipe_power_well_ops,
1876 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001877 {
1878 .name = "dpio-common-bc",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001879 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001880 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1881 .ops = &chv_dpio_cmn_power_well_ops,
1882 },
1883 {
1884 .name = "dpio-common-d",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001885 .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001886 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
1887 .ops = &chv_dpio_cmn_power_well_ops,
1888 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001889};
1890
Suketu Shah5aefb232015-04-16 14:22:10 +05301891bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
1892 int power_well_id)
1893{
1894 struct i915_power_well *power_well;
1895 bool ret;
1896
1897 power_well = lookup_power_well(dev_priv, power_well_id);
1898 ret = power_well->ops->is_enabled(dev_priv, power_well);
1899
1900 return ret;
1901}
1902
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001903static struct i915_power_well skl_power_wells[] = {
1904 {
1905 .name = "always-on",
1906 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001907 .domains = POWER_DOMAIN_MASK,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001908 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001909 .data = SKL_DISP_PW_ALWAYS_ON,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001910 },
1911 {
1912 .name = "power well 1",
Imre Deak4a76f292015-11-04 19:24:15 +02001913 /* Handled by the DMC firmware */
1914 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001915 .ops = &skl_power_well_ops,
1916 .data = SKL_DISP_PW_1,
1917 },
1918 {
1919 .name = "MISC IO power well",
Imre Deak4a76f292015-11-04 19:24:15 +02001920 /* Handled by the DMC firmware */
1921 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001922 .ops = &skl_power_well_ops,
1923 .data = SKL_DISP_PW_MISC_IO,
1924 },
1925 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001926 .name = "DC off",
1927 .domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
1928 .ops = &gen9_dc_off_power_well_ops,
1929 .data = SKL_DISP_PW_DC_OFF,
1930 },
1931 {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001932 .name = "power well 2",
1933 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1934 .ops = &skl_power_well_ops,
1935 .data = SKL_DISP_PW_2,
1936 },
1937 {
1938 .name = "DDI A/E power well",
1939 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1940 .ops = &skl_power_well_ops,
1941 .data = SKL_DISP_PW_DDI_A_E,
1942 },
1943 {
1944 .name = "DDI B power well",
1945 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1946 .ops = &skl_power_well_ops,
1947 .data = SKL_DISP_PW_DDI_B,
1948 },
1949 {
1950 .name = "DDI C power well",
1951 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1952 .ops = &skl_power_well_ops,
1953 .data = SKL_DISP_PW_DDI_C,
1954 },
1955 {
1956 .name = "DDI D power well",
1957 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1958 .ops = &skl_power_well_ops,
1959 .data = SKL_DISP_PW_DDI_D,
1960 },
1961};
1962
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301963static struct i915_power_well bxt_power_wells[] = {
1964 {
1965 .name = "always-on",
1966 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001967 .domains = POWER_DOMAIN_MASK,
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301968 .ops = &i9xx_always_on_power_well_ops,
1969 },
1970 {
1971 .name = "power well 1",
Imre Deakd7d7c9e2016-04-01 16:02:42 +03001972 .domains = 0,
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301973 .ops = &skl_power_well_ops,
1974 .data = SKL_DISP_PW_1,
1975 },
1976 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001977 .name = "DC off",
1978 .domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
1979 .ops = &gen9_dc_off_power_well_ops,
1980 .data = SKL_DISP_PW_DC_OFF,
1981 },
1982 {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301983 .name = "power well 2",
1984 .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1985 .ops = &skl_power_well_ops,
1986 .data = SKL_DISP_PW_2,
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001987 },
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301988};
1989
Imre Deak1b0e3a02015-11-05 23:04:11 +02001990static int
1991sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
1992 int disable_power_well)
1993{
1994 if (disable_power_well >= 0)
1995 return !!disable_power_well;
1996
Imre Deak1b0e3a02015-11-05 23:04:11 +02001997 return 1;
1998}
1999
Imre Deaka37baf32016-02-29 22:49:03 +02002000static uint32_t get_allowed_dc_mask(const struct drm_i915_private *dev_priv,
2001 int enable_dc)
2002{
2003 uint32_t mask;
2004 int requested_dc;
2005 int max_dc;
2006
2007 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
2008 max_dc = 2;
2009 mask = 0;
2010 } else if (IS_BROXTON(dev_priv)) {
2011 max_dc = 1;
2012 /*
2013 * DC9 has a separate HW flow from the rest of the DC states,
2014 * not depending on the DMC firmware. It's needed by system
2015 * suspend/resume, so allow it unconditionally.
2016 */
2017 mask = DC_STATE_EN_DC9;
2018 } else {
2019 max_dc = 0;
2020 mask = 0;
2021 }
2022
Imre Deak66e2c4c2016-02-29 22:49:04 +02002023 if (!i915.disable_power_well)
2024 max_dc = 0;
2025
Imre Deaka37baf32016-02-29 22:49:03 +02002026 if (enable_dc >= 0 && enable_dc <= max_dc) {
2027 requested_dc = enable_dc;
2028 } else if (enable_dc == -1) {
2029 requested_dc = max_dc;
2030 } else if (enable_dc > max_dc && enable_dc <= 2) {
2031 DRM_DEBUG_KMS("Adjusting requested max DC state (%d->%d)\n",
2032 enable_dc, max_dc);
2033 requested_dc = max_dc;
2034 } else {
2035 DRM_ERROR("Unexpected value for enable_dc (%d)\n", enable_dc);
2036 requested_dc = max_dc;
2037 }
2038
2039 if (requested_dc > 1)
2040 mask |= DC_STATE_EN_UPTO_DC6;
2041 if (requested_dc > 0)
2042 mask |= DC_STATE_EN_UPTO_DC5;
2043
2044 DRM_DEBUG_KMS("Allowed DC state mask %02x\n", mask);
2045
2046 return mask;
2047}
2048
Daniel Vetter9c065a72014-09-30 10:56:38 +02002049#define set_power_wells(power_domains, __power_wells) ({ \
2050 (power_domains)->power_wells = (__power_wells); \
2051 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
2052})
2053
Daniel Vettere4e76842014-09-30 10:56:42 +02002054/**
2055 * intel_power_domains_init - initializes the power domain structures
2056 * @dev_priv: i915 device instance
2057 *
2058 * Initializes the power domain structures for @dev_priv depending upon the
2059 * supported platform.
2060 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002061int intel_power_domains_init(struct drm_i915_private *dev_priv)
2062{
2063 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2064
Imre Deak1b0e3a02015-11-05 23:04:11 +02002065 i915.disable_power_well = sanitize_disable_power_well_option(dev_priv,
2066 i915.disable_power_well);
Imre Deaka37baf32016-02-29 22:49:03 +02002067 dev_priv->csr.allowed_dc_mask = get_allowed_dc_mask(dev_priv,
2068 i915.enable_dc);
Imre Deak1b0e3a02015-11-05 23:04:11 +02002069
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01002070 BUILD_BUG_ON(POWER_DOMAIN_NUM > 31);
2071
Daniel Vetter9c065a72014-09-30 10:56:38 +02002072 mutex_init(&power_domains->lock);
2073
2074 /*
2075 * The enabling order will be from lower to higher indexed wells,
2076 * the disabling order is reversed.
2077 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002078 if (IS_HASWELL(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002079 set_power_wells(power_domains, hsw_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002080 } else if (IS_BROADWELL(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002081 set_power_wells(power_domains, bdw_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002082 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002083 set_power_wells(power_domains, skl_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002084 } else if (IS_BROXTON(dev_priv)) {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302085 set_power_wells(power_domains, bxt_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002086 } else if (IS_CHERRYVIEW(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002087 set_power_wells(power_domains, chv_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002088 } else if (IS_VALLEYVIEW(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002089 set_power_wells(power_domains, vlv_power_wells);
2090 } else {
2091 set_power_wells(power_domains, i9xx_always_on_power_well);
2092 }
2093
2094 return 0;
2095}
2096
Daniel Vettere4e76842014-09-30 10:56:42 +02002097/**
2098 * intel_power_domains_fini - finalizes the power domain structures
2099 * @dev_priv: i915 device instance
2100 *
2101 * Finalizes the power domain structures for @dev_priv depending upon the
2102 * supported platform. This function also disables runtime pm and ensures that
2103 * the device stays powered up so that the driver can be reloaded.
2104 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002105void intel_power_domains_fini(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002106{
Imre Deak25b181b2015-12-17 13:44:56 +02002107 struct device *device = &dev_priv->dev->pdev->dev;
2108
Imre Deakaabee1b2015-12-15 20:10:29 +02002109 /*
2110 * The i915.ko module is still not prepared to be loaded when
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002111 * the power well is not enabled, so just enable it in case
Imre Deakaabee1b2015-12-15 20:10:29 +02002112 * we're going to unload/reload.
2113 * The following also reacquires the RPM reference the core passed
2114 * to the driver during loading, which is dropped in
2115 * intel_runtime_pm_enable(). We have to hand back the control of the
2116 * device to the core with this reference held.
2117 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002118 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002119
2120 /* Remove the refcount we took to keep power well support disabled. */
2121 if (!i915.disable_power_well)
2122 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak25b181b2015-12-17 13:44:56 +02002123
2124 /*
2125 * Remove the refcount we took in intel_runtime_pm_enable() in case
2126 * the platform doesn't support runtime PM.
2127 */
2128 if (!HAS_RUNTIME_PM(dev_priv))
2129 pm_runtime_put(device);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002130}
2131
Imre Deak30eade12015-11-04 19:24:13 +02002132static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002133{
2134 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2135 struct i915_power_well *power_well;
2136 int i;
2137
2138 mutex_lock(&power_domains->lock);
2139 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
2140 power_well->ops->sync_hw(dev_priv, power_well);
2141 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2142 power_well);
2143 }
2144 mutex_unlock(&power_domains->lock);
2145}
2146
Imre Deak73dfc222015-11-17 17:33:53 +02002147static void skl_display_core_init(struct drm_i915_private *dev_priv,
Imre Deak443a93a2016-04-04 15:42:57 +03002148 bool resume)
Imre Deak73dfc222015-11-17 17:33:53 +02002149{
2150 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deak443a93a2016-04-04 15:42:57 +03002151 struct i915_power_well *well;
Imre Deak73dfc222015-11-17 17:33:53 +02002152 uint32_t val;
2153
Imre Deakd26fa1d2015-11-04 19:24:17 +02002154 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2155
Imre Deak73dfc222015-11-17 17:33:53 +02002156 /* enable PCH reset handshake */
2157 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2158 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2159
2160 /* enable PG1 and Misc I/O */
2161 mutex_lock(&power_domains->lock);
Imre Deak443a93a2016-04-04 15:42:57 +03002162
2163 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2164 intel_power_well_enable(dev_priv, well);
2165
2166 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2167 intel_power_well_enable(dev_priv, well);
2168
Imre Deak73dfc222015-11-17 17:33:53 +02002169 mutex_unlock(&power_domains->lock);
2170
2171 if (!resume)
2172 return;
2173
2174 skl_init_cdclk(dev_priv);
2175
Imre Deak2abc5252016-03-04 21:57:41 +02002176 if (dev_priv->csr.dmc_payload)
2177 intel_csr_load_program(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002178}
2179
2180static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2181{
2182 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deak443a93a2016-04-04 15:42:57 +03002183 struct i915_power_well *well;
Imre Deak73dfc222015-11-17 17:33:53 +02002184
Imre Deakd26fa1d2015-11-04 19:24:17 +02002185 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2186
Imre Deak73dfc222015-11-17 17:33:53 +02002187 skl_uninit_cdclk(dev_priv);
2188
2189 /* The spec doesn't call for removing the reset handshake flag */
2190 /* disable PG1 and Misc I/O */
Imre Deak443a93a2016-04-04 15:42:57 +03002191
Imre Deak73dfc222015-11-17 17:33:53 +02002192 mutex_lock(&power_domains->lock);
Imre Deak443a93a2016-04-04 15:42:57 +03002193
2194 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2195 intel_power_well_disable(dev_priv, well);
2196
2197 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2198 intel_power_well_disable(dev_priv, well);
2199
Imre Deak73dfc222015-11-17 17:33:53 +02002200 mutex_unlock(&power_domains->lock);
2201}
2202
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002203void bxt_display_core_init(struct drm_i915_private *dev_priv,
2204 bool resume)
2205{
2206 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2207 struct i915_power_well *well;
2208 uint32_t val;
2209
2210 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2211
2212 /*
2213 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
2214 * or else the reset will hang because there is no PCH to respond.
2215 * Move the handshake programming to initialization sequence.
2216 * Previously was left up to BIOS.
2217 */
2218 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2219 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
2220 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
2221
2222 /* Enable PG1 */
2223 mutex_lock(&power_domains->lock);
2224
2225 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2226 intel_power_well_enable(dev_priv, well);
2227
2228 mutex_unlock(&power_domains->lock);
2229
2230 broxton_init_cdclk(dev_priv);
2231 broxton_ddi_phy_init(dev_priv);
2232
Imre Deakadc7f042016-04-04 17:27:10 +03002233 broxton_cdclk_verify_state(dev_priv);
2234 broxton_ddi_phy_verify_state(dev_priv);
2235
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002236 if (resume && dev_priv->csr.dmc_payload)
2237 intel_csr_load_program(dev_priv);
2238}
2239
2240void bxt_display_core_uninit(struct drm_i915_private *dev_priv)
2241{
2242 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2243 struct i915_power_well *well;
2244
2245 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2246
2247 broxton_ddi_phy_uninit(dev_priv);
2248 broxton_uninit_cdclk(dev_priv);
2249
2250 /* The spec doesn't call for removing the reset handshake flag */
2251
2252 /* Disable PG1 */
2253 mutex_lock(&power_domains->lock);
2254
2255 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2256 intel_power_well_disable(dev_priv, well);
2257
2258 mutex_unlock(&power_domains->lock);
2259}
2260
Ville Syrjälä70722462015-04-10 18:21:28 +03002261static void chv_phy_control_init(struct drm_i915_private *dev_priv)
2262{
2263 struct i915_power_well *cmn_bc =
2264 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2265 struct i915_power_well *cmn_d =
2266 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
2267
2268 /*
2269 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
2270 * workaround never ever read DISPLAY_PHY_CONTROL, and
2271 * instead maintain a shadow copy ourselves. Use the actual
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002272 * power well state and lane status to reconstruct the
2273 * expected initial value.
Ville Syrjälä70722462015-04-10 18:21:28 +03002274 */
2275 dev_priv->chv_phy_control =
Ville Syrjäläbc284542015-05-26 20:22:38 +03002276 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
2277 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002278 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
2279 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
2280 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
2281
2282 /*
2283 * If all lanes are disabled we leave the override disabled
2284 * with all power down bits cleared to match the state we
2285 * would use after disabling the port. Otherwise enable the
2286 * override and set the lane powerdown bits accding to the
2287 * current lane status.
2288 */
2289 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
2290 uint32_t status = I915_READ(DPLL(PIPE_A));
2291 unsigned int mask;
2292
2293 mask = status & DPLL_PORTB_READY_MASK;
2294 if (mask == 0xf)
2295 mask = 0x0;
2296 else
2297 dev_priv->chv_phy_control |=
2298 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
2299
2300 dev_priv->chv_phy_control |=
2301 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
2302
2303 mask = (status & DPLL_PORTC_READY_MASK) >> 4;
2304 if (mask == 0xf)
2305 mask = 0x0;
2306 else
2307 dev_priv->chv_phy_control |=
2308 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
2309
2310 dev_priv->chv_phy_control |=
2311 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
2312
Ville Syrjälä70722462015-04-10 18:21:28 +03002313 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002314
2315 dev_priv->chv_phy_assert[DPIO_PHY0] = false;
2316 } else {
2317 dev_priv->chv_phy_assert[DPIO_PHY0] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002318 }
2319
2320 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
2321 uint32_t status = I915_READ(DPIO_PHY_STATUS);
2322 unsigned int mask;
2323
2324 mask = status & DPLL_PORTD_READY_MASK;
2325
2326 if (mask == 0xf)
2327 mask = 0x0;
2328 else
2329 dev_priv->chv_phy_control |=
2330 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
2331
2332 dev_priv->chv_phy_control |=
2333 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
2334
Ville Syrjälä70722462015-04-10 18:21:28 +03002335 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002336
2337 dev_priv->chv_phy_assert[DPIO_PHY1] = false;
2338 } else {
2339 dev_priv->chv_phy_assert[DPIO_PHY1] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002340 }
2341
2342 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
2343
2344 DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
2345 dev_priv->chv_phy_control);
Ville Syrjälä70722462015-04-10 18:21:28 +03002346}
2347
Daniel Vetter9c065a72014-09-30 10:56:38 +02002348static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
2349{
2350 struct i915_power_well *cmn =
2351 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2352 struct i915_power_well *disp2d =
2353 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
2354
Daniel Vetter9c065a72014-09-30 10:56:38 +02002355 /* If the display might be already active skip this */
Ville Syrjälä5d93a6e2014-10-16 20:52:33 +03002356 if (cmn->ops->is_enabled(dev_priv, cmn) &&
2357 disp2d->ops->is_enabled(dev_priv, disp2d) &&
Daniel Vetter9c065a72014-09-30 10:56:38 +02002358 I915_READ(DPIO_CTL) & DPIO_CMNRST)
2359 return;
2360
2361 DRM_DEBUG_KMS("toggling display PHY side reset\n");
2362
2363 /* cmnlane needs DPLL registers */
2364 disp2d->ops->enable(dev_priv, disp2d);
2365
2366 /*
2367 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
2368 * Need to assert and de-assert PHY SB reset by gating the
2369 * common lane power, then un-gating it.
2370 * Simply ungating isn't enough to reset the PHY enough to get
2371 * ports and lanes running.
2372 */
2373 cmn->ops->disable(dev_priv, cmn);
2374}
2375
Daniel Vettere4e76842014-09-30 10:56:42 +02002376/**
2377 * intel_power_domains_init_hw - initialize hardware power domain state
2378 * @dev_priv: i915 device instance
2379 *
2380 * This function initializes the hardware power domain state and enables all
2381 * power domains using intel_display_set_init_power().
2382 */
Imre Deak73dfc222015-11-17 17:33:53 +02002383void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002384{
2385 struct drm_device *dev = dev_priv->dev;
2386 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2387
2388 power_domains->initializing = true;
2389
Imre Deak73dfc222015-11-17 17:33:53 +02002390 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
2391 skl_display_core_init(dev_priv, resume);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002392 } else if (IS_BROXTON(dev)) {
2393 bxt_display_core_init(dev_priv, resume);
Imre Deak73dfc222015-11-17 17:33:53 +02002394 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä770effb2015-07-08 23:45:51 +03002395 mutex_lock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002396 chv_phy_control_init(dev_priv);
Ville Syrjälä770effb2015-07-08 23:45:51 +03002397 mutex_unlock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002398 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002399 mutex_lock(&power_domains->lock);
2400 vlv_cmnlane_wa(dev_priv);
2401 mutex_unlock(&power_domains->lock);
2402 }
2403
2404 /* For now, we need the power well to be always enabled. */
2405 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002406 /* Disable power support if the user asked so. */
2407 if (!i915.disable_power_well)
2408 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
Imre Deak30eade12015-11-04 19:24:13 +02002409 intel_power_domains_sync_hw(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002410 power_domains->initializing = false;
2411}
2412
Daniel Vettere4e76842014-09-30 10:56:42 +02002413/**
Imre Deak73dfc222015-11-17 17:33:53 +02002414 * intel_power_domains_suspend - suspend power domain state
2415 * @dev_priv: i915 device instance
2416 *
2417 * This function prepares the hardware power domain state before entering
2418 * system suspend. It must be paired with intel_power_domains_init_hw().
2419 */
2420void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
2421{
Imre Deakd314cd42015-11-17 17:44:23 +02002422 /*
2423 * Even if power well support was disabled we still want to disable
2424 * power wells while we are system suspended.
2425 */
2426 if (!i915.disable_power_well)
2427 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak2622d792016-02-29 22:49:02 +02002428
2429 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
2430 skl_display_core_uninit(dev_priv);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002431 else if (IS_BROXTON(dev_priv))
2432 bxt_display_core_uninit(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002433}
2434
2435/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002436 * intel_runtime_pm_get - grab a runtime pm reference
2437 * @dev_priv: i915 device instance
2438 *
2439 * This function grabs a device-level runtime pm reference (mostly used for GEM
2440 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2441 *
2442 * Any runtime pm reference obtained by this function must have a symmetric
2443 * call to intel_runtime_pm_put() to release the reference again.
2444 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002445void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2446{
2447 struct drm_device *dev = dev_priv->dev;
2448 struct device *device = &dev->pdev->dev;
2449
Daniel Vetter9c065a72014-09-30 10:56:38 +02002450 pm_runtime_get_sync(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002451
2452 atomic_inc(&dev_priv->pm.wakeref_count);
Imre Deakc9b88462015-12-15 20:10:34 +02002453 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002454}
2455
Daniel Vettere4e76842014-09-30 10:56:42 +02002456/**
Imre Deak09731282016-02-17 14:17:42 +02002457 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
2458 * @dev_priv: i915 device instance
2459 *
2460 * This function grabs a device-level runtime pm reference if the device is
2461 * already in use and ensures that it is powered up.
2462 *
2463 * Any runtime pm reference obtained by this function must have a symmetric
2464 * call to intel_runtime_pm_put() to release the reference again.
2465 */
2466bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
2467{
2468 struct drm_device *dev = dev_priv->dev;
2469 struct device *device = &dev->pdev->dev;
Imre Deak09731282016-02-17 14:17:42 +02002470
Chris Wilson135dc792016-02-25 21:10:28 +00002471 if (IS_ENABLED(CONFIG_PM)) {
2472 int ret = pm_runtime_get_if_in_use(device);
Imre Deak09731282016-02-17 14:17:42 +02002473
Chris Wilson135dc792016-02-25 21:10:28 +00002474 /*
2475 * In cases runtime PM is disabled by the RPM core and we get
2476 * an -EINVAL return value we are not supposed to call this
2477 * function, since the power state is undefined. This applies
2478 * atm to the late/early system suspend/resume handlers.
2479 */
2480 WARN_ON_ONCE(ret < 0);
2481 if (ret <= 0)
2482 return false;
2483 }
Imre Deak09731282016-02-17 14:17:42 +02002484
2485 atomic_inc(&dev_priv->pm.wakeref_count);
2486 assert_rpm_wakelock_held(dev_priv);
2487
2488 return true;
2489}
2490
2491/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002492 * intel_runtime_pm_get_noresume - grab a runtime pm reference
2493 * @dev_priv: i915 device instance
2494 *
2495 * This function grabs a device-level runtime pm reference (mostly used for GEM
2496 * code to ensure the GTT or GT is on).
2497 *
2498 * It will _not_ power up the device but instead only check that it's powered
2499 * on. Therefore it is only valid to call this functions from contexts where
2500 * the device is known to be powered up and where trying to power it up would
2501 * result in hilarity and deadlocks. That pretty much means only the system
2502 * suspend/resume code where this is used to grab runtime pm references for
2503 * delayed setup down in work items.
2504 *
2505 * Any runtime pm reference obtained by this function must have a symmetric
2506 * call to intel_runtime_pm_put() to release the reference again.
2507 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002508void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
2509{
2510 struct drm_device *dev = dev_priv->dev;
2511 struct device *device = &dev->pdev->dev;
2512
Imre Deakc9b88462015-12-15 20:10:34 +02002513 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002514 pm_runtime_get_noresume(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002515
2516 atomic_inc(&dev_priv->pm.wakeref_count);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002517}
2518
Daniel Vettere4e76842014-09-30 10:56:42 +02002519/**
2520 * intel_runtime_pm_put - release a runtime pm reference
2521 * @dev_priv: i915 device instance
2522 *
2523 * This function drops the device-level runtime pm reference obtained by
2524 * intel_runtime_pm_get() and might power down the corresponding
2525 * hardware block right away if this is the last reference.
2526 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002527void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
2528{
2529 struct drm_device *dev = dev_priv->dev;
2530 struct device *device = &dev->pdev->dev;
2531
Imre Deak542db3c2015-12-15 20:10:36 +02002532 assert_rpm_wakelock_held(dev_priv);
Imre Deak2b19efe2015-12-15 20:10:37 +02002533 if (atomic_dec_and_test(&dev_priv->pm.wakeref_count))
2534 atomic_inc(&dev_priv->pm.atomic_seq);
Imre Deak1f814da2015-12-16 02:52:19 +02002535
Daniel Vetter9c065a72014-09-30 10:56:38 +02002536 pm_runtime_mark_last_busy(device);
2537 pm_runtime_put_autosuspend(device);
2538}
2539
Daniel Vettere4e76842014-09-30 10:56:42 +02002540/**
2541 * intel_runtime_pm_enable - enable runtime pm
2542 * @dev_priv: i915 device instance
2543 *
2544 * This function enables runtime pm at the end of the driver load sequence.
2545 *
2546 * Note that this function does currently not enable runtime pm for the
2547 * subordinate display power domains. That is only done on the first modeset
2548 * using intel_display_set_init_power().
2549 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002550void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002551{
2552 struct drm_device *dev = dev_priv->dev;
2553 struct device *device = &dev->pdev->dev;
2554
Imre Deakcbc68dc2015-12-17 19:04:33 +02002555 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
2556 pm_runtime_mark_last_busy(device);
2557
Imre Deak25b181b2015-12-17 13:44:56 +02002558 /*
2559 * Take a permanent reference to disable the RPM functionality and drop
2560 * it only when unloading the driver. Use the low level get/put helpers,
2561 * so the driver's own RPM reference tracking asserts also work on
2562 * platforms without RPM support.
2563 */
Imre Deakcbc68dc2015-12-17 19:04:33 +02002564 if (!HAS_RUNTIME_PM(dev)) {
2565 pm_runtime_dont_use_autosuspend(device);
Imre Deak25b181b2015-12-17 13:44:56 +02002566 pm_runtime_get_sync(device);
Imre Deakcbc68dc2015-12-17 19:04:33 +02002567 } else {
2568 pm_runtime_use_autosuspend(device);
2569 }
Daniel Vetter9c065a72014-09-30 10:56:38 +02002570
Imre Deakaabee1b2015-12-15 20:10:29 +02002571 /*
2572 * The core calls the driver load handler with an RPM reference held.
2573 * We drop that here and will reacquire it during unloading in
2574 * intel_power_domains_fini().
2575 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002576 pm_runtime_put_autosuspend(device);
2577}
2578