aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/state/power/cmn/igd_pwr.c
blob: e26f823e52832b1fb144717f7cfa7a9368dda2f0 (plain)
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
27
28
29
30
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: igd_pwr.c
 * $Revision: 1.7 $
 *-----------------------------------------------------------------------------
 * Copyright © 2002-2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *-----------------------------------------------------------------------------
 * Description:
 *  
 *-----------------------------------------------------------------------------
 */

#define MODULE_NAME hal.power

#include <config.h>
#include <io.h>

#include <igd_init.h>
#include <igd_errno.h>
#include <igd_pwr.h>
#include <igd_mode.h>

#include <module_init.h>
#include <mode.h>
#include <dsp.h>
#include <intelpci.h>
#include <dispatch.h>
#include "pwr_dispatch.h"           /* contains the power dispatch functions */

/*!
 * @addtogroup state_group
 * @{
 */

static dispatch_table_t pwr_dispatch_list[] = {
#ifdef CONFIG_PLB
	{PCI_DEVICE_ID_VGA_PLB, &pwr_dispatch_plb},
#endif
#ifdef CONFIG_TNC
	{PCI_DEVICE_ID_VGA_TNC, &pwr_dispatch_plb},
#endif
	{0, NULL}
};

typedef struct _power_context {
	pwr_dispatch_t *dispatch;
	void *power_state;
} power_context_t;

static power_context_t power_context;

static int igd_pwr_query(igd_driver_h driver_handle, unsigned int power_state)
{                                                           /* igd_pwr_query */
	return power_context.dispatch->pwr_query(power_state);
}                                                           /* igd_pwr_query */

/*!
 * This function alters the power state of the graphics device.  This
 * function saves and restores registers as necessary and changes the
 * power states of the mode and overlay module.
 * 
 * This function will convert a request for D2 to a request for D1.
 * 
 * @param driver_handle driver context
 * @param dwPowerState power state to set the device to.
 *
 * @return < 0 on Error
 * @return 0 on Success
 */
static int igd_pwr_alter(igd_driver_h driver_handle, unsigned int dwPowerState)
{                                                           /* igd_pwr_alter */
	unsigned char *mmio;
	int           retval   = 0;
	igd_context_t *context = (igd_context_t *) driver_handle;

	EMGD_DEBUG("in igd_pwr_alter");

	EMGD_DEBUG("Power State requested: 0x%x", dwPowerState);

	if(context == NULL) {
		EMGD_ERROR("In igd_pwr_alter:-Device context is null");
		return -IGD_ERROR_INVAL;
	}

	if(power_context.power_state == NULL) {
		EMGD_DEBUG("In igd_pwr_alter:- Memory not allocated yet."
				" Power Module Not Initialised");
		return -IGD_ERROR_INVAL;
	}

	mmio = context->device_context.virt_mmadr;

	if(context->device_context.power_state == dwPowerState ) {
		EMGD_DEBUG("Already in the present state");
		return -IGD_ERROR_INVAL;
	}

	switch(dwPowerState) {
	case IGD_POWERSTATE_D0:
		/* Do any chipset specific power management */
		retval = power_context.dispatch->pwr_set(context, dwPowerState);

		/* restore the registers */
		retval = context->mod_dispatch.reg_restore(context,
			power_context.power_state);
		if (retval) {
			return retval;
		}

		/* Officially change the power state after registers are restored */
		context->device_context.power_state = IGD_POWERSTATE_D0;

		/* restore power to plane, pipe and port */
		if(context->mod_dispatch.mode_pwr) {
			retval = context->mod_dispatch.mode_pwr(context,
				IGD_POWERSTATE_D0);
			if (retval) {
				return retval;
			}
		}

		/* enable overlay */
		if(context->mod_dispatch.overlay_pwr) {
			context->mod_dispatch.overlay_pwr(context, IGD_POWERSTATE_D0);
		}
		break;

	case IGD_POWERSTATE_D1:
	case IGD_POWERSTATE_D2:
		/* Standby - ACPI S1 */

		if (IGD_POWERSTATE_D2 == dwPowerState) {
			dwPowerState = IGD_POWERSTATE_D1;
		}

		/* Turn off the overlay */
		if(context->mod_dispatch.overlay_pwr) {
			context->mod_dispatch.overlay_pwr(context, dwPowerState);
		}

		/* save registers */
		retval = context->mod_dispatch.reg_save(context,
			power_context.power_state);
		if (retval) {
			return retval;
		}

		/* Change the state of the device to Dx.  This is required so the
		 * plane/pipe/port all use this.  This needs to happen after "reg_save"
		 * because igd_sync will timeout if power state is not D0
		 */
		context->device_context.power_state = dwPowerState;

		if(context->mod_dispatch.mode_pwr) {
			retval = context->mod_dispatch.mode_pwr(context, dwPowerState);
			if (retval) {
				return retval;
			}
		}

		/* Do any chipset specific power management */
		retval = power_context.dispatch->pwr_set(context, dwPowerState);

		break;

	case IGD_POWERSTATE_D3:
		/* Suspend to memory - ACPI S3 */

		/* disable overlay */
		if(context->mod_dispatch.overlay_pwr) {
			context->mod_dispatch.overlay_pwr(context, dwPowerState);
		}

		/* save registers */
		retval= context->mod_dispatch.reg_save(context,
			power_context.power_state);
		if (retval) {
			return retval;
		}

		/* Change the state of the device to Dx.  This is required so the
		 * plane/pipe/port all use this.  This needs to happen after "reg_save"
		 * because igd_sync will timeout if power state is not D0
		 */
		context->device_context.power_state = dwPowerState;

		/* Mode module Power */
		if(context->mod_dispatch.mode_pwr) {
			retval = context->mod_dispatch.mode_pwr(context, dwPowerState);
			if (retval) {
				return retval;
			}
		}


		/* Do any chipset specific power management */
		retval = power_context.dispatch->pwr_set(context, dwPowerState);

		break;

	default:
		/* state undefined */
		EMGD_ERROR("In igd_pwr_alter:-Undefined Power State");
		break;

	}

	return retval;
}                                                           /* igd_pwr_alter */

