xref: /openbmc/linux/drivers/gpu/drm/gma500/power.c (revision 74f481f1)
1bbbb262dSAlan Cox /**************************************************************************
2bbbb262dSAlan Cox  * Copyright (c) 2009-2011, Intel Corporation.
3bbbb262dSAlan Cox  * All Rights Reserved.
4bbbb262dSAlan Cox  *
5bbbb262dSAlan Cox  * Permission is hereby granted, free of charge, to any person obtaining a
6bbbb262dSAlan Cox  * copy of this software and associated documentation files (the "Software"),
7bbbb262dSAlan Cox  * to deal in the Software without restriction, including without limitation
8bbbb262dSAlan Cox  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bbbb262dSAlan Cox  * and/or sell copies of the Software, and to permit persons to whom the
10bbbb262dSAlan Cox  * Software is furnished to do so, subject to the following conditions:
11bbbb262dSAlan Cox  *
12bbbb262dSAlan Cox  * The above copyright notice and this permission notice (including the next
13bbbb262dSAlan Cox  * paragraph) shall be included in all copies or substantial portions of the
14bbbb262dSAlan Cox  * Software.
15bbbb262dSAlan Cox  *
16bbbb262dSAlan Cox  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bbbb262dSAlan Cox  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bbbb262dSAlan Cox  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bbbb262dSAlan Cox  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bbbb262dSAlan Cox  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bbbb262dSAlan Cox  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bbbb262dSAlan Cox  * SOFTWARE.
23bbbb262dSAlan Cox  *
24bbbb262dSAlan Cox  * Authors:
25bbbb262dSAlan Cox  *    Benjamin Defnet <benjamin.r.defnet@intel.com>
26bbbb262dSAlan Cox  *    Rajesh Poornachandran <rajesh.poornachandran@intel.com>
27bbbb262dSAlan Cox  * Massively reworked
28bbbb262dSAlan Cox  *    Alan Cox <alan@linux.intel.com>
29bbbb262dSAlan Cox  */
30bbbb262dSAlan Cox 
31d339386cSThomas Zimmermann #include "gem.h"
32bbbb262dSAlan Cox #include "power.h"
33bbbb262dSAlan Cox #include "psb_drv.h"
34bbbb262dSAlan Cox #include "psb_reg.h"
35bbbb262dSAlan Cox #include "psb_intel_reg.h"
3622908507SThomas Zimmermann #include "psb_irq.h"
37bbbb262dSAlan Cox #include <linux/mutex.h>
38bbbb262dSAlan Cox #include <linux/pm_runtime.h>
39bbbb262dSAlan Cox 
40bbbb262dSAlan Cox /**
41bbbb262dSAlan Cox  *	gma_power_init		-	initialise power manager
42bbbb262dSAlan Cox  *	@dev: our device
43bbbb262dSAlan Cox  *
44bbbb262dSAlan Cox  *	Set up for power management tracking of our hardware.
45bbbb262dSAlan Cox  */
gma_power_init(struct drm_device * dev)46bbbb262dSAlan Cox void gma_power_init(struct drm_device *dev)
47bbbb262dSAlan Cox {
48f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
49bbbb262dSAlan Cox 
50bbbb262dSAlan Cox 	/* FIXME: Move APM/OSPM base into relevant device code */
51bbbb262dSAlan Cox 	dev_priv->apm_base = dev_priv->apm_reg & 0xffff;
52bbbb262dSAlan Cox 	dev_priv->ospm_base &= 0xffff;
53bbbb262dSAlan Cox 
54c715bc1bSKirill A. Shutemov 	if (dev_priv->ops->init_pm)
55bbbb262dSAlan Cox 		dev_priv->ops->init_pm(dev);
56*672c4735SHans de Goede 
57*672c4735SHans de Goede 	/*
58*672c4735SHans de Goede 	 * Runtime pm support is broken atm. So for now unconditionally
59*672c4735SHans de Goede 	 * call pm_runtime_get() here and put it again in psb_driver_unload()
60*672c4735SHans de Goede 	 *
61*672c4735SHans de Goede 	 * To fix this we need to call pm_runtime_get() once for each active
62*672c4735SHans de Goede 	 * pipe at boot and then put() / get() for each pipe disable / enable
63*672c4735SHans de Goede 	 * so that the device gets runtime suspended when no pipes are active.
64*672c4735SHans de Goede 	 * Once this is in place the pm_runtime_get() below should be replaced
65*672c4735SHans de Goede 	 * by a pm_runtime_allow() call to undo the pm_runtime_forbid() from
66*672c4735SHans de Goede 	 * pci_pm_init().
67*672c4735SHans de Goede 	 */
68*672c4735SHans de Goede 	pm_runtime_get(dev->dev);
69*672c4735SHans de Goede 
70*672c4735SHans de Goede 	dev_priv->pm_initialized = true;
71bbbb262dSAlan Cox }
72bbbb262dSAlan Cox 
73bbbb262dSAlan Cox /**
74bbbb262dSAlan Cox  *	gma_power_uninit	-	end power manager
75bbbb262dSAlan Cox  *	@dev: device to end for
76bbbb262dSAlan Cox  *
77bbbb262dSAlan Cox  *	Undo the effects of gma_power_init
78bbbb262dSAlan Cox  */
gma_power_uninit(struct drm_device * dev)79bbbb262dSAlan Cox void gma_power_uninit(struct drm_device *dev)
80bbbb262dSAlan Cox {
81*672c4735SHans de Goede 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
82*672c4735SHans de Goede 
83*672c4735SHans de Goede 	if (!dev_priv->pm_initialized)
84*672c4735SHans de Goede 		return;
85*672c4735SHans de Goede 
86*672c4735SHans de Goede 	pm_runtime_put_noidle(dev->dev);
87bbbb262dSAlan Cox }
88bbbb262dSAlan Cox 
89bbbb262dSAlan Cox /**
90bbbb262dSAlan Cox  *	gma_suspend_display	-	suspend the display logic
91bbbb262dSAlan Cox  *	@dev: our DRM device
92bbbb262dSAlan Cox  *
93bbbb262dSAlan Cox  *	Suspend the display logic of the graphics interface
94bbbb262dSAlan Cox  */
gma_suspend_display(struct drm_device * dev)95bbbb262dSAlan Cox static void gma_suspend_display(struct drm_device *dev)
96bbbb262dSAlan Cox {
97f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
98bbbb262dSAlan Cox 
99bbbb262dSAlan Cox 	dev_priv->ops->save_regs(dev);
100bbbb262dSAlan Cox 	dev_priv->ops->power_down(dev);
101bbbb262dSAlan Cox }
102bbbb262dSAlan Cox 
103bbbb262dSAlan Cox /**
104bbbb262dSAlan Cox  *	gma_resume_display	-	resume display side logic
10563ad2a97SLee Jones  *	@pdev: PCI device
106bbbb262dSAlan Cox  *
107bbbb262dSAlan Cox  *	Resume the display hardware restoring state and enabling
108bbbb262dSAlan Cox  *	as necessary.
109bbbb262dSAlan Cox  */
gma_resume_display(struct pci_dev * pdev)110bbbb262dSAlan Cox static void gma_resume_display(struct pci_dev *pdev)
111bbbb262dSAlan Cox {
112bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
113f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
114bbbb262dSAlan Cox 
115bbbb262dSAlan Cox 	/* turn on the display power island */
116bbbb262dSAlan Cox 	dev_priv->ops->power_up(dev);
117bbbb262dSAlan Cox 
118bbbb262dSAlan Cox 	PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
119bbbb262dSAlan Cox 	pci_write_config_word(pdev, PSB_GMCH_CTRL,
120bbbb262dSAlan Cox 			dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
1218ba8209fSPatrik Jakobsson 
12242ceddb6SThomas Zimmermann 	/* Rebuild our GTT mappings */
12342ceddb6SThomas Zimmermann 	psb_gtt_resume(dev);
12442ceddb6SThomas Zimmermann 	psb_gem_mm_resume(dev);
125bbbb262dSAlan Cox 	dev_priv->ops->restore_regs(dev);
126bbbb262dSAlan Cox }
127bbbb262dSAlan Cox 
128bbbb262dSAlan Cox /**
129bbbb262dSAlan Cox  *	gma_suspend_pci		-	suspend PCI side
130bbbb262dSAlan Cox  *	@pdev: PCI device
131bbbb262dSAlan Cox  *
132bbbb262dSAlan Cox  *	Perform the suspend processing on our PCI device state
133bbbb262dSAlan Cox  */
gma_suspend_pci(struct pci_dev * pdev)134bbbb262dSAlan Cox static void gma_suspend_pci(struct pci_dev *pdev)
135bbbb262dSAlan Cox {
136bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
137f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
138bbbb262dSAlan Cox 	int bsm, vbt;
139bbbb262dSAlan Cox 
140bbbb262dSAlan Cox 	pci_save_state(pdev);
141bbbb262dSAlan Cox 	pci_read_config_dword(pdev, 0x5C, &bsm);
142648a8e34SAlan Cox 	dev_priv->regs.saveBSM = bsm;
143bbbb262dSAlan Cox 	pci_read_config_dword(pdev, 0xFC, &vbt);
144648a8e34SAlan Cox 	dev_priv->regs.saveVBT = vbt;
145bbbb262dSAlan Cox 
146bbbb262dSAlan Cox 	pci_disable_device(pdev);
147bbbb262dSAlan Cox 	pci_set_power_state(pdev, PCI_D3hot);
148bbbb262dSAlan Cox }
149bbbb262dSAlan Cox 
150bbbb262dSAlan Cox /**
151bbbb262dSAlan Cox  *	gma_resume_pci		-	resume helper
15263ad2a97SLee Jones  *	@pdev: our PCI device
153bbbb262dSAlan Cox  *
154bbbb262dSAlan Cox  *	Perform the resume processing on our PCI device state - rewrite
155bbbb262dSAlan Cox  *	register state and re-enable the PCI device
156bbbb262dSAlan Cox  */
gma_resume_pci(struct pci_dev * pdev)157*672c4735SHans de Goede static int gma_resume_pci(struct pci_dev *pdev)
158bbbb262dSAlan Cox {
159bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
160f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
161bbbb262dSAlan Cox 
162bbbb262dSAlan Cox 	pci_set_power_state(pdev, PCI_D0);
163bbbb262dSAlan Cox 	pci_restore_state(pdev);
164648a8e34SAlan Cox 	pci_write_config_dword(pdev, 0x5c, dev_priv->regs.saveBSM);
165648a8e34SAlan Cox 	pci_write_config_dword(pdev, 0xFC, dev_priv->regs.saveVBT);
166bbbb262dSAlan Cox 
167*672c4735SHans de Goede 	return pci_enable_device(pdev);
168bbbb262dSAlan Cox }
169bbbb262dSAlan Cox 
170bbbb262dSAlan Cox /**
171bbbb262dSAlan Cox  *	gma_power_suspend		-	bus callback for suspend
17263ad2a97SLee Jones  *	@_dev: our device
173bbbb262dSAlan Cox  *
174bbbb262dSAlan Cox  *	Called back by the PCI layer during a suspend of the system. We
175bbbb262dSAlan Cox  *	perform the necessary shut down steps and save enough state that
176bbbb262dSAlan Cox  *	we can undo this when resume is called.
177bbbb262dSAlan Cox  */
gma_power_suspend(struct device * _dev)178bbbb262dSAlan Cox int gma_power_suspend(struct device *_dev)
179bbbb262dSAlan Cox {
180d21b02afSGeliang Tang 	struct pci_dev *pdev = to_pci_dev(_dev);
181bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
182bbbb262dSAlan Cox 
1835d5fb746SPatrik Jakobsson 	gma_irq_uninstall(dev);
184bbbb262dSAlan Cox 	gma_suspend_display(dev);
185bbbb262dSAlan Cox 	gma_suspend_pci(pdev);
186bbbb262dSAlan Cox 	return 0;
187bbbb262dSAlan Cox }
188bbbb262dSAlan Cox 
189bbbb262dSAlan Cox /**
190bbbb262dSAlan Cox  *	gma_power_resume		-	resume power
19163ad2a97SLee Jones  *	@_dev: our device
192bbbb262dSAlan Cox  *
193bbbb262dSAlan Cox  *	Resume the PCI side of the graphics and then the displays
194bbbb262dSAlan Cox  */
gma_power_resume(struct device * _dev)195bbbb262dSAlan Cox int gma_power_resume(struct device *_dev)
196bbbb262dSAlan Cox {
197d21b02afSGeliang Tang 	struct pci_dev *pdev = to_pci_dev(_dev);
198bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
199bbbb262dSAlan Cox 
200bbbb262dSAlan Cox 	gma_resume_pci(pdev);
201bbbb262dSAlan Cox 	gma_resume_display(pdev);
2029b6a1657SHans de Goede 	gma_irq_install(dev);
203bbbb262dSAlan Cox 	return 0;
204bbbb262dSAlan Cox }
205bbbb262dSAlan Cox 
206bbbb262dSAlan Cox /**
207bbbb262dSAlan Cox  *	gma_power_begin		-	begin requiring power
208bbbb262dSAlan Cox  *	@dev: our DRM device
209bbbb262dSAlan Cox  *	@force_on: true to force power on
210bbbb262dSAlan Cox  *
211bbbb262dSAlan Cox  *	Begin an action that requires the display power island is enabled.
212bbbb262dSAlan Cox  *	We refcount the islands.
213bbbb262dSAlan Cox  */
gma_power_begin(struct drm_device * dev,bool force_on)214bbbb262dSAlan Cox bool gma_power_begin(struct drm_device *dev, bool force_on)
215bbbb262dSAlan Cox {
216*672c4735SHans de Goede 	if (force_on)
217*672c4735SHans de Goede 		return pm_runtime_resume_and_get(dev->dev) == 0;
218*672c4735SHans de Goede 	else
219*672c4735SHans de Goede 		return pm_runtime_get_if_in_use(dev->dev) == 1;
220bbbb262dSAlan Cox }
221bbbb262dSAlan Cox 
222bbbb262dSAlan Cox /**
223bbbb262dSAlan Cox  *	gma_power_end		-	end use of power
224bbbb262dSAlan Cox  *	@dev: Our DRM device
225bbbb262dSAlan Cox  *
226bbbb262dSAlan Cox  *	Indicate that one of our gma_power_begin() requested periods when
227bbbb262dSAlan Cox  *	the diplay island power is needed has completed.
228bbbb262dSAlan Cox  */
gma_power_end(struct drm_device * dev)229bbbb262dSAlan Cox void gma_power_end(struct drm_device *dev)
230bbbb262dSAlan Cox {
231a2c68495SThomas Zimmermann 	pm_runtime_put(dev->dev);
232bbbb262dSAlan Cox }
233