i915_suspend.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *
 * Copyright 2008 (c) Intel Corporation
 *   Jesse Barnes <jbarnes@virtuousgeek.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

27 28
#include <drm/drmP.h>
#include <drm/i915_drm.h>
29
#include "intel_drv.h"
30
#include "i915_reg.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

static u8 i915_read_indexed(struct drm_device *dev, u16 index_port, u16 data_port, u8 reg)
{
	struct drm_i915_private *dev_priv = dev->dev_private;

	I915_WRITE8(index_port, reg);
	return I915_READ8(data_port);
}

static u8 i915_read_ar(struct drm_device *dev, u16 st01, u8 reg, u16 palette_enable)
{
	struct drm_i915_private *dev_priv = dev->dev_private;

	I915_READ8(st01);
	I915_WRITE8(VGA_AR_INDEX, palette_enable | reg);
	return I915_READ8(VGA_AR_DATA_READ);
}

static void i915_write_ar(struct drm_device *dev, u16 st01, u8 reg, u8 val, u16 palette_enable)
{
	struct drm_i915_private *dev_priv = dev->dev_private;

	I915_READ8(st01);
	I915_WRITE8(VGA_AR_INDEX, palette_enable | reg);
	I915_WRITE8(VGA_AR_DATA_WRITE, val);
}

static void i915_write_indexed(struct drm_device *dev, u16 index_port, u16 data_port, u8 reg, u8 val)
{
	struct drm_i915_private *dev_priv = dev->dev_private;

	I915_WRITE8(index_port, reg);
	I915_WRITE8(data_port, val);
}

static void i915_save_vga(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	int i;
	u16 cr_index, cr_data, st01;

72 73 74 75
	/* VGA state */
	dev_priv->regfile.saveVGA0 = I915_READ(VGA0);
	dev_priv->regfile.saveVGA1 = I915_READ(VGA1);
	dev_priv->regfile.saveVGA_PD = I915_READ(VGA_PD);
76
	dev_priv->regfile.saveVGACNTRL = I915_READ(i915_vgacntrl_reg(dev));
77

78
	/* VGA color palette registers */
79
	dev_priv->regfile.saveDACMASK = I915_READ8(VGA_DACMASK);
80 81

	/* MSR bits */
82 83
	dev_priv->regfile.saveMSR = I915_READ8(VGA_MSR_READ);
	if (dev_priv->regfile.saveMSR & VGA_MSR_CGA_MODE) {
84 85 86 87 88 89 90 91 92 93 94 95 96 97
		cr_index = VGA_CR_INDEX_CGA;
		cr_data = VGA_CR_DATA_CGA;
		st01 = VGA_ST01_CGA;
	} else {
		cr_index = VGA_CR_INDEX_MDA;
		cr_data = VGA_CR_DATA_MDA;
		st01 = VGA_ST01_MDA;
	}

	/* CRT controller regs */
	i915_write_indexed(dev, cr_index, cr_data, 0x11,
			   i915_read_indexed(dev, cr_index, cr_data, 0x11) &
			   (~0x80));
	for (i = 0; i <= 0x24; i++)
98
		dev_priv->regfile.saveCR[i] =
99 100
			i915_read_indexed(dev, cr_index, cr_data, i);
	/* Make sure we don't turn off CR group 0 writes */
101
	dev_priv->regfile.saveCR[0x11] &= ~0x80;
102 103 104

	/* Attribute controller registers */
	I915_READ8(st01);
105
	dev_priv->regfile.saveAR_INDEX = I915_READ8(VGA_AR_INDEX);
106
	for (i = 0; i <= 0x14; i++)
107
		dev_priv->regfile.saveAR[i] = i915_read_ar(dev, st01, i, 0);
108
	I915_READ8(st01);
109
	I915_WRITE8(VGA_AR_INDEX, dev_priv->regfile.saveAR_INDEX);
110 111 112 113
	I915_READ8(st01);

	/* Graphics controller registers */
	for (i = 0; i < 9; i++)
114
		dev_priv->regfile.saveGR[i] =
115 116
			i915_read_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, i);

117
	dev_priv->regfile.saveGR[0x10] =
118
		i915_read_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, 0x10);
119
	dev_priv->regfile.saveGR[0x11] =
120
		i915_read_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, 0x11);
121
	dev_priv->regfile.saveGR[0x18] =
122 123 124 125
		i915_read_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, 0x18);

	/* Sequencer registers */
	for (i = 0; i < 8; i++)
