xref: /openbmc/linux/drivers/gpu/drm/gma500/power.c (revision 42ceddb6)
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 
31bbbb262dSAlan Cox #include "power.h"
32bbbb262dSAlan Cox #include "psb_drv.h"
33bbbb262dSAlan Cox #include "psb_reg.h"
34bbbb262dSAlan Cox #include "psb_intel_reg.h"
3522908507SThomas Zimmermann #include "psb_irq.h"
36bbbb262dSAlan Cox #include <linux/mutex.h>
37bbbb262dSAlan Cox #include <linux/pm_runtime.h>
38bbbb262dSAlan Cox 
39bbbb262dSAlan Cox static struct mutex power_mutex;	/* Serialize power ops */
4013e133eaSGuobin Huang static DEFINE_SPINLOCK(power_ctrl_lock);	/* Serialize power claim */
41bbbb262dSAlan Cox 
42bbbb262dSAlan Cox /**
43bbbb262dSAlan Cox  *	gma_power_init		-	initialise power manager
44bbbb262dSAlan Cox  *	@dev: our device
45bbbb262dSAlan Cox  *
46bbbb262dSAlan Cox  *	Set up for power management tracking of our hardware.
47bbbb262dSAlan Cox  */
48bbbb262dSAlan Cox void gma_power_init(struct drm_device *dev)
49bbbb262dSAlan Cox {
50f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
51bbbb262dSAlan Cox 
52bbbb262dSAlan Cox 	/* FIXME: Move APM/OSPM base into relevant device code */
53bbbb262dSAlan Cox 	dev_priv->apm_base = dev_priv->apm_reg & 0xffff;
54bbbb262dSAlan Cox 	dev_priv->ospm_base &= 0xffff;
55bbbb262dSAlan Cox 
56bbbb262dSAlan Cox 	dev_priv->display_power = true;	/* We start active */
57bbbb262dSAlan Cox 	dev_priv->display_count = 0;	/* Currently no users */
58bbbb262dSAlan Cox 	dev_priv->suspended = false;	/* And not suspended */
59bbbb262dSAlan Cox 	mutex_init(&power_mutex);
60bbbb262dSAlan Cox 
61c715bc1bSKirill A. Shutemov 	if (dev_priv->ops->init_pm)
62bbbb262dSAlan Cox 		dev_priv->ops->init_pm(dev);
63bbbb262dSAlan Cox }
64bbbb262dSAlan Cox 
65bbbb262dSAlan Cox /**
66bbbb262dSAlan Cox  *	gma_power_uninit	-	end power manager
67bbbb262dSAlan Cox  *	@dev: device to end for
68bbbb262dSAlan Cox  *
69bbbb262dSAlan Cox  *	Undo the effects of gma_power_init
70bbbb262dSAlan Cox  */
71bbbb262dSAlan Cox void gma_power_uninit(struct drm_device *dev)
72bbbb262dSAlan Cox {
73a2c68495SThomas Zimmermann 	pm_runtime_disable(dev->dev);
74a2c68495SThomas Zimmermann 	pm_runtime_set_suspended(dev->dev);
75bbbb262dSAlan Cox }
76bbbb262dSAlan Cox 
77bbbb262dSAlan Cox /**
78bbbb262dSAlan Cox  *	gma_suspend_display	-	suspend the display logic
79bbbb262dSAlan Cox  *	@dev: our DRM device
80bbbb262dSAlan Cox  *
81bbbb262dSAlan Cox  *	Suspend the display logic of the graphics interface
82bbbb262dSAlan Cox  */
83bbbb262dSAlan Cox static void gma_suspend_display(struct drm_device *dev)
84bbbb262dSAlan Cox {
85f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
86bbbb262dSAlan Cox 
87bbbb262dSAlan Cox 	if (dev_priv->suspended)
88bbbb262dSAlan Cox 		return;
89bbbb262dSAlan Cox 	dev_priv->ops->save_regs(dev);
90bbbb262dSAlan Cox 	dev_priv->ops->power_down(dev);
91bbbb262dSAlan Cox 	dev_priv->display_power = false;
92bbbb262dSAlan Cox }
93bbbb262dSAlan Cox 
94bbbb262dSAlan Cox /**
95bbbb262dSAlan Cox  *	gma_resume_display	-	resume display side logic
9663ad2a97SLee Jones  *	@pdev: PCI device
97bbbb262dSAlan Cox  *
98bbbb262dSAlan Cox  *	Resume the display hardware restoring state and enabling
99bbbb262dSAlan Cox  *	as necessary.
100bbbb262dSAlan Cox  */
101bbbb262dSAlan Cox static void gma_resume_display(struct pci_dev *pdev)
102bbbb262dSAlan Cox {
103bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
104f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
105bbbb262dSAlan Cox 
106bbbb262dSAlan Cox 	/* turn on the display power island */
107bbbb262dSAlan Cox 	dev_priv->ops->power_up(dev);
108bbbb262dSAlan Cox 	dev_priv->suspended = false;
109bbbb262dSAlan Cox 	dev_priv->display_power = true;
110bbbb262dSAlan Cox 
111bbbb262dSAlan Cox 	PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
112bbbb262dSAlan Cox 	pci_write_config_word(pdev, PSB_GMCH_CTRL,
113bbbb262dSAlan Cox 			dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
1148ba8209fSPatrik Jakobsson 
115*42ceddb6SThomas Zimmermann 	/* Rebuild our GTT mappings */
116*42ceddb6SThomas Zimmermann 	psb_gtt_resume(dev);
117*42ceddb6SThomas Zimmermann 	psb_gem_mm_resume(dev);
118bbbb262dSAlan Cox 	dev_priv->ops->restore_regs(dev);
119bbbb262dSAlan Cox }
120bbbb262dSAlan Cox 
121bbbb262dSAlan Cox /**
122bbbb262dSAlan Cox  *	gma_suspend_pci		-	suspend PCI side
123bbbb262dSAlan Cox  *	@pdev: PCI device
124bbbb262dSAlan Cox  *
125bbbb262dSAlan Cox  *	Perform the suspend processing on our PCI device state
126bbbb262dSAlan Cox  */
127bbbb262dSAlan Cox static void gma_suspend_pci(struct pci_dev *pdev)
128bbbb262dSAlan Cox {
129bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
130f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
131bbbb262dSAlan Cox 	int bsm, vbt;
132bbbb262dSAlan Cox 
133bbbb262dSAlan Cox 	if (dev_priv->suspended)
134bbbb262dSAlan Cox 		return;
135bbbb262dSAlan Cox 
136bbbb262dSAlan Cox 	pci_save_state(pdev);
137bbbb262dSAlan Cox 	pci_read_config_dword(pdev, 0x5C, &bsm);
138648a8e34SAlan Cox 	dev_priv->regs.saveBSM = bsm;
139bbbb262dSAlan Cox 	pci_read_config_dword(pdev, 0xFC, &vbt);
140648a8e34SAlan Cox 	dev_priv->regs.saveVBT = vbt;
141bbbb262dSAlan Cox 	pci_read_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, &dev_priv->msi_addr);
142bbbb262dSAlan Cox 	pci_read_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, &dev_priv->msi_data);
143bbbb262dSAlan Cox 
144bbbb262dSAlan Cox 	pci_disable_device(pdev);
145bbbb262dSAlan Cox 	pci_set_power_state(pdev, PCI_D3hot);
146bbbb262dSAlan Cox 
147bbbb262dSAlan Cox 	dev_priv->suspended = true;
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  */
157bbbb262dSAlan Cox static bool 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 	int ret;
162bbbb262dSAlan Cox 
163bbbb262dSAlan Cox 	if (!dev_priv->suspended)
164bbbb262dSAlan Cox 		return true;
165bbbb262dSAlan Cox 
166bbbb262dSAlan Cox 	pci_set_power_state(pdev, PCI_D0);
167bbbb262dSAlan Cox 	pci_restore_state(pdev);
168648a8e34SAlan Cox 	pci_write_config_dword(pdev, 0x5c, dev_priv->regs.saveBSM);
169648a8e34SAlan Cox 	pci_write_config_dword(pdev, 0xFC, dev_priv->regs.saveVBT);
170bbbb262dSAlan Cox 	/* restoring MSI address and data in PCIx space */
171bbbb262dSAlan Cox 	pci_write_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, dev_priv->msi_addr);
172bbbb262dSAlan Cox 	pci_write_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, dev_priv->msi_data);
173bbbb262dSAlan Cox 	ret = pci_enable_device(pdev);
174bbbb262dSAlan Cox 
175bbbb262dSAlan Cox 	if (ret != 0)
176bbbb262dSAlan Cox 		dev_err(&pdev->dev, "pci_enable failed: %d\n", ret);
177bbbb262dSAlan Cox 	else
178bbbb262dSAlan Cox 		dev_priv->suspended = false;
179bbbb262dSAlan Cox 	return !dev_priv->suspended;
180bbbb262dSAlan Cox }
181bbbb262dSAlan Cox 
182bbbb262dSAlan Cox /**
183bbbb262dSAlan Cox  *	gma_power_suspend		-	bus callback for suspend
18463ad2a97SLee Jones  *	@_dev: our device
185bbbb262dSAlan Cox  *
186bbbb262dSAlan Cox  *	Called back by the PCI layer during a suspend of the system. We
187bbbb262dSAlan Cox  *	perform the necessary shut down steps and save enough state that
188bbbb262dSAlan Cox  *	we can undo this when resume is called.
189bbbb262dSAlan Cox  */
190bbbb262dSAlan Cox int gma_power_suspend(struct device *_dev)
191bbbb262dSAlan Cox {
192d21b02afSGeliang Tang 	struct pci_dev *pdev = to_pci_dev(_dev);
193bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
194f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
195bbbb262dSAlan Cox 
196bbbb262dSAlan Cox 	mutex_lock(&power_mutex);
197bbbb262dSAlan Cox 	if (!dev_priv->suspended) {
198bbbb262dSAlan Cox 		if (dev_priv->display_count) {
199bbbb262dSAlan Cox 			mutex_unlock(&power_mutex);
20009016a11SAlan Cox 			dev_err(dev->dev, "GPU hardware busy, cannot suspend\n");
201bbbb262dSAlan Cox 			return -EBUSY;
202bbbb262dSAlan Cox 		}
203bbbb262dSAlan Cox 		psb_irq_uninstall(dev);
204bbbb262dSAlan Cox 		gma_suspend_display(dev);
205bbbb262dSAlan Cox 		gma_suspend_pci(pdev);
206bbbb262dSAlan Cox 	}
207bbbb262dSAlan Cox 	mutex_unlock(&power_mutex);
208bbbb262dSAlan Cox 	return 0;
209bbbb262dSAlan Cox }
210bbbb262dSAlan Cox 
211bbbb262dSAlan Cox /**
212bbbb262dSAlan Cox  *	gma_power_resume		-	resume power
21363ad2a97SLee Jones  *	@_dev: our device
214bbbb262dSAlan Cox  *
215bbbb262dSAlan Cox  *	Resume the PCI side of the graphics and then the displays
216bbbb262dSAlan Cox  */
217bbbb262dSAlan Cox int gma_power_resume(struct device *_dev)
218bbbb262dSAlan Cox {
219d21b02afSGeliang Tang 	struct pci_dev *pdev = to_pci_dev(_dev);
220bbbb262dSAlan Cox 	struct drm_device *dev = pci_get_drvdata(pdev);
221bbbb262dSAlan Cox 
222bbbb262dSAlan Cox 	mutex_lock(&power_mutex);
223bbbb262dSAlan Cox 	gma_resume_pci(pdev);
224bbbb262dSAlan Cox 	gma_resume_display(pdev);
225bbbb262dSAlan Cox 	psb_irq_preinstall(dev);
226bbbb262dSAlan Cox 	psb_irq_postinstall(dev);
227bbbb262dSAlan Cox 	mutex_unlock(&power_mutex);
228bbbb262dSAlan Cox 	return 0;
229bbbb262dSAlan Cox }
230bbbb262dSAlan Cox 
231bbbb262dSAlan Cox /**
232bbbb262dSAlan Cox  *	gma_power_is_on		-	returne true if power is on
233bbbb262dSAlan Cox  *	@dev: our DRM device
234bbbb262dSAlan Cox  *
235bbbb262dSAlan Cox  *	Returns true if the display island power is on at this moment
236bbbb262dSAlan Cox  */
237bbbb262dSAlan Cox bool gma_power_is_on(struct drm_device *dev)
238bbbb262dSAlan Cox {
239f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
240bbbb262dSAlan Cox 	return dev_priv->display_power;
241bbbb262dSAlan Cox }
242bbbb262dSAlan Cox 
243bbbb262dSAlan Cox /**
244bbbb262dSAlan Cox  *	gma_power_begin		-	begin requiring power
245bbbb262dSAlan Cox  *	@dev: our DRM device
246bbbb262dSAlan Cox  *	@force_on: true to force power on
247bbbb262dSAlan Cox  *
248bbbb262dSAlan Cox  *	Begin an action that requires the display power island is enabled.
249bbbb262dSAlan Cox  *	We refcount the islands.
250bbbb262dSAlan Cox  */
251bbbb262dSAlan Cox bool gma_power_begin(struct drm_device *dev, bool force_on)
252bbbb262dSAlan Cox {
253f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
254a2c68495SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
255bbbb262dSAlan Cox 	int ret;
256bbbb262dSAlan Cox 	unsigned long flags;
257bbbb262dSAlan Cox 
258bbbb262dSAlan Cox 	spin_lock_irqsave(&power_ctrl_lock, flags);
259bbbb262dSAlan Cox 	/* Power already on ? */
260bbbb262dSAlan Cox 	if (dev_priv->display_power) {
261bbbb262dSAlan Cox 		dev_priv->display_count++;
262a2c68495SThomas Zimmermann 		pm_runtime_get(dev->dev);
263bbbb262dSAlan Cox 		spin_unlock_irqrestore(&power_ctrl_lock, flags);
264bbbb262dSAlan Cox 		return true;
265bbbb262dSAlan Cox 	}
266bbbb262dSAlan Cox 	if (force_on == false)
267bbbb262dSAlan Cox 		goto out_false;
268bbbb262dSAlan Cox 
269bbbb262dSAlan Cox 	/* Ok power up needed */
270a2c68495SThomas Zimmermann 	ret = gma_resume_pci(pdev);
271bbbb262dSAlan Cox 	if (ret == 0) {
272bbbb262dSAlan Cox 		psb_irq_preinstall(dev);
273bbbb262dSAlan Cox 		psb_irq_postinstall(dev);
274a2c68495SThomas Zimmermann 		pm_runtime_get(dev->dev);
275bbbb262dSAlan Cox 		dev_priv->display_count++;
276bbbb262dSAlan Cox 		spin_unlock_irqrestore(&power_ctrl_lock, flags);
277bbbb262dSAlan Cox 		return true;
278bbbb262dSAlan Cox 	}
279bbbb262dSAlan Cox out_false:
280bbbb262dSAlan Cox 	spin_unlock_irqrestore(&power_ctrl_lock, flags);
281bbbb262dSAlan Cox 	return false;
282bbbb262dSAlan Cox }
283bbbb262dSAlan Cox 
284bbbb262dSAlan Cox /**
285bbbb262dSAlan Cox  *	gma_power_end		-	end use of power
286bbbb262dSAlan Cox  *	@dev: Our DRM device
287bbbb262dSAlan Cox  *
288bbbb262dSAlan Cox  *	Indicate that one of our gma_power_begin() requested periods when
289bbbb262dSAlan Cox  *	the diplay island power is needed has completed.
290bbbb262dSAlan Cox  */
291bbbb262dSAlan Cox void gma_power_end(struct drm_device *dev)
292bbbb262dSAlan Cox {
293f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
294bbbb262dSAlan Cox 	unsigned long flags;
295bbbb262dSAlan Cox 	spin_lock_irqsave(&power_ctrl_lock, flags);
296bbbb262dSAlan Cox 	dev_priv->display_count--;
297bbbb262dSAlan Cox 	WARN_ON(dev_priv->display_count < 0);
298bbbb262dSAlan Cox 	spin_unlock_irqrestore(&power_ctrl_lock, flags);
299a2c68495SThomas Zimmermann 	pm_runtime_put(dev->dev);
300bbbb262dSAlan Cox }
301bbbb262dSAlan Cox 
302bbbb262dSAlan Cox int psb_runtime_suspend(struct device *dev)
303bbbb262dSAlan Cox {
304bbbb262dSAlan Cox 	return gma_power_suspend(dev);
305bbbb262dSAlan Cox }
306bbbb262dSAlan Cox 
307bbbb262dSAlan Cox int psb_runtime_resume(struct device *dev)
308bbbb262dSAlan Cox {
30909016a11SAlan Cox 	return gma_power_resume(dev);
310bbbb262dSAlan Cox }
311bbbb262dSAlan Cox 
312bbbb262dSAlan Cox int psb_runtime_idle(struct device *dev)
313bbbb262dSAlan Cox {
314bbbb262dSAlan Cox 	struct drm_device *drmdev = pci_get_drvdata(to_pci_dev(dev));
315f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(drmdev);
316bbbb262dSAlan Cox 	if (dev_priv->display_count)
317bbbb262dSAlan Cox 		return 0;
318bbbb262dSAlan Cox 	else
319bbbb262dSAlan Cox 		return 1;
320bbbb262dSAlan Cox }
321888eef2eSPatrik Jakobsson 
322888eef2eSPatrik Jakobsson int gma_power_thaw(struct device *_dev)
323888eef2eSPatrik Jakobsson {
324888eef2eSPatrik Jakobsson 	return gma_power_resume(_dev);
325888eef2eSPatrik Jakobsson }
326888eef2eSPatrik Jakobsson 
327888eef2eSPatrik Jakobsson int gma_power_freeze(struct device *_dev)
328888eef2eSPatrik Jakobsson {
329888eef2eSPatrik Jakobsson 	return gma_power_suspend(_dev);
330888eef2eSPatrik Jakobsson }
331888eef2eSPatrik Jakobsson 
332888eef2eSPatrik Jakobsson int gma_power_restore(struct device *_dev)
333888eef2eSPatrik Jakobsson {
334888eef2eSPatrik Jakobsson 	return gma_power_resume(_dev);
335888eef2eSPatrik Jakobsson }
336