Back to home page

OSCL-LXR

 
 

    


0001 /**************************************************************************
0002  * Copyright (c) 2009-2011, Intel Corporation.
0003  * All Rights Reserved.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the "Software"),
0007  * to deal in the Software without restriction, including without limitation
0008  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009  * and/or sell copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice (including the next
0013  * paragraph) shall be included in all copies or substantial portions of the
0014  * Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0019  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0022  * SOFTWARE.
0023  *
0024  * Authors:
0025  *    Benjamin Defnet <benjamin.r.defnet@intel.com>
0026  *    Rajesh Poornachandran <rajesh.poornachandran@intel.com>
0027  * Massively reworked
0028  *    Alan Cox <alan@linux.intel.com>
0029  */
0030 
0031 #include "gem.h"
0032 #include "power.h"
0033 #include "psb_drv.h"
0034 #include "psb_reg.h"
0035 #include "psb_intel_reg.h"
0036 #include "psb_irq.h"
0037 #include <linux/mutex.h>
0038 #include <linux/pm_runtime.h>
0039 
0040 static struct mutex power_mutex;    /* Serialize power ops */
0041 static DEFINE_SPINLOCK(power_ctrl_lock);    /* Serialize power claim */
0042 
0043 /**
0044  *  gma_power_init      -   initialise power manager
0045  *  @dev: our device
0046  *
0047  *  Set up for power management tracking of our hardware.
0048  */
0049 void gma_power_init(struct drm_device *dev)
0050 {
0051     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0052 
0053     /* FIXME: Move APM/OSPM base into relevant device code */
0054     dev_priv->apm_base = dev_priv->apm_reg & 0xffff;
0055     dev_priv->ospm_base &= 0xffff;
0056 
0057     dev_priv->display_power = true; /* We start active */
0058     dev_priv->display_count = 0;    /* Currently no users */
0059     dev_priv->suspended = false;    /* And not suspended */
0060     mutex_init(&power_mutex);
0061 
0062     if (dev_priv->ops->init_pm)
0063         dev_priv->ops->init_pm(dev);
0064 }
0065 
0066 /**
0067  *  gma_power_uninit    -   end power manager
0068  *  @dev: device to end for
0069  *
0070  *  Undo the effects of gma_power_init
0071  */
0072 void gma_power_uninit(struct drm_device *dev)
0073 {
0074     pm_runtime_disable(dev->dev);
0075     pm_runtime_set_suspended(dev->dev);
0076 }
0077 
0078 /**
0079  *  gma_suspend_display -   suspend the display logic
0080  *  @dev: our DRM device
0081  *
0082  *  Suspend the display logic of the graphics interface
0083  */
0084 static void gma_suspend_display(struct drm_device *dev)
0085 {
0086     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0087 
0088     if (dev_priv->suspended)
0089         return;
0090     dev_priv->ops->save_regs(dev);
0091     dev_priv->ops->power_down(dev);
0092     dev_priv->display_power = false;
0093 }
0094 
0095 /**
0096  *  gma_resume_display  -   resume display side logic
0097  *  @pdev: PCI device
0098  *
0099  *  Resume the display hardware restoring state and enabling
0100  *  as necessary.
0101  */
0102 static void gma_resume_display(struct pci_dev *pdev)
0103 {
0104     struct drm_device *dev = pci_get_drvdata(pdev);
0105     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0106 
0107     /* turn on the display power island */
0108     dev_priv->ops->power_up(dev);
0109     dev_priv->suspended = false;
0110     dev_priv->display_power = true;
0111 
0112     PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
0113     pci_write_config_word(pdev, PSB_GMCH_CTRL,
0114             dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
0115 
0116     /* Rebuild our GTT mappings */
0117     psb_gtt_resume(dev);
0118     psb_gem_mm_resume(dev);
0119     dev_priv->ops->restore_regs(dev);
0120 }
0121 
0122 /**
0123  *  gma_suspend_pci     -   suspend PCI side
0124  *  @pdev: PCI device
0125  *
0126  *  Perform the suspend processing on our PCI device state
0127  */
0128 static void gma_suspend_pci(struct pci_dev *pdev)
0129 {
0130     struct drm_device *dev = pci_get_drvdata(pdev);
0131     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0132     int bsm, vbt;
0133 
0134     if (dev_priv->suspended)
0135         return;
0136 
0137     pci_save_state(pdev);
0138     pci_read_config_dword(pdev, 0x5C, &bsm);
0139     dev_priv->regs.saveBSM = bsm;
0140     pci_read_config_dword(pdev, 0xFC, &vbt);
0141     dev_priv->regs.saveVBT = vbt;
0142 
0143     pci_disable_device(pdev);
0144     pci_set_power_state(pdev, PCI_D3hot);
0145 
0146     dev_priv->suspended = true;
0147 }
0148 
0149 /**
0150  *  gma_resume_pci      -   resume helper
0151  *  @pdev: our PCI device
0152  *
0153  *  Perform the resume processing on our PCI device state - rewrite
0154  *  register state and re-enable the PCI device
0155  */
0156 static bool gma_resume_pci(struct pci_dev *pdev)
0157 {
0158     struct drm_device *dev = pci_get_drvdata(pdev);
0159     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0160     int ret;
0161 
0162     if (!dev_priv->suspended)
0163         return true;
0164 
0165     pci_set_power_state(pdev, PCI_D0);
0166     pci_restore_state(pdev);
0167     pci_write_config_dword(pdev, 0x5c, dev_priv->regs.saveBSM);
0168     pci_write_config_dword(pdev, 0xFC, dev_priv->regs.saveVBT);
0169     ret = pci_enable_device(pdev);
0170 
0171     if (ret != 0)
0172         dev_err(&pdev->dev, "pci_enable failed: %d\n", ret);
0173     else
0174         dev_priv->suspended = false;
0175     return !dev_priv->suspended;
0176 }
0177 
0178 /**
0179  *  gma_power_suspend       -   bus callback for suspend
0180  *  @_dev: our device
0181  *
0182  *  Called back by the PCI layer during a suspend of the system. We
0183  *  perform the necessary shut down steps and save enough state that
0184  *  we can undo this when resume is called.
0185  */
0186 int gma_power_suspend(struct device *_dev)
0187 {
0188     struct pci_dev *pdev = to_pci_dev(_dev);
0189     struct drm_device *dev = pci_get_drvdata(pdev);
0190     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0191 
0192     mutex_lock(&power_mutex);
0193     if (!dev_priv->suspended) {
0194         if (dev_priv->display_count) {
0195             mutex_unlock(&power_mutex);
0196             dev_err(dev->dev, "GPU hardware busy, cannot suspend\n");
0197             return -EBUSY;
0198         }
0199         gma_irq_uninstall(dev);
0200         gma_suspend_display(dev);
0201         gma_suspend_pci(pdev);
0202     }
0203     mutex_unlock(&power_mutex);
0204     return 0;
0205 }
0206 
0207 /**
0208  *  gma_power_resume        -   resume power
0209  *  @_dev: our device
0210  *
0211  *  Resume the PCI side of the graphics and then the displays
0212  */
0213 int gma_power_resume(struct device *_dev)
0214 {
0215     struct pci_dev *pdev = to_pci_dev(_dev);
0216     struct drm_device *dev = pci_get_drvdata(pdev);
0217 
0218     mutex_lock(&power_mutex);
0219     gma_resume_pci(pdev);
0220     gma_resume_display(pdev);
0221     gma_irq_install(dev);
0222     mutex_unlock(&power_mutex);
0223     return 0;
0224 }
0225 
0226 /**
0227  *  gma_power_is_on     -   returne true if power is on
0228  *  @dev: our DRM device
0229  *
0230  *  Returns true if the display island power is on at this moment
0231  */
0232 bool gma_power_is_on(struct drm_device *dev)
0233 {
0234     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0235     return dev_priv->display_power;
0236 }
0237 
0238 /**
0239  *  gma_power_begin     -   begin requiring power
0240  *  @dev: our DRM device
0241  *  @force_on: true to force power on
0242  *
0243  *  Begin an action that requires the display power island is enabled.
0244  *  We refcount the islands.
0245  */
0246 bool gma_power_begin(struct drm_device *dev, bool force_on)
0247 {
0248     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0249     struct pci_dev *pdev = to_pci_dev(dev->dev);
0250     int ret;
0251     unsigned long flags;
0252 
0253     spin_lock_irqsave(&power_ctrl_lock, flags);
0254     /* Power already on ? */
0255     if (dev_priv->display_power) {
0256         dev_priv->display_count++;
0257         pm_runtime_get(dev->dev);
0258         spin_unlock_irqrestore(&power_ctrl_lock, flags);
0259         return true;
0260     }
0261     if (force_on == false)
0262         goto out_false;
0263 
0264     /* Ok power up needed */
0265     ret = gma_resume_pci(pdev);
0266     if (ret == 0) {
0267         gma_irq_preinstall(dev);
0268         gma_irq_postinstall(dev);
0269         pm_runtime_get(dev->dev);
0270         dev_priv->display_count++;
0271         spin_unlock_irqrestore(&power_ctrl_lock, flags);
0272         return true;
0273     }
0274 out_false:
0275     spin_unlock_irqrestore(&power_ctrl_lock, flags);
0276     return false;
0277 }
0278 
0279 /**
0280  *  gma_power_end       -   end use of power
0281  *  @dev: Our DRM device
0282  *
0283  *  Indicate that one of our gma_power_begin() requested periods when
0284  *  the diplay island power is needed has completed.
0285  */
0286 void gma_power_end(struct drm_device *dev)
0287 {
0288     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0289     unsigned long flags;
0290     spin_lock_irqsave(&power_ctrl_lock, flags);
0291     dev_priv->display_count--;
0292     WARN_ON(dev_priv->display_count < 0);
0293     spin_unlock_irqrestore(&power_ctrl_lock, flags);
0294     pm_runtime_put(dev->dev);
0295 }
0296 
0297 int psb_runtime_suspend(struct device *dev)
0298 {
0299     return gma_power_suspend(dev);
0300 }
0301 
0302 int psb_runtime_resume(struct device *dev)
0303 {
0304     return gma_power_resume(dev);
0305 }
0306 
0307 int psb_runtime_idle(struct device *dev)
0308 {
0309     struct drm_device *drmdev = pci_get_drvdata(to_pci_dev(dev));
0310     struct drm_psb_private *dev_priv = to_drm_psb_private(drmdev);
0311     if (dev_priv->display_count)
0312         return 0;
0313     else
0314         return 1;
0315 }
0316 
0317 int gma_power_thaw(struct device *_dev)
0318 {
0319     return gma_power_resume(_dev);
0320 }
0321 
0322 int gma_power_freeze(struct device *_dev)
0323 {
0324     return gma_power_suspend(_dev);
0325 }
0326 
0327 int gma_power_restore(struct device *_dev)
0328 {
0329     return gma_power_resume(_dev);
0330 }