126
		dev_priv->regfile.saveSR[i] =
127 128 129 130 131 132 133 134 135
			i915_read_indexed(dev, VGA_SR_INDEX, VGA_SR_DATA, i);
}

static void i915_restore_vga(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	int i;
	u16 cr_index, cr_data, st01;

136
	/* VGA state */
137
	I915_WRITE(i915_vgacntrl_reg(dev), dev_priv->regfile.saveVGACNTRL);
138 139 140 141 142 143 144

	I915_WRITE(VGA0, dev_priv->regfile.saveVGA0);
	I915_WRITE(VGA1, dev_priv->regfile.saveVGA1);
	I915_WRITE(VGA_PD, dev_priv->regfile.saveVGA_PD);
	POSTING_READ(VGA_PD);
	udelay(150);

145
	/* MSR bits */
146 147
	I915_WRITE8(VGA_MSR_WRITE, dev_priv->regfile.saveMSR);
	if (dev_priv->regfile.saveMSR & VGA_MSR_CGA_MODE) {
148 149 150 151 152 153 154 155 156 157 158 159
		cr_index = VGA_CR_INDEX_CGA;
		cr_data = VGA_CR_DATA_CGA;
		st01 = VGA_ST01_CGA;
	} else {
		cr_index = VGA_CR_INDEX_MDA;
		cr_data = VGA_CR_DATA_MDA;
		st01 = VGA_ST01_MDA;
	}

	/* Sequencer registers, don't write SR07 */
	for (i = 0; i < 7; i++)
		i915_write_indexed(dev, VGA_SR_INDEX, VGA_SR_DATA, i,
160
				   dev_priv->regfile.saveSR[i]);
161 162 163

	/* CRT controller regs */
	/* Enable CR group 0 writes */
164
	i915_write_indexed(dev, cr_index, cr_data, 0x11, dev_priv->regfile.saveCR[0x11]);
165
	for (i = 0; i <= 0x24; i++)
166
		i915_write_indexed(dev, cr_index, cr_data, i, dev_priv->regfile.saveCR[i]);
167 168 169 170

	/* Graphics controller regs */
	for (i = 0; i < 9; i++)
		i915_write_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, i,
171
				   dev_priv->regfile.saveGR[i]);
172 173

	i915_write_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, 0x10,
174
			   dev_priv->regfile.saveGR[0x10]);
175
	i915_write_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, 0x11,
176
			   dev_priv->regfile.saveGR[0x11]);
177
	i915_write_indexed(dev, VGA_GR_INDEX, VGA_GR_DATA, 0x18,
178
			   dev_priv->regfile.saveGR[0x18]);
179 180 181 182

	/* Attribute controller registers */
	I915_READ8(st01); /* switch back to index mode */
	for (i = 0; i <= 0x14; i++)
183
		i915_write_ar(dev, st01, i, dev_priv->regfile.saveAR[i], 0);
184
	I915_READ8(st01); /* switch back to index mode */
185
	I915_WRITE8(VGA_AR_INDEX, dev_priv->regfile.saveAR_INDEX | 0x20);
186 187 188
	I915_READ8(st01);

	/* VGA color palette registers */
189
	I915_WRITE8(VGA_DACMASK, dev_priv->regfile.saveDACMASK);
190 191
}

