blob: f212ef31c729afb2b0569937a9e791c3f31aeb26 [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
Imre Deak9c8d0b82016-06-13 16:44:34 +030068static struct i915_power_well *
69lookup_power_well(struct drm_i915_private *dev_priv, int power_well_id);
70
Daniel Stone9895ad02015-11-20 15:55:33 +000071const char *
72intel_display_power_domain_str(enum intel_display_power_domain domain)
73{
74 switch (domain) {
75 case POWER_DOMAIN_PIPE_A:
76 return "PIPE_A";
77 case POWER_DOMAIN_PIPE_B:
78 return "PIPE_B";
79 case POWER_DOMAIN_PIPE_C:
80 return "PIPE_C";
81 case POWER_DOMAIN_PIPE_A_PANEL_FITTER:
82 return "PIPE_A_PANEL_FITTER";
83 case POWER_DOMAIN_PIPE_B_PANEL_FITTER:
84 return "PIPE_B_PANEL_FITTER";
85 case POWER_DOMAIN_PIPE_C_PANEL_FITTER:
86 return "PIPE_C_PANEL_FITTER";
87 case POWER_DOMAIN_TRANSCODER_A:
88 return "TRANSCODER_A";
89 case POWER_DOMAIN_TRANSCODER_B:
90 return "TRANSCODER_B";
91 case POWER_DOMAIN_TRANSCODER_C:
92 return "TRANSCODER_C";
93 case POWER_DOMAIN_TRANSCODER_EDP:
94 return "TRANSCODER_EDP";
Jani Nikula4d1de972016-03-18 17:05:42 +020095 case POWER_DOMAIN_TRANSCODER_DSI_A:
96 return "TRANSCODER_DSI_A";
97 case POWER_DOMAIN_TRANSCODER_DSI_C:
98 return "TRANSCODER_DSI_C";
Daniel Stone9895ad02015-11-20 15:55:33 +000099 case POWER_DOMAIN_PORT_DDI_A_LANES:
100 return "PORT_DDI_A_LANES";
101 case POWER_DOMAIN_PORT_DDI_B_LANES:
102 return "PORT_DDI_B_LANES";
103 case POWER_DOMAIN_PORT_DDI_C_LANES:
104 return "PORT_DDI_C_LANES";
105 case POWER_DOMAIN_PORT_DDI_D_LANES:
106 return "PORT_DDI_D_LANES";
107 case POWER_DOMAIN_PORT_DDI_E_LANES:
108 return "PORT_DDI_E_LANES";
109 case POWER_DOMAIN_PORT_DSI:
110 return "PORT_DSI";
111 case POWER_DOMAIN_PORT_CRT:
112 return "PORT_CRT";
113 case POWER_DOMAIN_PORT_OTHER:
114 return "PORT_OTHER";
115 case POWER_DOMAIN_VGA:
116 return "VGA";
117 case POWER_DOMAIN_AUDIO:
118 return "AUDIO";
119 case POWER_DOMAIN_PLLS:
120 return "PLLS";
121 case POWER_DOMAIN_AUX_A:
122 return "AUX_A";
123 case POWER_DOMAIN_AUX_B:
124 return "AUX_B";
125 case POWER_DOMAIN_AUX_C:
126 return "AUX_C";
127 case POWER_DOMAIN_AUX_D:
128 return "AUX_D";
129 case POWER_DOMAIN_GMBUS:
130 return "GMBUS";
131 case POWER_DOMAIN_INIT:
132 return "INIT";
133 case POWER_DOMAIN_MODESET:
134 return "MODESET";
135 default:
136 MISSING_CASE(domain);
137 return "?";
138 }
139}
140
Damien Lespiaue8ca9322015-07-30 18:20:26 -0300141static void intel_power_well_enable(struct drm_i915_private *dev_priv,
142 struct i915_power_well *power_well)
143{
144 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
145 power_well->ops->enable(dev_priv, power_well);
146 power_well->hw_enabled = true;
147}
148
Damien Lespiaudcddab32015-07-30 18:20:27 -0300149static void intel_power_well_disable(struct drm_i915_private *dev_priv,
150 struct i915_power_well *power_well)
151{
152 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
153 power_well->hw_enabled = false;
154 power_well->ops->disable(dev_priv, power_well);
155}
156
Imre Deakb409ca92016-06-13 16:44:33 +0300157static void intel_power_well_get(struct drm_i915_private *dev_priv,
158 struct i915_power_well *power_well)
159{
160 if (!power_well->count++)
161 intel_power_well_enable(dev_priv, power_well);
162}
163
164static void intel_power_well_put(struct drm_i915_private *dev_priv,
165 struct i915_power_well *power_well)
166{
167 WARN(!power_well->count, "Use count on power well %s is already zero",
168 power_well->name);
169
170 if (!--power_well->count)
171 intel_power_well_disable(dev_priv, power_well);
172}
173
Daniel Vettere4e76842014-09-30 10:56:42 +0200174/*
Daniel Vetter9c065a72014-09-30 10:56:38 +0200175 * We should only use the power well if we explicitly asked the hardware to
176 * enable it, so check if it's enabled and also check if we've requested it to
177 * be enabled.
178 */
179static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
180 struct i915_power_well *power_well)
181{
182 return I915_READ(HSW_PWR_WELL_DRIVER) ==
183 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
184}
185
Daniel Vettere4e76842014-09-30 10:56:42 +0200186/**
187 * __intel_display_power_is_enabled - unlocked check for a power domain
188 * @dev_priv: i915 device instance
189 * @domain: power domain to check
190 *
191 * This is the unlocked version of intel_display_power_is_enabled() and should
192 * only be used from error capture and recovery code where deadlocks are
193 * possible.
194 *
195 * Returns:
196 * True when the power domain is enabled, false otherwise.
197 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200198bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
199 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200200{
201 struct i915_power_domains *power_domains;
202 struct i915_power_well *power_well;
203 bool is_enabled;
204 int i;
205
206 if (dev_priv->pm.suspended)
207 return false;
208
209 power_domains = &dev_priv->power_domains;
210
211 is_enabled = true;
212
213 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
214 if (power_well->always_on)
215 continue;
216
217 if (!power_well->hw_enabled) {
218 is_enabled = false;
219 break;
220 }
221 }
222
223 return is_enabled;
224}
225
Daniel Vettere4e76842014-09-30 10:56:42 +0200226/**
Damien Lespiauf61ccae2014-11-25 13:45:41 +0000227 * intel_display_power_is_enabled - check for a power domain
Daniel Vettere4e76842014-09-30 10:56:42 +0200228 * @dev_priv: i915 device instance
229 * @domain: power domain to check
230 *
231 * This function can be used to check the hw power domain state. It is mostly
232 * used in hardware state readout functions. Everywhere else code should rely
233 * upon explicit power domain reference counting to ensure that the hardware
234 * block is powered up before accessing it.
235 *
236 * Callers must hold the relevant modesetting locks to ensure that concurrent
237 * threads can't disable the power well while the caller tries to read a few
238 * registers.
239 *
240 * Returns:
241 * True when the power domain is enabled, false otherwise.
242 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200243bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
244 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200245{
246 struct i915_power_domains *power_domains;
247 bool ret;
248
249 power_domains = &dev_priv->power_domains;
250
251 mutex_lock(&power_domains->lock);
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200252 ret = __intel_display_power_is_enabled(dev_priv, domain);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200253 mutex_unlock(&power_domains->lock);
254
255 return ret;
256}
257
Daniel Vettere4e76842014-09-30 10:56:42 +0200258/**
259 * intel_display_set_init_power - set the initial power domain state
260 * @dev_priv: i915 device instance
261 * @enable: whether to enable or disable the initial power domain state
262 *
263 * For simplicity our driver load/unload and system suspend/resume code assumes
264 * that all power domains are always enabled. This functions controls the state
265 * of this little hack. While the initial power domain state is enabled runtime
266 * pm is effectively disabled.
267 */
Daniel Vetterd9bc89d92014-09-30 10:56:40 +0200268void intel_display_set_init_power(struct drm_i915_private *dev_priv,
269 bool enable)
270{
271 if (dev_priv->power_domains.init_power_on == enable)
272 return;
273
274 if (enable)
275 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
276 else
277 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
278
279 dev_priv->power_domains.init_power_on = enable;
280}
281
Daniel Vetter9c065a72014-09-30 10:56:38 +0200282/*
283 * Starting with Haswell, we have a "Power Down Well" that can be turned off
284 * when not needed anymore. We have 4 registers that can request the power well
285 * to be enabled, and it will only be disabled if none of the registers is
286 * requesting it to be enabled.
287 */
288static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
289{
290 struct drm_device *dev = dev_priv->dev;
291
292 /*
293 * After we re-enable the power well, if we touch VGA register 0x3d5
294 * we'll get unclaimed register interrupts. This stops after we write
295 * anything to the VGA MSR register. The vgacon module uses this
296 * register all the time, so if we unbind our driver and, as a
297 * consequence, bind vgacon, we'll get stuck in an infinite loop at
298 * console_unlock(). So make here we touch the VGA MSR register, making
299 * sure vgacon can keep working normally without triggering interrupts
300 * and error messages.
301 */
302 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
303 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
304 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
305
Damien Lespiau25400392015-03-06 18:50:52 +0000306 if (IS_BROADWELL(dev))
Damien Lespiau4c6c03b2015-03-06 18:50:48 +0000307 gen8_irq_power_well_post_enable(dev_priv,
308 1 << PIPE_C | 1 << PIPE_B);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200309}
310
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200311static void hsw_power_well_pre_disable(struct drm_i915_private *dev_priv)
312{
313 if (IS_BROADWELL(dev_priv))
314 gen8_irq_power_well_pre_disable(dev_priv,
315 1 << PIPE_C | 1 << PIPE_B);
316}
317
Damien Lespiaud14c0342015-03-06 18:50:51 +0000318static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
319 struct i915_power_well *power_well)
320{
321 struct drm_device *dev = dev_priv->dev;
322
323 /*
324 * After we re-enable the power well, if we touch VGA register 0x3d5
325 * we'll get unclaimed register interrupts. This stops after we write
326 * anything to the VGA MSR register. The vgacon module uses this
327 * register all the time, so if we unbind our driver and, as a
328 * consequence, bind vgacon, we'll get stuck in an infinite loop at
329 * console_unlock(). So make here we touch the VGA MSR register, making
330 * sure vgacon can keep working normally without triggering interrupts
331 * and error messages.
332 */
333 if (power_well->data == SKL_DISP_PW_2) {
334 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
335 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
336 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
337
338 gen8_irq_power_well_post_enable(dev_priv,
339 1 << PIPE_C | 1 << PIPE_B);
340 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000341}
342
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200343static void skl_power_well_pre_disable(struct drm_i915_private *dev_priv,
344 struct i915_power_well *power_well)
345{
346 if (power_well->data == SKL_DISP_PW_2)
347 gen8_irq_power_well_pre_disable(dev_priv,
348 1 << PIPE_C | 1 << PIPE_B);
349}
350
Daniel Vetter9c065a72014-09-30 10:56:38 +0200351static void hsw_set_power_well(struct drm_i915_private *dev_priv,
352 struct i915_power_well *power_well, bool enable)
353{
354 bool is_enabled, enable_requested;
355 uint32_t tmp;
356
357 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
358 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
359 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
360
361 if (enable) {
362 if (!enable_requested)
363 I915_WRITE(HSW_PWR_WELL_DRIVER,
364 HSW_PWR_WELL_ENABLE_REQUEST);
365
366 if (!is_enabled) {
367 DRM_DEBUG_KMS("Enabling power well\n");
Chris Wilson2c2ccc32016-06-30 15:33:32 +0100368 if (intel_wait_for_register(dev_priv,
369 HSW_PWR_WELL_DRIVER,
370 HSW_PWR_WELL_STATE_ENABLED,
371 HSW_PWR_WELL_STATE_ENABLED,
372 20))
Daniel Vetter9c065a72014-09-30 10:56:38 +0200373 DRM_ERROR("Timeout enabling power well\n");
Paulo Zanoni6d729bf2014-10-07 16:11:11 -0300374 hsw_power_well_post_enable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200375 }
376
Daniel Vetter9c065a72014-09-30 10:56:38 +0200377 } else {
378 if (enable_requested) {
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200379 hsw_power_well_pre_disable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200380 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
381 POSTING_READ(HSW_PWR_WELL_DRIVER);
382 DRM_DEBUG_KMS("Requesting to disable the power well\n");
383 }
384 }
385}
386
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000387#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
388 BIT(POWER_DOMAIN_TRANSCODER_A) | \
389 BIT(POWER_DOMAIN_PIPE_B) | \
390 BIT(POWER_DOMAIN_TRANSCODER_B) | \
391 BIT(POWER_DOMAIN_PIPE_C) | \
392 BIT(POWER_DOMAIN_TRANSCODER_C) | \
393 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
394 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100395 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
396 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
397 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
398 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000399 BIT(POWER_DOMAIN_AUX_B) | \
400 BIT(POWER_DOMAIN_AUX_C) | \
401 BIT(POWER_DOMAIN_AUX_D) | \
402 BIT(POWER_DOMAIN_AUDIO) | \
403 BIT(POWER_DOMAIN_VGA) | \
404 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000405#define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100406 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
407 BIT(POWER_DOMAIN_PORT_DDI_E_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000408 BIT(POWER_DOMAIN_INIT))
409#define SKL_DISPLAY_DDI_B_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100410 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000411 BIT(POWER_DOMAIN_INIT))
412#define SKL_DISPLAY_DDI_C_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100413 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000414 BIT(POWER_DOMAIN_INIT))
415#define SKL_DISPLAY_DDI_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100416 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000417 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100418#define SKL_DISPLAY_DC_OFF_POWER_DOMAINS ( \
419 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
420 BIT(POWER_DOMAIN_MODESET) | \
421 BIT(POWER_DOMAIN_AUX_A) | \
422 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000423
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530424#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
425 BIT(POWER_DOMAIN_TRANSCODER_A) | \
426 BIT(POWER_DOMAIN_PIPE_B) | \
427 BIT(POWER_DOMAIN_TRANSCODER_B) | \
428 BIT(POWER_DOMAIN_PIPE_C) | \
429 BIT(POWER_DOMAIN_TRANSCODER_C) | \
430 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
431 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +0100432 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
433 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530434 BIT(POWER_DOMAIN_AUX_B) | \
435 BIT(POWER_DOMAIN_AUX_C) | \
436 BIT(POWER_DOMAIN_AUDIO) | \
437 BIT(POWER_DOMAIN_VGA) | \
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +0100438 BIT(POWER_DOMAIN_GMBUS) | \
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530439 BIT(POWER_DOMAIN_INIT))
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100440#define BXT_DISPLAY_DC_OFF_POWER_DOMAINS ( \
441 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
442 BIT(POWER_DOMAIN_MODESET) | \
443 BIT(POWER_DOMAIN_AUX_A) | \
444 BIT(POWER_DOMAIN_INIT))
Imre Deak9c8d0b82016-06-13 16:44:34 +0300445#define BXT_DPIO_CMN_A_POWER_DOMAINS ( \
446 BIT(POWER_DOMAIN_PORT_DDI_A_LANES) | \
447 BIT(POWER_DOMAIN_AUX_A) | \
448 BIT(POWER_DOMAIN_INIT))
449#define BXT_DPIO_CMN_BC_POWER_DOMAINS ( \
450 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
451 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
452 BIT(POWER_DOMAIN_AUX_B) | \
453 BIT(POWER_DOMAIN_AUX_C) | \
454 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530455
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530456static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
457{
Imre Deakbfcdabe2016-04-01 16:02:37 +0300458 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
459 "DC9 already programmed to be enabled.\n");
460 WARN_ONCE(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
461 "DC5 still not disabled to enable DC9.\n");
462 WARN_ONCE(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
463 WARN_ONCE(intel_irqs_enabled(dev_priv),
464 "Interrupts not disabled yet.\n");
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530465
466 /*
467 * TODO: check for the following to verify the conditions to enter DC9
468 * state are satisfied:
469 * 1] Check relevant display engine registers to verify if mode set
470 * disable sequence was followed.
471 * 2] Check if display uninitialize sequence is initialized.
472 */
473}
474
475static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
476{
Imre Deakbfcdabe2016-04-01 16:02:37 +0300477 WARN_ONCE(intel_irqs_enabled(dev_priv),
478 "Interrupts not disabled yet.\n");
479 WARN_ONCE(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
480 "DC5 still not disabled.\n");
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530481
482 /*
483 * TODO: check for the following to verify DC9 state was indeed
484 * entered before programming to disable it:
485 * 1] Check relevant display engine registers to verify if mode
486 * set disable sequence was followed.
487 * 2] Check if display uninitialize sequence is initialized.
488 */
489}
490
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200491static void gen9_write_dc_state(struct drm_i915_private *dev_priv,
492 u32 state)
493{
494 int rewrites = 0;
495 int rereads = 0;
496 u32 v;
497
498 I915_WRITE(DC_STATE_EN, state);
499
500 /* It has been observed that disabling the dc6 state sometimes
501 * doesn't stick and dmc keeps returning old value. Make sure
502 * the write really sticks enough times and also force rewrite until
503 * we are confident that state is exactly what we want.
504 */
505 do {
506 v = I915_READ(DC_STATE_EN);
507
508 if (v != state) {
509 I915_WRITE(DC_STATE_EN, state);
510 rewrites++;
511 rereads = 0;
512 } else if (rereads++ > 5) {
513 break;
514 }
515
516 } while (rewrites < 100);
517
518 if (v != state)
519 DRM_ERROR("Writing dc state to 0x%x failed, now 0x%x\n",
520 state, v);
521
522 /* Most of the times we need one retry, avoid spam */
523 if (rewrites > 1)
524 DRM_DEBUG_KMS("Rewrote dc state to 0x%x %d times\n",
525 state, rewrites);
526}
527
Imre Deakda2f41d2016-04-20 20:27:56 +0300528static u32 gen9_dc_mask(struct drm_i915_private *dev_priv)
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530529{
Imre Deakda2f41d2016-04-20 20:27:56 +0300530 u32 mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530531
Imre Deak13ae3a02015-11-04 19:24:16 +0200532 mask = DC_STATE_EN_UPTO_DC5;
533 if (IS_BROXTON(dev_priv))
534 mask |= DC_STATE_EN_DC9;
535 else
536 mask |= DC_STATE_EN_UPTO_DC6;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530537
Imre Deakda2f41d2016-04-20 20:27:56 +0300538 return mask;
539}
540
541void gen9_sanitize_dc_state(struct drm_i915_private *dev_priv)
542{
543 u32 val;
544
545 val = I915_READ(DC_STATE_EN) & gen9_dc_mask(dev_priv);
546
547 DRM_DEBUG_KMS("Resetting DC state tracking from %02x to %02x\n",
548 dev_priv->csr.dc_state, val);
549 dev_priv->csr.dc_state = val;
550}
551
552static void gen9_set_dc_state(struct drm_i915_private *dev_priv, uint32_t state)
553{
554 uint32_t val;
555 uint32_t mask;
556
Imre Deaka37baf32016-02-29 22:49:03 +0200557 if (WARN_ON_ONCE(state & ~dev_priv->csr.allowed_dc_mask))
558 state &= dev_priv->csr.allowed_dc_mask;
Patrik Jakobsson443646c2015-11-16 15:01:06 +0100559
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530560 val = I915_READ(DC_STATE_EN);
Imre Deakda2f41d2016-04-20 20:27:56 +0300561 mask = gen9_dc_mask(dev_priv);
Imre Deak13ae3a02015-11-04 19:24:16 +0200562 DRM_DEBUG_KMS("Setting DC state from %02x to %02x\n",
563 val & mask, state);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200564
565 /* Check if DMC is ignoring our DC state requests */
566 if ((val & mask) != dev_priv->csr.dc_state)
567 DRM_ERROR("DC state mismatch (0x%x -> 0x%x)\n",
568 dev_priv->csr.dc_state, val & mask);
569
Imre Deak13ae3a02015-11-04 19:24:16 +0200570 val &= ~mask;
571 val |= state;
Mika Kuoppala779cb5d2016-02-18 17:58:09 +0200572
573 gen9_write_dc_state(dev_priv, val);
Patrik Jakobsson832dba82016-02-18 17:21:11 +0200574
575 dev_priv->csr.dc_state = val & mask;
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530576}
577
Imre Deak13ae3a02015-11-04 19:24:16 +0200578void bxt_enable_dc9(struct drm_i915_private *dev_priv)
579{
580 assert_can_enable_dc9(dev_priv);
581
582 DRM_DEBUG_KMS("Enabling DC9\n");
583
Imre Deak78597992016-06-16 16:37:20 +0300584 intel_power_sequencer_reset(dev_priv);
Imre Deak13ae3a02015-11-04 19:24:16 +0200585 gen9_set_dc_state(dev_priv, DC_STATE_EN_DC9);
586}
587
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530588void bxt_disable_dc9(struct drm_i915_private *dev_priv)
589{
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530590 assert_can_disable_dc9(dev_priv);
591
592 DRM_DEBUG_KMS("Disabling DC9\n");
593
Imre Deak13ae3a02015-11-04 19:24:16 +0200594 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530595}
596
Daniel Vetteraf5fead2015-10-28 23:58:57 +0200597static void assert_csr_loaded(struct drm_i915_private *dev_priv)
598{
599 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
600 "CSR program storage start is NULL\n");
601 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
602 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
603}
604
Suketu Shah5aefb232015-04-16 14:22:10 +0530605static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shahdc174302015-04-17 19:46:16 +0530606{
Suketu Shah5aefb232015-04-16 14:22:10 +0530607 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
608 SKL_DISP_PW_2);
609
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700610 WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530611
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700612 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
613 "DC5 already programmed to be enabled.\n");
Imre Deakc9b88462015-12-15 20:10:34 +0200614 assert_rpm_wakelock_held(dev_priv);
Suketu Shah5aefb232015-04-16 14:22:10 +0530615
616 assert_csr_loaded(dev_priv);
617}
618
Imre Deakf62c79b2016-04-20 20:27:57 +0300619void gen9_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shah5aefb232015-04-16 14:22:10 +0530620{
Suketu Shah5aefb232015-04-16 14:22:10 +0530621 assert_can_enable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530622
623 DRM_DEBUG_KMS("Enabling DC5\n");
624
Imre Deak13ae3a02015-11-04 19:24:16 +0200625 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC5);
Suketu Shahdc174302015-04-17 19:46:16 +0530626}
627
Suketu Shah93c7cb62015-04-16 14:22:13 +0530628static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530629{
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700630 WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
631 "Backlight is not disabled.\n");
632 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
633 "DC6 already programmed to be enabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530634
635 assert_csr_loaded(dev_priv);
636}
637
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530638void skl_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shah93c7cb62015-04-16 14:22:13 +0530639{
Suketu Shah93c7cb62015-04-16 14:22:13 +0530640 assert_can_enable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530641
642 DRM_DEBUG_KMS("Enabling DC6\n");
643
Imre Deak13ae3a02015-11-04 19:24:16 +0200644 gen9_set_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
645
Suketu Shahf75a1982015-04-16 14:22:11 +0530646}
647
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530648void skl_disable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530649{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530650 DRM_DEBUG_KMS("Disabling DC6\n");
651
Imre Deak13ae3a02015-11-04 19:24:16 +0200652 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Suketu Shahf75a1982015-04-16 14:22:11 +0530653}
654
Imre Deakc6782b72016-04-05 13:26:05 +0300655static void
656gen9_sanitize_power_well_requests(struct drm_i915_private *dev_priv,
657 struct i915_power_well *power_well)
658{
659 enum skl_disp_power_wells power_well_id = power_well->data;
660 u32 val;
661 u32 mask;
662
663 mask = SKL_POWER_WELL_REQ(power_well_id);
664
665 val = I915_READ(HSW_PWR_WELL_KVMR);
666 if (WARN_ONCE(val & mask, "Clearing unexpected KVMR request for %s\n",
667 power_well->name))
668 I915_WRITE(HSW_PWR_WELL_KVMR, val & ~mask);
669
670 val = I915_READ(HSW_PWR_WELL_BIOS);
671 val |= I915_READ(HSW_PWR_WELL_DEBUG);
672
673 if (!(val & mask))
674 return;
675
676 /*
677 * DMC is known to force on the request bits for power well 1 on SKL
678 * and BXT and the misc IO power well on SKL but we don't expect any
679 * other request bits to be set, so WARN for those.
680 */
681 if (power_well_id == SKL_DISP_PW_1 ||
Imre Deak80dbe992016-04-19 13:00:36 +0300682 ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
683 power_well_id == SKL_DISP_PW_MISC_IO))
Imre Deakc6782b72016-04-05 13:26:05 +0300684 DRM_DEBUG_DRIVER("Clearing auxiliary requests for %s forced on "
685 "by DMC\n", power_well->name);
686 else
687 WARN_ONCE(1, "Clearing unexpected auxiliary requests for %s\n",
688 power_well->name);
689
690 I915_WRITE(HSW_PWR_WELL_BIOS, val & ~mask);
691 I915_WRITE(HSW_PWR_WELL_DEBUG, val & ~mask);
692}
693
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000694static void skl_set_power_well(struct drm_i915_private *dev_priv,
695 struct i915_power_well *power_well, bool enable)
696{
697 uint32_t tmp, fuse_status;
698 uint32_t req_mask, state_mask;
Damien Lespiau2a518352015-03-06 18:50:49 +0000699 bool is_enabled, enable_requested, check_fuse_status = false;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000700
701 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
702 fuse_status = I915_READ(SKL_FUSE_STATUS);
703
704 switch (power_well->data) {
705 case SKL_DISP_PW_1:
Chris Wilson117c1142016-06-30 15:33:33 +0100706 if (intel_wait_for_register(dev_priv,
707 SKL_FUSE_STATUS,
708 SKL_FUSE_PG0_DIST_STATUS,
709 SKL_FUSE_PG0_DIST_STATUS,
710 1)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000711 DRM_ERROR("PG0 not enabled\n");
712 return;
713 }
714 break;
715 case SKL_DISP_PW_2:
716 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
717 DRM_ERROR("PG1 in disabled state\n");
718 return;
719 }
720 break;
721 case SKL_DISP_PW_DDI_A_E:
722 case SKL_DISP_PW_DDI_B:
723 case SKL_DISP_PW_DDI_C:
724 case SKL_DISP_PW_DDI_D:
725 case SKL_DISP_PW_MISC_IO:
726 break;
727 default:
728 WARN(1, "Unknown power well %lu\n", power_well->data);
729 return;
730 }
731
732 req_mask = SKL_POWER_WELL_REQ(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000733 enable_requested = tmp & req_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000734 state_mask = SKL_POWER_WELL_STATE(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000735 is_enabled = tmp & state_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000736
Ville Syrjäläaae8ba82016-02-19 20:47:30 +0200737 if (!enable && enable_requested)
738 skl_power_well_pre_disable(dev_priv, power_well);
739
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000740 if (enable) {
Damien Lespiau2a518352015-03-06 18:50:49 +0000741 if (!enable_requested) {
Suketu Shahdc174302015-04-17 19:46:16 +0530742 WARN((tmp & state_mask) &&
743 !I915_READ(HSW_PWR_WELL_BIOS),
744 "Invalid for power well status to be enabled, unless done by the BIOS, \
745 when request is to disable!\n");
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000746 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000747 }
748
Damien Lespiau2a518352015-03-06 18:50:49 +0000749 if (!is_enabled) {
Damien Lespiau510e6fd2015-03-06 18:50:50 +0000750 DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000751 check_fuse_status = true;
752 }
753 } else {
Damien Lespiau2a518352015-03-06 18:50:49 +0000754 if (enable_requested) {
Imre Deak4a76f292015-11-04 19:24:15 +0200755 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
756 POSTING_READ(HSW_PWR_WELL_DRIVER);
757 DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000758 }
Imre Deakc6782b72016-04-05 13:26:05 +0300759
Imre Deak5f304c82016-04-15 22:32:58 +0300760 if (IS_GEN9(dev_priv))
Imre Deakc6782b72016-04-05 13:26:05 +0300761 gen9_sanitize_power_well_requests(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000762 }
763
Imre Deak1d963af2016-04-01 16:02:36 +0300764 if (wait_for(!!(I915_READ(HSW_PWR_WELL_DRIVER) & state_mask) == enable,
765 1))
766 DRM_ERROR("%s %s timeout\n",
767 power_well->name, enable ? "enable" : "disable");
768
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000769 if (check_fuse_status) {
770 if (power_well->data == SKL_DISP_PW_1) {
Chris Wilson8b00f552016-06-30 15:33:34 +0100771 if (intel_wait_for_register(dev_priv,
772 SKL_FUSE_STATUS,
773 SKL_FUSE_PG1_DIST_STATUS,
774 SKL_FUSE_PG1_DIST_STATUS,
775 1))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000776 DRM_ERROR("PG1 distributing status timeout\n");
777 } else if (power_well->data == SKL_DISP_PW_2) {
Chris Wilson8b00f552016-06-30 15:33:34 +0100778 if (intel_wait_for_register(dev_priv,
779 SKL_FUSE_STATUS,
780 SKL_FUSE_PG2_DIST_STATUS,
781 SKL_FUSE_PG2_DIST_STATUS,
782 1))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000783 DRM_ERROR("PG2 distributing status timeout\n");
784 }
785 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000786
787 if (enable && !is_enabled)
788 skl_power_well_post_enable(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000789}
790
Daniel Vetter9c065a72014-09-30 10:56:38 +0200791static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
792 struct i915_power_well *power_well)
793{
794 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
795
796 /*
797 * We're taking over the BIOS, so clear any requests made by it since
798 * the driver is in charge now.
799 */
800 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
801 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
802}
803
804static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
805 struct i915_power_well *power_well)
806{
807 hsw_set_power_well(dev_priv, power_well, true);
808}
809
810static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
811 struct i915_power_well *power_well)
812{
813 hsw_set_power_well(dev_priv, power_well, false);
814}
815
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000816static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
817 struct i915_power_well *power_well)
818{
819 uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
820 SKL_POWER_WELL_STATE(power_well->data);
821
822 return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
823}
824
825static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
826 struct i915_power_well *power_well)
827{
828 skl_set_power_well(dev_priv, power_well, power_well->count > 0);
829
830 /* Clear any request made by BIOS as driver is taking over */
831 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
832}
833
834static void skl_power_well_enable(struct drm_i915_private *dev_priv,
835 struct i915_power_well *power_well)
836{
837 skl_set_power_well(dev_priv, power_well, true);
838}
839
840static void skl_power_well_disable(struct drm_i915_private *dev_priv,
841 struct i915_power_well *power_well)
842{
843 skl_set_power_well(dev_priv, power_well, false);
844}
845
Imre Deak9c8d0b82016-06-13 16:44:34 +0300846static enum dpio_phy bxt_power_well_to_phy(struct i915_power_well *power_well)
847{
848 enum skl_disp_power_wells power_well_id = power_well->data;
849
850 return power_well_id == BXT_DPIO_CMN_A ? DPIO_PHY1 : DPIO_PHY0;
851}
852
853static void bxt_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
854 struct i915_power_well *power_well)
855{
856 enum skl_disp_power_wells power_well_id = power_well->data;
857 struct i915_power_well *cmn_a_well;
858
859 if (power_well_id == BXT_DPIO_CMN_BC) {
860 /*
861 * We need to copy the GRC calibration value from the eDP PHY,
862 * so make sure it's powered up.
863 */
864 cmn_a_well = lookup_power_well(dev_priv, BXT_DPIO_CMN_A);
865 intel_power_well_get(dev_priv, cmn_a_well);
866 }
867
868 bxt_ddi_phy_init(dev_priv, bxt_power_well_to_phy(power_well));
869
870 if (power_well_id == BXT_DPIO_CMN_BC)
871 intel_power_well_put(dev_priv, cmn_a_well);
872}
873
874static void bxt_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
875 struct i915_power_well *power_well)
876{
877 bxt_ddi_phy_uninit(dev_priv, bxt_power_well_to_phy(power_well));
878}
879
880static bool bxt_dpio_cmn_power_well_enabled(struct drm_i915_private *dev_priv,
881 struct i915_power_well *power_well)
882{
883 return bxt_ddi_phy_is_enabled(dev_priv,
884 bxt_power_well_to_phy(power_well));
885}
886
887static void bxt_dpio_cmn_power_well_sync_hw(struct drm_i915_private *dev_priv,
888 struct i915_power_well *power_well)
889{
890 if (power_well->count > 0)
891 bxt_dpio_cmn_power_well_enable(dev_priv, power_well);
892 else
893 bxt_dpio_cmn_power_well_disable(dev_priv, power_well);
894}
895
896
897static void bxt_verify_ddi_phy_power_wells(struct drm_i915_private *dev_priv)
898{
899 struct i915_power_well *power_well;
900
901 power_well = lookup_power_well(dev_priv, BXT_DPIO_CMN_A);
902 if (power_well->count > 0)
903 bxt_ddi_phy_verify_state(dev_priv,
904 bxt_power_well_to_phy(power_well));
905
906 power_well = lookup_power_well(dev_priv, BXT_DPIO_CMN_BC);
907 if (power_well->count > 0)
908 bxt_ddi_phy_verify_state(dev_priv,
909 bxt_power_well_to_phy(power_well));
910}
911
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100912static bool gen9_dc_off_power_well_enabled(struct drm_i915_private *dev_priv,
913 struct i915_power_well *power_well)
914{
915 return (I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5_DC6_MASK) == 0;
916}
917
Ville Syrjälä18a80672016-05-16 16:59:40 +0300918static void gen9_assert_dbuf_enabled(struct drm_i915_private *dev_priv)
919{
920 u32 tmp = I915_READ(DBUF_CTL);
921
922 WARN((tmp & (DBUF_POWER_STATE | DBUF_POWER_REQUEST)) !=
923 (DBUF_POWER_STATE | DBUF_POWER_REQUEST),
924 "Unexpected DBuf power power state (0x%08x)\n", tmp);
925}
926
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100927static void gen9_dc_off_power_well_enable(struct drm_i915_private *dev_priv,
928 struct i915_power_well *power_well)
929{
Imre Deak5b773eb2016-02-29 22:49:05 +0200930 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
Imre Deakadc7f042016-04-04 17:27:10 +0300931
Ville Syrjälä342be922016-05-13 23:41:39 +0300932 WARN_ON(dev_priv->cdclk_freq !=
933 dev_priv->display.get_display_clock_speed(dev_priv->dev));
934
Ville Syrjälä18a80672016-05-16 16:59:40 +0300935 gen9_assert_dbuf_enabled(dev_priv);
936
Ville Syrjälä342be922016-05-13 23:41:39 +0300937 if (IS_BROXTON(dev_priv))
Imre Deak9c8d0b82016-06-13 16:44:34 +0300938 bxt_verify_ddi_phy_power_wells(dev_priv);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100939}
940
941static void gen9_dc_off_power_well_disable(struct drm_i915_private *dev_priv,
942 struct i915_power_well *power_well)
943{
Imre Deakf74ed082016-04-18 14:48:21 +0300944 if (!dev_priv->csr.dmc_payload)
945 return;
946
Imre Deaka37baf32016-02-29 22:49:03 +0200947 if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC6)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100948 skl_enable_dc6(dev_priv);
Imre Deaka37baf32016-02-29 22:49:03 +0200949 else if (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5)
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100950 gen9_enable_dc5(dev_priv);
951}
952
953static void gen9_dc_off_power_well_sync_hw(struct drm_i915_private *dev_priv,
954 struct i915_power_well *power_well)
955{
Imre Deaka37baf32016-02-29 22:49:03 +0200956 if (power_well->count > 0)
957 gen9_dc_off_power_well_enable(dev_priv, power_well);
958 else
959 gen9_dc_off_power_well_disable(dev_priv, power_well);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +0100960}
961
Daniel Vetter9c065a72014-09-30 10:56:38 +0200962static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
963 struct i915_power_well *power_well)
964{
965}
966
967static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
968 struct i915_power_well *power_well)
969{
970 return true;
971}
972
973static void vlv_set_power_well(struct drm_i915_private *dev_priv,
974 struct i915_power_well *power_well, bool enable)
975{
976 enum punit_power_well power_well_id = power_well->data;
977 u32 mask;
978 u32 state;
979 u32 ctrl;
980
981 mask = PUNIT_PWRGT_MASK(power_well_id);
982 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
983 PUNIT_PWRGT_PWR_GATE(power_well_id);
984
985 mutex_lock(&dev_priv->rps.hw_lock);
986
987#define COND \
988 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
989
990 if (COND)
991 goto out;
992
993 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
994 ctrl &= ~mask;
995 ctrl |= state;
996 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
997
998 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +0900999 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001000 state,
1001 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
1002
1003#undef COND
1004
1005out:
1006 mutex_unlock(&dev_priv->rps.hw_lock);
1007}
1008
1009static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
1010 struct i915_power_well *power_well)
1011{
1012 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
1013}
1014
1015static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
1016 struct i915_power_well *power_well)
1017{
1018 vlv_set_power_well(dev_priv, power_well, true);
1019}
1020
1021static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
1022 struct i915_power_well *power_well)
1023{
1024 vlv_set_power_well(dev_priv, power_well, false);
1025}
1026
1027static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
1028 struct i915_power_well *power_well)
1029{
1030 int power_well_id = power_well->data;
1031 bool enabled = false;
1032 u32 mask;
1033 u32 state;
1034 u32 ctrl;
1035
1036 mask = PUNIT_PWRGT_MASK(power_well_id);
1037 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
1038
1039 mutex_lock(&dev_priv->rps.hw_lock);
1040
1041 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
1042 /*
1043 * We only ever set the power-on and power-gate states, anything
1044 * else is unexpected.
1045 */
1046 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
1047 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
1048 if (state == ctrl)
1049 enabled = true;
1050
1051 /*
1052 * A transient state at this point would mean some unexpected party
1053 * is poking at the power controls too.
1054 */
1055 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
1056 WARN_ON(ctrl != state);
1057
1058 mutex_unlock(&dev_priv->rps.hw_lock);
1059
1060 return enabled;
1061}
1062
Ville Syrjälä766078d2016-04-11 16:56:30 +03001063static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
1064{
1065 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
1066
1067 /*
1068 * Disable trickle feed and enable pnd deadline calculation
1069 */
1070 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
1071 I915_WRITE(CBR1_VLV, 0);
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +03001072
1073 WARN_ON(dev_priv->rawclk_freq == 0);
1074
1075 I915_WRITE(RAWCLK_FREQ_VLV,
1076 DIV_ROUND_CLOSEST(dev_priv->rawclk_freq, 1000));
Ville Syrjälä766078d2016-04-11 16:56:30 +03001077}
1078
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001079static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02001080{
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001081 enum pipe pipe;
1082
1083 /*
1084 * Enable the CRI clock source so we can get at the
1085 * display and the reference clock for VGA
1086 * hotplug / manual detection. Supposedly DSI also
1087 * needs the ref clock up and running.
1088 *
1089 * CHV DPLL B/C have some issues if VGA mode is enabled.
1090 */
1091 for_each_pipe(dev_priv->dev, pipe) {
1092 u32 val = I915_READ(DPLL(pipe));
1093
1094 val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1095 if (pipe != PIPE_A)
1096 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1097
1098 I915_WRITE(DPLL(pipe), val);
1099 }
Daniel Vetter9c065a72014-09-30 10:56:38 +02001100
Ville Syrjälä766078d2016-04-11 16:56:30 +03001101 vlv_init_display_clock_gating(dev_priv);
1102
Daniel Vetter9c065a72014-09-30 10:56:38 +02001103 spin_lock_irq(&dev_priv->irq_lock);
1104 valleyview_enable_display_irqs(dev_priv);
1105 spin_unlock_irq(&dev_priv->irq_lock);
1106
1107 /*
1108 * During driver initialization/resume we can avoid restoring the
1109 * part of the HW/SW state that will be inited anyway explicitly.
1110 */
1111 if (dev_priv->power_domains.initializing)
1112 return;
1113
Daniel Vetterb9632912014-09-30 10:56:44 +02001114 intel_hpd_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001115
1116 i915_redisable_vga_power_on(dev_priv->dev);
1117}
1118
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001119static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
1120{
1121 spin_lock_irq(&dev_priv->irq_lock);
1122 valleyview_disable_display_irqs(dev_priv);
1123 spin_unlock_irq(&dev_priv->irq_lock);
1124
Ville Syrjälä2230fde2016-02-19 18:41:52 +02001125 /* make sure we're done processing display irqs */
1126 synchronize_irq(dev_priv->dev->irq);
1127
Imre Deak78597992016-06-16 16:37:20 +03001128 intel_power_sequencer_reset(dev_priv);
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001129}
1130
1131static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
1132 struct i915_power_well *power_well)
1133{
1134 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1135
1136 vlv_set_power_well(dev_priv, power_well, true);
1137
1138 vlv_display_power_well_init(dev_priv);
1139}
1140
Daniel Vetter9c065a72014-09-30 10:56:38 +02001141static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
1142 struct i915_power_well *power_well)
1143{
1144 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
1145
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001146 vlv_display_power_well_deinit(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001147
1148 vlv_set_power_well(dev_priv, power_well, false);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001149}
1150
1151static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1152 struct i915_power_well *power_well)
1153{
1154 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1155
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001156 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001157 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1158
1159 vlv_set_power_well(dev_priv, power_well, true);
1160
1161 /*
1162 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1163 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1164 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1165 * b. The other bits such as sfr settings / modesel may all
1166 * be set to 0.
1167 *
1168 * This should only be done on init and resume from S3 with
1169 * both PLLs disabled, or we risk losing DPIO and PLL
1170 * synchronization.
1171 */
1172 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1173}
1174
1175static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1176 struct i915_power_well *power_well)
1177{
1178 enum pipe pipe;
1179
1180 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
1181
1182 for_each_pipe(dev_priv, pipe)
1183 assert_pll_disabled(dev_priv, pipe);
1184
1185 /* Assert common reset */
1186 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
1187
1188 vlv_set_power_well(dev_priv, power_well, false);
1189}
1190
Ville Syrjälä30142272015-07-08 23:46:01 +03001191#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
1192
1193static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1194 int power_well_id)
1195{
1196 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Ville Syrjälä30142272015-07-08 23:46:01 +03001197 int i;
1198
Imre Deakfc17f222015-11-04 19:24:11 +02001199 for (i = 0; i < power_domains->power_well_count; i++) {
1200 struct i915_power_well *power_well;
1201
1202 power_well = &power_domains->power_wells[i];
Ville Syrjälä30142272015-07-08 23:46:01 +03001203 if (power_well->data == power_well_id)
1204 return power_well;
1205 }
1206
1207 return NULL;
1208}
1209
1210#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
1211
1212static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
1213{
1214 struct i915_power_well *cmn_bc =
1215 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1216 struct i915_power_well *cmn_d =
1217 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1218 u32 phy_control = dev_priv->chv_phy_control;
1219 u32 phy_status = 0;
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001220 u32 phy_status_mask = 0xffffffff;
Ville Syrjälä30142272015-07-08 23:46:01 +03001221 u32 tmp;
1222
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001223 /*
1224 * The BIOS can leave the PHY is some weird state
1225 * where it doesn't fully power down some parts.
1226 * Disable the asserts until the PHY has been fully
1227 * reset (ie. the power well has been disabled at
1228 * least once).
1229 */
1230 if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1231 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1232 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1233 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1234 PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1235 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1236 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1237
1238 if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1239 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1240 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1241 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1242
Ville Syrjälä30142272015-07-08 23:46:01 +03001243 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1244 phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1245
1246 /* this assumes override is only used to enable lanes */
1247 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1248 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1249
1250 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1251 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1252
1253 /* CL1 is on whenever anything is on in either channel */
1254 if (BITS_SET(phy_control,
1255 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1256 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1257 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1258
1259 /*
1260 * The DPLLB check accounts for the pipe B + port A usage
1261 * with CL2 powered up but all the lanes in the second channel
1262 * powered down.
1263 */
1264 if (BITS_SET(phy_control,
1265 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1266 (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1267 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1268
1269 if (BITS_SET(phy_control,
1270 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1271 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1272 if (BITS_SET(phy_control,
1273 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1274 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1275
1276 if (BITS_SET(phy_control,
1277 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1278 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1279 if (BITS_SET(phy_control,
1280 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1281 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1282 }
1283
1284 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1285 phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1286
1287 /* this assumes override is only used to enable lanes */
1288 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1289 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1290
1291 if (BITS_SET(phy_control,
1292 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1293 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1294
1295 if (BITS_SET(phy_control,
1296 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1297 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1298 if (BITS_SET(phy_control,
1299 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1300 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1301 }
1302
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001303 phy_status &= phy_status_mask;
1304
Ville Syrjälä30142272015-07-08 23:46:01 +03001305 /*
1306 * The PHY may be busy with some initial calibration and whatnot,
1307 * so the power state can take a while to actually change.
1308 */
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001309 if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
Ville Syrjälä30142272015-07-08 23:46:01 +03001310 WARN(phy_status != tmp,
1311 "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1312 tmp, phy_status, dev_priv->chv_phy_control);
1313}
1314
1315#undef BITS_SET
1316
Daniel Vetter9c065a72014-09-30 10:56:38 +02001317static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1318 struct i915_power_well *power_well)
1319{
1320 enum dpio_phy phy;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001321 enum pipe pipe;
1322 uint32_t tmp;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001323
1324 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1325 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1326
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001327 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1328 pipe = PIPE_A;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001329 phy = DPIO_PHY0;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001330 } else {
1331 pipe = PIPE_C;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001332 phy = DPIO_PHY1;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001333 }
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001334
1335 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001336 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1337 vlv_set_power_well(dev_priv, power_well, true);
1338
1339 /* Poll for phypwrgood signal */
1340 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
1341 DRM_ERROR("Display PHY %d is not power up\n", phy);
1342
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001343 mutex_lock(&dev_priv->sb_lock);
1344
1345 /* Enable dynamic power down */
1346 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
Ville Syrjäläee279212015-07-08 23:45:57 +03001347 tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1348 DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001349 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1350
1351 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1352 tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1353 tmp |= DPIO_DYNPWRDOWNEN_CH1;
1354 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
Ville Syrjälä3e288782015-07-08 23:45:58 +03001355 } else {
1356 /*
1357 * Force the non-existing CL2 off. BXT does this
1358 * too, so maybe it saves some power even though
1359 * CL2 doesn't exist?
1360 */
1361 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1362 tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1363 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001364 }
1365
1366 mutex_unlock(&dev_priv->sb_lock);
1367
Ville Syrjälä70722462015-04-10 18:21:28 +03001368 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1369 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001370
1371 DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1372 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001373
1374 assert_chv_phy_status(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001375}
1376
1377static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1378 struct i915_power_well *power_well)
1379{
1380 enum dpio_phy phy;
1381
1382 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1383 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1384
1385 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1386 phy = DPIO_PHY0;
1387 assert_pll_disabled(dev_priv, PIPE_A);
1388 assert_pll_disabled(dev_priv, PIPE_B);
1389 } else {
1390 phy = DPIO_PHY1;
1391 assert_pll_disabled(dev_priv, PIPE_C);
1392 }
1393
Ville Syrjälä70722462015-04-10 18:21:28 +03001394 dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1395 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001396
1397 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001398
1399 DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1400 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001401
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001402 /* PHY is fully reset now, so we can enable the PHY state asserts */
1403 dev_priv->chv_phy_assert[phy] = true;
1404
Ville Syrjälä30142272015-07-08 23:46:01 +03001405 assert_chv_phy_status(dev_priv);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001406}
1407
Ville Syrjälä6669e392015-07-08 23:46:00 +03001408static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1409 enum dpio_channel ch, bool override, unsigned int mask)
1410{
1411 enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1412 u32 reg, val, expected, actual;
1413
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001414 /*
1415 * The BIOS can leave the PHY is some weird state
1416 * where it doesn't fully power down some parts.
1417 * Disable the asserts until the PHY has been fully
1418 * reset (ie. the power well has been disabled at
1419 * least once).
1420 */
1421 if (!dev_priv->chv_phy_assert[phy])
1422 return;
1423
Ville Syrjälä6669e392015-07-08 23:46:00 +03001424 if (ch == DPIO_CH0)
1425 reg = _CHV_CMN_DW0_CH0;
1426 else
1427 reg = _CHV_CMN_DW6_CH1;
1428
1429 mutex_lock(&dev_priv->sb_lock);
1430 val = vlv_dpio_read(dev_priv, pipe, reg);
1431 mutex_unlock(&dev_priv->sb_lock);
1432
1433 /*
1434 * This assumes !override is only used when the port is disabled.
1435 * All lanes should power down even without the override when
1436 * the port is disabled.
1437 */
1438 if (!override || mask == 0xf) {
1439 expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1440 /*
1441 * If CH1 common lane is not active anymore
1442 * (eg. for pipe B DPLL) the entire channel will
1443 * shut down, which causes the common lane registers
1444 * to read as 0. That means we can't actually check
1445 * the lane power down status bits, but as the entire
1446 * register reads as 0 it's a good indication that the
1447 * channel is indeed entirely powered down.
1448 */
1449 if (ch == DPIO_CH1 && val == 0)
1450 expected = 0;
1451 } else if (mask != 0x0) {
1452 expected = DPIO_ANYDL_POWERDOWN;
1453 } else {
1454 expected = 0;
1455 }
1456
1457 if (ch == DPIO_CH0)
1458 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1459 else
1460 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1461 actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1462
1463 WARN(actual != expected,
1464 "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1465 !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1466 !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1467 reg, val);
1468}
1469
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001470bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1471 enum dpio_channel ch, bool override)
1472{
1473 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1474 bool was_override;
1475
1476 mutex_lock(&power_domains->lock);
1477
1478 was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1479
1480 if (override == was_override)
1481 goto out;
1482
1483 if (override)
1484 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1485 else
1486 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1487
1488 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1489
1490 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1491 phy, ch, dev_priv->chv_phy_control);
1492
Ville Syrjälä30142272015-07-08 23:46:01 +03001493 assert_chv_phy_status(dev_priv);
1494
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001495out:
1496 mutex_unlock(&power_domains->lock);
1497
1498 return was_override;
1499}
1500
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001501void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1502 bool override, unsigned int mask)
1503{
1504 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1505 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1506 enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1507 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1508
1509 mutex_lock(&power_domains->lock);
1510
1511 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1512 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1513
1514 if (override)
1515 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1516 else
1517 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1518
1519 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1520
1521 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1522 phy, ch, mask, dev_priv->chv_phy_control);
1523
Ville Syrjälä30142272015-07-08 23:46:01 +03001524 assert_chv_phy_status(dev_priv);
1525
Ville Syrjälä6669e392015-07-08 23:46:00 +03001526 assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1527
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001528 mutex_unlock(&power_domains->lock);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001529}
1530
1531static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1532 struct i915_power_well *power_well)
1533{
1534 enum pipe pipe = power_well->data;
1535 bool enabled;
1536 u32 state, ctrl;
1537
1538 mutex_lock(&dev_priv->rps.hw_lock);
1539
1540 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1541 /*
1542 * We only ever set the power-on and power-gate states, anything
1543 * else is unexpected.
1544 */
1545 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1546 enabled = state == DP_SSS_PWR_ON(pipe);
1547
1548 /*
1549 * A transient state at this point would mean some unexpected party
1550 * is poking at the power controls too.
1551 */
1552 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1553 WARN_ON(ctrl << 16 != state);
1554
1555 mutex_unlock(&dev_priv->rps.hw_lock);
1556
1557 return enabled;
1558}
1559
1560static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1561 struct i915_power_well *power_well,
1562 bool enable)
1563{
1564 enum pipe pipe = power_well->data;
1565 u32 state;
1566 u32 ctrl;
1567
1568 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1569
1570 mutex_lock(&dev_priv->rps.hw_lock);
1571
1572#define COND \
1573 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1574
1575 if (COND)
1576 goto out;
1577
1578 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1579 ctrl &= ~DP_SSC_MASK(pipe);
1580 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1581 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1582
1583 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +09001584 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001585 state,
1586 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1587
1588#undef COND
1589
1590out:
1591 mutex_unlock(&dev_priv->rps.hw_lock);
1592}
1593
1594static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1595 struct i915_power_well *power_well)
1596{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001597 WARN_ON_ONCE(power_well->data != PIPE_A);
1598
Daniel Vetter9c065a72014-09-30 10:56:38 +02001599 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1600}
1601
1602static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1603 struct i915_power_well *power_well)
1604{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001605 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001606
1607 chv_set_pipe_power_well(dev_priv, power_well, true);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001608
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001609 vlv_display_power_well_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001610}
1611
1612static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1613 struct i915_power_well *power_well)
1614{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001615 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001616
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001617 vlv_display_power_well_deinit(dev_priv);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001618
Daniel Vetter9c065a72014-09-30 10:56:38 +02001619 chv_set_pipe_power_well(dev_priv, power_well, false);
1620}
1621
Imre Deak09731282016-02-17 14:17:42 +02001622static void
1623__intel_display_power_get_domain(struct drm_i915_private *dev_priv,
1624 enum intel_display_power_domain domain)
1625{
1626 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1627 struct i915_power_well *power_well;
1628 int i;
1629
Imre Deakb409ca92016-06-13 16:44:33 +03001630 for_each_power_well(i, power_well, BIT(domain), power_domains)
1631 intel_power_well_get(dev_priv, power_well);
Imre Deak09731282016-02-17 14:17:42 +02001632
1633 power_domains->domain_use_count[domain]++;
1634}
1635
Daniel Vettere4e76842014-09-30 10:56:42 +02001636/**
1637 * intel_display_power_get - grab a power domain reference
1638 * @dev_priv: i915 device instance
1639 * @domain: power domain to reference
1640 *
1641 * This function grabs a power domain reference for @domain and ensures that the
1642 * power domain and all its parents are powered up. Therefore users should only
1643 * grab a reference to the innermost power domain they need.
1644 *
1645 * Any power domain reference obtained by this function must have a symmetric
1646 * call to intel_display_power_put() to release the reference again.
1647 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001648void intel_display_power_get(struct drm_i915_private *dev_priv,
1649 enum intel_display_power_domain domain)
1650{
Imre Deak09731282016-02-17 14:17:42 +02001651 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001652
1653 intel_runtime_pm_get(dev_priv);
1654
Imre Deak09731282016-02-17 14:17:42 +02001655 mutex_lock(&power_domains->lock);
1656
1657 __intel_display_power_get_domain(dev_priv, domain);
1658
1659 mutex_unlock(&power_domains->lock);
1660}
1661
1662/**
1663 * intel_display_power_get_if_enabled - grab a reference for an enabled display power domain
1664 * @dev_priv: i915 device instance
1665 * @domain: power domain to reference
1666 *
1667 * This function grabs a power domain reference for @domain and ensures that the
1668 * power domain and all its parents are powered up. Therefore users should only
1669 * grab a reference to the innermost power domain they need.
1670 *
1671 * Any power domain reference obtained by this function must have a symmetric
1672 * call to intel_display_power_put() to release the reference again.
1673 */
1674bool intel_display_power_get_if_enabled(struct drm_i915_private *dev_priv,
1675 enum intel_display_power_domain domain)
1676{
1677 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1678 bool is_enabled;
1679
1680 if (!intel_runtime_pm_get_if_in_use(dev_priv))
1681 return false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001682
1683 mutex_lock(&power_domains->lock);
1684
Imre Deak09731282016-02-17 14:17:42 +02001685 if (__intel_display_power_is_enabled(dev_priv, domain)) {
1686 __intel_display_power_get_domain(dev_priv, domain);
1687 is_enabled = true;
1688 } else {
1689 is_enabled = false;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001690 }
1691
Daniel Vetter9c065a72014-09-30 10:56:38 +02001692 mutex_unlock(&power_domains->lock);
Imre Deak09731282016-02-17 14:17:42 +02001693
1694 if (!is_enabled)
1695 intel_runtime_pm_put(dev_priv);
1696
1697 return is_enabled;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001698}
1699
Daniel Vettere4e76842014-09-30 10:56:42 +02001700/**
1701 * intel_display_power_put - release a power domain reference
1702 * @dev_priv: i915 device instance
1703 * @domain: power domain to reference
1704 *
1705 * This function drops the power domain reference obtained by
1706 * intel_display_power_get() and might power down the corresponding hardware
1707 * block right away if this is the last reference.
1708 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001709void intel_display_power_put(struct drm_i915_private *dev_priv,
1710 enum intel_display_power_domain domain)
1711{
1712 struct i915_power_domains *power_domains;
1713 struct i915_power_well *power_well;
1714 int i;
1715
1716 power_domains = &dev_priv->power_domains;
1717
1718 mutex_lock(&power_domains->lock);
1719
Daniel Stone11c86db2015-11-20 15:55:34 +00001720 WARN(!power_domains->domain_use_count[domain],
1721 "Use count on domain %s is already zero\n",
1722 intel_display_power_domain_str(domain));
Daniel Vetter9c065a72014-09-30 10:56:38 +02001723 power_domains->domain_use_count[domain]--;
1724
Imre Deakb409ca92016-06-13 16:44:33 +03001725 for_each_power_well_rev(i, power_well, BIT(domain), power_domains)
1726 intel_power_well_put(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001727
1728 mutex_unlock(&power_domains->lock);
1729
1730 intel_runtime_pm_put(dev_priv);
1731}
1732
Ville Syrjälä9d0996b2016-04-18 14:02:28 +03001733#define HSW_DISPLAY_POWER_DOMAINS ( \
1734 BIT(POWER_DOMAIN_PIPE_B) | \
1735 BIT(POWER_DOMAIN_PIPE_C) | \
1736 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1737 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1738 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1739 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1740 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1741 BIT(POWER_DOMAIN_TRANSCODER_C) | \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001742 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1743 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1744 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Ville Syrjälä9d0996b2016-04-18 14:02:28 +03001745 BIT(POWER_DOMAIN_PORT_CRT) | /* DDI E */ \
1746 BIT(POWER_DOMAIN_VGA) | \
1747 BIT(POWER_DOMAIN_AUDIO) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001748 BIT(POWER_DOMAIN_INIT))
1749
Ville Syrjälä9d0996b2016-04-18 14:02:28 +03001750#define BDW_DISPLAY_POWER_DOMAINS ( \
1751 BIT(POWER_DOMAIN_PIPE_B) | \
1752 BIT(POWER_DOMAIN_PIPE_C) | \
1753 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1754 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1755 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1756 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1757 BIT(POWER_DOMAIN_TRANSCODER_C) | \
1758 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1759 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1760 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
1761 BIT(POWER_DOMAIN_PORT_CRT) | /* DDI E */ \
1762 BIT(POWER_DOMAIN_VGA) | \
1763 BIT(POWER_DOMAIN_AUDIO) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001764 BIT(POWER_DOMAIN_INIT))
1765
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001766#define VLV_DISPLAY_POWER_DOMAINS ( \
1767 BIT(POWER_DOMAIN_PIPE_A) | \
1768 BIT(POWER_DOMAIN_PIPE_B) | \
1769 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1770 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1771 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1772 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1773 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1774 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1775 BIT(POWER_DOMAIN_PORT_DSI) | \
1776 BIT(POWER_DOMAIN_PORT_CRT) | \
1777 BIT(POWER_DOMAIN_VGA) | \
1778 BIT(POWER_DOMAIN_AUDIO) | \
1779 BIT(POWER_DOMAIN_AUX_B) | \
1780 BIT(POWER_DOMAIN_AUX_C) | \
1781 BIT(POWER_DOMAIN_GMBUS) | \
1782 BIT(POWER_DOMAIN_INIT))
Daniel Vetter9c065a72014-09-30 10:56:38 +02001783
1784#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001785 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1786 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001787 BIT(POWER_DOMAIN_PORT_CRT) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001788 BIT(POWER_DOMAIN_AUX_B) | \
1789 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001790 BIT(POWER_DOMAIN_INIT))
1791
1792#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001793 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001794 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001795 BIT(POWER_DOMAIN_INIT))
1796
1797#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001798 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001799 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001800 BIT(POWER_DOMAIN_INIT))
1801
1802#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001803 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001804 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001805 BIT(POWER_DOMAIN_INIT))
1806
1807#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001808 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001809 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001810 BIT(POWER_DOMAIN_INIT))
1811
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03001812#define CHV_DISPLAY_POWER_DOMAINS ( \
1813 BIT(POWER_DOMAIN_PIPE_A) | \
1814 BIT(POWER_DOMAIN_PIPE_B) | \
1815 BIT(POWER_DOMAIN_PIPE_C) | \
1816 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
1817 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
1818 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
1819 BIT(POWER_DOMAIN_TRANSCODER_A) | \
1820 BIT(POWER_DOMAIN_TRANSCODER_B) | \
1821 BIT(POWER_DOMAIN_TRANSCODER_C) | \
1822 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1823 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
1824 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
1825 BIT(POWER_DOMAIN_PORT_DSI) | \
1826 BIT(POWER_DOMAIN_VGA) | \
1827 BIT(POWER_DOMAIN_AUDIO) | \
1828 BIT(POWER_DOMAIN_AUX_B) | \
1829 BIT(POWER_DOMAIN_AUX_C) | \
1830 BIT(POWER_DOMAIN_AUX_D) | \
1831 BIT(POWER_DOMAIN_GMBUS) | \
1832 BIT(POWER_DOMAIN_INIT))
1833
Daniel Vetter9c065a72014-09-30 10:56:38 +02001834#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001835 BIT(POWER_DOMAIN_PORT_DDI_B_LANES) | \
1836 BIT(POWER_DOMAIN_PORT_DDI_C_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001837 BIT(POWER_DOMAIN_AUX_B) | \
1838 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001839 BIT(POWER_DOMAIN_INIT))
1840
1841#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
Patrik Jakobsson6331a702015-11-09 16:48:21 +01001842 BIT(POWER_DOMAIN_PORT_DDI_D_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001843 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001844 BIT(POWER_DOMAIN_INIT))
1845
Daniel Vetter9c065a72014-09-30 10:56:38 +02001846static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1847 .sync_hw = i9xx_always_on_power_well_noop,
1848 .enable = i9xx_always_on_power_well_noop,
1849 .disable = i9xx_always_on_power_well_noop,
1850 .is_enabled = i9xx_always_on_power_well_enabled,
1851};
1852
1853static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1854 .sync_hw = chv_pipe_power_well_sync_hw,
1855 .enable = chv_pipe_power_well_enable,
1856 .disable = chv_pipe_power_well_disable,
1857 .is_enabled = chv_pipe_power_well_enabled,
1858};
1859
1860static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1861 .sync_hw = vlv_power_well_sync_hw,
1862 .enable = chv_dpio_cmn_power_well_enable,
1863 .disable = chv_dpio_cmn_power_well_disable,
1864 .is_enabled = vlv_power_well_enabled,
1865};
1866
1867static struct i915_power_well i9xx_always_on_power_well[] = {
1868 {
1869 .name = "always-on",
1870 .always_on = 1,
1871 .domains = POWER_DOMAIN_MASK,
1872 .ops = &i9xx_always_on_power_well_ops,
1873 },
1874};
1875
1876static const struct i915_power_well_ops hsw_power_well_ops = {
1877 .sync_hw = hsw_power_well_sync_hw,
1878 .enable = hsw_power_well_enable,
1879 .disable = hsw_power_well_disable,
1880 .is_enabled = hsw_power_well_enabled,
1881};
1882
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001883static const struct i915_power_well_ops skl_power_well_ops = {
1884 .sync_hw = skl_power_well_sync_hw,
1885 .enable = skl_power_well_enable,
1886 .disable = skl_power_well_disable,
1887 .is_enabled = skl_power_well_enabled,
1888};
1889
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01001890static const struct i915_power_well_ops gen9_dc_off_power_well_ops = {
1891 .sync_hw = gen9_dc_off_power_well_sync_hw,
1892 .enable = gen9_dc_off_power_well_enable,
1893 .disable = gen9_dc_off_power_well_disable,
1894 .is_enabled = gen9_dc_off_power_well_enabled,
1895};
1896
Imre Deak9c8d0b82016-06-13 16:44:34 +03001897static const struct i915_power_well_ops bxt_dpio_cmn_power_well_ops = {
1898 .sync_hw = bxt_dpio_cmn_power_well_sync_hw,
1899 .enable = bxt_dpio_cmn_power_well_enable,
1900 .disable = bxt_dpio_cmn_power_well_disable,
1901 .is_enabled = bxt_dpio_cmn_power_well_enabled,
1902};
1903
Daniel Vetter9c065a72014-09-30 10:56:38 +02001904static struct i915_power_well hsw_power_wells[] = {
1905 {
1906 .name = "always-on",
1907 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001908 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001909 .ops = &i9xx_always_on_power_well_ops,
1910 },
1911 {
1912 .name = "display",
1913 .domains = HSW_DISPLAY_POWER_DOMAINS,
1914 .ops = &hsw_power_well_ops,
1915 },
1916};
1917
1918static struct i915_power_well bdw_power_wells[] = {
1919 {
1920 .name = "always-on",
1921 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001922 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001923 .ops = &i9xx_always_on_power_well_ops,
1924 },
1925 {
1926 .name = "display",
1927 .domains = BDW_DISPLAY_POWER_DOMAINS,
1928 .ops = &hsw_power_well_ops,
1929 },
1930};
1931
1932static const struct i915_power_well_ops vlv_display_power_well_ops = {
1933 .sync_hw = vlv_power_well_sync_hw,
1934 .enable = vlv_display_power_well_enable,
1935 .disable = vlv_display_power_well_disable,
1936 .is_enabled = vlv_power_well_enabled,
1937};
1938
1939static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1940 .sync_hw = vlv_power_well_sync_hw,
1941 .enable = vlv_dpio_cmn_power_well_enable,
1942 .disable = vlv_dpio_cmn_power_well_disable,
1943 .is_enabled = vlv_power_well_enabled,
1944};
1945
1946static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1947 .sync_hw = vlv_power_well_sync_hw,
1948 .enable = vlv_power_well_enable,
1949 .disable = vlv_power_well_disable,
1950 .is_enabled = vlv_power_well_enabled,
1951};
1952
1953static struct i915_power_well vlv_power_wells[] = {
1954 {
1955 .name = "always-on",
1956 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03001957 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001958 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02001959 .data = PUNIT_POWER_WELL_ALWAYS_ON,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001960 },
1961 {
1962 .name = "display",
1963 .domains = VLV_DISPLAY_POWER_DOMAINS,
1964 .data = PUNIT_POWER_WELL_DISP2D,
1965 .ops = &vlv_display_power_well_ops,
1966 },
1967 {
1968 .name = "dpio-tx-b-01",
1969 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1970 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1971 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1972 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1973 .ops = &vlv_dpio_power_well_ops,
1974 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1975 },
1976 {
1977 .name = "dpio-tx-b-23",
1978 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1979 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1980 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1981 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1982 .ops = &vlv_dpio_power_well_ops,
1983 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1984 },
1985 {
1986 .name = "dpio-tx-c-01",
1987 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1988 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1989 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1990 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1991 .ops = &vlv_dpio_power_well_ops,
1992 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1993 },
1994 {
1995 .name = "dpio-tx-c-23",
1996 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1997 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1998 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1999 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
2000 .ops = &vlv_dpio_power_well_ops,
2001 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
2002 },
2003 {
2004 .name = "dpio-common",
2005 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
2006 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
2007 .ops = &vlv_dpio_cmn_power_well_ops,
2008 },
2009};
2010
2011static struct i915_power_well chv_power_wells[] = {
2012 {
2013 .name = "always-on",
2014 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03002015 .domains = POWER_DOMAIN_MASK,
Daniel Vetter9c065a72014-09-30 10:56:38 +02002016 .ops = &i9xx_always_on_power_well_ops,
2017 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02002018 {
2019 .name = "display",
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02002020 /*
Ville Syrjäläfde61e42015-05-26 20:22:39 +03002021 * Pipe A power well is the new disp2d well. Pipe B and C
2022 * power wells don't actually exist. Pipe A power well is
2023 * required for any pipe to work.
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02002024 */
Ville Syrjälä465ac0c2016-04-18 14:02:27 +03002025 .domains = CHV_DISPLAY_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02002026 .data = PIPE_A,
2027 .ops = &chv_pipe_power_well_ops,
2028 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02002029 {
2030 .name = "dpio-common-bc",
Ville Syrjälä71849b62015-04-10 18:21:29 +03002031 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02002032 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
2033 .ops = &chv_dpio_cmn_power_well_ops,
2034 },
2035 {
2036 .name = "dpio-common-d",
Ville Syrjälä71849b62015-04-10 18:21:29 +03002037 .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02002038 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
2039 .ops = &chv_dpio_cmn_power_well_ops,
2040 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02002041};
2042
Suketu Shah5aefb232015-04-16 14:22:10 +05302043bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
2044 int power_well_id)
2045{
2046 struct i915_power_well *power_well;
2047 bool ret;
2048
2049 power_well = lookup_power_well(dev_priv, power_well_id);
2050 ret = power_well->ops->is_enabled(dev_priv, power_well);
2051
2052 return ret;
2053}
2054
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002055static struct i915_power_well skl_power_wells[] = {
2056 {
2057 .name = "always-on",
2058 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03002059 .domains = POWER_DOMAIN_MASK,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002060 .ops = &i9xx_always_on_power_well_ops,
Imre Deak56fcfd62015-11-04 19:24:10 +02002061 .data = SKL_DISP_PW_ALWAYS_ON,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002062 },
2063 {
2064 .name = "power well 1",
Imre Deak4a76f292015-11-04 19:24:15 +02002065 /* Handled by the DMC firmware */
2066 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002067 .ops = &skl_power_well_ops,
2068 .data = SKL_DISP_PW_1,
2069 },
2070 {
2071 .name = "MISC IO power well",
Imre Deak4a76f292015-11-04 19:24:15 +02002072 /* Handled by the DMC firmware */
2073 .domains = 0,
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002074 .ops = &skl_power_well_ops,
2075 .data = SKL_DISP_PW_MISC_IO,
2076 },
2077 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01002078 .name = "DC off",
2079 .domains = SKL_DISPLAY_DC_OFF_POWER_DOMAINS,
2080 .ops = &gen9_dc_off_power_well_ops,
2081 .data = SKL_DISP_PW_DC_OFF,
2082 },
2083 {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002084 .name = "power well 2",
2085 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2086 .ops = &skl_power_well_ops,
2087 .data = SKL_DISP_PW_2,
2088 },
2089 {
2090 .name = "DDI A/E power well",
2091 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
2092 .ops = &skl_power_well_ops,
2093 .data = SKL_DISP_PW_DDI_A_E,
2094 },
2095 {
2096 .name = "DDI B power well",
2097 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
2098 .ops = &skl_power_well_ops,
2099 .data = SKL_DISP_PW_DDI_B,
2100 },
2101 {
2102 .name = "DDI C power well",
2103 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
2104 .ops = &skl_power_well_ops,
2105 .data = SKL_DISP_PW_DDI_C,
2106 },
2107 {
2108 .name = "DDI D power well",
2109 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
2110 .ops = &skl_power_well_ops,
2111 .data = SKL_DISP_PW_DDI_D,
2112 },
2113};
2114
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302115static struct i915_power_well bxt_power_wells[] = {
2116 {
2117 .name = "always-on",
2118 .always_on = 1,
Ville Syrjälä998bd662016-04-18 14:02:26 +03002119 .domains = POWER_DOMAIN_MASK,
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302120 .ops = &i9xx_always_on_power_well_ops,
2121 },
2122 {
2123 .name = "power well 1",
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002124 .domains = 0,
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302125 .ops = &skl_power_well_ops,
2126 .data = SKL_DISP_PW_1,
2127 },
2128 {
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01002129 .name = "DC off",
2130 .domains = BXT_DISPLAY_DC_OFF_POWER_DOMAINS,
2131 .ops = &gen9_dc_off_power_well_ops,
2132 .data = SKL_DISP_PW_DC_OFF,
2133 },
2134 {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302135 .name = "power well 2",
2136 .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
2137 .ops = &skl_power_well_ops,
2138 .data = SKL_DISP_PW_2,
Patrik Jakobsson9f836f92015-11-16 16:20:01 +01002139 },
Imre Deak9c8d0b82016-06-13 16:44:34 +03002140 {
2141 .name = "dpio-common-a",
2142 .domains = BXT_DPIO_CMN_A_POWER_DOMAINS,
2143 .ops = &bxt_dpio_cmn_power_well_ops,
2144 .data = BXT_DPIO_CMN_A,
2145 },
2146 {
2147 .name = "dpio-common-bc",
2148 .domains = BXT_DPIO_CMN_BC_POWER_DOMAINS,
2149 .ops = &bxt_dpio_cmn_power_well_ops,
2150 .data = BXT_DPIO_CMN_BC,
2151 },
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302152};
2153
Imre Deak1b0e3a02015-11-05 23:04:11 +02002154static int
2155sanitize_disable_power_well_option(const struct drm_i915_private *dev_priv,
2156 int disable_power_well)
2157{
2158 if (disable_power_well >= 0)
2159 return !!disable_power_well;
2160
Imre Deak1b0e3a02015-11-05 23:04:11 +02002161 return 1;
2162}
2163
Imre Deaka37baf32016-02-29 22:49:03 +02002164static uint32_t get_allowed_dc_mask(const struct drm_i915_private *dev_priv,
2165 int enable_dc)
2166{
2167 uint32_t mask;
2168 int requested_dc;
2169 int max_dc;
2170
2171 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
2172 max_dc = 2;
2173 mask = 0;
2174 } else if (IS_BROXTON(dev_priv)) {
2175 max_dc = 1;
2176 /*
2177 * DC9 has a separate HW flow from the rest of the DC states,
2178 * not depending on the DMC firmware. It's needed by system
2179 * suspend/resume, so allow it unconditionally.
2180 */
2181 mask = DC_STATE_EN_DC9;
2182 } else {
2183 max_dc = 0;
2184 mask = 0;
2185 }
2186
Imre Deak66e2c4c2016-02-29 22:49:04 +02002187 if (!i915.disable_power_well)
2188 max_dc = 0;
2189
Imre Deaka37baf32016-02-29 22:49:03 +02002190 if (enable_dc >= 0 && enable_dc <= max_dc) {
2191 requested_dc = enable_dc;
2192 } else if (enable_dc == -1) {
2193 requested_dc = max_dc;
2194 } else if (enable_dc > max_dc && enable_dc <= 2) {
2195 DRM_DEBUG_KMS("Adjusting requested max DC state (%d->%d)\n",
2196 enable_dc, max_dc);
2197 requested_dc = max_dc;
2198 } else {
2199 DRM_ERROR("Unexpected value for enable_dc (%d)\n", enable_dc);
2200 requested_dc = max_dc;
2201 }
2202
2203 if (requested_dc > 1)
2204 mask |= DC_STATE_EN_UPTO_DC6;
2205 if (requested_dc > 0)
2206 mask |= DC_STATE_EN_UPTO_DC5;
2207
2208 DRM_DEBUG_KMS("Allowed DC state mask %02x\n", mask);
2209
2210 return mask;
2211}
2212
Daniel Vetter9c065a72014-09-30 10:56:38 +02002213#define set_power_wells(power_domains, __power_wells) ({ \
2214 (power_domains)->power_wells = (__power_wells); \
2215 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
2216})
2217
Daniel Vettere4e76842014-09-30 10:56:42 +02002218/**
2219 * intel_power_domains_init - initializes the power domain structures
2220 * @dev_priv: i915 device instance
2221 *
2222 * Initializes the power domain structures for @dev_priv depending upon the
2223 * supported platform.
2224 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002225int intel_power_domains_init(struct drm_i915_private *dev_priv)
2226{
2227 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2228
Imre Deak1b0e3a02015-11-05 23:04:11 +02002229 i915.disable_power_well = sanitize_disable_power_well_option(dev_priv,
2230 i915.disable_power_well);
Imre Deaka37baf32016-02-29 22:49:03 +02002231 dev_priv->csr.allowed_dc_mask = get_allowed_dc_mask(dev_priv,
2232 i915.enable_dc);
Imre Deak1b0e3a02015-11-05 23:04:11 +02002233
Ville Syrjäläf0ab43e2015-11-09 16:48:19 +01002234 BUILD_BUG_ON(POWER_DOMAIN_NUM > 31);
2235
Daniel Vetter9c065a72014-09-30 10:56:38 +02002236 mutex_init(&power_domains->lock);
2237
2238 /*
2239 * The enabling order will be from lower to higher indexed wells,
2240 * the disabling order is reversed.
2241 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002242 if (IS_HASWELL(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002243 set_power_wells(power_domains, hsw_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002244 } else if (IS_BROADWELL(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002245 set_power_wells(power_domains, bdw_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002246 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00002247 set_power_wells(power_domains, skl_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002248 } else if (IS_BROXTON(dev_priv)) {
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05302249 set_power_wells(power_domains, bxt_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002250 } else if (IS_CHERRYVIEW(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002251 set_power_wells(power_domains, chv_power_wells);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002252 } else if (IS_VALLEYVIEW(dev_priv)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002253 set_power_wells(power_domains, vlv_power_wells);
2254 } else {
2255 set_power_wells(power_domains, i9xx_always_on_power_well);
2256 }
2257
2258 return 0;
2259}
2260
Daniel Vettere4e76842014-09-30 10:56:42 +02002261/**
2262 * intel_power_domains_fini - finalizes the power domain structures
2263 * @dev_priv: i915 device instance
2264 *
2265 * Finalizes the power domain structures for @dev_priv depending upon the
2266 * supported platform. This function also disables runtime pm and ensures that
2267 * the device stays powered up so that the driver can be reloaded.
2268 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002269void intel_power_domains_fini(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002270{
Imre Deak25b181b2015-12-17 13:44:56 +02002271 struct device *device = &dev_priv->dev->pdev->dev;
2272
Imre Deakaabee1b2015-12-15 20:10:29 +02002273 /*
2274 * The i915.ko module is still not prepared to be loaded when
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002275 * the power well is not enabled, so just enable it in case
Imre Deakaabee1b2015-12-15 20:10:29 +02002276 * we're going to unload/reload.
2277 * The following also reacquires the RPM reference the core passed
2278 * to the driver during loading, which is dropped in
2279 * intel_runtime_pm_enable(). We have to hand back the control of the
2280 * device to the core with this reference held.
2281 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002282 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002283
2284 /* Remove the refcount we took to keep power well support disabled. */
2285 if (!i915.disable_power_well)
2286 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak25b181b2015-12-17 13:44:56 +02002287
2288 /*
2289 * Remove the refcount we took in intel_runtime_pm_enable() in case
2290 * the platform doesn't support runtime PM.
2291 */
2292 if (!HAS_RUNTIME_PM(dev_priv))
2293 pm_runtime_put(device);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002294}
2295
Imre Deak30eade12015-11-04 19:24:13 +02002296static void intel_power_domains_sync_hw(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002297{
2298 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2299 struct i915_power_well *power_well;
2300 int i;
2301
2302 mutex_lock(&power_domains->lock);
2303 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
2304 power_well->ops->sync_hw(dev_priv, power_well);
2305 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
2306 power_well);
2307 }
2308 mutex_unlock(&power_domains->lock);
2309}
2310
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002311static void gen9_dbuf_enable(struct drm_i915_private *dev_priv)
2312{
2313 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
2314 POSTING_READ(DBUF_CTL);
2315
2316 udelay(10);
2317
2318 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
2319 DRM_ERROR("DBuf power enable timeout\n");
2320}
2321
2322static void gen9_dbuf_disable(struct drm_i915_private *dev_priv)
2323{
2324 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
2325 POSTING_READ(DBUF_CTL);
2326
2327 udelay(10);
2328
2329 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
2330 DRM_ERROR("DBuf power disable timeout!\n");
2331}
2332
Imre Deak73dfc222015-11-17 17:33:53 +02002333static void skl_display_core_init(struct drm_i915_private *dev_priv,
Imre Deak443a93a2016-04-04 15:42:57 +03002334 bool resume)
Imre Deak73dfc222015-11-17 17:33:53 +02002335{
2336 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deak443a93a2016-04-04 15:42:57 +03002337 struct i915_power_well *well;
Imre Deak73dfc222015-11-17 17:33:53 +02002338 uint32_t val;
2339
Imre Deakd26fa1d2015-11-04 19:24:17 +02002340 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2341
Imre Deak73dfc222015-11-17 17:33:53 +02002342 /* enable PCH reset handshake */
2343 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2344 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
2345
2346 /* enable PG1 and Misc I/O */
2347 mutex_lock(&power_domains->lock);
Imre Deak443a93a2016-04-04 15:42:57 +03002348
2349 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2350 intel_power_well_enable(dev_priv, well);
2351
2352 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2353 intel_power_well_enable(dev_priv, well);
2354
Imre Deak73dfc222015-11-17 17:33:53 +02002355 mutex_unlock(&power_domains->lock);
2356
Imre Deak73dfc222015-11-17 17:33:53 +02002357 skl_init_cdclk(dev_priv);
2358
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002359 gen9_dbuf_enable(dev_priv);
2360
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03002361 if (resume && dev_priv->csr.dmc_payload)
Imre Deak2abc5252016-03-04 21:57:41 +02002362 intel_csr_load_program(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002363}
2364
2365static void skl_display_core_uninit(struct drm_i915_private *dev_priv)
2366{
2367 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deak443a93a2016-04-04 15:42:57 +03002368 struct i915_power_well *well;
Imre Deak73dfc222015-11-17 17:33:53 +02002369
Imre Deakd26fa1d2015-11-04 19:24:17 +02002370 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2371
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002372 gen9_dbuf_disable(dev_priv);
2373
Imre Deak73dfc222015-11-17 17:33:53 +02002374 skl_uninit_cdclk(dev_priv);
2375
2376 /* The spec doesn't call for removing the reset handshake flag */
2377 /* disable PG1 and Misc I/O */
Imre Deak443a93a2016-04-04 15:42:57 +03002378
Imre Deak73dfc222015-11-17 17:33:53 +02002379 mutex_lock(&power_domains->lock);
Imre Deak443a93a2016-04-04 15:42:57 +03002380
2381 well = lookup_power_well(dev_priv, SKL_DISP_PW_MISC_IO);
2382 intel_power_well_disable(dev_priv, well);
2383
2384 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2385 intel_power_well_disable(dev_priv, well);
2386
Imre Deak73dfc222015-11-17 17:33:53 +02002387 mutex_unlock(&power_domains->lock);
2388}
2389
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002390void bxt_display_core_init(struct drm_i915_private *dev_priv,
2391 bool resume)
2392{
2393 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2394 struct i915_power_well *well;
2395 uint32_t val;
2396
2397 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2398
2399 /*
2400 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
2401 * or else the reset will hang because there is no PCH to respond.
2402 * Move the handshake programming to initialization sequence.
2403 * Previously was left up to BIOS.
2404 */
2405 val = I915_READ(HSW_NDE_RSTWRN_OPT);
2406 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
2407 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
2408
2409 /* Enable PG1 */
2410 mutex_lock(&power_domains->lock);
2411
2412 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2413 intel_power_well_enable(dev_priv, well);
2414
2415 mutex_unlock(&power_domains->lock);
2416
Imre Deak324513c2016-06-13 16:44:36 +03002417 bxt_init_cdclk(dev_priv);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002418
2419 gen9_dbuf_enable(dev_priv);
2420
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002421 if (resume && dev_priv->csr.dmc_payload)
2422 intel_csr_load_program(dev_priv);
2423}
2424
2425void bxt_display_core_uninit(struct drm_i915_private *dev_priv)
2426{
2427 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2428 struct i915_power_well *well;
2429
2430 gen9_set_dc_state(dev_priv, DC_STATE_DISABLE);
2431
Ville Syrjälä70c2c182016-05-13 23:41:30 +03002432 gen9_dbuf_disable(dev_priv);
2433
Imre Deak324513c2016-06-13 16:44:36 +03002434 bxt_uninit_cdclk(dev_priv);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002435
2436 /* The spec doesn't call for removing the reset handshake flag */
2437
2438 /* Disable PG1 */
2439 mutex_lock(&power_domains->lock);
2440
2441 well = lookup_power_well(dev_priv, SKL_DISP_PW_1);
2442 intel_power_well_disable(dev_priv, well);
2443
2444 mutex_unlock(&power_domains->lock);
2445}
2446
Ville Syrjälä70722462015-04-10 18:21:28 +03002447static void chv_phy_control_init(struct drm_i915_private *dev_priv)
2448{
2449 struct i915_power_well *cmn_bc =
2450 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2451 struct i915_power_well *cmn_d =
2452 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
2453
2454 /*
2455 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
2456 * workaround never ever read DISPLAY_PHY_CONTROL, and
2457 * instead maintain a shadow copy ourselves. Use the actual
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002458 * power well state and lane status to reconstruct the
2459 * expected initial value.
Ville Syrjälä70722462015-04-10 18:21:28 +03002460 */
2461 dev_priv->chv_phy_control =
Ville Syrjäläbc284542015-05-26 20:22:38 +03002462 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
2463 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002464 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
2465 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
2466 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
2467
2468 /*
2469 * If all lanes are disabled we leave the override disabled
2470 * with all power down bits cleared to match the state we
2471 * would use after disabling the port. Otherwise enable the
2472 * override and set the lane powerdown bits accding to the
2473 * current lane status.
2474 */
2475 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
2476 uint32_t status = I915_READ(DPLL(PIPE_A));
2477 unsigned int mask;
2478
2479 mask = status & DPLL_PORTB_READY_MASK;
2480 if (mask == 0xf)
2481 mask = 0x0;
2482 else
2483 dev_priv->chv_phy_control |=
2484 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
2485
2486 dev_priv->chv_phy_control |=
2487 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
2488
2489 mask = (status & DPLL_PORTC_READY_MASK) >> 4;
2490 if (mask == 0xf)
2491 mask = 0x0;
2492 else
2493 dev_priv->chv_phy_control |=
2494 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
2495
2496 dev_priv->chv_phy_control |=
2497 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
2498
Ville Syrjälä70722462015-04-10 18:21:28 +03002499 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002500
2501 dev_priv->chv_phy_assert[DPIO_PHY0] = false;
2502 } else {
2503 dev_priv->chv_phy_assert[DPIO_PHY0] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002504 }
2505
2506 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
2507 uint32_t status = I915_READ(DPIO_PHY_STATUS);
2508 unsigned int mask;
2509
2510 mask = status & DPLL_PORTD_READY_MASK;
2511
2512 if (mask == 0xf)
2513 mask = 0x0;
2514 else
2515 dev_priv->chv_phy_control |=
2516 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
2517
2518 dev_priv->chv_phy_control |=
2519 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
2520
Ville Syrjälä70722462015-04-10 18:21:28 +03002521 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03002522
2523 dev_priv->chv_phy_assert[DPIO_PHY1] = false;
2524 } else {
2525 dev_priv->chv_phy_assert[DPIO_PHY1] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03002526 }
2527
2528 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
2529
2530 DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
2531 dev_priv->chv_phy_control);
Ville Syrjälä70722462015-04-10 18:21:28 +03002532}
2533
Daniel Vetter9c065a72014-09-30 10:56:38 +02002534static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
2535{
2536 struct i915_power_well *cmn =
2537 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
2538 struct i915_power_well *disp2d =
2539 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
2540
Daniel Vetter9c065a72014-09-30 10:56:38 +02002541 /* If the display might be already active skip this */
Ville Syrjälä5d93a6e2014-10-16 20:52:33 +03002542 if (cmn->ops->is_enabled(dev_priv, cmn) &&
2543 disp2d->ops->is_enabled(dev_priv, disp2d) &&
Daniel Vetter9c065a72014-09-30 10:56:38 +02002544 I915_READ(DPIO_CTL) & DPIO_CMNRST)
2545 return;
2546
2547 DRM_DEBUG_KMS("toggling display PHY side reset\n");
2548
2549 /* cmnlane needs DPLL registers */
2550 disp2d->ops->enable(dev_priv, disp2d);
2551
2552 /*
2553 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
2554 * Need to assert and de-assert PHY SB reset by gating the
2555 * common lane power, then un-gating it.
2556 * Simply ungating isn't enough to reset the PHY enough to get
2557 * ports and lanes running.
2558 */
2559 cmn->ops->disable(dev_priv, cmn);
2560}
2561
Daniel Vettere4e76842014-09-30 10:56:42 +02002562/**
2563 * intel_power_domains_init_hw - initialize hardware power domain state
2564 * @dev_priv: i915 device instance
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002565 * @resume: Called from resume code paths or not
Daniel Vettere4e76842014-09-30 10:56:42 +02002566 *
2567 * This function initializes the hardware power domain state and enables all
2568 * power domains using intel_display_set_init_power().
2569 */
Imre Deak73dfc222015-11-17 17:33:53 +02002570void intel_power_domains_init_hw(struct drm_i915_private *dev_priv, bool resume)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002571{
2572 struct drm_device *dev = dev_priv->dev;
2573 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2574
2575 power_domains->initializing = true;
2576
Imre Deak73dfc222015-11-17 17:33:53 +02002577 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
2578 skl_display_core_init(dev_priv, resume);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002579 } else if (IS_BROXTON(dev)) {
2580 bxt_display_core_init(dev_priv, resume);
Imre Deak73dfc222015-11-17 17:33:53 +02002581 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä770effb2015-07-08 23:45:51 +03002582 mutex_lock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002583 chv_phy_control_init(dev_priv);
Ville Syrjälä770effb2015-07-08 23:45:51 +03002584 mutex_unlock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002585 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002586 mutex_lock(&power_domains->lock);
2587 vlv_cmnlane_wa(dev_priv);
2588 mutex_unlock(&power_domains->lock);
2589 }
2590
2591 /* For now, we need the power well to be always enabled. */
2592 intel_display_set_init_power(dev_priv, true);
Imre Deakd314cd42015-11-17 17:44:23 +02002593 /* Disable power support if the user asked so. */
2594 if (!i915.disable_power_well)
2595 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
Imre Deak30eade12015-11-04 19:24:13 +02002596 intel_power_domains_sync_hw(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002597 power_domains->initializing = false;
2598}
2599
Daniel Vettere4e76842014-09-30 10:56:42 +02002600/**
Imre Deak73dfc222015-11-17 17:33:53 +02002601 * intel_power_domains_suspend - suspend power domain state
2602 * @dev_priv: i915 device instance
2603 *
2604 * This function prepares the hardware power domain state before entering
2605 * system suspend. It must be paired with intel_power_domains_init_hw().
2606 */
2607void intel_power_domains_suspend(struct drm_i915_private *dev_priv)
2608{
Imre Deakd314cd42015-11-17 17:44:23 +02002609 /*
2610 * Even if power well support was disabled we still want to disable
2611 * power wells while we are system suspended.
2612 */
2613 if (!i915.disable_power_well)
2614 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Imre Deak2622d792016-02-29 22:49:02 +02002615
2616 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
2617 skl_display_core_uninit(dev_priv);
Imre Deakd7d7c9e2016-04-01 16:02:42 +03002618 else if (IS_BROXTON(dev_priv))
2619 bxt_display_core_uninit(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02002620}
2621
2622/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002623 * intel_runtime_pm_get - grab a runtime pm reference
2624 * @dev_priv: i915 device instance
2625 *
2626 * This function grabs a device-level runtime pm reference (mostly used for GEM
2627 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2628 *
2629 * Any runtime pm reference obtained by this function must have a symmetric
2630 * call to intel_runtime_pm_put() to release the reference again.
2631 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002632void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2633{
2634 struct drm_device *dev = dev_priv->dev;
2635 struct device *device = &dev->pdev->dev;
2636
Daniel Vetter9c065a72014-09-30 10:56:38 +02002637 pm_runtime_get_sync(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002638
2639 atomic_inc(&dev_priv->pm.wakeref_count);
Imre Deakc9b88462015-12-15 20:10:34 +02002640 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002641}
2642
Daniel Vettere4e76842014-09-30 10:56:42 +02002643/**
Imre Deak09731282016-02-17 14:17:42 +02002644 * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use
2645 * @dev_priv: i915 device instance
2646 *
2647 * This function grabs a device-level runtime pm reference if the device is
2648 * already in use and ensures that it is powered up.
2649 *
2650 * Any runtime pm reference obtained by this function must have a symmetric
2651 * call to intel_runtime_pm_put() to release the reference again.
2652 */
2653bool intel_runtime_pm_get_if_in_use(struct drm_i915_private *dev_priv)
2654{
2655 struct drm_device *dev = dev_priv->dev;
2656 struct device *device = &dev->pdev->dev;
Imre Deak09731282016-02-17 14:17:42 +02002657
Chris Wilson135dc792016-02-25 21:10:28 +00002658 if (IS_ENABLED(CONFIG_PM)) {
2659 int ret = pm_runtime_get_if_in_use(device);
Imre Deak09731282016-02-17 14:17:42 +02002660
Chris Wilson135dc792016-02-25 21:10:28 +00002661 /*
2662 * In cases runtime PM is disabled by the RPM core and we get
2663 * an -EINVAL return value we are not supposed to call this
2664 * function, since the power state is undefined. This applies
2665 * atm to the late/early system suspend/resume handlers.
2666 */
2667 WARN_ON_ONCE(ret < 0);
2668 if (ret <= 0)
2669 return false;
2670 }
Imre Deak09731282016-02-17 14:17:42 +02002671
2672 atomic_inc(&dev_priv->pm.wakeref_count);
2673 assert_rpm_wakelock_held(dev_priv);
2674
2675 return true;
2676}
2677
2678/**
Daniel Vettere4e76842014-09-30 10:56:42 +02002679 * intel_runtime_pm_get_noresume - grab a runtime pm reference
2680 * @dev_priv: i915 device instance
2681 *
2682 * This function grabs a device-level runtime pm reference (mostly used for GEM
2683 * code to ensure the GTT or GT is on).
2684 *
2685 * It will _not_ power up the device but instead only check that it's powered
2686 * on. Therefore it is only valid to call this functions from contexts where
2687 * the device is known to be powered up and where trying to power it up would
2688 * result in hilarity and deadlocks. That pretty much means only the system
2689 * suspend/resume code where this is used to grab runtime pm references for
2690 * delayed setup down in work items.
2691 *
2692 * Any runtime pm reference obtained by this function must have a symmetric
2693 * call to intel_runtime_pm_put() to release the reference again.
2694 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002695void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
2696{
2697 struct drm_device *dev = dev_priv->dev;
2698 struct device *device = &dev->pdev->dev;
2699
Imre Deakc9b88462015-12-15 20:10:34 +02002700 assert_rpm_wakelock_held(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002701 pm_runtime_get_noresume(device);
Imre Deak1f814da2015-12-16 02:52:19 +02002702
2703 atomic_inc(&dev_priv->pm.wakeref_count);
Daniel Vetter9c065a72014-09-30 10:56:38 +02002704}
2705
Daniel Vettere4e76842014-09-30 10:56:42 +02002706/**
2707 * intel_runtime_pm_put - release a runtime pm reference
2708 * @dev_priv: i915 device instance
2709 *
2710 * This function drops the device-level runtime pm reference obtained by
2711 * intel_runtime_pm_get() and might power down the corresponding
2712 * hardware block right away if this is the last reference.
2713 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002714void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
2715{
2716 struct drm_device *dev = dev_priv->dev;
2717 struct device *device = &dev->pdev->dev;
2718
Imre Deak542db3c2015-12-15 20:10:36 +02002719 assert_rpm_wakelock_held(dev_priv);
Imre Deak2b19efe2015-12-15 20:10:37 +02002720 if (atomic_dec_and_test(&dev_priv->pm.wakeref_count))
2721 atomic_inc(&dev_priv->pm.atomic_seq);
Imre Deak1f814da2015-12-16 02:52:19 +02002722
Daniel Vetter9c065a72014-09-30 10:56:38 +02002723 pm_runtime_mark_last_busy(device);
2724 pm_runtime_put_autosuspend(device);
2725}
2726
Daniel Vettere4e76842014-09-30 10:56:42 +02002727/**
2728 * intel_runtime_pm_enable - enable runtime pm
2729 * @dev_priv: i915 device instance
2730 *
2731 * This function enables runtime pm at the end of the driver load sequence.
2732 *
2733 * Note that this function does currently not enable runtime pm for the
2734 * subordinate display power domains. That is only done on the first modeset
2735 * using intel_display_set_init_power().
2736 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002737void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002738{
2739 struct drm_device *dev = dev_priv->dev;
2740 struct device *device = &dev->pdev->dev;
2741
Imre Deakcbc68dc2015-12-17 19:04:33 +02002742 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
2743 pm_runtime_mark_last_busy(device);
2744
Imre Deak25b181b2015-12-17 13:44:56 +02002745 /*
2746 * Take a permanent reference to disable the RPM functionality and drop
2747 * it only when unloading the driver. Use the low level get/put helpers,
2748 * so the driver's own RPM reference tracking asserts also work on
2749 * platforms without RPM support.
2750 */
Imre Deakcbc68dc2015-12-17 19:04:33 +02002751 if (!HAS_RUNTIME_PM(dev)) {
2752 pm_runtime_dont_use_autosuspend(device);
Imre Deak25b181b2015-12-17 13:44:56 +02002753 pm_runtime_get_sync(device);
Imre Deakcbc68dc2015-12-17 19:04:33 +02002754 } else {
2755 pm_runtime_use_autosuspend(device);
2756 }
Daniel Vetter9c065a72014-09-30 10:56:38 +02002757
Imre Deakaabee1b2015-12-15 20:10:29 +02002758 /*
2759 * The core calls the driver load handler with an RPM reference held.
2760 * We drop that here and will reacquire it during unloading in
2761 * intel_power_domains_fini().
2762 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002763 pm_runtime_put_autosuspend(device);
2764}
2765