1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "power.h"
32#include "psb_drv.h"
33#include "psb_reg.h"
34#include "psb_intel_reg.h"
35#include <linux/mutex.h>
36#include <linux/pm_runtime.h>
37
38static struct mutex power_mutex;
39static spinlock_t power_ctrl_lock;
40
41
42
43
44
45
46
47void gma_power_init(struct drm_device *dev)
48{
49 struct drm_psb_private *dev_priv = dev->dev_private;
50
51
52 dev_priv->apm_base = dev_priv->apm_reg & 0xffff;
53 dev_priv->ospm_base &= 0xffff;
54
55 dev_priv->display_power = true;
56 dev_priv->display_count = 0;
57 dev_priv->suspended = false;
58 spin_lock_init(&power_ctrl_lock);
59 mutex_init(&power_mutex);
60
61 if (dev_priv->ops->init_pm)
62 dev_priv->ops->init_pm(dev);
63}
64
65
66
67
68
69
70
71void gma_power_uninit(struct drm_device *dev)
72{
73 pm_runtime_disable(&dev->pdev->dev);
74 pm_runtime_set_suspended(&dev->pdev->dev);
75}
76
77
78
79
80
81
82
83static void gma_suspend_display(struct drm_device *dev)
84{
85 struct drm_psb_private *dev_priv = dev->dev_private;
86
87 if (dev_priv->suspended)
88 return;
89 dev_priv->ops->save_regs(dev);
90 dev_priv->ops->power_down(dev);
91 dev_priv->display_power = false;
92}
93
94
95
96
97
98
99
100static void gma_resume_display(struct pci_dev *pdev)
101{
102 struct drm_device *dev = pci_get_drvdata(pdev);
103 struct drm_psb_private *dev_priv = dev->dev_private;
104
105
106 dev_priv->ops->power_up(dev);
107 dev_priv->suspended = false;
108 dev_priv->display_power = true;
109
110 PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
111 pci_write_config_word(pdev, PSB_GMCH_CTRL,
112 dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
113
114 psb_gtt_restore(dev);
115 dev_priv->ops->restore_regs(dev);
116}
117
118
119
120
121
122
123
124static void gma_suspend_pci(struct pci_dev *pdev)
125{
126 struct drm_device *dev = pci_get_drvdata(pdev);
127 struct drm_psb_private *dev_priv = dev->dev_private;
128 int bsm, vbt;
129
130 if (dev_priv->suspended)
131 return;
132
133 pci_save_state(pdev);
134 pci_read_config_dword(pdev, 0x5C, &bsm);
135 dev_priv->regs.saveBSM = bsm;
136 pci_read_config_dword(pdev, 0xFC, &vbt);
137 dev_priv->regs.saveVBT = vbt;
138 pci_read_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, &dev_priv->msi_addr);
139 pci_read_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, &dev_priv->msi_data);
140
141 pci_disable_device(pdev);
142 pci_set_power_state(pdev, PCI_D3hot);
143
144 dev_priv->suspended = true;
145}
146
147
148
149
150
151
152
153
154static bool gma_resume_pci(struct pci_dev *pdev)
155{
156 struct drm_device *dev = pci_get_drvdata(pdev);
157 struct drm_psb_private *dev_priv = dev->dev_private;
158 int ret;
159
160 if (!dev_priv->suspended)
161 return true;
162
163 pci_set_power_state(pdev, PCI_D0);
164 pci_restore_state(pdev);
165 pci_write_config_dword(pdev, 0x5c, dev_priv->regs.saveBSM);
166 pci_write_config_dword(pdev, 0xFC, dev_priv->regs.saveVBT);
167
168 pci_write_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, dev_priv->msi_addr);
169 pci_write_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, dev_priv->msi_data);
170 ret = pci_enable_device(pdev);
171
172 if (ret != 0)
173 dev_err(&pdev->dev, "pci_enable failed: %d\n", ret);
174 else
175 dev_priv->suspended = false;
176 return !dev_priv->suspended;
177}
178
179
180
181
182
183
184
185
186
187
188int gma_power_suspend(struct device *_dev)
189{
190 struct pci_dev *pdev = to_pci_dev(_dev);
191 struct drm_device *dev = pci_get_drvdata(pdev);
192 struct drm_psb_private *dev_priv = dev->dev_private;
193
194 mutex_lock(&power_mutex);
195 if (!dev_priv->suspended) {
196 if (dev_priv->display_count) {
197 mutex_unlock(&power_mutex);
198 dev_err(dev->dev, "GPU hardware busy, cannot suspend\n");
199 return -EBUSY;
200 }
201 psb_irq_uninstall(dev);
202 gma_suspend_display(dev);
203 gma_suspend_pci(pdev);
204 }
205 mutex_unlock(&power_mutex);
206 return 0;
207}
208
209
210
211
212
213
214
215int gma_power_resume(struct device *_dev)
216{
217 struct pci_dev *pdev = to_pci_dev(_dev);
218 struct drm_device *dev = pci_get_drvdata(pdev);
219
220 mutex_lock(&power_mutex);
221 gma_resume_pci(pdev);
222 gma_resume_display(pdev);
223 psb_irq_preinstall(dev);
224 psb_irq_postinstall(dev);
225 mutex_unlock(&power_mutex);
226 return 0;
227}
228
229
230
231
232
233
234
235bool gma_power_is_on(struct drm_device *dev)
236{
237 struct drm_psb_private *dev_priv = dev->dev_private;
238 return dev_priv->display_power;
239}
240
241
242
243
244
245
246
247
248
249bool gma_power_begin(struct drm_device *dev, bool force_on)
250{
251 struct drm_psb_private *dev_priv = dev->dev_private;
252 int ret;
253 unsigned long flags;
254
255 spin_lock_irqsave(&power_ctrl_lock, flags);
256
257 if (dev_priv->display_power) {
258 dev_priv->display_count++;
259 pm_runtime_get(&dev->pdev->dev);
260 spin_unlock_irqrestore(&power_ctrl_lock, flags);
261 return true;
262 }
263 if (force_on == false)
264 goto out_false;
265
266
267 ret = gma_resume_pci(dev->pdev);
268 if (ret == 0) {
269 psb_irq_preinstall(dev);
270 psb_irq_postinstall(dev);
271 pm_runtime_get(&dev->pdev->dev);
272 dev_priv->display_count++;
273 spin_unlock_irqrestore(&power_ctrl_lock, flags);
274 return true;
275 }
276out_false:
277 spin_unlock_irqrestore(&power_ctrl_lock, flags);
278 return false;
279}
280
281
282
283
284
285
286
287
288void gma_power_end(struct drm_device *dev)
289{
290 struct drm_psb_private *dev_priv = dev->dev_private;
291 unsigned long flags;
292 spin_lock_irqsave(&power_ctrl_lock, flags);
293 dev_priv->display_count--;
294 WARN_ON(dev_priv->display_count < 0);
295 spin_unlock_irqrestore(&power_ctrl_lock, flags);
296 pm_runtime_put(&dev->pdev->dev);
297}
298
299int psb_runtime_suspend(struct device *dev)
300{
301 return gma_power_suspend(dev);
302}
303
304int psb_runtime_resume(struct device *dev)
305{
306 return gma_power_resume(dev);
307}
308
309int psb_runtime_idle(struct device *dev)
310{
311 struct drm_device *drmdev = pci_get_drvdata(to_pci_dev(dev));
312 struct drm_psb_private *dev_priv = drmdev->dev_private;
313 if (dev_priv->display_count)
314 return 0;
315 else
316 return 1;
317}
318
319int gma_power_thaw(struct device *_dev)
320{
321 return gma_power_resume(_dev);
322}
323
324int gma_power_freeze(struct device *_dev)
325{
326 return gma_power_suspend(_dev);
327}
328
329int gma_power_restore(struct device *_dev)
330{
331 return gma_power_resume(_dev);
332}
333