blob: 81319fd1023cee0ca3c9885769784016b93471df [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
Suketu Shahf75a1982015-04-16 14:22:11 +053052#define GEN9_ENABLE_DC5(dev) 0
53#define SKL_ENABLE_DC6(dev) IS_SKYLAKE(dev)
Suketu Shahdc174302015-04-17 19:46:16 +053054
Daniel Vetter9c065a72014-09-30 10:56:38 +020055#define for_each_power_well(i, power_well, domain_mask, power_domains) \
56 for (i = 0; \
57 i < (power_domains)->power_well_count && \
58 ((power_well) = &(power_domains)->power_wells[i]); \
59 i++) \
60 if ((power_well)->domains & (domain_mask))
61
62#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
63 for (i = (power_domains)->power_well_count - 1; \
64 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
65 i--) \
66 if ((power_well)->domains & (domain_mask))
67
Suketu Shah5aefb232015-04-16 14:22:10 +053068bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
69 int power_well_id);
70
Damien Lespiaue8ca9322015-07-30 18:20:26 -030071static void intel_power_well_enable(struct drm_i915_private *dev_priv,
72 struct i915_power_well *power_well)
73{
74 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
75 power_well->ops->enable(dev_priv, power_well);
76 power_well->hw_enabled = true;
77}
78
Damien Lespiaudcddab32015-07-30 18:20:27 -030079static void intel_power_well_disable(struct drm_i915_private *dev_priv,
80 struct i915_power_well *power_well)
81{
82 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
83 power_well->hw_enabled = false;
84 power_well->ops->disable(dev_priv, power_well);
85}
86
Daniel Vettere4e76842014-09-30 10:56:42 +020087/*
Daniel Vetter9c065a72014-09-30 10:56:38 +020088 * We should only use the power well if we explicitly asked the hardware to
89 * enable it, so check if it's enabled and also check if we've requested it to
90 * be enabled.
91 */
92static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
93 struct i915_power_well *power_well)
94{
95 return I915_READ(HSW_PWR_WELL_DRIVER) ==
96 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
97}
98
Daniel Vettere4e76842014-09-30 10:56:42 +020099/**
100 * __intel_display_power_is_enabled - unlocked check for a power domain
101 * @dev_priv: i915 device instance
102 * @domain: power domain to check
103 *
104 * This is the unlocked version of intel_display_power_is_enabled() and should
105 * only be used from error capture and recovery code where deadlocks are
106 * possible.
107 *
108 * Returns:
109 * True when the power domain is enabled, false otherwise.
110 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200111bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
112 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200113{
114 struct i915_power_domains *power_domains;
115 struct i915_power_well *power_well;
116 bool is_enabled;
117 int i;
118
119 if (dev_priv->pm.suspended)
120 return false;
121
122 power_domains = &dev_priv->power_domains;
123
124 is_enabled = true;
125
126 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
127 if (power_well->always_on)
128 continue;
129
130 if (!power_well->hw_enabled) {
131 is_enabled = false;
132 break;
133 }
134 }
135
136 return is_enabled;
137}
138
Daniel Vettere4e76842014-09-30 10:56:42 +0200139/**
Damien Lespiauf61ccae2014-11-25 13:45:41 +0000140 * intel_display_power_is_enabled - check for a power domain
Daniel Vettere4e76842014-09-30 10:56:42 +0200141 * @dev_priv: i915 device instance
142 * @domain: power domain to check
143 *
144 * This function can be used to check the hw power domain state. It is mostly
145 * used in hardware state readout functions. Everywhere else code should rely
146 * upon explicit power domain reference counting to ensure that the hardware
147 * block is powered up before accessing it.
148 *
149 * Callers must hold the relevant modesetting locks to ensure that concurrent
150 * threads can't disable the power well while the caller tries to read a few
151 * registers.
152 *
153 * Returns:
154 * True when the power domain is enabled, false otherwise.
155 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200156bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
157 enum intel_display_power_domain domain)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200158{
159 struct i915_power_domains *power_domains;
160 bool ret;
161
162 power_domains = &dev_priv->power_domains;
163
164 mutex_lock(&power_domains->lock);
Daniel Vetterf458ebb2014-09-30 10:56:39 +0200165 ret = __intel_display_power_is_enabled(dev_priv, domain);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200166 mutex_unlock(&power_domains->lock);
167
168 return ret;
169}
170
Daniel Vettere4e76842014-09-30 10:56:42 +0200171/**
172 * intel_display_set_init_power - set the initial power domain state
173 * @dev_priv: i915 device instance
174 * @enable: whether to enable or disable the initial power domain state
175 *
176 * For simplicity our driver load/unload and system suspend/resume code assumes
177 * that all power domains are always enabled. This functions controls the state
178 * of this little hack. While the initial power domain state is enabled runtime
179 * pm is effectively disabled.
180 */
Daniel Vetterd9bc89d92014-09-30 10:56:40 +0200181void intel_display_set_init_power(struct drm_i915_private *dev_priv,
182 bool enable)
183{
184 if (dev_priv->power_domains.init_power_on == enable)
185 return;
186
187 if (enable)
188 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
189 else
190 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
191
192 dev_priv->power_domains.init_power_on = enable;
193}
194
Daniel Vetter9c065a72014-09-30 10:56:38 +0200195/*
196 * Starting with Haswell, we have a "Power Down Well" that can be turned off
197 * when not needed anymore. We have 4 registers that can request the power well
198 * to be enabled, and it will only be disabled if none of the registers is
199 * requesting it to be enabled.
200 */
201static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
202{
203 struct drm_device *dev = dev_priv->dev;
204
205 /*
206 * After we re-enable the power well, if we touch VGA register 0x3d5
207 * we'll get unclaimed register interrupts. This stops after we write
208 * anything to the VGA MSR register. The vgacon module uses this
209 * register all the time, so if we unbind our driver and, as a
210 * consequence, bind vgacon, we'll get stuck in an infinite loop at
211 * console_unlock(). So make here we touch the VGA MSR register, making
212 * sure vgacon can keep working normally without triggering interrupts
213 * and error messages.
214 */
215 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
216 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
217 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
218
Damien Lespiau25400392015-03-06 18:50:52 +0000219 if (IS_BROADWELL(dev))
Damien Lespiau4c6c03b2015-03-06 18:50:48 +0000220 gen8_irq_power_well_post_enable(dev_priv,
221 1 << PIPE_C | 1 << PIPE_B);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200222}
223
Damien Lespiaud14c0342015-03-06 18:50:51 +0000224static void skl_power_well_post_enable(struct drm_i915_private *dev_priv,
225 struct i915_power_well *power_well)
226{
227 struct drm_device *dev = dev_priv->dev;
228
229 /*
230 * After we re-enable the power well, if we touch VGA register 0x3d5
231 * we'll get unclaimed register interrupts. This stops after we write
232 * anything to the VGA MSR register. The vgacon module uses this
233 * register all the time, so if we unbind our driver and, as a
234 * consequence, bind vgacon, we'll get stuck in an infinite loop at
235 * console_unlock(). So make here we touch the VGA MSR register, making
236 * sure vgacon can keep working normally without triggering interrupts
237 * and error messages.
238 */
239 if (power_well->data == SKL_DISP_PW_2) {
240 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
241 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
242 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
243
244 gen8_irq_power_well_post_enable(dev_priv,
245 1 << PIPE_C | 1 << PIPE_B);
246 }
247
Damien Lespiau1d2b9522015-03-06 18:50:53 +0000248 if (power_well->data == SKL_DISP_PW_1) {
249 intel_prepare_ddi(dev);
Damien Lespiaud14c0342015-03-06 18:50:51 +0000250 gen8_irq_power_well_post_enable(dev_priv, 1 << PIPE_A);
Damien Lespiau1d2b9522015-03-06 18:50:53 +0000251 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000252}
253
Daniel Vetter9c065a72014-09-30 10:56:38 +0200254static void hsw_set_power_well(struct drm_i915_private *dev_priv,
255 struct i915_power_well *power_well, bool enable)
256{
257 bool is_enabled, enable_requested;
258 uint32_t tmp;
259
260 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
261 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
262 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
263
264 if (enable) {
265 if (!enable_requested)
266 I915_WRITE(HSW_PWR_WELL_DRIVER,
267 HSW_PWR_WELL_ENABLE_REQUEST);
268
269 if (!is_enabled) {
270 DRM_DEBUG_KMS("Enabling power well\n");
271 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
272 HSW_PWR_WELL_STATE_ENABLED), 20))
273 DRM_ERROR("Timeout enabling power well\n");
Paulo Zanoni6d729bf2014-10-07 16:11:11 -0300274 hsw_power_well_post_enable(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200275 }
276
Daniel Vetter9c065a72014-09-30 10:56:38 +0200277 } else {
278 if (enable_requested) {
279 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
280 POSTING_READ(HSW_PWR_WELL_DRIVER);
281 DRM_DEBUG_KMS("Requesting to disable the power well\n");
282 }
283 }
284}
285
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000286#define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
287 BIT(POWER_DOMAIN_TRANSCODER_A) | \
288 BIT(POWER_DOMAIN_PIPE_B) | \
289 BIT(POWER_DOMAIN_TRANSCODER_B) | \
290 BIT(POWER_DOMAIN_PIPE_C) | \
291 BIT(POWER_DOMAIN_TRANSCODER_C) | \
292 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
293 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
294 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
295 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
296 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
297 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
298 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
299 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
Xiong Zhangd8e19f92015-08-13 18:00:12 +0800300 BIT(POWER_DOMAIN_PORT_DDI_E_2_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000301 BIT(POWER_DOMAIN_AUX_B) | \
302 BIT(POWER_DOMAIN_AUX_C) | \
303 BIT(POWER_DOMAIN_AUX_D) | \
304 BIT(POWER_DOMAIN_AUDIO) | \
305 BIT(POWER_DOMAIN_VGA) | \
306 BIT(POWER_DOMAIN_INIT))
307#define SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS ( \
308 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
309 BIT(POWER_DOMAIN_PLLS) | \
310 BIT(POWER_DOMAIN_PIPE_A) | \
311 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
312 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
313 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
314 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
315 BIT(POWER_DOMAIN_AUX_A) | \
316 BIT(POWER_DOMAIN_INIT))
317#define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS ( \
318 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
319 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
Xiong Zhangd8e19f92015-08-13 18:00:12 +0800320 BIT(POWER_DOMAIN_PORT_DDI_E_2_LANES) | \
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000321 BIT(POWER_DOMAIN_INIT))
322#define SKL_DISPLAY_DDI_B_POWER_DOMAINS ( \
323 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
324 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
325 BIT(POWER_DOMAIN_INIT))
326#define SKL_DISPLAY_DDI_C_POWER_DOMAINS ( \
327 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
328 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
329 BIT(POWER_DOMAIN_INIT))
330#define SKL_DISPLAY_DDI_D_POWER_DOMAINS ( \
331 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
332 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
333 BIT(POWER_DOMAIN_INIT))
334#define SKL_DISPLAY_MISC_IO_POWER_DOMAINS ( \
Damien Lespiauaeaa2122015-04-30 16:39:16 +0100335 SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
Damien Lespiau62227092015-04-30 16:39:20 +0100336 BIT(POWER_DOMAIN_PLLS) | \
Damien Lespiauaeaa2122015-04-30 16:39:16 +0100337 BIT(POWER_DOMAIN_INIT))
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000338#define SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
339 (POWER_DOMAIN_MASK & ~(SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
340 SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
341 SKL_DISPLAY_DDI_A_E_POWER_DOMAINS | \
342 SKL_DISPLAY_DDI_B_POWER_DOMAINS | \
343 SKL_DISPLAY_DDI_C_POWER_DOMAINS | \
344 SKL_DISPLAY_DDI_D_POWER_DOMAINS | \
345 SKL_DISPLAY_MISC_IO_POWER_DOMAINS)) | \
346 BIT(POWER_DOMAIN_INIT))
347
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +0530348#define BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS ( \
349 BIT(POWER_DOMAIN_TRANSCODER_A) | \
350 BIT(POWER_DOMAIN_PIPE_B) | \
351 BIT(POWER_DOMAIN_TRANSCODER_B) | \
352 BIT(POWER_DOMAIN_PIPE_C) | \
353 BIT(POWER_DOMAIN_TRANSCODER_C) | \
354 BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) | \
355 BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) | \
356 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
357 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
358 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
359 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
360 BIT(POWER_DOMAIN_AUX_B) | \
361 BIT(POWER_DOMAIN_AUX_C) | \
362 BIT(POWER_DOMAIN_AUDIO) | \
363 BIT(POWER_DOMAIN_VGA) | \
364 BIT(POWER_DOMAIN_INIT))
365#define BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS ( \
366 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS | \
367 BIT(POWER_DOMAIN_PIPE_A) | \
368 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
369 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) | \
370 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
371 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
372 BIT(POWER_DOMAIN_AUX_A) | \
373 BIT(POWER_DOMAIN_PLLS) | \
374 BIT(POWER_DOMAIN_INIT))
375#define BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS ( \
376 (POWER_DOMAIN_MASK & ~(BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS | \
377 BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS)) | \
378 BIT(POWER_DOMAIN_INIT))
379
A.Sunil Kamath664326f2014-11-24 13:37:44 +0530380static void assert_can_enable_dc9(struct drm_i915_private *dev_priv)
381{
382 struct drm_device *dev = dev_priv->dev;
383
384 WARN(!IS_BROXTON(dev), "Platform doesn't support DC9.\n");
385 WARN((I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
386 "DC9 already programmed to be enabled.\n");
387 WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
388 "DC5 still not disabled to enable DC9.\n");
389 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on.\n");
390 WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
391
392 /*
393 * TODO: check for the following to verify the conditions to enter DC9
394 * state are satisfied:
395 * 1] Check relevant display engine registers to verify if mode set
396 * disable sequence was followed.
397 * 2] Check if display uninitialize sequence is initialized.
398 */
399}
400
401static void assert_can_disable_dc9(struct drm_i915_private *dev_priv)
402{
403 WARN(intel_irqs_enabled(dev_priv), "Interrupts not disabled yet.\n");
404 WARN(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_DC9),
405 "DC9 already programmed to be disabled.\n");
406 WARN(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5,
407 "DC5 still not disabled.\n");
408
409 /*
410 * TODO: check for the following to verify DC9 state was indeed
411 * entered before programming to disable it:
412 * 1] Check relevant display engine registers to verify if mode
413 * set disable sequence was followed.
414 * 2] Check if display uninitialize sequence is initialized.
415 */
416}
417
418void bxt_enable_dc9(struct drm_i915_private *dev_priv)
419{
420 uint32_t val;
421
422 assert_can_enable_dc9(dev_priv);
423
424 DRM_DEBUG_KMS("Enabling DC9\n");
425
426 val = I915_READ(DC_STATE_EN);
427 val |= DC_STATE_EN_DC9;
428 I915_WRITE(DC_STATE_EN, val);
429 POSTING_READ(DC_STATE_EN);
430}
431
432void bxt_disable_dc9(struct drm_i915_private *dev_priv)
433{
434 uint32_t val;
435
436 assert_can_disable_dc9(dev_priv);
437
438 DRM_DEBUG_KMS("Disabling DC9\n");
439
440 val = I915_READ(DC_STATE_EN);
441 val &= ~DC_STATE_EN_DC9;
442 I915_WRITE(DC_STATE_EN, val);
443 POSTING_READ(DC_STATE_EN);
444}
445
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530446static void gen9_set_dc_state_debugmask_memory_up(
447 struct drm_i915_private *dev_priv)
448{
449 uint32_t val;
450
451 /* The below bit doesn't need to be cleared ever afterwards */
452 val = I915_READ(DC_STATE_DEBUG);
453 if (!(val & DC_STATE_DEBUG_MASK_MEMORY_UP)) {
454 val |= DC_STATE_DEBUG_MASK_MEMORY_UP;
455 I915_WRITE(DC_STATE_DEBUG, val);
456 POSTING_READ(DC_STATE_DEBUG);
457 }
458}
459
Daniel Vetteraf5fead2015-10-28 23:58:57 +0200460static void assert_csr_loaded(struct drm_i915_private *dev_priv)
461{
462 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
463 "CSR program storage start is NULL\n");
464 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
465 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
466}
467
Suketu Shah5aefb232015-04-16 14:22:10 +0530468static void assert_can_enable_dc5(struct drm_i915_private *dev_priv)
Suketu Shahdc174302015-04-17 19:46:16 +0530469{
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530470 struct drm_device *dev = dev_priv->dev;
Suketu Shah5aefb232015-04-16 14:22:10 +0530471 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
472 SKL_DISP_PW_2);
473
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700474 WARN_ONCE(!IS_SKYLAKE(dev), "Platform doesn't support DC5.\n");
475 WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
476 WARN_ONCE(pg2_enabled, "PG2 not disabled to enable DC5.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530477
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700478 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC5),
479 "DC5 already programmed to be enabled.\n");
480 WARN_ONCE(dev_priv->pm.suspended,
481 "DC5 cannot be enabled, if platform is runtime-suspended.\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530482
483 assert_csr_loaded(dev_priv);
484}
485
486static void assert_can_disable_dc5(struct drm_i915_private *dev_priv)
487{
488 bool pg2_enabled = intel_display_power_well_is_enabled(dev_priv,
489 SKL_DISP_PW_2);
Suketu Shah93c7cb62015-04-16 14:22:13 +0530490 /*
491 * During initialization, the firmware may not be loaded yet.
492 * We still want to make sure that the DC enabling flag is cleared.
493 */
494 if (dev_priv->power_domains.initializing)
495 return;
Suketu Shah5aefb232015-04-16 14:22:10 +0530496
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700497 WARN_ONCE(!pg2_enabled, "PG2 not enabled to disable DC5.\n");
498 WARN_ONCE(dev_priv->pm.suspended,
Suketu Shah5aefb232015-04-16 14:22:10 +0530499 "Disabling of DC5 while platform is runtime-suspended should never happen.\n");
500}
501
502static void gen9_enable_dc5(struct drm_i915_private *dev_priv)
503{
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530504 uint32_t val;
505
Suketu Shah5aefb232015-04-16 14:22:10 +0530506 assert_can_enable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530507
508 DRM_DEBUG_KMS("Enabling DC5\n");
509
510 gen9_set_dc_state_debugmask_memory_up(dev_priv);
511
512 val = I915_READ(DC_STATE_EN);
513 val &= ~DC_STATE_EN_UPTO_DC5_DC6_MASK;
514 val |= DC_STATE_EN_UPTO_DC5;
515 I915_WRITE(DC_STATE_EN, val);
516 POSTING_READ(DC_STATE_EN);
Suketu Shahdc174302015-04-17 19:46:16 +0530517}
518
519static void gen9_disable_dc5(struct drm_i915_private *dev_priv)
520{
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530521 uint32_t val;
522
Suketu Shah5aefb232015-04-16 14:22:10 +0530523 assert_can_disable_dc5(dev_priv);
A.Sunil Kamath6b457d32015-04-16 14:22:09 +0530524
525 DRM_DEBUG_KMS("Disabling DC5\n");
526
527 val = I915_READ(DC_STATE_EN);
528 val &= ~DC_STATE_EN_UPTO_DC5;
529 I915_WRITE(DC_STATE_EN, val);
530 POSTING_READ(DC_STATE_EN);
Suketu Shahdc174302015-04-17 19:46:16 +0530531}
532
Suketu Shah93c7cb62015-04-16 14:22:13 +0530533static void assert_can_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530534{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530535 struct drm_device *dev = dev_priv->dev;
Suketu Shah93c7cb62015-04-16 14:22:13 +0530536
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700537 WARN_ONCE(!IS_SKYLAKE(dev), "Platform doesn't support DC6.\n");
538 WARN_ONCE(!HAS_RUNTIME_PM(dev), "Runtime PM not enabled.\n");
539 WARN_ONCE(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
540 "Backlight is not disabled.\n");
541 WARN_ONCE((I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
542 "DC6 already programmed to be enabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530543
544 assert_csr_loaded(dev_priv);
545}
546
547static void assert_can_disable_dc6(struct drm_i915_private *dev_priv)
548{
549 /*
550 * During initialization, the firmware may not be loaded yet.
551 * We still want to make sure that the DC enabling flag is cleared.
552 */
553 if (dev_priv->power_domains.initializing)
554 return;
555
556 assert_csr_loaded(dev_priv);
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700557 WARN_ONCE(!(I915_READ(DC_STATE_EN) & DC_STATE_EN_UPTO_DC6),
558 "DC6 already programmed to be disabled.\n");
Suketu Shah93c7cb62015-04-16 14:22:13 +0530559}
560
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530561void skl_enable_dc6(struct drm_i915_private *dev_priv)
Suketu Shah93c7cb62015-04-16 14:22:13 +0530562{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530563 uint32_t val;
564
Suketu Shah93c7cb62015-04-16 14:22:13 +0530565 assert_can_enable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530566
567 DRM_DEBUG_KMS("Enabling DC6\n");
568
569 gen9_set_dc_state_debugmask_memory_up(dev_priv);
570
571 val = I915_READ(DC_STATE_EN);
572 val &= ~DC_STATE_EN_UPTO_DC5_DC6_MASK;
573 val |= DC_STATE_EN_UPTO_DC6;
574 I915_WRITE(DC_STATE_EN, val);
575 POSTING_READ(DC_STATE_EN);
Suketu Shahf75a1982015-04-16 14:22:11 +0530576}
577
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530578void skl_disable_dc6(struct drm_i915_private *dev_priv)
Suketu Shahf75a1982015-04-16 14:22:11 +0530579{
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530580 uint32_t val;
581
Suketu Shah93c7cb62015-04-16 14:22:13 +0530582 assert_can_disable_dc6(dev_priv);
A.Sunil Kamath74b4f372015-04-16 14:22:12 +0530583
584 DRM_DEBUG_KMS("Disabling DC6\n");
585
586 val = I915_READ(DC_STATE_EN);
587 val &= ~DC_STATE_EN_UPTO_DC6;
588 I915_WRITE(DC_STATE_EN, val);
589 POSTING_READ(DC_STATE_EN);
Suketu Shahf75a1982015-04-16 14:22:11 +0530590}
591
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000592static void skl_set_power_well(struct drm_i915_private *dev_priv,
593 struct i915_power_well *power_well, bool enable)
594{
Suketu Shahdc174302015-04-17 19:46:16 +0530595 struct drm_device *dev = dev_priv->dev;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000596 uint32_t tmp, fuse_status;
597 uint32_t req_mask, state_mask;
Damien Lespiau2a518352015-03-06 18:50:49 +0000598 bool is_enabled, enable_requested, check_fuse_status = false;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000599
600 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
601 fuse_status = I915_READ(SKL_FUSE_STATUS);
602
603 switch (power_well->data) {
604 case SKL_DISP_PW_1:
605 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
606 SKL_FUSE_PG0_DIST_STATUS), 1)) {
607 DRM_ERROR("PG0 not enabled\n");
608 return;
609 }
610 break;
611 case SKL_DISP_PW_2:
612 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
613 DRM_ERROR("PG1 in disabled state\n");
614 return;
615 }
616 break;
617 case SKL_DISP_PW_DDI_A_E:
618 case SKL_DISP_PW_DDI_B:
619 case SKL_DISP_PW_DDI_C:
620 case SKL_DISP_PW_DDI_D:
621 case SKL_DISP_PW_MISC_IO:
622 break;
623 default:
624 WARN(1, "Unknown power well %lu\n", power_well->data);
625 return;
626 }
627
628 req_mask = SKL_POWER_WELL_REQ(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000629 enable_requested = tmp & req_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000630 state_mask = SKL_POWER_WELL_STATE(power_well->data);
Damien Lespiau2a518352015-03-06 18:50:49 +0000631 is_enabled = tmp & state_mask;
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000632
633 if (enable) {
Damien Lespiau2a518352015-03-06 18:50:49 +0000634 if (!enable_requested) {
Suketu Shahdc174302015-04-17 19:46:16 +0530635 WARN((tmp & state_mask) &&
636 !I915_READ(HSW_PWR_WELL_BIOS),
637 "Invalid for power well status to be enabled, unless done by the BIOS, \
638 when request is to disable!\n");
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530639 if (power_well->data == SKL_DISP_PW_2) {
640 if (GEN9_ENABLE_DC5(dev))
641 gen9_disable_dc5(dev_priv);
Suketu Shahf75a1982015-04-16 14:22:11 +0530642 if (SKL_ENABLE_DC6(dev)) {
Suketu Shahf75a1982015-04-16 14:22:11 +0530643 /*
644 * DDI buffer programming unnecessary during driver-load/resume
645 * as it's already done during modeset initialization then.
646 * It's also invalid here as encoder list is still uninitialized.
647 */
648 if (!dev_priv->power_domains.initializing)
649 intel_prepare_ddi(dev);
Suketu Shahf75a1982015-04-16 14:22:11 +0530650 }
651 }
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000652 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000653 }
654
Damien Lespiau2a518352015-03-06 18:50:49 +0000655 if (!is_enabled) {
Damien Lespiau510e6fd2015-03-06 18:50:50 +0000656 DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000657 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
658 state_mask), 1))
659 DRM_ERROR("%s enable timeout\n",
660 power_well->name);
661 check_fuse_status = true;
662 }
663 } else {
Damien Lespiau2a518352015-03-06 18:50:49 +0000664 if (enable_requested) {
Animesh Manna08aef7c2015-08-26 01:36:09 +0530665 if (IS_SKYLAKE(dev) &&
666 (power_well->data == SKL_DISP_PW_1) &&
667 (intel_csr_load_status_get(dev_priv) == FW_LOADED))
668 DRM_DEBUG_KMS("Not Disabling PW1, dmc will handle\n");
669 else {
670 I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
671 POSTING_READ(HSW_PWR_WELL_DRIVER);
672 DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
673 }
Suketu Shahdc174302015-04-17 19:46:16 +0530674
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530675 if (GEN9_ENABLE_DC5(dev) &&
Suketu Shahdc174302015-04-17 19:46:16 +0530676 power_well->data == SKL_DISP_PW_2) {
677 enum csr_state state;
Suketu Shahf75a1982015-04-16 14:22:11 +0530678 /* TODO: wait for a completion event or
679 * similar here instead of busy
680 * waiting using wait_for function.
681 */
Suketu Shahdc174302015-04-17 19:46:16 +0530682 wait_for((state = intel_csr_load_status_get(dev_priv)) !=
683 FW_UNINITIALIZED, 1000);
684 if (state != FW_LOADED)
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700685 DRM_DEBUG("CSR firmware not ready (%d)\n",
Suketu Shahdc174302015-04-17 19:46:16 +0530686 state);
687 else
Animesh Manna0a9d2be2015-09-29 11:01:59 +0530688 gen9_enable_dc5(dev_priv);
Suketu Shahdc174302015-04-17 19:46:16 +0530689 }
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000690 }
691 }
692
693 if (check_fuse_status) {
694 if (power_well->data == SKL_DISP_PW_1) {
695 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
696 SKL_FUSE_PG1_DIST_STATUS), 1))
697 DRM_ERROR("PG1 distributing status timeout\n");
698 } else if (power_well->data == SKL_DISP_PW_2) {
699 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
700 SKL_FUSE_PG2_DIST_STATUS), 1))
701 DRM_ERROR("PG2 distributing status timeout\n");
702 }
703 }
Damien Lespiaud14c0342015-03-06 18:50:51 +0000704
705 if (enable && !is_enabled)
706 skl_power_well_post_enable(dev_priv, power_well);
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000707}
708
Daniel Vetter9c065a72014-09-30 10:56:38 +0200709static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
710 struct i915_power_well *power_well)
711{
712 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
713
714 /*
715 * We're taking over the BIOS, so clear any requests made by it since
716 * the driver is in charge now.
717 */
718 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
719 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
720}
721
722static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
723 struct i915_power_well *power_well)
724{
725 hsw_set_power_well(dev_priv, power_well, true);
726}
727
728static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
729 struct i915_power_well *power_well)
730{
731 hsw_set_power_well(dev_priv, power_well, false);
732}
733
Satheeshakrishna M94dd5132015-02-04 13:57:44 +0000734static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
735 struct i915_power_well *power_well)
736{
737 uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
738 SKL_POWER_WELL_STATE(power_well->data);
739
740 return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
741}
742
743static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
744 struct i915_power_well *power_well)
745{
746 skl_set_power_well(dev_priv, power_well, power_well->count > 0);
747
748 /* Clear any request made by BIOS as driver is taking over */
749 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
750}
751
752static void skl_power_well_enable(struct drm_i915_private *dev_priv,
753 struct i915_power_well *power_well)
754{
755 skl_set_power_well(dev_priv, power_well, true);
756}
757
758static void skl_power_well_disable(struct drm_i915_private *dev_priv,
759 struct i915_power_well *power_well)
760{
761 skl_set_power_well(dev_priv, power_well, false);
762}
763
Daniel Vetter9c065a72014-09-30 10:56:38 +0200764static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
765 struct i915_power_well *power_well)
766{
767}
768
769static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
770 struct i915_power_well *power_well)
771{
772 return true;
773}
774
775static void vlv_set_power_well(struct drm_i915_private *dev_priv,
776 struct i915_power_well *power_well, bool enable)
777{
778 enum punit_power_well power_well_id = power_well->data;
779 u32 mask;
780 u32 state;
781 u32 ctrl;
782
783 mask = PUNIT_PWRGT_MASK(power_well_id);
784 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
785 PUNIT_PWRGT_PWR_GATE(power_well_id);
786
787 mutex_lock(&dev_priv->rps.hw_lock);
788
789#define COND \
790 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
791
792 if (COND)
793 goto out;
794
795 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
796 ctrl &= ~mask;
797 ctrl |= state;
798 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
799
800 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +0900801 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +0200802 state,
803 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
804
805#undef COND
806
807out:
808 mutex_unlock(&dev_priv->rps.hw_lock);
809}
810
811static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
812 struct i915_power_well *power_well)
813{
814 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
815}
816
817static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
818 struct i915_power_well *power_well)
819{
820 vlv_set_power_well(dev_priv, power_well, true);
821}
822
823static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
824 struct i915_power_well *power_well)
825{
826 vlv_set_power_well(dev_priv, power_well, false);
827}
828
829static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
830 struct i915_power_well *power_well)
831{
832 int power_well_id = power_well->data;
833 bool enabled = false;
834 u32 mask;
835 u32 state;
836 u32 ctrl;
837
838 mask = PUNIT_PWRGT_MASK(power_well_id);
839 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
840
841 mutex_lock(&dev_priv->rps.hw_lock);
842
843 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
844 /*
845 * We only ever set the power-on and power-gate states, anything
846 * else is unexpected.
847 */
848 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
849 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
850 if (state == ctrl)
851 enabled = true;
852
853 /*
854 * A transient state at this point would mean some unexpected party
855 * is poking at the power controls too.
856 */
857 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
858 WARN_ON(ctrl != state);
859
860 mutex_unlock(&dev_priv->rps.hw_lock);
861
862 return enabled;
863}
864
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300865static void vlv_display_power_well_init(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +0200866{
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300867 enum pipe pipe;
868
869 /*
870 * Enable the CRI clock source so we can get at the
871 * display and the reference clock for VGA
872 * hotplug / manual detection. Supposedly DSI also
873 * needs the ref clock up and running.
874 *
875 * CHV DPLL B/C have some issues if VGA mode is enabled.
876 */
877 for_each_pipe(dev_priv->dev, pipe) {
878 u32 val = I915_READ(DPLL(pipe));
879
880 val |= DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
881 if (pipe != PIPE_A)
882 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
883
884 I915_WRITE(DPLL(pipe), val);
885 }
Daniel Vetter9c065a72014-09-30 10:56:38 +0200886
887 spin_lock_irq(&dev_priv->irq_lock);
888 valleyview_enable_display_irqs(dev_priv);
889 spin_unlock_irq(&dev_priv->irq_lock);
890
891 /*
892 * During driver initialization/resume we can avoid restoring the
893 * part of the HW/SW state that will be inited anyway explicitly.
894 */
895 if (dev_priv->power_domains.initializing)
896 return;
897
Daniel Vetterb9632912014-09-30 10:56:44 +0200898 intel_hpd_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200899
900 i915_redisable_vga_power_on(dev_priv->dev);
901}
902
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300903static void vlv_display_power_well_deinit(struct drm_i915_private *dev_priv)
904{
905 spin_lock_irq(&dev_priv->irq_lock);
906 valleyview_disable_display_irqs(dev_priv);
907 spin_unlock_irq(&dev_priv->irq_lock);
908
909 vlv_power_sequencer_reset(dev_priv);
910}
911
912static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
913 struct i915_power_well *power_well)
914{
915 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
916
917 vlv_set_power_well(dev_priv, power_well, true);
918
919 vlv_display_power_well_init(dev_priv);
920}
921
Daniel Vetter9c065a72014-09-30 10:56:38 +0200922static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
923 struct i915_power_well *power_well)
924{
925 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
926
Ville Syrjälä2be7d542015-06-29 15:25:51 +0300927 vlv_display_power_well_deinit(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200928
929 vlv_set_power_well(dev_priv, power_well, false);
Daniel Vetter9c065a72014-09-30 10:56:38 +0200930}
931
932static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
933 struct i915_power_well *power_well)
934{
935 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
936
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +0300937 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +0200938 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
939
940 vlv_set_power_well(dev_priv, power_well, true);
941
942 /*
943 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
944 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
945 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
946 * b. The other bits such as sfr settings / modesel may all
947 * be set to 0.
948 *
949 * This should only be done on init and resume from S3 with
950 * both PLLs disabled, or we risk losing DPIO and PLL
951 * synchronization.
952 */
953 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
954}
955
956static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
957 struct i915_power_well *power_well)
958{
959 enum pipe pipe;
960
961 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
962
963 for_each_pipe(dev_priv, pipe)
964 assert_pll_disabled(dev_priv, pipe);
965
966 /* Assert common reset */
967 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
968
969 vlv_set_power_well(dev_priv, power_well, false);
970}
971
Ville Syrjälä30142272015-07-08 23:46:01 +0300972#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
973
974static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
975 int power_well_id)
976{
977 struct i915_power_domains *power_domains = &dev_priv->power_domains;
978 struct i915_power_well *power_well;
979 int i;
980
981 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
982 if (power_well->data == power_well_id)
983 return power_well;
984 }
985
986 return NULL;
987}
988
989#define BITS_SET(val, bits) (((val) & (bits)) == (bits))
990
991static void assert_chv_phy_status(struct drm_i915_private *dev_priv)
992{
993 struct i915_power_well *cmn_bc =
994 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
995 struct i915_power_well *cmn_d =
996 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
997 u32 phy_control = dev_priv->chv_phy_control;
998 u32 phy_status = 0;
Ville Syrjälä3be60de2015-09-08 18:05:45 +0300999 u32 phy_status_mask = 0xffffffff;
Ville Syrjälä30142272015-07-08 23:46:01 +03001000 u32 tmp;
1001
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001002 /*
1003 * The BIOS can leave the PHY is some weird state
1004 * where it doesn't fully power down some parts.
1005 * Disable the asserts until the PHY has been fully
1006 * reset (ie. the power well has been disabled at
1007 * least once).
1008 */
1009 if (!dev_priv->chv_phy_assert[DPIO_PHY0])
1010 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0) |
1011 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0) |
1012 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1) |
1013 PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1) |
1014 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0) |
1015 PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1));
1016
1017 if (!dev_priv->chv_phy_assert[DPIO_PHY1])
1018 phy_status_mask &= ~(PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0) |
1019 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0) |
1020 PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1));
1021
Ville Syrjälä30142272015-07-08 23:46:01 +03001022 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1023 phy_status |= PHY_POWERGOOD(DPIO_PHY0);
1024
1025 /* this assumes override is only used to enable lanes */
1026 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0)) == 0)
1027 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0);
1028
1029 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1)) == 0)
1030 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1);
1031
1032 /* CL1 is on whenever anything is on in either channel */
1033 if (BITS_SET(phy_control,
1034 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH0) |
1035 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)))
1036 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH0);
1037
1038 /*
1039 * The DPLLB check accounts for the pipe B + port A usage
1040 * with CL2 powered up but all the lanes in the second channel
1041 * powered down.
1042 */
1043 if (BITS_SET(phy_control,
1044 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY0, DPIO_CH1)) &&
1045 (I915_READ(DPLL(PIPE_B)) & DPLL_VCO_ENABLE) == 0)
1046 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY0, DPIO_CH1);
1047
1048 if (BITS_SET(phy_control,
1049 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH0)))
1050 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 0);
1051 if (BITS_SET(phy_control,
1052 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH0)))
1053 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH0, 1);
1054
1055 if (BITS_SET(phy_control,
1056 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY0, DPIO_CH1)))
1057 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 0);
1058 if (BITS_SET(phy_control,
1059 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY0, DPIO_CH1)))
1060 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY0, DPIO_CH1, 1);
1061 }
1062
1063 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1064 phy_status |= PHY_POWERGOOD(DPIO_PHY1);
1065
1066 /* this assumes override is only used to enable lanes */
1067 if ((phy_control & PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0)) == 0)
1068 phy_control |= PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0);
1069
1070 if (BITS_SET(phy_control,
1071 PHY_CH_POWER_DOWN_OVRD(0xf, DPIO_PHY1, DPIO_CH0)))
1072 phy_status |= PHY_STATUS_CMN_LDO(DPIO_PHY1, DPIO_CH0);
1073
1074 if (BITS_SET(phy_control,
1075 PHY_CH_POWER_DOWN_OVRD(0x3, DPIO_PHY1, DPIO_CH0)))
1076 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 0);
1077 if (BITS_SET(phy_control,
1078 PHY_CH_POWER_DOWN_OVRD(0xc, DPIO_PHY1, DPIO_CH0)))
1079 phy_status |= PHY_STATUS_SPLINE_LDO(DPIO_PHY1, DPIO_CH0, 1);
1080 }
1081
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001082 phy_status &= phy_status_mask;
1083
Ville Syrjälä30142272015-07-08 23:46:01 +03001084 /*
1085 * The PHY may be busy with some initial calibration and whatnot,
1086 * so the power state can take a while to actually change.
1087 */
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001088 if (wait_for((tmp = I915_READ(DISPLAY_PHY_STATUS) & phy_status_mask) == phy_status, 10))
Ville Syrjälä30142272015-07-08 23:46:01 +03001089 WARN(phy_status != tmp,
1090 "Unexpected PHY_STATUS 0x%08x, expected 0x%08x (PHY_CONTROL=0x%08x)\n",
1091 tmp, phy_status, dev_priv->chv_phy_control);
1092}
1093
1094#undef BITS_SET
1095
Daniel Vetter9c065a72014-09-30 10:56:38 +02001096static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
1097 struct i915_power_well *power_well)
1098{
1099 enum dpio_phy phy;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001100 enum pipe pipe;
1101 uint32_t tmp;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001102
1103 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1104 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1105
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001106 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1107 pipe = PIPE_A;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001108 phy = DPIO_PHY0;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001109 } else {
1110 pipe = PIPE_C;
Daniel Vetter9c065a72014-09-30 10:56:38 +02001111 phy = DPIO_PHY1;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001112 }
Ville Syrjälä5a8fbb72015-06-29 15:25:53 +03001113
1114 /* since ref/cri clock was enabled */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001115 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
1116 vlv_set_power_well(dev_priv, power_well, true);
1117
1118 /* Poll for phypwrgood signal */
1119 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
1120 DRM_ERROR("Display PHY %d is not power up\n", phy);
1121
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001122 mutex_lock(&dev_priv->sb_lock);
1123
1124 /* Enable dynamic power down */
1125 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW28);
Ville Syrjäläee279212015-07-08 23:45:57 +03001126 tmp |= DPIO_DYNPWRDOWNEN_CH0 | DPIO_CL1POWERDOWNEN |
1127 DPIO_SUS_CLK_CONFIG_GATE_CLKREQ;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001128 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW28, tmp);
1129
1130 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1131 tmp = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW6_CH1);
1132 tmp |= DPIO_DYNPWRDOWNEN_CH1;
1133 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW6_CH1, tmp);
Ville Syrjälä3e288782015-07-08 23:45:58 +03001134 } else {
1135 /*
1136 * Force the non-existing CL2 off. BXT does this
1137 * too, so maybe it saves some power even though
1138 * CL2 doesn't exist?
1139 */
1140 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW30);
1141 tmp |= DPIO_CL2_LDOFUSE_PWRENB;
1142 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW30, tmp);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001143 }
1144
1145 mutex_unlock(&dev_priv->sb_lock);
1146
Ville Syrjälä70722462015-04-10 18:21:28 +03001147 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(phy);
1148 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001149
1150 DRM_DEBUG_KMS("Enabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1151 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001152
1153 assert_chv_phy_status(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001154}
1155
1156static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
1157 struct i915_power_well *power_well)
1158{
1159 enum dpio_phy phy;
1160
1161 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
1162 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
1163
1164 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
1165 phy = DPIO_PHY0;
1166 assert_pll_disabled(dev_priv, PIPE_A);
1167 assert_pll_disabled(dev_priv, PIPE_B);
1168 } else {
1169 phy = DPIO_PHY1;
1170 assert_pll_disabled(dev_priv, PIPE_C);
1171 }
1172
Ville Syrjälä70722462015-04-10 18:21:28 +03001173 dev_priv->chv_phy_control &= ~PHY_COM_LANE_RESET_DEASSERT(phy);
1174 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001175
1176 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001177
1178 DRM_DEBUG_KMS("Disabled DPIO PHY%d (PHY_CONTROL=0x%08x)\n",
1179 phy, dev_priv->chv_phy_control);
Ville Syrjälä30142272015-07-08 23:46:01 +03001180
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001181 /* PHY is fully reset now, so we can enable the PHY state asserts */
1182 dev_priv->chv_phy_assert[phy] = true;
1183
Ville Syrjälä30142272015-07-08 23:46:01 +03001184 assert_chv_phy_status(dev_priv);
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001185}
1186
Ville Syrjälä6669e392015-07-08 23:46:00 +03001187static void assert_chv_phy_powergate(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1188 enum dpio_channel ch, bool override, unsigned int mask)
1189{
1190 enum pipe pipe = phy == DPIO_PHY0 ? PIPE_A : PIPE_C;
1191 u32 reg, val, expected, actual;
1192
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001193 /*
1194 * The BIOS can leave the PHY is some weird state
1195 * where it doesn't fully power down some parts.
1196 * Disable the asserts until the PHY has been fully
1197 * reset (ie. the power well has been disabled at
1198 * least once).
1199 */
1200 if (!dev_priv->chv_phy_assert[phy])
1201 return;
1202
Ville Syrjälä6669e392015-07-08 23:46:00 +03001203 if (ch == DPIO_CH0)
1204 reg = _CHV_CMN_DW0_CH0;
1205 else
1206 reg = _CHV_CMN_DW6_CH1;
1207
1208 mutex_lock(&dev_priv->sb_lock);
1209 val = vlv_dpio_read(dev_priv, pipe, reg);
1210 mutex_unlock(&dev_priv->sb_lock);
1211
1212 /*
1213 * This assumes !override is only used when the port is disabled.
1214 * All lanes should power down even without the override when
1215 * the port is disabled.
1216 */
1217 if (!override || mask == 0xf) {
1218 expected = DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1219 /*
1220 * If CH1 common lane is not active anymore
1221 * (eg. for pipe B DPLL) the entire channel will
1222 * shut down, which causes the common lane registers
1223 * to read as 0. That means we can't actually check
1224 * the lane power down status bits, but as the entire
1225 * register reads as 0 it's a good indication that the
1226 * channel is indeed entirely powered down.
1227 */
1228 if (ch == DPIO_CH1 && val == 0)
1229 expected = 0;
1230 } else if (mask != 0x0) {
1231 expected = DPIO_ANYDL_POWERDOWN;
1232 } else {
1233 expected = 0;
1234 }
1235
1236 if (ch == DPIO_CH0)
1237 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH0;
1238 else
1239 actual = val >> DPIO_ANYDL_POWERDOWN_SHIFT_CH1;
1240 actual &= DPIO_ALLDL_POWERDOWN | DPIO_ANYDL_POWERDOWN;
1241
1242 WARN(actual != expected,
1243 "Unexpected DPIO lane power down: all %d, any %d. Expected: all %d, any %d. (0x%x = 0x%08x)\n",
1244 !!(actual & DPIO_ALLDL_POWERDOWN), !!(actual & DPIO_ANYDL_POWERDOWN),
1245 !!(expected & DPIO_ALLDL_POWERDOWN), !!(expected & DPIO_ANYDL_POWERDOWN),
1246 reg, val);
1247}
1248
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001249bool chv_phy_powergate_ch(struct drm_i915_private *dev_priv, enum dpio_phy phy,
1250 enum dpio_channel ch, bool override)
1251{
1252 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1253 bool was_override;
1254
1255 mutex_lock(&power_domains->lock);
1256
1257 was_override = dev_priv->chv_phy_control & PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1258
1259 if (override == was_override)
1260 goto out;
1261
1262 if (override)
1263 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1264 else
1265 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1266
1267 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1268
1269 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d (DPIO_PHY_CONTROL=0x%08x)\n",
1270 phy, ch, dev_priv->chv_phy_control);
1271
Ville Syrjälä30142272015-07-08 23:46:01 +03001272 assert_chv_phy_status(dev_priv);
1273
Ville Syrjäläb0b33842015-07-08 23:45:55 +03001274out:
1275 mutex_unlock(&power_domains->lock);
1276
1277 return was_override;
1278}
1279
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001280void chv_phy_powergate_lanes(struct intel_encoder *encoder,
1281 bool override, unsigned int mask)
1282{
1283 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1284 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1285 enum dpio_phy phy = vlv_dport_to_phy(enc_to_dig_port(&encoder->base));
1286 enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
1287
1288 mutex_lock(&power_domains->lock);
1289
1290 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD(0xf, phy, ch);
1291 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD(mask, phy, ch);
1292
1293 if (override)
1294 dev_priv->chv_phy_control |= PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1295 else
1296 dev_priv->chv_phy_control &= ~PHY_CH_POWER_DOWN_OVRD_EN(phy, ch);
1297
1298 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1299
1300 DRM_DEBUG_KMS("Power gating DPIO PHY%d CH%d lanes 0x%x (PHY_CONTROL=0x%08x)\n",
1301 phy, ch, mask, dev_priv->chv_phy_control);
1302
Ville Syrjälä30142272015-07-08 23:46:01 +03001303 assert_chv_phy_status(dev_priv);
1304
Ville Syrjälä6669e392015-07-08 23:46:00 +03001305 assert_chv_phy_powergate(dev_priv, phy, ch, override, mask);
1306
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001307 mutex_unlock(&power_domains->lock);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001308}
1309
1310static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
1311 struct i915_power_well *power_well)
1312{
1313 enum pipe pipe = power_well->data;
1314 bool enabled;
1315 u32 state, ctrl;
1316
1317 mutex_lock(&dev_priv->rps.hw_lock);
1318
1319 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
1320 /*
1321 * We only ever set the power-on and power-gate states, anything
1322 * else is unexpected.
1323 */
1324 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
1325 enabled = state == DP_SSS_PWR_ON(pipe);
1326
1327 /*
1328 * A transient state at this point would mean some unexpected party
1329 * is poking at the power controls too.
1330 */
1331 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
1332 WARN_ON(ctrl << 16 != state);
1333
1334 mutex_unlock(&dev_priv->rps.hw_lock);
1335
1336 return enabled;
1337}
1338
1339static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
1340 struct i915_power_well *power_well,
1341 bool enable)
1342{
1343 enum pipe pipe = power_well->data;
1344 u32 state;
1345 u32 ctrl;
1346
1347 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
1348
1349 mutex_lock(&dev_priv->rps.hw_lock);
1350
1351#define COND \
1352 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
1353
1354 if (COND)
1355 goto out;
1356
1357 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
1358 ctrl &= ~DP_SSC_MASK(pipe);
1359 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
1360 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
1361
1362 if (wait_for(COND, 100))
Masanari Iida7e35ab82015-05-10 01:00:23 +09001363 DRM_ERROR("timeout setting power well state %08x (%08x)\n",
Daniel Vetter9c065a72014-09-30 10:56:38 +02001364 state,
1365 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
1366
1367#undef COND
1368
1369out:
1370 mutex_unlock(&dev_priv->rps.hw_lock);
1371}
1372
1373static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
1374 struct i915_power_well *power_well)
1375{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001376 WARN_ON_ONCE(power_well->data != PIPE_A);
1377
Daniel Vetter9c065a72014-09-30 10:56:38 +02001378 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
1379}
1380
1381static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
1382 struct i915_power_well *power_well)
1383{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001384 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001385
1386 chv_set_pipe_power_well(dev_priv, power_well, true);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001387
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001388 vlv_display_power_well_init(dev_priv);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001389}
1390
1391static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
1392 struct i915_power_well *power_well)
1393{
Ville Syrjälä8fcd5cd2015-06-29 15:25:50 +03001394 WARN_ON_ONCE(power_well->data != PIPE_A);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001395
Ville Syrjälä2be7d542015-06-29 15:25:51 +03001396 vlv_display_power_well_deinit(dev_priv);
Ville Syrjäläafd62752014-10-30 19:43:03 +02001397
Daniel Vetter9c065a72014-09-30 10:56:38 +02001398 chv_set_pipe_power_well(dev_priv, power_well, false);
1399}
1400
Daniel Vettere4e76842014-09-30 10:56:42 +02001401/**
1402 * intel_display_power_get - grab a power domain reference
1403 * @dev_priv: i915 device instance
1404 * @domain: power domain to reference
1405 *
1406 * This function grabs a power domain reference for @domain and ensures that the
1407 * power domain and all its parents are powered up. Therefore users should only
1408 * grab a reference to the innermost power domain they need.
1409 *
1410 * Any power domain reference obtained by this function must have a symmetric
1411 * call to intel_display_power_put() to release the reference again.
1412 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001413void intel_display_power_get(struct drm_i915_private *dev_priv,
1414 enum intel_display_power_domain domain)
1415{
1416 struct i915_power_domains *power_domains;
1417 struct i915_power_well *power_well;
1418 int i;
1419
1420 intel_runtime_pm_get(dev_priv);
1421
1422 power_domains = &dev_priv->power_domains;
1423
1424 mutex_lock(&power_domains->lock);
1425
1426 for_each_power_well(i, power_well, BIT(domain), power_domains) {
Damien Lespiaue8ca9322015-07-30 18:20:26 -03001427 if (!power_well->count++)
1428 intel_power_well_enable(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001429 }
1430
1431 power_domains->domain_use_count[domain]++;
1432
1433 mutex_unlock(&power_domains->lock);
1434}
1435
Daniel Vettere4e76842014-09-30 10:56:42 +02001436/**
1437 * intel_display_power_put - release a power domain reference
1438 * @dev_priv: i915 device instance
1439 * @domain: power domain to reference
1440 *
1441 * This function drops the power domain reference obtained by
1442 * intel_display_power_get() and might power down the corresponding hardware
1443 * block right away if this is the last reference.
1444 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001445void intel_display_power_put(struct drm_i915_private *dev_priv,
1446 enum intel_display_power_domain domain)
1447{
1448 struct i915_power_domains *power_domains;
1449 struct i915_power_well *power_well;
1450 int i;
1451
1452 power_domains = &dev_priv->power_domains;
1453
1454 mutex_lock(&power_domains->lock);
1455
1456 WARN_ON(!power_domains->domain_use_count[domain]);
1457 power_domains->domain_use_count[domain]--;
1458
1459 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
1460 WARN_ON(!power_well->count);
1461
Damien Lespiaudcddab32015-07-30 18:20:27 -03001462 if (!--power_well->count && i915.disable_power_well)
1463 intel_power_well_disable(dev_priv, power_well);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001464 }
1465
1466 mutex_unlock(&power_domains->lock);
1467
1468 intel_runtime_pm_put(dev_priv);
1469}
1470
Daniel Vetter9c065a72014-09-30 10:56:38 +02001471#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
1472 BIT(POWER_DOMAIN_PIPE_A) | \
1473 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
1474 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
1475 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
1476 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
1477 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
1478 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
1479 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
1480 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
1481 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
1482 BIT(POWER_DOMAIN_PORT_CRT) | \
1483 BIT(POWER_DOMAIN_PLLS) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001484 BIT(POWER_DOMAIN_AUX_A) | \
1485 BIT(POWER_DOMAIN_AUX_B) | \
1486 BIT(POWER_DOMAIN_AUX_C) | \
1487 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001488 BIT(POWER_DOMAIN_INIT))
1489#define HSW_DISPLAY_POWER_DOMAINS ( \
1490 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
1491 BIT(POWER_DOMAIN_INIT))
1492
1493#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
1494 HSW_ALWAYS_ON_POWER_DOMAINS | \
1495 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
1496#define BDW_DISPLAY_POWER_DOMAINS ( \
1497 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
1498 BIT(POWER_DOMAIN_INIT))
1499
1500#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
1501#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
1502
1503#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
1504 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
1505 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
1506 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
1507 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
1508 BIT(POWER_DOMAIN_PORT_CRT) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001509 BIT(POWER_DOMAIN_AUX_B) | \
1510 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001511 BIT(POWER_DOMAIN_INIT))
1512
1513#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
1514 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
1515 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001516 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001517 BIT(POWER_DOMAIN_INIT))
1518
1519#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
1520 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001521 BIT(POWER_DOMAIN_AUX_B) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001522 BIT(POWER_DOMAIN_INIT))
1523
1524#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
1525 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
1526 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001527 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001528 BIT(POWER_DOMAIN_INIT))
1529
1530#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
1531 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001532 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001533 BIT(POWER_DOMAIN_INIT))
1534
Daniel Vetter9c065a72014-09-30 10:56:38 +02001535#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
1536 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
1537 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
1538 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
1539 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001540 BIT(POWER_DOMAIN_AUX_B) | \
1541 BIT(POWER_DOMAIN_AUX_C) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001542 BIT(POWER_DOMAIN_INIT))
1543
1544#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
1545 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
1546 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
Satheeshakrishna M14071212015-01-16 15:57:51 +00001547 BIT(POWER_DOMAIN_AUX_D) | \
Daniel Vetter9c065a72014-09-30 10:56:38 +02001548 BIT(POWER_DOMAIN_INIT))
1549
Daniel Vetter9c065a72014-09-30 10:56:38 +02001550static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
1551 .sync_hw = i9xx_always_on_power_well_noop,
1552 .enable = i9xx_always_on_power_well_noop,
1553 .disable = i9xx_always_on_power_well_noop,
1554 .is_enabled = i9xx_always_on_power_well_enabled,
1555};
1556
1557static const struct i915_power_well_ops chv_pipe_power_well_ops = {
1558 .sync_hw = chv_pipe_power_well_sync_hw,
1559 .enable = chv_pipe_power_well_enable,
1560 .disable = chv_pipe_power_well_disable,
1561 .is_enabled = chv_pipe_power_well_enabled,
1562};
1563
1564static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
1565 .sync_hw = vlv_power_well_sync_hw,
1566 .enable = chv_dpio_cmn_power_well_enable,
1567 .disable = chv_dpio_cmn_power_well_disable,
1568 .is_enabled = vlv_power_well_enabled,
1569};
1570
1571static struct i915_power_well i9xx_always_on_power_well[] = {
1572 {
1573 .name = "always-on",
1574 .always_on = 1,
1575 .domains = POWER_DOMAIN_MASK,
1576 .ops = &i9xx_always_on_power_well_ops,
1577 },
1578};
1579
1580static const struct i915_power_well_ops hsw_power_well_ops = {
1581 .sync_hw = hsw_power_well_sync_hw,
1582 .enable = hsw_power_well_enable,
1583 .disable = hsw_power_well_disable,
1584 .is_enabled = hsw_power_well_enabled,
1585};
1586
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001587static const struct i915_power_well_ops skl_power_well_ops = {
1588 .sync_hw = skl_power_well_sync_hw,
1589 .enable = skl_power_well_enable,
1590 .disable = skl_power_well_disable,
1591 .is_enabled = skl_power_well_enabled,
1592};
1593
Daniel Vetter9c065a72014-09-30 10:56:38 +02001594static struct i915_power_well hsw_power_wells[] = {
1595 {
1596 .name = "always-on",
1597 .always_on = 1,
1598 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
1599 .ops = &i9xx_always_on_power_well_ops,
1600 },
1601 {
1602 .name = "display",
1603 .domains = HSW_DISPLAY_POWER_DOMAINS,
1604 .ops = &hsw_power_well_ops,
1605 },
1606};
1607
1608static struct i915_power_well bdw_power_wells[] = {
1609 {
1610 .name = "always-on",
1611 .always_on = 1,
1612 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
1613 .ops = &i9xx_always_on_power_well_ops,
1614 },
1615 {
1616 .name = "display",
1617 .domains = BDW_DISPLAY_POWER_DOMAINS,
1618 .ops = &hsw_power_well_ops,
1619 },
1620};
1621
1622static const struct i915_power_well_ops vlv_display_power_well_ops = {
1623 .sync_hw = vlv_power_well_sync_hw,
1624 .enable = vlv_display_power_well_enable,
1625 .disable = vlv_display_power_well_disable,
1626 .is_enabled = vlv_power_well_enabled,
1627};
1628
1629static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1630 .sync_hw = vlv_power_well_sync_hw,
1631 .enable = vlv_dpio_cmn_power_well_enable,
1632 .disable = vlv_dpio_cmn_power_well_disable,
1633 .is_enabled = vlv_power_well_enabled,
1634};
1635
1636static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1637 .sync_hw = vlv_power_well_sync_hw,
1638 .enable = vlv_power_well_enable,
1639 .disable = vlv_power_well_disable,
1640 .is_enabled = vlv_power_well_enabled,
1641};
1642
1643static struct i915_power_well vlv_power_wells[] = {
1644 {
1645 .name = "always-on",
1646 .always_on = 1,
1647 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1648 .ops = &i9xx_always_on_power_well_ops,
1649 },
1650 {
1651 .name = "display",
1652 .domains = VLV_DISPLAY_POWER_DOMAINS,
1653 .data = PUNIT_POWER_WELL_DISP2D,
1654 .ops = &vlv_display_power_well_ops,
1655 },
1656 {
1657 .name = "dpio-tx-b-01",
1658 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1659 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1660 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1661 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1662 .ops = &vlv_dpio_power_well_ops,
1663 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1664 },
1665 {
1666 .name = "dpio-tx-b-23",
1667 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1668 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1669 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1670 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1671 .ops = &vlv_dpio_power_well_ops,
1672 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1673 },
1674 {
1675 .name = "dpio-tx-c-01",
1676 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1677 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1678 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1679 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1680 .ops = &vlv_dpio_power_well_ops,
1681 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1682 },
1683 {
1684 .name = "dpio-tx-c-23",
1685 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1686 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1687 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1688 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1689 .ops = &vlv_dpio_power_well_ops,
1690 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1691 },
1692 {
1693 .name = "dpio-common",
1694 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1695 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1696 .ops = &vlv_dpio_cmn_power_well_ops,
1697 },
1698};
1699
1700static struct i915_power_well chv_power_wells[] = {
1701 {
1702 .name = "always-on",
1703 .always_on = 1,
1704 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1705 .ops = &i9xx_always_on_power_well_ops,
1706 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001707 {
1708 .name = "display",
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001709 /*
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001710 * Pipe A power well is the new disp2d well. Pipe B and C
1711 * power wells don't actually exist. Pipe A power well is
1712 * required for any pipe to work.
Ville Syrjäläbaa4e572014-10-27 16:07:32 +02001713 */
Ville Syrjäläfde61e42015-05-26 20:22:39 +03001714 .domains = VLV_DISPLAY_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001715 .data = PIPE_A,
1716 .ops = &chv_pipe_power_well_ops,
1717 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001718 {
1719 .name = "dpio-common-bc",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001720 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001721 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1722 .ops = &chv_dpio_cmn_power_well_ops,
1723 },
1724 {
1725 .name = "dpio-common-d",
Ville Syrjälä71849b62015-04-10 18:21:29 +03001726 .domains = CHV_DPIO_CMN_D_POWER_DOMAINS,
Daniel Vetter9c065a72014-09-30 10:56:38 +02001727 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
1728 .ops = &chv_dpio_cmn_power_well_ops,
1729 },
Daniel Vetter9c065a72014-09-30 10:56:38 +02001730};
1731
Suketu Shah5aefb232015-04-16 14:22:10 +05301732bool intel_display_power_well_is_enabled(struct drm_i915_private *dev_priv,
1733 int power_well_id)
1734{
1735 struct i915_power_well *power_well;
1736 bool ret;
1737
1738 power_well = lookup_power_well(dev_priv, power_well_id);
1739 ret = power_well->ops->is_enabled(dev_priv, power_well);
1740
1741 return ret;
1742}
1743
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001744static struct i915_power_well skl_power_wells[] = {
1745 {
1746 .name = "always-on",
1747 .always_on = 1,
1748 .domains = SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1749 .ops = &i9xx_always_on_power_well_ops,
1750 },
1751 {
1752 .name = "power well 1",
1753 .domains = SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS,
1754 .ops = &skl_power_well_ops,
1755 .data = SKL_DISP_PW_1,
1756 },
1757 {
1758 .name = "MISC IO power well",
1759 .domains = SKL_DISPLAY_MISC_IO_POWER_DOMAINS,
1760 .ops = &skl_power_well_ops,
1761 .data = SKL_DISP_PW_MISC_IO,
1762 },
1763 {
1764 .name = "power well 2",
1765 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1766 .ops = &skl_power_well_ops,
1767 .data = SKL_DISP_PW_2,
1768 },
1769 {
1770 .name = "DDI A/E power well",
1771 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1772 .ops = &skl_power_well_ops,
1773 .data = SKL_DISP_PW_DDI_A_E,
1774 },
1775 {
1776 .name = "DDI B power well",
1777 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1778 .ops = &skl_power_well_ops,
1779 .data = SKL_DISP_PW_DDI_B,
1780 },
1781 {
1782 .name = "DDI C power well",
1783 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1784 .ops = &skl_power_well_ops,
1785 .data = SKL_DISP_PW_DDI_C,
1786 },
1787 {
1788 .name = "DDI D power well",
1789 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1790 .ops = &skl_power_well_ops,
1791 .data = SKL_DISP_PW_DDI_D,
1792 },
1793};
1794
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301795static struct i915_power_well bxt_power_wells[] = {
1796 {
1797 .name = "always-on",
1798 .always_on = 1,
1799 .domains = BXT_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1800 .ops = &i9xx_always_on_power_well_ops,
1801 },
1802 {
1803 .name = "power well 1",
1804 .domains = BXT_DISPLAY_POWERWELL_1_POWER_DOMAINS,
1805 .ops = &skl_power_well_ops,
1806 .data = SKL_DISP_PW_1,
1807 },
1808 {
1809 .name = "power well 2",
1810 .domains = BXT_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1811 .ops = &skl_power_well_ops,
1812 .data = SKL_DISP_PW_2,
1813 }
1814};
1815
Daniel Vetter9c065a72014-09-30 10:56:38 +02001816#define set_power_wells(power_domains, __power_wells) ({ \
1817 (power_domains)->power_wells = (__power_wells); \
1818 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
1819})
1820
Daniel Vettere4e76842014-09-30 10:56:42 +02001821/**
1822 * intel_power_domains_init - initializes the power domain structures
1823 * @dev_priv: i915 device instance
1824 *
1825 * Initializes the power domain structures for @dev_priv depending upon the
1826 * supported platform.
1827 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02001828int intel_power_domains_init(struct drm_i915_private *dev_priv)
1829{
1830 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1831
1832 mutex_init(&power_domains->lock);
1833
1834 /*
1835 * The enabling order will be from lower to higher indexed wells,
1836 * the disabling order is reversed.
1837 */
1838 if (IS_HASWELL(dev_priv->dev)) {
1839 set_power_wells(power_domains, hsw_power_wells);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001840 } else if (IS_BROADWELL(dev_priv->dev)) {
1841 set_power_wells(power_domains, bdw_power_wells);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07001842 } else if (IS_SKYLAKE(dev_priv->dev) || IS_KABYLAKE(dev_priv->dev)) {
Satheeshakrishna M94dd5132015-02-04 13:57:44 +00001843 set_power_wells(power_domains, skl_power_wells);
Satheeshakrishna M0b4a2a32014-07-11 14:51:13 +05301844 } else if (IS_BROXTON(dev_priv->dev)) {
1845 set_power_wells(power_domains, bxt_power_wells);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001846 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1847 set_power_wells(power_domains, chv_power_wells);
1848 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
1849 set_power_wells(power_domains, vlv_power_wells);
1850 } else {
1851 set_power_wells(power_domains, i9xx_always_on_power_well);
1852 }
1853
1854 return 0;
1855}
1856
Daniel Vettere4e76842014-09-30 10:56:42 +02001857/**
1858 * intel_power_domains_fini - finalizes the power domain structures
1859 * @dev_priv: i915 device instance
1860 *
1861 * Finalizes the power domain structures for @dev_priv depending upon the
1862 * supported platform. This function also disables runtime pm and ensures that
1863 * the device stays powered up so that the driver can be reloaded.
1864 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001865void intel_power_domains_fini(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02001866{
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001867 /* The i915.ko module is still not prepared to be loaded when
1868 * the power well is not enabled, so just enable it in case
1869 * we're going to unload/reload. */
1870 intel_display_set_init_power(dev_priv, true);
Daniel Vetter9c065a72014-09-30 10:56:38 +02001871}
1872
1873static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
1874{
1875 struct i915_power_domains *power_domains = &dev_priv->power_domains;
1876 struct i915_power_well *power_well;
1877 int i;
1878
1879 mutex_lock(&power_domains->lock);
1880 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
1881 power_well->ops->sync_hw(dev_priv, power_well);
1882 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
1883 power_well);
1884 }
1885 mutex_unlock(&power_domains->lock);
1886}
1887
Ville Syrjälä70722462015-04-10 18:21:28 +03001888static void chv_phy_control_init(struct drm_i915_private *dev_priv)
1889{
1890 struct i915_power_well *cmn_bc =
1891 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1892 struct i915_power_well *cmn_d =
1893 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_D);
1894
1895 /*
1896 * DISPLAY_PHY_CONTROL can get corrupted if read. As a
1897 * workaround never ever read DISPLAY_PHY_CONTROL, and
1898 * instead maintain a shadow copy ourselves. Use the actual
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001899 * power well state and lane status to reconstruct the
1900 * expected initial value.
Ville Syrjälä70722462015-04-10 18:21:28 +03001901 */
1902 dev_priv->chv_phy_control =
Ville Syrjäläbc284542015-05-26 20:22:38 +03001903 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY0) |
1904 PHY_LDO_SEQ_DELAY(PHY_LDO_DELAY_600NS, DPIO_PHY1) |
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001905 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH0) |
1906 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY0, DPIO_CH1) |
1907 PHY_CH_POWER_MODE(PHY_CH_DEEP_PSR, DPIO_PHY1, DPIO_CH0);
1908
1909 /*
1910 * If all lanes are disabled we leave the override disabled
1911 * with all power down bits cleared to match the state we
1912 * would use after disabling the port. Otherwise enable the
1913 * override and set the lane powerdown bits accding to the
1914 * current lane status.
1915 */
1916 if (cmn_bc->ops->is_enabled(dev_priv, cmn_bc)) {
1917 uint32_t status = I915_READ(DPLL(PIPE_A));
1918 unsigned int mask;
1919
1920 mask = status & DPLL_PORTB_READY_MASK;
1921 if (mask == 0xf)
1922 mask = 0x0;
1923 else
1924 dev_priv->chv_phy_control |=
1925 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH0);
1926
1927 dev_priv->chv_phy_control |=
1928 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH0);
1929
1930 mask = (status & DPLL_PORTC_READY_MASK) >> 4;
1931 if (mask == 0xf)
1932 mask = 0x0;
1933 else
1934 dev_priv->chv_phy_control |=
1935 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY0, DPIO_CH1);
1936
1937 dev_priv->chv_phy_control |=
1938 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY0, DPIO_CH1);
1939
Ville Syrjälä70722462015-04-10 18:21:28 +03001940 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY0);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001941
1942 dev_priv->chv_phy_assert[DPIO_PHY0] = false;
1943 } else {
1944 dev_priv->chv_phy_assert[DPIO_PHY0] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001945 }
1946
1947 if (cmn_d->ops->is_enabled(dev_priv, cmn_d)) {
1948 uint32_t status = I915_READ(DPIO_PHY_STATUS);
1949 unsigned int mask;
1950
1951 mask = status & DPLL_PORTD_READY_MASK;
1952
1953 if (mask == 0xf)
1954 mask = 0x0;
1955 else
1956 dev_priv->chv_phy_control |=
1957 PHY_CH_POWER_DOWN_OVRD_EN(DPIO_PHY1, DPIO_CH0);
1958
1959 dev_priv->chv_phy_control |=
1960 PHY_CH_POWER_DOWN_OVRD(mask, DPIO_PHY1, DPIO_CH0);
1961
Ville Syrjälä70722462015-04-10 18:21:28 +03001962 dev_priv->chv_phy_control |= PHY_COM_LANE_RESET_DEASSERT(DPIO_PHY1);
Ville Syrjälä3be60de2015-09-08 18:05:45 +03001963
1964 dev_priv->chv_phy_assert[DPIO_PHY1] = false;
1965 } else {
1966 dev_priv->chv_phy_assert[DPIO_PHY1] = true;
Ville Syrjäläe0fce782015-07-08 23:45:54 +03001967 }
1968
1969 I915_WRITE(DISPLAY_PHY_CONTROL, dev_priv->chv_phy_control);
1970
1971 DRM_DEBUG_KMS("Initial PHY_CONTROL=0x%08x\n",
1972 dev_priv->chv_phy_control);
Ville Syrjälä70722462015-04-10 18:21:28 +03001973}
1974
Daniel Vetter9c065a72014-09-30 10:56:38 +02001975static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
1976{
1977 struct i915_power_well *cmn =
1978 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1979 struct i915_power_well *disp2d =
1980 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
1981
Daniel Vetter9c065a72014-09-30 10:56:38 +02001982 /* If the display might be already active skip this */
Ville Syrjälä5d93a6e2014-10-16 20:52:33 +03001983 if (cmn->ops->is_enabled(dev_priv, cmn) &&
1984 disp2d->ops->is_enabled(dev_priv, disp2d) &&
Daniel Vetter9c065a72014-09-30 10:56:38 +02001985 I915_READ(DPIO_CTL) & DPIO_CMNRST)
1986 return;
1987
1988 DRM_DEBUG_KMS("toggling display PHY side reset\n");
1989
1990 /* cmnlane needs DPLL registers */
1991 disp2d->ops->enable(dev_priv, disp2d);
1992
1993 /*
1994 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1995 * Need to assert and de-assert PHY SB reset by gating the
1996 * common lane power, then un-gating it.
1997 * Simply ungating isn't enough to reset the PHY enough to get
1998 * ports and lanes running.
1999 */
2000 cmn->ops->disable(dev_priv, cmn);
2001}
2002
Daniel Vettere4e76842014-09-30 10:56:42 +02002003/**
2004 * intel_power_domains_init_hw - initialize hardware power domain state
2005 * @dev_priv: i915 device instance
2006 *
2007 * This function initializes the hardware power domain state and enables all
2008 * power domains using intel_display_set_init_power().
2009 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002010void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
2011{
2012 struct drm_device *dev = dev_priv->dev;
2013 struct i915_power_domains *power_domains = &dev_priv->power_domains;
2014
2015 power_domains->initializing = true;
2016
Ville Syrjälä70722462015-04-10 18:21:28 +03002017 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä770effb2015-07-08 23:45:51 +03002018 mutex_lock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002019 chv_phy_control_init(dev_priv);
Ville Syrjälä770effb2015-07-08 23:45:51 +03002020 mutex_unlock(&power_domains->lock);
Ville Syrjälä70722462015-04-10 18:21:28 +03002021 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9c065a72014-09-30 10:56:38 +02002022 mutex_lock(&power_domains->lock);
2023 vlv_cmnlane_wa(dev_priv);
2024 mutex_unlock(&power_domains->lock);
2025 }
2026
2027 /* For now, we need the power well to be always enabled. */
2028 intel_display_set_init_power(dev_priv, true);
2029 intel_power_domains_resume(dev_priv);
2030 power_domains->initializing = false;
2031}
2032
Daniel Vettere4e76842014-09-30 10:56:42 +02002033/**
Geert Uytterhoevenca2b1402015-03-09 21:21:08 +01002034 * intel_aux_display_runtime_get - grab an auxiliary power domain reference
Daniel Vettere4e76842014-09-30 10:56:42 +02002035 * @dev_priv: i915 device instance
2036 *
2037 * This function grabs a power domain reference for the auxiliary power domain
2038 * (for access to the GMBUS and DP AUX blocks) and ensures that it and all its
2039 * parents are powered up. Therefore users should only grab a reference to the
2040 * innermost power domain they need.
2041 *
2042 * Any power domain reference obtained by this function must have a symmetric
2043 * call to intel_aux_display_runtime_put() to release the reference again.
2044 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002045void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
2046{
2047 intel_runtime_pm_get(dev_priv);
2048}
2049
Daniel Vettere4e76842014-09-30 10:56:42 +02002050/**
Geert Uytterhoevenca2b1402015-03-09 21:21:08 +01002051 * intel_aux_display_runtime_put - release an auxiliary power domain reference
Daniel Vettere4e76842014-09-30 10:56:42 +02002052 * @dev_priv: i915 device instance
2053 *
Geert Uytterhoevenca2b1402015-03-09 21:21:08 +01002054 * This function drops the auxiliary power domain reference obtained by
Daniel Vettere4e76842014-09-30 10:56:42 +02002055 * intel_aux_display_runtime_get() and might power down the corresponding
2056 * hardware block right away if this is the last reference.
2057 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002058void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
2059{
2060 intel_runtime_pm_put(dev_priv);
2061}
2062
Daniel Vettere4e76842014-09-30 10:56:42 +02002063/**
2064 * intel_runtime_pm_get - grab a runtime pm reference
2065 * @dev_priv: i915 device instance
2066 *
2067 * This function grabs a device-level runtime pm reference (mostly used for GEM
2068 * code to ensure the GTT or GT is on) and ensures that it is powered up.
2069 *
2070 * Any runtime pm reference obtained by this function must have a symmetric
2071 * call to intel_runtime_pm_put() to release the reference again.
2072 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002073void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
2074{
2075 struct drm_device *dev = dev_priv->dev;
2076 struct device *device = &dev->pdev->dev;
2077
2078 if (!HAS_RUNTIME_PM(dev))
2079 return;
2080
2081 pm_runtime_get_sync(device);
2082 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
2083}
2084
Daniel Vettere4e76842014-09-30 10:56:42 +02002085/**
2086 * intel_runtime_pm_get_noresume - grab a runtime pm reference
2087 * @dev_priv: i915 device instance
2088 *
2089 * This function grabs a device-level runtime pm reference (mostly used for GEM
2090 * code to ensure the GTT or GT is on).
2091 *
2092 * It will _not_ power up the device but instead only check that it's powered
2093 * on. Therefore it is only valid to call this functions from contexts where
2094 * the device is known to be powered up and where trying to power it up would
2095 * result in hilarity and deadlocks. That pretty much means only the system
2096 * suspend/resume code where this is used to grab runtime pm references for
2097 * delayed setup down in work items.
2098 *
2099 * Any runtime pm reference obtained by this function must have a symmetric
2100 * call to intel_runtime_pm_put() to release the reference again.
2101 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002102void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
2103{
2104 struct drm_device *dev = dev_priv->dev;
2105 struct device *device = &dev->pdev->dev;
2106
2107 if (!HAS_RUNTIME_PM(dev))
2108 return;
2109
2110 WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
2111 pm_runtime_get_noresume(device);
2112}
2113
Daniel Vettere4e76842014-09-30 10:56:42 +02002114/**
2115 * intel_runtime_pm_put - release a runtime pm reference
2116 * @dev_priv: i915 device instance
2117 *
2118 * This function drops the device-level runtime pm reference obtained by
2119 * intel_runtime_pm_get() and might power down the corresponding
2120 * hardware block right away if this is the last reference.
2121 */
Daniel Vetter9c065a72014-09-30 10:56:38 +02002122void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
2123{
2124 struct drm_device *dev = dev_priv->dev;
2125 struct device *device = &dev->pdev->dev;
2126
2127 if (!HAS_RUNTIME_PM(dev))
2128 return;
2129
2130 pm_runtime_mark_last_busy(device);
2131 pm_runtime_put_autosuspend(device);
2132}
2133
Daniel Vettere4e76842014-09-30 10:56:42 +02002134/**
2135 * intel_runtime_pm_enable - enable runtime pm
2136 * @dev_priv: i915 device instance
2137 *
2138 * This function enables runtime pm at the end of the driver load sequence.
2139 *
2140 * Note that this function does currently not enable runtime pm for the
2141 * subordinate display power domains. That is only done on the first modeset
2142 * using intel_display_set_init_power().
2143 */
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002144void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
Daniel Vetter9c065a72014-09-30 10:56:38 +02002145{
2146 struct drm_device *dev = dev_priv->dev;
2147 struct device *device = &dev->pdev->dev;
2148
2149 if (!HAS_RUNTIME_PM(dev))
2150 return;
2151
Daniel Vetter9c065a72014-09-30 10:56:38 +02002152 /*
2153 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
2154 * requirement.
2155 */
2156 if (!intel_enable_rc6(dev)) {
2157 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
2158 return;
2159 }
2160
2161 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
2162 pm_runtime_mark_last_busy(device);
2163 pm_runtime_use_autosuspend(device);
2164
2165 pm_runtime_put_autosuspend(device);
2166}
2167