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_de.h"
28 #include "display/intel_fbc.h"
29 #include "display/intel_gmbus.h"
30 #include "display/intel_vga.h"
31 
32 #include "i915_drv.h"
33 #include "i915_reg.h"
34 #include "i915_suspend.h"
35 #include "intel_pci_config.h"
36 
37 static void intel_save_swf(struct drm_i915_private *dev_priv)
38 {
39 	int i;
40 
41 	/* Scratch space */
42 	if (GRAPHICS_VER(dev_priv) == 2 && IS_MOBILE(dev_priv)) {
43 		for (i = 0; i < 7; i++) {
44 			dev_priv->regfile.saveSWF0[i] = intel_de_read(dev_priv, SWF0(i));
45 			dev_priv->regfile.saveSWF1[i] = intel_de_read(dev_priv, SWF1(i));
46 		}
47 		for (i = 0; i < 3; i++)
48 			dev_priv->regfile.saveSWF3[i] = intel_de_read(dev_priv, SWF3(i));
49 	} else if (GRAPHICS_VER(dev_priv) == 2) {
50 		for (i = 0; i < 7; i++)
51 			dev_priv->regfile.saveSWF1[i] = intel_de_read(dev_priv, SWF1(i));
52 	} else if (HAS_GMCH(dev_priv)) {
53 		for (i = 0; i < 16; i++) {
54 			dev_priv->regfile.saveSWF0[i] = intel_de_read(dev_priv, SWF0(i));
55 			dev_priv->regfile.saveSWF1[i] = intel_de_read(dev_priv, SWF1(i));
56 		}
57 		for (i = 0; i < 3; i++)
58 			dev_priv->regfile.saveSWF3[i] = intel_de_read(dev_priv, SWF3(i));
59 	}
60 }
61 
62 static void intel_restore_swf(struct drm_i915_private *dev_priv)
63 {
64 	int i;
65 
66 	/* Scratch space */
67 	if (GRAPHICS_VER(dev_priv) == 2 && IS_MOBILE(dev_priv)) {
68 		for (i = 0; i < 7; i++) {
69 			intel_de_write(dev_priv, SWF0(i), dev_priv->regfile.saveSWF0[i]);
70 			intel_de_write(dev_priv, SWF1(i), dev_priv->regfile.saveSWF1[i]);
71 		}
72 		for (i = 0; i < 3; i++)
73 			intel_de_write(dev_priv, SWF3(i), dev_priv->regfile.saveSWF3[i]);
74 	} else if (GRAPHICS_VER(dev_priv) == 2) {
75 		for (i = 0; i < 7; i++)
76 			intel_de_write(dev_priv, SWF1(i), dev_priv->regfile.saveSWF1[i]);
77 	} else if (HAS_GMCH(dev_priv)) {
78 		for (i = 0; i < 16; i++) {
79 			intel_de_write(dev_priv, SWF0(i), dev_priv->regfile.saveSWF0[i]);
80 			intel_de_write(dev_priv, SWF1(i), dev_priv->regfile.saveSWF1[i]);
81 		}
82 		for (i = 0; i < 3; i++)
83 			intel_de_write(dev_priv, SWF3(i), dev_priv->regfile.saveSWF3[i]);
84 	}
85 }
86 
87 void i915_save_display(struct drm_i915_private *dev_priv)
88 {
89 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
90 
91 	if (!HAS_DISPLAY(dev_priv))
92 		return;
93 
94 	/* Display arbitration control */
95 	if (GRAPHICS_VER(dev_priv) <= 4)
96 		dev_priv->regfile.saveDSPARB = intel_de_read(dev_priv, DSPARB);
97 
98 	if (GRAPHICS_VER(dev_priv) == 4)
99 		pci_read_config_word(pdev, GCDGMBUS,
100 				     &dev_priv->regfile.saveGCDGMBUS);
101 
102 	intel_save_swf(dev_priv);
103 }
104 
105 void i915_restore_display(struct drm_i915_private *dev_priv)
106 {
107 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
108 
109 	if (!HAS_DISPLAY(dev_priv))
110 		return;
111 
112 	intel_restore_swf(dev_priv);
113 
114 	if (GRAPHICS_VER(dev_priv) == 4)
115 		pci_write_config_word(pdev, GCDGMBUS,
116 				      dev_priv->regfile.saveGCDGMBUS);
117 
118 	/* Display arbitration */
119 	if (GRAPHICS_VER(dev_priv) <= 4)
120 		intel_de_write(dev_priv, DSPARB, dev_priv->regfile.saveDSPARB);
121 
122 	/* only restore FBC info on the platform that supports FBC*/
123 	intel_fbc_global_disable(dev_priv);
124 
125 	intel_vga_redisable(dev_priv);
126 
127 	intel_gmbus_reset(dev_priv);
128 }
129