power.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**************************************************************************
  2. * Copyright (c) 2009-2011, Intel Corporation.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Authors:
  25. * Benjamin Defnet <benjamin.r.defnet@intel.com>
  26. * Rajesh Poornachandran <rajesh.poornachandran@intel.com>
  27. * Massively reworked
  28. * Alan Cox <alan@linux.intel.com>
  29. */
  30. #include "power.h"
  31. #include "psb_drv.h"
  32. #include "psb_reg.h"
  33. #include "psb_intel_reg.h"
  34. #include <linux/mutex.h>
  35. #include <linux/pm_runtime.h>
  36. static struct mutex power_mutex; /* Serialize power ops */
  37. static spinlock_t power_ctrl_lock; /* Serialize power claim */
  38. /**
  39. * gma_power_init - initialise power manager
  40. * @dev: our device
  41. *
  42. * Set up for power management tracking of our hardware.
  43. */
  44. void gma_power_init(struct drm_device *dev)
  45. {
  46. struct drm_psb_private *dev_priv = dev->dev_private;
  47. /* FIXME: Move APM/OSPM base into relevant device code */
  48. dev_priv->apm_base = dev_priv->apm_reg & 0xffff;
  49. dev_priv->ospm_base &= 0xffff;
  50. dev_priv->display_power = true; /* We start active */
  51. dev_priv->display_count = 0; /* Currently no users */
  52. dev_priv->suspended = false; /* And not suspended */
  53. spin_lock_init(&power_ctrl_lock);
  54. mutex_init(&power_mutex);
  55. if (dev_priv->ops->init_pm)
  56. dev_priv->ops->init_pm(dev);
  57. }
  58. /**
  59. * gma_power_uninit - end power manager
  60. * @dev: device to end for
  61. *
  62. * Undo the effects of gma_power_init
  63. */
  64. void gma_power_uninit(struct drm_device *dev)
  65. {
  66. pm_runtime_disable(&dev->pdev->dev);
  67. pm_runtime_set_suspended(&dev->pdev->dev);
  68. }
  69. /**
  70. * gma_suspend_display - suspend the display logic
  71. * @dev: our DRM device
  72. *
  73. * Suspend the display logic of the graphics interface
  74. */
  75. static void gma_suspend_display(struct drm_device *dev)
  76. {
  77. struct drm_psb_private *dev_priv = dev->dev_private;
  78. if (dev_priv->suspended)
  79. return;
  80. dev_priv->ops->save_regs(dev);
  81. dev_priv->ops->power_down(dev);
  82. dev_priv->display_power = false;
  83. }
  84. /**
  85. * gma_resume_display - resume display side logic
  86. *
  87. * Resume the display hardware restoring state and enabling
  88. * as necessary.
  89. */
  90. static void gma_resume_display(struct pci_dev *pdev)
  91. {
  92. struct drm_device *dev = pci_get_drvdata(pdev);
  93. struct drm_psb_private *dev_priv = dev->dev_private;
  94. /* turn on the display power island */
  95. dev_priv->ops->power_up(dev);
  96. dev_priv->suspended = false;
  97. dev_priv->display_power = true;
  98. PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
  99. pci_write_config_word(pdev, PSB_GMCH_CTRL,
  100. dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
  101. psb_gtt_restore(dev); /* Rebuild our GTT mappings */
  102. dev_priv->ops->restore_regs(dev);
  103. }
  104. /**
  105. * gma_suspend_pci - suspend PCI side
  106. * @pdev: PCI device
  107. *
  108. * Perform the suspend processing on our PCI device state
  109. */
  110. static void gma_suspend_pci(struct pci_dev *pdev)
  111. {
  112. struct drm_device *dev = pci_get_drvdata(pdev);
  113. struct drm_psb_private *dev_priv = dev->dev_private;
  114. int bsm, vbt;
  115. if (dev_priv->suspended)
  116. return;
  117. pci_save_state(pdev);
  118. pci_read_config_dword(pdev, 0x5C, &bsm);
  119. dev_priv->regs.saveBSM = bsm;
  120. pci_read_config_dword(pdev, 0xFC, &vbt);
  121. dev_priv->regs.saveVBT = vbt;
  122. pci_read_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, &dev_priv->msi_addr);
  123. pci_read_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, &dev_priv->msi_data);
  124. pci_disable_device(pdev);
  125. pci_set_power_state(pdev, PCI_D3hot);
  126. dev_priv->suspended = true;
  127. }
  128. /**
  129. * gma_resume_pci - resume helper
  130. * @dev: our PCI device
  131. *
  132. * Perform the resume processing on our PCI device state - rewrite
  133. * register state and re-enable the PCI device
  134. */
  135. static bool gma_resume_pci(struct pci_dev *pdev)
  136. {
  137. struct drm_device *dev = pci_get_drvdata(pdev);
  138. struct drm_psb_private *dev_priv = dev->dev_private;
  139. int ret;
  140. if (!dev_priv->suspended)
  141. return true;
  142. pci_set_power_state(pdev, PCI_D0);
  143. pci_restore_state(pdev);
  144. pci_write_config_dword(pdev, 0x5c, dev_priv->regs.saveBSM);
  145. pci_write_config_dword(pdev, 0xFC, dev_priv->regs.saveVBT);
  146. /* restoring MSI address and data in PCIx space */
  147. pci_write_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, dev_priv->msi_addr);
  148. pci_write_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, dev_priv->msi_data);
  149. ret = pci_enable_device(pdev);
  150. if (ret != 0)
  151. dev_err(&pdev->dev, "pci_enable failed: %d\n", ret);
  152. else
  153. dev_priv->suspended = false;
  154. return !dev_priv->suspended;
  155. }
  156. /**
  157. * gma_power_suspend - bus callback for suspend
  158. * @pdev: our PCI device
  159. * @state: suspend type
  160. *
  161. * Called back by the PCI layer during a suspend of the system. We
  162. * perform the necessary shut down steps and save enough state that
  163. * we can undo this when resume is called.
  164. */
  165. int gma_power_suspend(struct device *_dev)
  166. {
  167. struct pci_dev *pdev = to_pci_dev(_dev);
  168. struct drm_device *dev = pci_get_drvdata(pdev);
  169. struct drm_psb_private *dev_priv = dev->dev_private;
  170. mutex_lock(&power_mutex);
  171. if (!dev_priv->suspended) {
  172. if (dev_priv->display_count) {
  173. mutex_unlock(&power_mutex);
  174. dev_err(dev->dev, "GPU hardware busy, cannot suspend\n");
  175. return -EBUSY;
  176. }
  177. psb_irq_uninstall(dev);
  178. gma_suspend_display(dev);
  179. gma_suspend_pci(pdev);
  180. }
  181. mutex_unlock(&power_mutex);
  182. return 0;
  183. }
  184. /**
  185. * gma_power_resume - resume power
  186. * @pdev: PCI device
  187. *
  188. * Resume the PCI side of the graphics and then the displays
  189. */
  190. int gma_power_resume(struct device *_dev)
  191. {
  192. struct pci_dev *pdev = to_pci_dev(_dev);
  193. struct drm_device *dev = pci_get_drvdata(pdev);
  194. mutex_lock(&power_mutex);
  195. gma_resume_pci(pdev);
  196. gma_resume_display(pdev);
  197. psb_irq_preinstall(dev);
  198. psb_irq_postinstall(dev);
  199. mutex_unlock(&power_mutex);
  200. return 0;
  201. }
  202. /**
  203. * gma_power_is_on - returne true if power is on
  204. * @dev: our DRM device
  205. *
  206. * Returns true if the display island power is on at this moment
  207. */
  208. bool gma_power_is_on(struct drm_device *dev)
  209. {
  210. struct drm_psb_private *dev_priv = dev->dev_private;
  211. return dev_priv->display_power;
  212. }
  213. /**
  214. * gma_power_begin - begin requiring power
  215. * @dev: our DRM device
  216. * @force_on: true to force power on
  217. *
  218. * Begin an action that requires the display power island is enabled.
  219. * We refcount the islands.
  220. */
  221. bool gma_power_begin(struct drm_device *dev, bool force_on)
  222. {
  223. struct drm_psb_private *dev_priv = dev->dev_private;
  224. int ret;
  225. unsigned long flags;
  226. spin_lock_irqsave(&power_ctrl_lock, flags);
  227. /* Power already on ? */
  228. if (dev_priv->display_power) {
  229. dev_priv->display_count++;
  230. pm_runtime_get(&dev->pdev->dev);
  231. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  232. return true;
  233. }
  234. if (force_on == false)
  235. goto out_false;
  236. /* Ok power up needed */
  237. ret = gma_resume_pci(dev->pdev);
  238. if (ret == 0) {
  239. psb_irq_preinstall(dev);
  240. psb_irq_postinstall(dev);
  241. pm_runtime_get(&dev->pdev->dev);
  242. dev_priv->display_count++;
  243. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  244. return true;
  245. }
  246. out_false:
  247. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  248. return false;
  249. }
  250. /**
  251. * gma_power_end - end use of power
  252. * @dev: Our DRM device
  253. *
  254. * Indicate that one of our gma_power_begin() requested periods when
  255. * the diplay island power is needed has completed.
  256. */
  257. void gma_power_end(struct drm_device *dev)
  258. {
  259. struct drm_psb_private *dev_priv = dev->dev_private;
  260. unsigned long flags;
  261. spin_lock_irqsave(&power_ctrl_lock, flags);
  262. dev_priv->display_count--;
  263. WARN_ON(dev_priv->display_count < 0);
  264. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  265. pm_runtime_put(&dev->pdev->dev);
  266. }
  267. int psb_runtime_suspend(struct device *dev)
  268. {
  269. return gma_power_suspend(dev);
  270. }
  271. int psb_runtime_resume(struct device *dev)
  272. {
  273. return gma_power_resume(dev);
  274. }
  275. int psb_runtime_idle(struct device *dev)
  276. {
  277. struct drm_device *drmdev = pci_get_drvdata(to_pci_dev(dev));
  278. struct drm_psb_private *dev_priv = drmdev->dev_private;
  279. if (dev_priv->display_count)
  280. return 0;
  281. else
  282. return 1;
  283. }
  284. int gma_power_thaw(struct device *_dev)
  285. {
  286. return gma_power_resume(_dev);
  287. }
  288. int gma_power_freeze(struct device *_dev)
  289. {
  290. return gma_power_suspend(_dev);
  291. }
  292. int gma_power_restore(struct device *_dev)
  293. {
  294. return gma_power_resume(_dev);
  295. }