/*!
 * This function should be called from context manager.
 * To delete the memory allocated ealier.
 * 
 * @param context
 *
 * @return void
 */
static void _pwr_shutdown(igd_context_t *context)
{

	if(power_context.power_state) {
		context->mod_dispatch.reg_free(context, power_context.power_state);
	}
	power_context.power_state = NULL;
	context->device_context.power_state = IGD_POWERSTATE_UNDEFINED;
	return;
}

/*!
 * This function should only be called only once from context manager .
 * It initializes the power module.
 * 
 * @param context
 *
 * @return < 0 on Error
 * @return 0 on Success
 */
int _pwr_init(igd_context_t *context)
{                                                               /* _pwr_init */
	EMGD_ASSERT(context, "Null Context", -IGD_ERROR_INVAL);

	/* Hook up the IGD dispatch table entires for power */
	context->dispatch.pwr_alter = igd_pwr_alter;
	context->dispatch.pwr_query = igd_pwr_query;

	/* Inter-module dispatch */
	context->mod_dispatch.pwr_shutdown = _pwr_shutdown;

	power_context.dispatch = (pwr_dispatch_t *)dispatch_acquire(context,
		pwr_dispatch_list);

	if(!power_context.dispatch) {
		return -IGD_ERROR_NODEV;
	}

	/* Initialize chipset specific default power behavior */
	power_context.dispatch->pwr_init(context);

	power_context.power_state = context->mod_dispatch.reg_alloc(context,
		IGD_REG_SAVE_VGA |
		IGD_REG_SAVE_DAC | IGD_REG_SAVE_MMIO | IGD_REG_SAVE_RB |
		IGD_REG_SAVE_MODE | IGD_REG_SAVE_3D );

	if(!power_context.power_state) {
		return -IGD_ERROR_NOMEM;
	}

	return 0;
}                                                               /* _pwr_init */

/*-----------------------------------------------------------------------------
 * $Id: igd_pwr.c,v 1.7 2010/07/23 16:54:50 bpaauwe Exp $
 * $Source: /nfs/fm/proj/eia/cvsroot/koheo/linux/egd_drm/emgd/state/power/cmn/igd_pwr.c,v $
 *-----------------------------------------------------------------------------
 */