1/*
2 *
3 * Copyright 2008 (c) Intel Corporation
4 *   Jesse Barnes <jbarnes@virtuousgeek.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27#include "display/intel_fbc.h"
28#include "display/intel_gmbus.h"
29#include "display/intel_vga.h"
30
31#include "i915_drv.h"
32#include "i915_reg.h"
33#include "i915_suspend.h"
34
35static void i915_save_display(struct drm_i915_private *dev_priv)
36{
37	struct pci_dev *pdev = dev_priv->drm.pdev;
38
39	/* Display arbitration control */
40	if (INTEL_GEN(dev_priv) <= 4)
41		dev_priv->regfile.saveDSPARB = I915_READ(DSPARB);
42
43	if (IS_GEN(dev_priv, 4))
44		pci_read_config_word(pdev, GCDGMBUS,
45				     &dev_priv->regfile.saveGCDGMBUS);
46}
47
48static void i915_restore_display(struct drm_i915_private *dev_priv)
49{
50	struct pci_dev *pdev = dev_priv->drm.pdev;
51
52	if (IS_GEN(dev_priv, 4))
53		pci_write_config_word(pdev, GCDGMBUS,
54				      dev_priv->regfile.saveGCDGMBUS);
55
56	/* Display arbitration */
57	if (INTEL_GEN(dev_priv) <= 4)
58		I915_WRITE(DSPARB, dev_priv->regfile.saveDSPARB);
59
60	/* only restore FBC info on the platform that supports FBC*/
61	intel_fbc_global_disable(dev_priv);
62
63	intel_vga_redisable(dev_priv);
64
65	intel_gmbus_reset(dev_priv);
66}
67
68int i915_save_state(struct drm_i915_private *dev_priv)
69{
70	int i;
71
72	i915_save_display(dev_priv);
73
74	/* Scratch space */
75	if (IS_GEN(dev_priv, 2) && IS_MOBILE(dev_priv)) {
76		for (i = 0; i < 7; i++) {
77			dev_priv->regfile.saveSWF0[i] = I915_READ(SWF0(i));
78			dev_priv->regfile.saveSWF1[i] = I915_READ(SWF1(i));
79		}
80		for (i = 0; i < 3; i++)
81			dev_priv->regfile.saveSWF3[i] = I915_READ(SWF3(i));
82	} else if (IS_GEN(dev_priv, 2)) {
83		for (i = 0; i < 7; i++)
84			dev_priv->regfile.saveSWF1[i] = I915_READ(SWF1(i));
85	} else if (HAS_GMCH(dev_priv)) {
86		for (i = 0; i < 16; i++) {
87			dev_priv->regfile.saveSWF0[i] = I915_READ(SWF0(i));
88			dev_priv->regfile.saveSWF1[i] = I915_READ(SWF1(i));
89		}
90		for (i = 0; i < 3; i++)
91			dev_priv->regfile.saveSWF3[i] = I915_READ(SWF3(i));
92	}
93
94	return 0;
95}
96
97int i915_restore_state(struct drm_i915_private *dev_priv)
98{
99	int i;
100
101	i915_restore_display(dev_priv);
102
103	/* Scratch space */
104	if (IS_GEN(dev_priv, 2) && IS_MOBILE(dev_priv)) {
105		for (i = 0; i < 7; i++) {
106			I915_WRITE(SWF0(i), dev_priv->regfile.saveSWF0[i]);
107			I915_WRITE(SWF1(i), dev_priv->regfile.saveSWF1[i]);
108		}
109		for (i = 0; i < 3; i++)
110			I915_WRITE(SWF3(i), dev_priv->regfile.saveSWF3[i]);
111	} else if (IS_GEN(dev_priv, 2)) {
112		for (i = 0; i < 7; i++)
113			I915_WRITE(SWF1(i), dev_priv->regfile.saveSWF1[i]);
114	} else if (HAS_GMCH(dev_priv)) {
115		for (i = 0; i < 16; i++) {
116			I915_WRITE(SWF0(i), dev_priv->regfile.saveSWF0[i]);
117			I915_WRITE(SWF1(i), dev_priv->regfile.saveSWF1[i]);
118		}
119		for (i = 0; i < 3; i++)
120			I915_WRITE(SWF3(i), dev_priv->regfile.saveSWF3[i]);
121	}
122
123	return 0;
124}
125