192
static void i915_save_display(struct drm_device *dev)
193 194 195 196
{
	struct drm_i915_private *dev_priv = dev->dev_private;

	/* Display arbitration control */
197 198
	if (INTEL_INFO(dev)->gen <= 4)
		dev_priv->regfile.saveDSPARB = I915_READ(DSPARB);
199 200

	/* This is only meaningful in non-KMS mode */
201
	/* Don't regfile.save them in KMS mode */
202
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
203
		i915_save_display_reg(dev);
204

205
	/* LVDS state */
206
	if (HAS_PCH_SPLIT(dev)) {
207
		dev_priv->regfile.savePP_CONTROL = I915_READ(PCH_PP_CONTROL);
208 209
		if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
			dev_priv->regfile.saveLVDS = I915_READ(PCH_LVDS);
210 211 212 213 214
	} else if (IS_VALLEYVIEW(dev)) {
		dev_priv->regfile.saveBLC_HIST_CTL =
			I915_READ(VLV_BLC_HIST_CTL(PIPE_A));
		dev_priv->regfile.saveBLC_HIST_CTL_B =
			I915_READ(VLV_BLC_HIST_CTL(PIPE_B));
215
	} else {
216 217
		dev_priv->regfile.savePP_CONTROL = I915_READ(PP_CONTROL);
		dev_priv->regfile.saveBLC_HIST_CTL = I915_READ(BLC_HIST_CTL);
218
		if (IS_MOBILE(dev) && !IS_I830(dev))
219
			dev_priv->regfile.saveLVDS = I915_READ(LVDS);
220 221
	}

222
	if (HAS_PCH_SPLIT(dev)) {
223 224 225
		dev_priv->regfile.savePP_ON_DELAYS = I915_READ(PCH_PP_ON_DELAYS);
		dev_priv->regfile.savePP_OFF_DELAYS = I915_READ(PCH_PP_OFF_DELAYS);
		dev_priv->regfile.savePP_DIVISOR = I915_READ(PCH_PP_DIVISOR);
226
	} else if (!IS_VALLEYVIEW(dev)) {
227 228 229
		dev_priv->regfile.savePP_ON_DELAYS = I915_READ(PP_ON_DELAYS);
		dev_priv->regfile.savePP_OFF_DELAYS = I915_READ(PP_OFF_DELAYS);
		dev_priv->regfile.savePP_DIVISOR = I915_READ(PP_DIVISOR);
230
	}
231

232 233 234
	/* save FBC interval */
	if (HAS_FBC(dev) && INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev))
		dev_priv->regfile.saveFBC_CONTROL = I915_READ(FBC_CONTROL);
235

236 237
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
		i915_save_vga(dev);
238 239
}

240
static void i915_restore_display(struct drm_device *dev)
241 242
{
	struct drm_i915_private *dev_priv = dev->dev_private;
243
	u32 mask = 0xffffffff;
244

245
	/* Display arbitration */
246 247
	if (INTEL_INFO(dev)->gen <= 4)
		I915_WRITE(DSPARB, dev_priv->regfile.saveDSPARB);
248

249
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
250
		i915_restore_display_reg(dev);
251

252 253 254
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		mask = ~LVDS_PORT_EN;

255
	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
256
		I915_WRITE(PCH_LVDS, dev_priv->regfile.saveLVDS & mask);
257
	else if (INTEL_INFO(dev)->gen <= 4 && IS_MOBILE(dev) && !IS_I830(dev))
258
		I915_WRITE(LVDS, dev_priv->regfile.saveLVDS & mask);
259

260
	if (HAS_PCH_SPLIT(dev)) {
261 262 263 264
		I915_WRITE(PCH_PP_ON_DELAYS, dev_priv->regfile.savePP_ON_DELAYS);
		I915_WRITE(PCH_PP_OFF_DELAYS, dev_priv->regfile.savePP_OFF_DELAYS);
		I915_WRITE(PCH_PP_DIVISOR, dev_priv->regfile.savePP_DIVISOR);
		I915_WRITE(PCH_PP_CONTROL, dev_priv->regfile.savePP_CONTROL);
265 266 267 268 269
	} else if (IS_VALLEYVIEW(dev)) {
		I915_WRITE(VLV_BLC_HIST_CTL(PIPE_A),
			   dev_priv->regfile.saveBLC_HIST_CTL);
		I915_WRITE(VLV_BLC_HIST_CTL(PIPE_B),
			   dev_priv->regfile.saveBLC_HIST_CTL);
270
	} else {
271 272 273 274 275
		I915_WRITE(BLC_HIST_CTL, dev_priv->regfile.saveBLC_HIST_CTL);
		I915_WRITE(PP_ON_DELAYS, dev_priv->regfile.savePP_ON_DELAYS);
		I915_WRITE(PP_OFF_DELAYS, dev_priv->regfile.savePP_OFF_DELAYS);
		I915_WRITE(PP_DIVISOR, dev_priv->regfile.savePP_DIVISOR);
		I915_WRITE(PP_CONTROL, dev_priv->regfile.savePP_CONTROL);
276
	}
277

278
	/* only restore FBC info on the platform that supports FBC*/
279
	intel_disable_fbc(dev);
280 281 282 283

	/* restore FBC interval */
	if (HAS_FBC(dev) && INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev))
		I915_WRITE(FBC_CONTROL, dev_priv->regfile.saveFBC_CONTROL);
284

285 286
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
		i915_restore_vga(dev);
287
	else
288
		i915_redisable_vga(dev);
289 290 291 292 293 294 295
}

