Merge tag 'drm-intel-next-2015-02-14' of git://anongit.freedesktop.org/drm-intel...
[pandora-kernel.git] / drivers / gpu / drm / i915 / intel_runtime_pm.c
1 /*
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"
34
35 /**
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
52 #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++)                                                       \
57                 if ((power_well)->domains & (domain_mask))
58
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--)                                                        \
63                 if ((power_well)->domains & (domain_mask))
64
65 /*
66  * We should only use the power well if we explicitly asked the hardware to
67  * enable it, so check if it's enabled and also check if we've requested it to
68  * be enabled.
69  */
70 static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
71                                    struct i915_power_well *power_well)
72 {
73         return I915_READ(HSW_PWR_WELL_DRIVER) ==
74                      (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
75 }
76
77 /**
78  * __intel_display_power_is_enabled - unlocked check for a power domain
79  * @dev_priv: i915 device instance
80  * @domain: power domain to check
81  *
82  * This is the unlocked version of intel_display_power_is_enabled() and should
83  * only be used from error capture and recovery code where deadlocks are
84  * possible.
85  *
86  * Returns:
87  * True when the power domain is enabled, false otherwise.
88  */
89 bool __intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
90                                       enum intel_display_power_domain domain)
91 {
92         struct i915_power_domains *power_domains;
93         struct i915_power_well *power_well;
94         bool is_enabled;
95         int i;
96
97         if (dev_priv->pm.suspended)
98                 return false;
99
100         power_domains = &dev_priv->power_domains;
101
102         is_enabled = true;
103
104         for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
105                 if (power_well->always_on)
106                         continue;
107
108                 if (!power_well->hw_enabled) {
109                         is_enabled = false;
110                         break;
111                 }
112         }
113
114         return is_enabled;
115 }
116
117 /**
118  * intel_display_power_is_enabled - check for a power domain
119  * @dev_priv: i915 device instance
120  * @domain: power domain to check
121  *
122  * This function can be used to check the hw power domain state. It is mostly
123  * used in hardware state readout functions. Everywhere else code should rely
124  * upon explicit power domain reference counting to ensure that the hardware
125  * block is powered up before accessing it.
126  *
127  * Callers must hold the relevant modesetting locks to ensure that concurrent
128  * threads can't disable the power well while the caller tries to read a few
129  * registers.
130  *
131  * Returns:
132  * True when the power domain is enabled, false otherwise.
133  */
134 bool intel_display_power_is_enabled(struct drm_i915_private *dev_priv,
135                                     enum intel_display_power_domain domain)
136 {
137         struct i915_power_domains *power_domains;
138         bool ret;
139
140         power_domains = &dev_priv->power_domains;
141
142         mutex_lock(&power_domains->lock);
143         ret = __intel_display_power_is_enabled(dev_priv, domain);
144         mutex_unlock(&power_domains->lock);
145
146         return ret;
147 }
148
149 /**
150  * intel_display_set_init_power - set the initial power domain state
151  * @dev_priv: i915 device instance
152  * @enable: whether to enable or disable the initial power domain state
153  *
154  * For simplicity our driver load/unload and system suspend/resume code assumes
155  * that all power domains are always enabled. This functions controls the state
156  * of this little hack. While the initial power domain state is enabled runtime
157  * pm is effectively disabled.
158  */
159 void intel_display_set_init_power(struct drm_i915_private *dev_priv,
160                                   bool enable)
161 {
162         if (dev_priv->power_domains.init_power_on == enable)
163                 return;
164
165         if (enable)
166                 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
167         else
168                 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
169
170         dev_priv->power_domains.init_power_on = enable;
171 }
172
173 /*
174  * Starting with Haswell, we have a "Power Down Well" that can be turned off
175  * when not needed anymore. We have 4 registers that can request the power well
176  * to be enabled, and it will only be disabled if none of the registers is
177  * requesting it to be enabled.
178  */
179 static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
180 {
181         struct drm_device *dev = dev_priv->dev;
182
183         /*
184          * After we re-enable the power well, if we touch VGA register 0x3d5
185          * we'll get unclaimed register interrupts. This stops after we write
186          * anything to the VGA MSR register. The vgacon module uses this
187          * register all the time, so if we unbind our driver and, as a
188          * consequence, bind vgacon, we'll get stuck in an infinite loop at
189          * console_unlock(). So make here we touch the VGA MSR register, making
190          * sure vgacon can keep working normally without triggering interrupts
191          * and error messages.
192          */
193         vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
194         outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
195         vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
196
197         if (IS_BROADWELL(dev) || (INTEL_INFO(dev)->gen >= 9))
198                 gen8_irq_power_well_post_enable(dev_priv);
199 }
200
201 static void hsw_set_power_well(struct drm_i915_private *dev_priv,
202                                struct i915_power_well *power_well, bool enable)
203 {
204         bool is_enabled, enable_requested;
205         uint32_t tmp;
206
207         tmp = I915_READ(HSW_PWR_WELL_DRIVER);
208         is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
209         enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
210
211         if (enable) {
212                 if (!enable_requested)
213                         I915_WRITE(HSW_PWR_WELL_DRIVER,
214                                    HSW_PWR_WELL_ENABLE_REQUEST);
215
216                 if (!is_enabled) {
217                         DRM_DEBUG_KMS("Enabling power well\n");
218                         if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
219                                       HSW_PWR_WELL_STATE_ENABLED), 20))
220                                 DRM_ERROR("Timeout enabling power well\n");
221                         hsw_power_well_post_enable(dev_priv);
222                 }
223
224         } else {
225                 if (enable_requested) {
226                         I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
227                         POSTING_READ(HSW_PWR_WELL_DRIVER);
228                         DRM_DEBUG_KMS("Requesting to disable the power well\n");
229                 }
230         }
231 }
232
233 #define SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS (         \
234         BIT(POWER_DOMAIN_TRANSCODER_A) |                \
235         BIT(POWER_DOMAIN_PIPE_B) |                      \
236         BIT(POWER_DOMAIN_TRANSCODER_B) |                \
237         BIT(POWER_DOMAIN_PIPE_C) |                      \
238         BIT(POWER_DOMAIN_TRANSCODER_C) |                \
239         BIT(POWER_DOMAIN_PIPE_B_PANEL_FITTER) |         \
240         BIT(POWER_DOMAIN_PIPE_C_PANEL_FITTER) |         \
241         BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |          \
242         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |          \
243         BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |          \
244         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |          \
245         BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |          \
246         BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |          \
247         BIT(POWER_DOMAIN_AUX_B) |                       \
248         BIT(POWER_DOMAIN_AUX_C) |                       \
249         BIT(POWER_DOMAIN_AUX_D) |                       \
250         BIT(POWER_DOMAIN_AUDIO) |                       \
251         BIT(POWER_DOMAIN_VGA) |                         \
252         BIT(POWER_DOMAIN_INIT))
253 #define SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS (         \
254         SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS |         \
255         BIT(POWER_DOMAIN_PLLS) |                        \
256         BIT(POWER_DOMAIN_PIPE_A) |                      \
257         BIT(POWER_DOMAIN_TRANSCODER_EDP) |              \
258         BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER) |         \
259         BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) |          \
260         BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) |          \
261         BIT(POWER_DOMAIN_AUX_A) |                       \
262         BIT(POWER_DOMAIN_INIT))
263 #define SKL_DISPLAY_DDI_A_E_POWER_DOMAINS (             \
264         BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) |          \
265         BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) |          \
266         BIT(POWER_DOMAIN_INIT))
267 #define SKL_DISPLAY_DDI_B_POWER_DOMAINS (               \
268         BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |          \
269         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |          \
270         BIT(POWER_DOMAIN_INIT))
271 #define SKL_DISPLAY_DDI_C_POWER_DOMAINS (               \
272         BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |          \
273         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |          \
274         BIT(POWER_DOMAIN_INIT))
275 #define SKL_DISPLAY_DDI_D_POWER_DOMAINS (               \
276         BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |          \
277         BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |          \
278         BIT(POWER_DOMAIN_INIT))
279 #define SKL_DISPLAY_MISC_IO_POWER_DOMAINS (             \
280         SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS)
281 #define SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS (           \
282         (POWER_DOMAIN_MASK & ~(SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS |  \
283         SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS |         \
284         SKL_DISPLAY_DDI_A_E_POWER_DOMAINS |             \
285         SKL_DISPLAY_DDI_B_POWER_DOMAINS |               \
286         SKL_DISPLAY_DDI_C_POWER_DOMAINS |               \
287         SKL_DISPLAY_DDI_D_POWER_DOMAINS |               \
288         SKL_DISPLAY_MISC_IO_POWER_DOMAINS)) |           \
289         BIT(POWER_DOMAIN_INIT))
290
291 static void skl_set_power_well(struct drm_i915_private *dev_priv,
292                         struct i915_power_well *power_well, bool enable)
293 {
294         uint32_t tmp, fuse_status;
295         uint32_t req_mask, state_mask;
296         bool check_fuse_status = false;
297
298         tmp = I915_READ(HSW_PWR_WELL_DRIVER);
299         fuse_status = I915_READ(SKL_FUSE_STATUS);
300
301         switch (power_well->data) {
302         case SKL_DISP_PW_1:
303                 if (wait_for((I915_READ(SKL_FUSE_STATUS) &
304                         SKL_FUSE_PG0_DIST_STATUS), 1)) {
305                         DRM_ERROR("PG0 not enabled\n");
306                         return;
307                 }
308                 break;
309         case SKL_DISP_PW_2:
310                 if (!(fuse_status & SKL_FUSE_PG1_DIST_STATUS)) {
311                         DRM_ERROR("PG1 in disabled state\n");
312                         return;
313                 }
314                 break;
315         case SKL_DISP_PW_DDI_A_E:
316         case SKL_DISP_PW_DDI_B:
317         case SKL_DISP_PW_DDI_C:
318         case SKL_DISP_PW_DDI_D:
319         case SKL_DISP_PW_MISC_IO:
320                 break;
321         default:
322                 WARN(1, "Unknown power well %lu\n", power_well->data);
323                 return;
324         }
325
326         req_mask = SKL_POWER_WELL_REQ(power_well->data);
327         state_mask = SKL_POWER_WELL_STATE(power_well->data);
328
329         if (enable) {
330                 if (!(tmp & req_mask)) {
331                         I915_WRITE(HSW_PWR_WELL_DRIVER, tmp | req_mask);
332                         DRM_DEBUG_KMS("Enabling %s\n", power_well->name);
333                 }
334
335                 if (!(tmp & state_mask)) {
336                         if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
337                                 state_mask), 1))
338                                 DRM_ERROR("%s enable timeout\n",
339                                         power_well->name);
340                         check_fuse_status = true;
341                 }
342         } else {
343                 if (tmp & req_mask) {
344                         I915_WRITE(HSW_PWR_WELL_DRIVER, tmp & ~req_mask);
345                         POSTING_READ(HSW_PWR_WELL_DRIVER);
346                         DRM_DEBUG_KMS("Disabling %s\n", power_well->name);
347                 }
348         }
349
350         if (check_fuse_status) {
351                 if (power_well->data == SKL_DISP_PW_1) {
352                         if (wait_for((I915_READ(SKL_FUSE_STATUS) &
353                                 SKL_FUSE_PG1_DIST_STATUS), 1))
354                                 DRM_ERROR("PG1 distributing status timeout\n");
355                 } else if (power_well->data == SKL_DISP_PW_2) {
356                         if (wait_for((I915_READ(SKL_FUSE_STATUS) &
357                                 SKL_FUSE_PG2_DIST_STATUS), 1))
358                                 DRM_ERROR("PG2 distributing status timeout\n");
359                 }
360         }
361 }
362
363 static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
364                                    struct i915_power_well *power_well)
365 {
366         hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
367
368         /*
369          * We're taking over the BIOS, so clear any requests made by it since
370          * the driver is in charge now.
371          */
372         if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
373                 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
374 }
375
376 static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
377                                   struct i915_power_well *power_well)
378 {
379         hsw_set_power_well(dev_priv, power_well, true);
380 }
381
382 static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
383                                    struct i915_power_well *power_well)
384 {
385         hsw_set_power_well(dev_priv, power_well, false);
386 }
387
388 static bool skl_power_well_enabled(struct drm_i915_private *dev_priv,
389                                         struct i915_power_well *power_well)
390 {
391         uint32_t mask = SKL_POWER_WELL_REQ(power_well->data) |
392                 SKL_POWER_WELL_STATE(power_well->data);
393
394         return (I915_READ(HSW_PWR_WELL_DRIVER) & mask) == mask;
395 }
396
397 static void skl_power_well_sync_hw(struct drm_i915_private *dev_priv,
398                                 struct i915_power_well *power_well)
399 {
400         skl_set_power_well(dev_priv, power_well, power_well->count > 0);
401
402         /* Clear any request made by BIOS as driver is taking over */
403         I915_WRITE(HSW_PWR_WELL_BIOS, 0);
404 }
405
406 static void skl_power_well_enable(struct drm_i915_private *dev_priv,
407                                 struct i915_power_well *power_well)
408 {
409         skl_set_power_well(dev_priv, power_well, true);
410 }
411
412 static void skl_power_well_disable(struct drm_i915_private *dev_priv,
413                                 struct i915_power_well *power_well)
414 {
415         skl_set_power_well(dev_priv, power_well, false);
416 }
417
418 static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
419                                            struct i915_power_well *power_well)
420 {
421 }
422
423 static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
424                                              struct i915_power_well *power_well)
425 {
426         return true;
427 }
428
429 static void vlv_set_power_well(struct drm_i915_private *dev_priv,
430                                struct i915_power_well *power_well, bool enable)
431 {
432         enum punit_power_well power_well_id = power_well->data;
433         u32 mask;
434         u32 state;
435         u32 ctrl;
436
437         mask = PUNIT_PWRGT_MASK(power_well_id);
438         state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
439                          PUNIT_PWRGT_PWR_GATE(power_well_id);
440
441         mutex_lock(&dev_priv->rps.hw_lock);
442
443 #define COND \
444         ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
445
446         if (COND)
447                 goto out;
448
449         ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
450         ctrl &= ~mask;
451         ctrl |= state;
452         vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
453
454         if (wait_for(COND, 100))
455                 DRM_ERROR("timout setting power well state %08x (%08x)\n",
456                           state,
457                           vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
458
459 #undef COND
460
461 out:
462         mutex_unlock(&dev_priv->rps.hw_lock);
463 }
464
465 static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
466                                    struct i915_power_well *power_well)
467 {
468         vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
469 }
470
471 static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
472                                   struct i915_power_well *power_well)
473 {
474         vlv_set_power_well(dev_priv, power_well, true);
475 }
476
477 static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
478                                    struct i915_power_well *power_well)
479 {
480         vlv_set_power_well(dev_priv, power_well, false);
481 }
482
483 static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
484                                    struct i915_power_well *power_well)
485 {
486         int power_well_id = power_well->data;
487         bool enabled = false;
488         u32 mask;
489         u32 state;
490         u32 ctrl;
491
492         mask = PUNIT_PWRGT_MASK(power_well_id);
493         ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
494
495         mutex_lock(&dev_priv->rps.hw_lock);
496
497         state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
498         /*
499          * We only ever set the power-on and power-gate states, anything
500          * else is unexpected.
501          */
502         WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
503                 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
504         if (state == ctrl)
505                 enabled = true;
506
507         /*
508          * A transient state at this point would mean some unexpected party
509          * is poking at the power controls too.
510          */
511         ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
512         WARN_ON(ctrl != state);
513
514         mutex_unlock(&dev_priv->rps.hw_lock);
515
516         return enabled;
517 }
518
519 static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
520                                           struct i915_power_well *power_well)
521 {
522         WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
523
524         vlv_set_power_well(dev_priv, power_well, true);
525
526         spin_lock_irq(&dev_priv->irq_lock);
527         valleyview_enable_display_irqs(dev_priv);
528         spin_unlock_irq(&dev_priv->irq_lock);
529
530         /*
531          * During driver initialization/resume we can avoid restoring the
532          * part of the HW/SW state that will be inited anyway explicitly.
533          */
534         if (dev_priv->power_domains.initializing)
535                 return;
536
537         intel_hpd_init(dev_priv);
538
539         i915_redisable_vga_power_on(dev_priv->dev);
540 }
541
542 static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
543                                            struct i915_power_well *power_well)
544 {
545         WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
546
547         spin_lock_irq(&dev_priv->irq_lock);
548         valleyview_disable_display_irqs(dev_priv);
549         spin_unlock_irq(&dev_priv->irq_lock);
550
551         vlv_set_power_well(dev_priv, power_well, false);
552
553         vlv_power_sequencer_reset(dev_priv);
554 }
555
556 static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
557                                            struct i915_power_well *power_well)
558 {
559         WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
560
561         /*
562          * Enable the CRI clock source so we can get at the
563          * display and the reference clock for VGA
564          * hotplug / manual detection.
565          */
566         I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
567                    DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
568         udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
569
570         vlv_set_power_well(dev_priv, power_well, true);
571
572         /*
573          * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
574          *  6.  De-assert cmn_reset/side_reset. Same as VLV X0.
575          *   a. GUnit 0x2110 bit[0] set to 1 (def 0)
576          *   b. The other bits such as sfr settings / modesel may all
577          *      be set to 0.
578          *
579          * This should only be done on init and resume from S3 with
580          * both PLLs disabled, or we risk losing DPIO and PLL
581          * synchronization.
582          */
583         I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
584 }
585
586 static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
587                                             struct i915_power_well *power_well)
588 {
589         enum pipe pipe;
590
591         WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
592
593         for_each_pipe(dev_priv, pipe)
594                 assert_pll_disabled(dev_priv, pipe);
595
596         /* Assert common reset */
597         I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
598
599         vlv_set_power_well(dev_priv, power_well, false);
600 }
601
602 static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
603                                            struct i915_power_well *power_well)
604 {
605         enum dpio_phy phy;
606
607         WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
608                      power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
609
610         /*
611          * Enable the CRI clock source so we can get at the
612          * display and the reference clock for VGA
613          * hotplug / manual detection.
614          */
615         if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
616                 phy = DPIO_PHY0;
617                 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
618                            DPLL_REFA_CLK_ENABLE_VLV);
619                 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
620                            DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
621         } else {
622                 phy = DPIO_PHY1;
623                 I915_WRITE(DPLL(PIPE_C), I915_READ(DPLL(PIPE_C)) |
624                            DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
625         }
626         udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
627         vlv_set_power_well(dev_priv, power_well, true);
628
629         /* Poll for phypwrgood signal */
630         if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
631                 DRM_ERROR("Display PHY %d is not power up\n", phy);
632
633         I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) |
634                    PHY_COM_LANE_RESET_DEASSERT(phy));
635 }
636
637 static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
638                                             struct i915_power_well *power_well)
639 {
640         enum dpio_phy phy;
641
642         WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
643                      power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
644
645         if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
646                 phy = DPIO_PHY0;
647                 assert_pll_disabled(dev_priv, PIPE_A);
648                 assert_pll_disabled(dev_priv, PIPE_B);
649         } else {
650                 phy = DPIO_PHY1;
651                 assert_pll_disabled(dev_priv, PIPE_C);
652         }
653
654         I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) &
655                    ~PHY_COM_LANE_RESET_DEASSERT(phy));
656
657         vlv_set_power_well(dev_priv, power_well, false);
658 }
659
660 static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
661                                         struct i915_power_well *power_well)
662 {
663         enum pipe pipe = power_well->data;
664         bool enabled;
665         u32 state, ctrl;
666
667         mutex_lock(&dev_priv->rps.hw_lock);
668
669         state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
670         /*
671          * We only ever set the power-on and power-gate states, anything
672          * else is unexpected.
673          */
674         WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
675         enabled = state == DP_SSS_PWR_ON(pipe);
676
677         /*
678          * A transient state at this point would mean some unexpected party
679          * is poking at the power controls too.
680          */
681         ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
682         WARN_ON(ctrl << 16 != state);
683
684         mutex_unlock(&dev_priv->rps.hw_lock);
685
686         return enabled;
687 }
688
689 static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
690                                     struct i915_power_well *power_well,
691                                     bool enable)
692 {
693         enum pipe pipe = power_well->data;
694         u32 state;
695         u32 ctrl;
696
697         state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
698
699         mutex_lock(&dev_priv->rps.hw_lock);
700
701 #define COND \
702         ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
703
704         if (COND)
705                 goto out;
706
707         ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
708         ctrl &= ~DP_SSC_MASK(pipe);
709         ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
710         vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
711
712         if (wait_for(COND, 100))
713                 DRM_ERROR("timout setting power well state %08x (%08x)\n",
714                           state,
715                           vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
716
717 #undef COND
718
719 out:
720         mutex_unlock(&dev_priv->rps.hw_lock);
721 }
722
723 static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
724                                         struct i915_power_well *power_well)
725 {
726         chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
727 }
728
729 static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
730                                        struct i915_power_well *power_well)
731 {
732         WARN_ON_ONCE(power_well->data != PIPE_A &&
733                      power_well->data != PIPE_B &&
734                      power_well->data != PIPE_C);
735
736         chv_set_pipe_power_well(dev_priv, power_well, true);
737
738         if (power_well->data == PIPE_A) {
739                 spin_lock_irq(&dev_priv->irq_lock);
740                 valleyview_enable_display_irqs(dev_priv);
741                 spin_unlock_irq(&dev_priv->irq_lock);
742
743                 /*
744                  * During driver initialization/resume we can avoid restoring the
745                  * part of the HW/SW state that will be inited anyway explicitly.
746                  */
747                 if (dev_priv->power_domains.initializing)
748                         return;
749
750                 intel_hpd_init(dev_priv);
751
752                 i915_redisable_vga_power_on(dev_priv->dev);
753         }
754 }
755
756 static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
757                                         struct i915_power_well *power_well)
758 {
759         WARN_ON_ONCE(power_well->data != PIPE_A &&
760                      power_well->data != PIPE_B &&
761                      power_well->data != PIPE_C);
762
763         if (power_well->data == PIPE_A) {
764                 spin_lock_irq(&dev_priv->irq_lock);
765                 valleyview_disable_display_irqs(dev_priv);
766                 spin_unlock_irq(&dev_priv->irq_lock);
767         }
768
769         chv_set_pipe_power_well(dev_priv, power_well, false);
770
771         if (power_well->data == PIPE_A)
772                 vlv_power_sequencer_reset(dev_priv);
773 }
774
775 /**
776  * intel_display_power_get - grab a power domain reference
777  * @dev_priv: i915 device instance
778  * @domain: power domain to reference
779  *
780  * This function grabs a power domain reference for @domain and ensures that the
781  * power domain and all its parents are powered up. Therefore users should only
782  * grab a reference to the innermost power domain they need.
783  *
784  * Any power domain reference obtained by this function must have a symmetric
785  * call to intel_display_power_put() to release the reference again.
786  */
787 void intel_display_power_get(struct drm_i915_private *dev_priv,
788                              enum intel_display_power_domain domain)
789 {
790         struct i915_power_domains *power_domains;
791         struct i915_power_well *power_well;
792         int i;
793
794         intel_runtime_pm_get(dev_priv);
795
796         power_domains = &dev_priv->power_domains;
797
798         mutex_lock(&power_domains->lock);
799
800         for_each_power_well(i, power_well, BIT(domain), power_domains) {
801                 if (!power_well->count++) {
802                         DRM_DEBUG_KMS("enabling %s\n", power_well->name);
803                         power_well->ops->enable(dev_priv, power_well);
804                         power_well->hw_enabled = true;
805                 }
806         }
807
808         power_domains->domain_use_count[domain]++;
809
810         mutex_unlock(&power_domains->lock);
811 }
812
813 /**
814  * intel_display_power_put - release a power domain reference
815  * @dev_priv: i915 device instance
816  * @domain: power domain to reference
817  *
818  * This function drops the power domain reference obtained by
819  * intel_display_power_get() and might power down the corresponding hardware
820  * block right away if this is the last reference.
821  */
822 void intel_display_power_put(struct drm_i915_private *dev_priv,
823                              enum intel_display_power_domain domain)
824 {
825         struct i915_power_domains *power_domains;
826         struct i915_power_well *power_well;
827         int i;
828
829         power_domains = &dev_priv->power_domains;
830
831         mutex_lock(&power_domains->lock);
832
833         WARN_ON(!power_domains->domain_use_count[domain]);
834         power_domains->domain_use_count[domain]--;
835
836         for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
837                 WARN_ON(!power_well->count);
838
839                 if (!--power_well->count && i915.disable_power_well) {
840                         DRM_DEBUG_KMS("disabling %s\n", power_well->name);
841                         power_well->hw_enabled = false;
842                         power_well->ops->disable(dev_priv, power_well);
843                 }
844         }
845
846         mutex_unlock(&power_domains->lock);
847
848         intel_runtime_pm_put(dev_priv);
849 }
850
851 #define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
852
853 #define HSW_ALWAYS_ON_POWER_DOMAINS (                   \
854         BIT(POWER_DOMAIN_PIPE_A) |                      \
855         BIT(POWER_DOMAIN_TRANSCODER_EDP) |              \
856         BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) |          \
857         BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) |          \
858         BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |          \
859         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |          \
860         BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |          \
861         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |          \
862         BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |          \
863         BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |          \
864         BIT(POWER_DOMAIN_PORT_CRT) |                    \
865         BIT(POWER_DOMAIN_PLLS) |                        \
866         BIT(POWER_DOMAIN_AUX_A) |                       \
867         BIT(POWER_DOMAIN_AUX_B) |                       \
868         BIT(POWER_DOMAIN_AUX_C) |                       \
869         BIT(POWER_DOMAIN_AUX_D) |                       \
870         BIT(POWER_DOMAIN_INIT))
871 #define HSW_DISPLAY_POWER_DOMAINS (                             \
872         (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) |    \
873         BIT(POWER_DOMAIN_INIT))
874
875 #define BDW_ALWAYS_ON_POWER_DOMAINS (                   \
876         HSW_ALWAYS_ON_POWER_DOMAINS |                   \
877         BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
878 #define BDW_DISPLAY_POWER_DOMAINS (                             \
879         (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) |    \
880         BIT(POWER_DOMAIN_INIT))
881
882 #define VLV_ALWAYS_ON_POWER_DOMAINS     BIT(POWER_DOMAIN_INIT)
883 #define VLV_DISPLAY_POWER_DOMAINS       POWER_DOMAIN_MASK
884
885 #define VLV_DPIO_CMN_BC_POWER_DOMAINS (         \
886         BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |  \
887         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |  \
888         BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |  \
889         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |  \
890         BIT(POWER_DOMAIN_PORT_CRT) |            \
891         BIT(POWER_DOMAIN_AUX_B) |               \
892         BIT(POWER_DOMAIN_AUX_C) |               \
893         BIT(POWER_DOMAIN_INIT))
894
895 #define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS (  \
896         BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |  \
897         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |  \
898         BIT(POWER_DOMAIN_AUX_B) |               \
899         BIT(POWER_DOMAIN_INIT))
900
901 #define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS (  \
902         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |  \
903         BIT(POWER_DOMAIN_AUX_B) |               \
904         BIT(POWER_DOMAIN_INIT))
905
906 #define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS (  \
907         BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |  \
908         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |  \
909         BIT(POWER_DOMAIN_AUX_C) |               \
910         BIT(POWER_DOMAIN_INIT))
911
912 #define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS (  \
913         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |  \
914         BIT(POWER_DOMAIN_AUX_C) |               \
915         BIT(POWER_DOMAIN_INIT))
916
917 #define CHV_PIPE_A_POWER_DOMAINS (      \
918         BIT(POWER_DOMAIN_PIPE_A) |      \
919         BIT(POWER_DOMAIN_INIT))
920
921 #define CHV_PIPE_B_POWER_DOMAINS (      \
922         BIT(POWER_DOMAIN_PIPE_B) |      \
923         BIT(POWER_DOMAIN_INIT))
924
925 #define CHV_PIPE_C_POWER_DOMAINS (      \
926         BIT(POWER_DOMAIN_PIPE_C) |      \
927         BIT(POWER_DOMAIN_INIT))
928
929 #define CHV_DPIO_CMN_BC_POWER_DOMAINS (         \
930         BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |  \
931         BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |  \
932         BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |  \
933         BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |  \
934         BIT(POWER_DOMAIN_AUX_B) |               \
935         BIT(POWER_DOMAIN_AUX_C) |               \
936         BIT(POWER_DOMAIN_INIT))
937
938 #define CHV_DPIO_CMN_D_POWER_DOMAINS (          \
939         BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |  \
940         BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |  \
941         BIT(POWER_DOMAIN_AUX_D) |               \
942         BIT(POWER_DOMAIN_INIT))
943
944 #define CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS (  \
945         BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |  \
946         BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |  \
947         BIT(POWER_DOMAIN_AUX_D) |               \
948         BIT(POWER_DOMAIN_INIT))
949
950 #define CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS (  \
951         BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |  \
952         BIT(POWER_DOMAIN_AUX_D) |               \
953         BIT(POWER_DOMAIN_INIT))
954
955 static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
956         .sync_hw = i9xx_always_on_power_well_noop,
957         .enable = i9xx_always_on_power_well_noop,
958         .disable = i9xx_always_on_power_well_noop,
959         .is_enabled = i9xx_always_on_power_well_enabled,
960 };
961
962 static const struct i915_power_well_ops chv_pipe_power_well_ops = {
963         .sync_hw = chv_pipe_power_well_sync_hw,
964         .enable = chv_pipe_power_well_enable,
965         .disable = chv_pipe_power_well_disable,
966         .is_enabled = chv_pipe_power_well_enabled,
967 };
968
969 static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
970         .sync_hw = vlv_power_well_sync_hw,
971         .enable = chv_dpio_cmn_power_well_enable,
972         .disable = chv_dpio_cmn_power_well_disable,
973         .is_enabled = vlv_power_well_enabled,
974 };
975
976 static struct i915_power_well i9xx_always_on_power_well[] = {
977         {
978                 .name = "always-on",
979                 .always_on = 1,
980                 .domains = POWER_DOMAIN_MASK,
981                 .ops = &i9xx_always_on_power_well_ops,
982         },
983 };
984
985 static const struct i915_power_well_ops hsw_power_well_ops = {
986         .sync_hw = hsw_power_well_sync_hw,
987         .enable = hsw_power_well_enable,
988         .disable = hsw_power_well_disable,
989         .is_enabled = hsw_power_well_enabled,
990 };
991
992 static const struct i915_power_well_ops skl_power_well_ops = {
993         .sync_hw = skl_power_well_sync_hw,
994         .enable = skl_power_well_enable,
995         .disable = skl_power_well_disable,
996         .is_enabled = skl_power_well_enabled,
997 };
998
999 static struct i915_power_well hsw_power_wells[] = {
1000         {
1001                 .name = "always-on",
1002                 .always_on = 1,
1003                 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
1004                 .ops = &i9xx_always_on_power_well_ops,
1005         },
1006         {
1007                 .name = "display",
1008                 .domains = HSW_DISPLAY_POWER_DOMAINS,
1009                 .ops = &hsw_power_well_ops,
1010         },
1011 };
1012
1013 static struct i915_power_well bdw_power_wells[] = {
1014         {
1015                 .name = "always-on",
1016                 .always_on = 1,
1017                 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
1018                 .ops = &i9xx_always_on_power_well_ops,
1019         },
1020         {
1021                 .name = "display",
1022                 .domains = BDW_DISPLAY_POWER_DOMAINS,
1023                 .ops = &hsw_power_well_ops,
1024         },
1025 };
1026
1027 static const struct i915_power_well_ops vlv_display_power_well_ops = {
1028         .sync_hw = vlv_power_well_sync_hw,
1029         .enable = vlv_display_power_well_enable,
1030         .disable = vlv_display_power_well_disable,
1031         .is_enabled = vlv_power_well_enabled,
1032 };
1033
1034 static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
1035         .sync_hw = vlv_power_well_sync_hw,
1036         .enable = vlv_dpio_cmn_power_well_enable,
1037         .disable = vlv_dpio_cmn_power_well_disable,
1038         .is_enabled = vlv_power_well_enabled,
1039 };
1040
1041 static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
1042         .sync_hw = vlv_power_well_sync_hw,
1043         .enable = vlv_power_well_enable,
1044         .disable = vlv_power_well_disable,
1045         .is_enabled = vlv_power_well_enabled,
1046 };
1047
1048 static struct i915_power_well vlv_power_wells[] = {
1049         {
1050                 .name = "always-on",
1051                 .always_on = 1,
1052                 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1053                 .ops = &i9xx_always_on_power_well_ops,
1054         },
1055         {
1056                 .name = "display",
1057                 .domains = VLV_DISPLAY_POWER_DOMAINS,
1058                 .data = PUNIT_POWER_WELL_DISP2D,
1059                 .ops = &vlv_display_power_well_ops,
1060         },
1061         {
1062                 .name = "dpio-tx-b-01",
1063                 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1064                            VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1065                            VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1066                            VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1067                 .ops = &vlv_dpio_power_well_ops,
1068                 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1069         },
1070         {
1071                 .name = "dpio-tx-b-23",
1072                 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1073                            VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1074                            VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1075                            VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1076                 .ops = &vlv_dpio_power_well_ops,
1077                 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1078         },
1079         {
1080                 .name = "dpio-tx-c-01",
1081                 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1082                            VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1083                            VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1084                            VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1085                 .ops = &vlv_dpio_power_well_ops,
1086                 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1087         },
1088         {
1089                 .name = "dpio-tx-c-23",
1090                 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1091                            VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
1092                            VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1093                            VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1094                 .ops = &vlv_dpio_power_well_ops,
1095                 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1096         },
1097         {
1098                 .name = "dpio-common",
1099                 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
1100                 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1101                 .ops = &vlv_dpio_cmn_power_well_ops,
1102         },
1103 };
1104
1105 static struct i915_power_well chv_power_wells[] = {
1106         {
1107                 .name = "always-on",
1108                 .always_on = 1,
1109                 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
1110                 .ops = &i9xx_always_on_power_well_ops,
1111         },
1112 #if 0
1113         {
1114                 .name = "display",
1115                 .domains = VLV_DISPLAY_POWER_DOMAINS,
1116                 .data = PUNIT_POWER_WELL_DISP2D,
1117                 .ops = &vlv_display_power_well_ops,
1118         },
1119 #endif
1120         {
1121                 .name = "pipe-a",
1122                 /*
1123                  * FIXME: pipe A power well seems to be the new disp2d well.
1124                  * At least all registers seem to be housed there. Figure
1125                  * out if this a a temporary situation in pre-production
1126                  * hardware or a permanent state of affairs.
1127                  */
1128                 .domains = CHV_PIPE_A_POWER_DOMAINS | VLV_DISPLAY_POWER_DOMAINS,
1129                 .data = PIPE_A,
1130                 .ops = &chv_pipe_power_well_ops,
1131         },
1132 #if 0
1133         {
1134                 .name = "pipe-b",
1135                 .domains = CHV_PIPE_B_POWER_DOMAINS,
1136                 .data = PIPE_B,
1137                 .ops = &chv_pipe_power_well_ops,
1138         },
1139         {
1140                 .name = "pipe-c",
1141                 .domains = CHV_PIPE_C_POWER_DOMAINS,
1142                 .data = PIPE_C,
1143                 .ops = &chv_pipe_power_well_ops,
1144         },
1145 #endif
1146         {
1147                 .name = "dpio-common-bc",
1148                 /*
1149                  * XXX: cmnreset for one PHY seems to disturb the other.
1150                  * As a workaround keep both powered on at the same
1151                  * time for now.
1152                  */
1153                 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
1154                 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
1155                 .ops = &chv_dpio_cmn_power_well_ops,
1156         },
1157         {
1158                 .name = "dpio-common-d",
1159                 /*
1160                  * XXX: cmnreset for one PHY seems to disturb the other.
1161                  * As a workaround keep both powered on at the same
1162                  * time for now.
1163                  */
1164                 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
1165                 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
1166                 .ops = &chv_dpio_cmn_power_well_ops,
1167         },
1168 #if 0
1169         {
1170                 .name = "dpio-tx-b-01",
1171                 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1172                            VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
1173                 .ops = &vlv_dpio_power_well_ops,
1174                 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
1175         },
1176         {
1177                 .name = "dpio-tx-b-23",
1178                 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
1179                            VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
1180                 .ops = &vlv_dpio_power_well_ops,
1181                 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
1182         },
1183         {
1184                 .name = "dpio-tx-c-01",
1185                 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1186                            VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1187                 .ops = &vlv_dpio_power_well_ops,
1188                 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
1189         },
1190         {
1191                 .name = "dpio-tx-c-23",
1192                 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
1193                            VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
1194                 .ops = &vlv_dpio_power_well_ops,
1195                 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
1196         },
1197         {
1198                 .name = "dpio-tx-d-01",
1199                 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
1200                            CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
1201                 .ops = &vlv_dpio_power_well_ops,
1202                 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_01,
1203         },
1204         {
1205                 .name = "dpio-tx-d-23",
1206                 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
1207                            CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
1208                 .ops = &vlv_dpio_power_well_ops,
1209                 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_23,
1210         },
1211 #endif
1212 };
1213
1214 static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
1215                                                  enum punit_power_well power_well_id)
1216 {
1217         struct i915_power_domains *power_domains = &dev_priv->power_domains;
1218         struct i915_power_well *power_well;
1219         int i;
1220
1221         for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
1222                 if (power_well->data == power_well_id)
1223                         return power_well;
1224         }
1225
1226         return NULL;
1227 }
1228
1229 static struct i915_power_well skl_power_wells[] = {
1230         {
1231                 .name = "always-on",
1232                 .always_on = 1,
1233                 .domains = SKL_DISPLAY_ALWAYS_ON_POWER_DOMAINS,
1234                 .ops = &i9xx_always_on_power_well_ops,
1235         },
1236         {
1237                 .name = "power well 1",
1238                 .domains = SKL_DISPLAY_POWERWELL_1_POWER_DOMAINS,
1239                 .ops = &skl_power_well_ops,
1240                 .data = SKL_DISP_PW_1,
1241         },
1242         {
1243                 .name = "MISC IO power well",
1244                 .domains = SKL_DISPLAY_MISC_IO_POWER_DOMAINS,
1245                 .ops = &skl_power_well_ops,
1246                 .data = SKL_DISP_PW_MISC_IO,
1247         },
1248         {
1249                 .name = "power well 2",
1250                 .domains = SKL_DISPLAY_POWERWELL_2_POWER_DOMAINS,
1251                 .ops = &skl_power_well_ops,
1252                 .data = SKL_DISP_PW_2,
1253         },
1254         {
1255                 .name = "DDI A/E power well",
1256                 .domains = SKL_DISPLAY_DDI_A_E_POWER_DOMAINS,
1257                 .ops = &skl_power_well_ops,
1258                 .data = SKL_DISP_PW_DDI_A_E,
1259         },
1260         {
1261                 .name = "DDI B power well",
1262                 .domains = SKL_DISPLAY_DDI_B_POWER_DOMAINS,
1263                 .ops = &skl_power_well_ops,
1264                 .data = SKL_DISP_PW_DDI_B,
1265         },
1266         {
1267                 .name = "DDI C power well",
1268                 .domains = SKL_DISPLAY_DDI_C_POWER_DOMAINS,
1269                 .ops = &skl_power_well_ops,
1270                 .data = SKL_DISP_PW_DDI_C,
1271         },
1272         {
1273                 .name = "DDI D power well",
1274                 .domains = SKL_DISPLAY_DDI_D_POWER_DOMAINS,
1275                 .ops = &skl_power_well_ops,
1276                 .data = SKL_DISP_PW_DDI_D,
1277         },
1278 };
1279
1280 #define set_power_wells(power_domains, __power_wells) ({                \
1281         (power_domains)->power_wells = (__power_wells);                 \
1282         (power_domains)->power_well_count = ARRAY_SIZE(__power_wells);  \
1283 })
1284
1285 /**
1286  * intel_power_domains_init - initializes the power domain structures
1287  * @dev_priv: i915 device instance
1288  *
1289  * Initializes the power domain structures for @dev_priv depending upon the
1290  * supported platform.
1291  */
1292 int intel_power_domains_init(struct drm_i915_private *dev_priv)
1293 {
1294         struct i915_power_domains *power_domains = &dev_priv->power_domains;
1295
1296         mutex_init(&power_domains->lock);
1297
1298         /*
1299          * The enabling order will be from lower to higher indexed wells,
1300          * the disabling order is reversed.
1301          */
1302         if (IS_HASWELL(dev_priv->dev)) {
1303                 set_power_wells(power_domains, hsw_power_wells);
1304         } else if (IS_BROADWELL(dev_priv->dev)) {
1305                 set_power_wells(power_domains, bdw_power_wells);
1306         } else if (IS_SKYLAKE(dev_priv->dev)) {
1307                 set_power_wells(power_domains, skl_power_wells);
1308         } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1309                 set_power_wells(power_domains, chv_power_wells);
1310         } else if (IS_VALLEYVIEW(dev_priv->dev)) {
1311                 set_power_wells(power_domains, vlv_power_wells);
1312         } else {
1313                 set_power_wells(power_domains, i9xx_always_on_power_well);
1314         }
1315
1316         return 0;
1317 }
1318
1319 static void intel_runtime_pm_disable(struct drm_i915_private *dev_priv)
1320 {
1321         struct drm_device *dev = dev_priv->dev;
1322         struct device *device = &dev->pdev->dev;
1323
1324         if (!HAS_RUNTIME_PM(dev))
1325                 return;
1326
1327         if (!intel_enable_rc6(dev))
1328                 return;
1329
1330         /* Make sure we're not suspended first. */
1331         pm_runtime_get_sync(device);
1332         pm_runtime_disable(device);
1333 }
1334
1335 /**
1336  * intel_power_domains_fini - finalizes the power domain structures
1337  * @dev_priv: i915 device instance
1338  *
1339  * Finalizes the power domain structures for @dev_priv depending upon the
1340  * supported platform. This function also disables runtime pm and ensures that
1341  * the device stays powered up so that the driver can be reloaded.
1342  */
1343 void intel_power_domains_fini(struct drm_i915_private *dev_priv)
1344 {
1345         intel_runtime_pm_disable(dev_priv);
1346
1347         /* The i915.ko module is still not prepared to be loaded when
1348          * the power well is not enabled, so just enable it in case
1349          * we're going to unload/reload. */
1350         intel_display_set_init_power(dev_priv, true);
1351 }
1352
1353 static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
1354 {
1355         struct i915_power_domains *power_domains = &dev_priv->power_domains;
1356         struct i915_power_well *power_well;
1357         int i;
1358
1359         mutex_lock(&power_domains->lock);
1360         for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
1361                 power_well->ops->sync_hw(dev_priv, power_well);
1362                 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
1363                                                                      power_well);
1364         }
1365         mutex_unlock(&power_domains->lock);
1366 }
1367
1368 static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
1369 {
1370         struct i915_power_well *cmn =
1371                 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
1372         struct i915_power_well *disp2d =
1373                 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
1374
1375         /* If the display might be already active skip this */
1376         if (cmn->ops->is_enabled(dev_priv, cmn) &&
1377             disp2d->ops->is_enabled(dev_priv, disp2d) &&
1378             I915_READ(DPIO_CTL) & DPIO_CMNRST)
1379                 return;
1380
1381         DRM_DEBUG_KMS("toggling display PHY side reset\n");
1382
1383         /* cmnlane needs DPLL registers */
1384         disp2d->ops->enable(dev_priv, disp2d);
1385
1386         /*
1387          * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1388          * Need to assert and de-assert PHY SB reset by gating the
1389          * common lane power, then un-gating it.
1390          * Simply ungating isn't enough to reset the PHY enough to get
1391          * ports and lanes running.
1392          */
1393         cmn->ops->disable(dev_priv, cmn);
1394 }
1395
1396 /**
1397  * intel_power_domains_init_hw - initialize hardware power domain state
1398  * @dev_priv: i915 device instance
1399  *
1400  * This function initializes the hardware power domain state and enables all
1401  * power domains using intel_display_set_init_power().
1402  */
1403 void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
1404 {
1405         struct drm_device *dev = dev_priv->dev;
1406         struct i915_power_domains *power_domains = &dev_priv->power_domains;
1407
1408         power_domains->initializing = true;
1409
1410         if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1411                 mutex_lock(&power_domains->lock);
1412                 vlv_cmnlane_wa(dev_priv);
1413                 mutex_unlock(&power_domains->lock);
1414         }
1415
1416         /* For now, we need the power well to be always enabled. */
1417         intel_display_set_init_power(dev_priv, true);
1418         intel_power_domains_resume(dev_priv);
1419         power_domains->initializing = false;
1420 }
1421
1422 /**
1423  * intel_aux_display_runtime_get - grab an auxilliary power domain reference
1424  * @dev_priv: i915 device instance
1425  *
1426  * This function grabs a power domain reference for the auxiliary power domain
1427  * (for access to the GMBUS and DP AUX blocks) and ensures that it and all its
1428  * parents are powered up. Therefore users should only grab a reference to the
1429  * innermost power domain they need.
1430  *
1431  * Any power domain reference obtained by this function must have a symmetric
1432  * call to intel_aux_display_runtime_put() to release the reference again.
1433  */
1434 void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
1435 {
1436         intel_runtime_pm_get(dev_priv);
1437 }
1438
1439 /**
1440  * intel_aux_display_runtime_put - release an auxilliary power domain reference
1441  * @dev_priv: i915 device instance
1442  *
1443  * This function drops the auxilliary power domain reference obtained by
1444  * intel_aux_display_runtime_get() and might power down the corresponding
1445  * hardware block right away if this is the last reference.
1446  */
1447 void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
1448 {
1449         intel_runtime_pm_put(dev_priv);
1450 }
1451
1452 /**
1453  * intel_runtime_pm_get - grab a runtime pm reference
1454  * @dev_priv: i915 device instance
1455  *
1456  * This function grabs a device-level runtime pm reference (mostly used for GEM
1457  * code to ensure the GTT or GT is on) and ensures that it is powered up.
1458  *
1459  * Any runtime pm reference obtained by this function must have a symmetric
1460  * call to intel_runtime_pm_put() to release the reference again.
1461  */
1462 void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
1463 {
1464         struct drm_device *dev = dev_priv->dev;
1465         struct device *device = &dev->pdev->dev;
1466
1467         if (!HAS_RUNTIME_PM(dev))
1468                 return;
1469
1470         pm_runtime_get_sync(device);
1471         WARN(dev_priv->pm.suspended, "Device still suspended.\n");
1472 }
1473
1474 /**
1475  * intel_runtime_pm_get_noresume - grab a runtime pm reference
1476  * @dev_priv: i915 device instance
1477  *
1478  * This function grabs a device-level runtime pm reference (mostly used for GEM
1479  * code to ensure the GTT or GT is on).
1480  *
1481  * It will _not_ power up the device but instead only check that it's powered
1482  * on.  Therefore it is only valid to call this functions from contexts where
1483  * the device is known to be powered up and where trying to power it up would
1484  * result in hilarity and deadlocks. That pretty much means only the system
1485  * suspend/resume code where this is used to grab runtime pm references for
1486  * delayed setup down in work items.
1487  *
1488  * Any runtime pm reference obtained by this function must have a symmetric
1489  * call to intel_runtime_pm_put() to release the reference again.
1490  */
1491 void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
1492 {
1493         struct drm_device *dev = dev_priv->dev;
1494         struct device *device = &dev->pdev->dev;
1495
1496         if (!HAS_RUNTIME_PM(dev))
1497                 return;
1498
1499         WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
1500         pm_runtime_get_noresume(device);
1501 }
1502
1503 /**
1504  * intel_runtime_pm_put - release a runtime pm reference
1505  * @dev_priv: i915 device instance
1506  *
1507  * This function drops the device-level runtime pm reference obtained by
1508  * intel_runtime_pm_get() and might power down the corresponding
1509  * hardware block right away if this is the last reference.
1510  */
1511 void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
1512 {
1513         struct drm_device *dev = dev_priv->dev;
1514         struct device *device = &dev->pdev->dev;
1515
1516         if (!HAS_RUNTIME_PM(dev))
1517                 return;
1518
1519         pm_runtime_mark_last_busy(device);
1520         pm_runtime_put_autosuspend(device);
1521 }
1522
1523 /**
1524  * intel_runtime_pm_enable - enable runtime pm
1525  * @dev_priv: i915 device instance
1526  *
1527  * This function enables runtime pm at the end of the driver load sequence.
1528  *
1529  * Note that this function does currently not enable runtime pm for the
1530  * subordinate display power domains. That is only done on the first modeset
1531  * using intel_display_set_init_power().
1532  */
1533 void intel_runtime_pm_enable(struct drm_i915_private *dev_priv)
1534 {
1535         struct drm_device *dev = dev_priv->dev;
1536         struct device *device = &dev->pdev->dev;
1537
1538         if (!HAS_RUNTIME_PM(dev))
1539                 return;
1540
1541         pm_runtime_set_active(device);
1542
1543         /*
1544          * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
1545          * requirement.
1546          */
1547         if (!intel_enable_rc6(dev)) {
1548                 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
1549                 return;
1550         }
1551
1552         pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
1553         pm_runtime_mark_last_busy(device);
1554         pm_runtime_use_autosuspend(device);
1555
1556         pm_runtime_put_autosuspend(device);
1557 }
1558