power.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. dev_priv->ops->restore_regs(dev);
  102. }
  103. /**
  104. * gma_suspend_pci - suspend PCI side
  105. * @pdev: PCI device
  106. *
  107. * Perform the suspend processing on our PCI device state
  108. */
  109. static void gma_suspend_pci(struct pci_dev *pdev)
  110. {
  111. struct drm_device *dev = pci_get_drvdata(pdev);
  112. struct drm_psb_private *dev_priv = dev->dev_private;
  113. int bsm, vbt;
  114. if (dev_priv->suspended)
  115. return;
  116. pci_save_state(pdev);
  117. pci_read_config_dword(pdev, 0x5C, &bsm);
  118. dev_priv->regs.saveBSM = bsm;
  119. pci_read_config_dword(pdev, 0xFC, &vbt);
  120. dev_priv->regs.saveVBT = vbt;
  121. pci_read_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, &dev_priv->msi_addr);
  122. pci_read_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, &dev_priv->msi_data);
  123. pci_disable_device(pdev);
  124. pci_set_power_state(pdev, PCI_D3hot);
  125. dev_priv->suspended = true;
  126. }
  127. /**
  128. * gma_resume_pci - resume helper
  129. * @dev: our PCI device
  130. *
  131. * Perform the resume processing on our PCI device state - rewrite
  132. * register state and re-enable the PCI device
  133. */
  134. static bool gma_resume_pci(struct pci_dev *pdev)
  135. {
  136. struct drm_device *dev = pci_get_drvdata(pdev);
  137. struct drm_psb_private *dev_priv = dev->dev_private;
  138. int ret;
  139. if (!dev_priv->suspended)
  140. return true;
  141. pci_set_power_state(pdev, PCI_D0);
  142. pci_restore_state(pdev);
  143. pci_write_config_dword(pdev, 0x5c, dev_priv->regs.saveBSM);
  144. pci_write_config_dword(pdev, 0xFC, dev_priv->regs.saveVBT);
  145. /* restoring MSI address and data in PCIx space */
  146. pci_write_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, dev_priv->msi_addr);
  147. pci_write_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, dev_priv->msi_data);
  148. ret = pci_enable_device(pdev);
  149. if (ret != 0)
  150. dev_err(&pdev->dev, "pci_enable failed: %d\n", ret);
  151. else
  152. dev_priv->suspended = false;
  153. return !dev_priv->suspended;
  154. }
  155. /**
  156. * gma_power_suspend - bus callback for suspend
  157. * @pdev: our PCI device
  158. * @state: suspend type
  159. *
  160. * Called back by the PCI layer during a suspend of the system. We
  161. * perform the necessary shut down steps and save enough state that
  162. * we can undo this when resume is called.
  163. */
  164. int gma_power_suspend(struct device *_dev)
  165. {
  166. struct pci_dev *pdev = container_of(_dev, struct pci_dev, dev);
  167. struct drm_device *dev = pci_get_drvdata(pdev);
  168. struct drm_psb_private *dev_priv = dev->dev_private;
  169. mutex_lock(&power_mutex);
  170. if (!dev_priv->suspended) {
  171. if (dev_priv->display_count) {
  172. mutex_unlock(&power_mutex);
  173. dev_err(dev->dev, "GPU hardware busy, cannot suspend\n");
  174. return -EBUSY;
  175. }
  176. psb_irq_uninstall(dev);
  177. gma_suspend_display(dev);
  178. gma_suspend_pci(pdev);
  179. }
  180. mutex_unlock(&power_mutex);
  181. return 0;
  182. }
  183. /**
  184. * gma_power_resume - resume power
  185. * @pdev: PCI device
  186. *
  187. * Resume the PCI side of the graphics and then the displays
  188. */
  189. int gma_power_resume(struct device *_dev)
  190. {
  191. struct pci_dev *pdev = container_of(_dev, struct pci_dev, dev);
  192. struct drm_device *dev = pci_get_drvdata(pdev);
  193. mutex_lock(&power_mutex);
  194. gma_resume_pci(pdev);
  195. gma_resume_display(pdev);
  196. psb_irq_preinstall(dev);
  197. psb_irq_postinstall(dev);
  198. mutex_unlock(&power_mutex);
  199. return 0;
  200. }
  201. /**
  202. * gma_power_is_on - returne true if power is on
  203. * @dev: our DRM device
  204. *
  205. * Returns true if the display island power is on at this moment
  206. */
  207. bool gma_power_is_on(struct drm_device *dev)
  208. {
  209. struct drm_psb_private *dev_priv = dev->dev_private;
  210. return dev_priv->display_power;
  211. }
  212. /**
  213. * gma_power_begin - begin requiring power
  214. * @dev: our DRM device
  215. * @force_on: true to force power on
  216. *
  217. * Begin an action that requires the display power island is enabled.
  218. * We refcount the islands.
  219. */
  220. bool gma_power_begin(struct drm_device *dev, bool force_on)
  221. {
  222. struct drm_psb_private *dev_priv = dev->dev_private;
  223. int ret;
  224. unsigned long flags;
  225. spin_lock_irqsave(&power_ctrl_lock, flags);
  226. /* Power already on ? */
  227. if (dev_priv->display_power) {
  228. dev_priv->display_count++;
  229. pm_runtime_get(&dev->pdev->dev);
  230. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  231. return true;
  232. }
  233. if (force_on == false)
  234. goto out_false;
  235. /* Ok power up needed */
  236. ret = gma_resume_pci(dev->pdev);
  237. if (ret == 0) {
  238. psb_irq_preinstall(dev);
  239. psb_irq_postinstall(dev);
  240. pm_runtime_get(&dev->pdev->dev);
  241. dev_priv->display_count++;
  242. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  243. return true;
  244. }
  245. out_false:
  246. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  247. return false;
  248. }
  249. /**
  250. * gma_power_end - end use of power
  251. * @dev: Our DRM device
  252. *
  253. * Indicate that one of our gma_power_begin() requested periods when
  254. * the diplay island power is needed has completed.
  255. */
  256. void gma_power_end(struct drm_device *dev)
  257. {
  258. struct drm_psb_private *dev_priv = dev->dev_private;
  259. unsigned long flags;
  260. spin_lock_irqsave(&power_ctrl_lock, flags);
  261. dev_priv->display_count--;
  262. WARN_ON(dev_priv->display_count < 0);
  263. spin_unlock_irqrestore(&power_ctrl_lock, flags);
  264. pm_runtime_put(&dev->pdev->dev);
  265. }
  266. int psb_runtime_suspend(struct device *dev)
  267. {
  268. return gma_power_suspend(dev);
  269. }
  270. int psb_runtime_resume(struct device *dev)
  271. {
  272. return gma_power_resume(dev);
  273. }
  274. int psb_runtime_idle(struct device *dev)
  275. {
  276. struct drm_device *drmdev = pci_get_drvdata(to_pci_dev(dev));
  277. struct drm_psb_private *dev_priv = drmdev->dev_private;
  278. if (dev_priv->display_count)
  279. return 0;
  280. else
  281. return 1;
  282. }