int i915_save_state(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	int i;

296 297
	mutex_lock(&dev->struct_mutex);

298 299
	i915_save_display(dev);

300 301 302
	if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
		/* Interrupt state */
		if (HAS_PCH_SPLIT(dev)) {
303 304 305 306 307 308 309
			dev_priv->regfile.saveDEIER = I915_READ(DEIER);
			dev_priv->regfile.saveDEIMR = I915_READ(DEIMR);
			dev_priv->regfile.saveGTIER = I915_READ(GTIER);
			dev_priv->regfile.saveGTIMR = I915_READ(GTIMR);
			dev_priv->regfile.saveFDI_RXA_IMR = I915_READ(_FDI_RXA_IMR);
			dev_priv->regfile.saveFDI_RXB_IMR = I915_READ(_FDI_RXB_IMR);
			dev_priv->regfile.saveMCHBAR_RENDER_STANDBY =
310
				I915_READ(RSTDBYCTL);
311
			dev_priv->regfile.savePCH_PORT_HOTPLUG = I915_READ(PCH_PORT_HOTPLUG);
312
		} else {
313 314
			dev_priv->regfile.saveIER = I915_READ(IER);
			dev_priv->regfile.saveIMR = I915_READ(IMR);
315
		}
316
	}
317 318

	/* Cache mode state */
319 320
	if (INTEL_INFO(dev)->gen < 7)
		dev_priv->regfile.saveCACHE_MODE_0 = I915_READ(CACHE_MODE_0);
321 322

	/* Memory Arbitration state */
323
	dev_priv->regfile.saveMI_ARB_STATE = I915_READ(MI_ARB_STATE);
324 325 326

	/* Scratch space */
	for (i = 0; i < 16; i++) {
327 328
		dev_priv->regfile.saveSWF0[i] = I915_READ(SWF00 + (i << 2));
		dev_priv->regfile.saveSWF1[i] = I915_READ(SWF10 + (i << 2));
329 330
	}
	for (i = 0; i < 3; i++)
331
		dev_priv->regfile.saveSWF2[i] = I915_READ(SWF30 + (i << 2));
332

333 334
	mutex_unlock(&dev->struct_mutex);

335 336 337 338 339 340 341 342
	return 0;
}

int i915_restore_state(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	int i;

343 344
	mutex_lock(&dev->struct_mutex);

345
	i915_gem_restore_fences(dev);
346 347
	i915_restore_display(dev);

348 349 350
	if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
		/* Interrupt state */
		if (HAS_PCH_SPLIT(dev)) {
351 352 353 354 355 356 357
			I915_WRITE(DEIER, dev_priv->regfile.saveDEIER);
			I915_WRITE(DEIMR, dev_priv->regfile.saveDEIMR);
			I915_WRITE(GTIER, dev_priv->regfile.saveGTIER);
			I915_WRITE(GTIMR, dev_priv->regfile.saveGTIMR);
			I915_WRITE(_FDI_RXA_IMR, dev_priv->regfile.saveFDI_RXA_IMR);
			I915_WRITE(_FDI_RXB_IMR, dev_priv->regfile.saveFDI_RXB_IMR);
			I915_WRITE(PCH_PORT_HOTPLUG, dev_priv->regfile.savePCH_PORT_HOTPLUG);
358 359
			I915_WRITE(RSTDBYCTL,
				   dev_priv->regfile.saveMCHBAR_RENDER_STANDBY);
360
		} else {
361 362
			I915_WRITE(IER, dev_priv->regfile.saveIER);
			I915_WRITE(IMR, dev_priv->regfile.saveIMR);
363
		}
364
	}
365

366
	/* Cache mode state */
367 368 369
	if (INTEL_INFO(dev)->gen < 7)
		I915_WRITE(CACHE_MODE_0, dev_priv->regfile.saveCACHE_MODE_0 |
			   0xffff0000);
370 371

	/* Memory arbitration state */
372
	I915_WRITE(MI_ARB_STATE, dev_priv->regfile.saveMI_ARB_STATE | 0xffff0000);
373 374

	for (i = 0; i < 16; i++) {
375 376
		I915_WRITE(SWF00 + (i << 2), dev_priv->regfile.saveSWF0[i]);
		I915_WRITE(SWF10 + (i << 2), dev_priv->regfile.saveSWF1[i]);
377 378
	}
	for (i = 0; i < 3; i++)
379
		I915_WRITE(SWF30 + (i << 2), dev_priv->regfile.saveSWF2[i]);
380

381 382
	mutex_unlock(&dev->struct_mutex);

383
	intel_i2c_reset(dev);
384

385 386
	return 0;
}