aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/state/reg/cmn/reg.c
blob: 9813046b03eb68034eae837937bf0ba3329a64fe (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: reg.c
 * $Revision: 1.11 $
 *-----------------------------------------------------------------------------
 * 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:
 *  This is the implementation file for the register module's main API's called
 *  by external devices.
 *-----------------------------------------------------------------------------
 */

#define MODULE_NAME hal.state

#include <io.h>

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

#include <context.h>
#include <utils.h>
#include <dsp.h>
#include <mode.h>
#include <memory.h>
#include <utils.h>

#include <intelpci.h>
#include <dispatch.h>

#include "reg_dispatch.h"

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

typedef struct _reg_context {
	unsigned long flags;     /* IGD_DRIVER_SAVE_RESTORE */
	reg_dispatch_t *dispatch;
	reg_buffer_t *device_state;
	void *gmm_state;
} reg_context_t;

static reg_context_t reg_ctx[1];


static dispatch_table_t reg_dispatch_list[] = {

#ifdef CONFIG_PLB
	{PCI_DEVICE_ID_VGA_PLB, &reg_dispatch_plb},
#endif
#ifdef CONFIG_TNC
	{PCI_DEVICE_ID_VGA_TNC, &reg_dispatch_tnc},
#endif
	{0, NULL}
};

void *reg_alloc(igd_context_t *context,
	unsigned long flags)
{
	return (void *)reg_ctx->dispatch->reg_alloc(context, flags,
		reg_ctx->dispatch->platform_context);
}

void reg_free(igd_context_t *context,
	void *reg_set)
{
	reg_ctx->dispatch->reg_free(context, (reg_buffer_t *)reg_set,
		reg_ctx->dispatch->platform_context);
}

int reg_save(igd_context_t *context,
	void *reg_set)
{
	return reg_ctx->dispatch->reg_save(context, (reg_buffer_t *)reg_set,
		reg_ctx->dispatch->platform_context);
}

int reg_restore(igd_context_t *context,
	void *reg_set)
{
	return reg_ctx->dispatch->reg_restore(context, (reg_buffer_t *)reg_set,
		reg_ctx->dispatch->platform_context);
}

/*!
 * This function calls reg_save() to save the state of the graphics engine
 * and then reg_restore to restore the previous state.
 *
 * @param driver_handle  our driver context
 *
 * @return 0 on success
 * @return -IGD_INVAL on failure
 */
static int igd_driver_save_restore(igd_driver_h driver_handle)
{
	igd_context_t *context = (igd_context_t *)driver_handle;
	reg_buffer_t *reg_state;
	short restored = 0;
	unsigned long save_flags;

	EMGD_ASSERT(context, "Null driver_handle!\n", -IGD_ERROR_INVAL);

	/*
	 * If the gmm_state exists, the registers are not setup, so we should not
	 * try to save the GTT, the ring buffer and appcontext, or it will cause a
	 * sync which will not be fulfilled, so it will wait forever.  We can
	 * however save everything else.  If the gmm_state is null, then everything
	 * is still setup and we can save everything including the GTT, ring buffer
	 * and appcontext.
	 */
	if (reg_ctx->gmm_state) {
		save_flags = IGD_REG_SAVE_ALL & ~IGD_REG_SAVE_GTT & ~IGD_REG_SAVE_RB;
	} else {
		save_flags = IGD_REG_SAVE_ALL;
	}

	/* Save current state. We have to save the current state first before
	 * restoring the GART bindings. Otherwise we will end up restoring wrong
	 * pages when return back to the graphic mode console */
	reg_state = reg_ctx->dispatch->reg_alloc(context, save_flags,
		reg_ctx->dispatch->platform_context);
	reg_ctx->dispatch->reg_save(context, reg_state,
		reg_ctx->dispatch->platform_context);

	/* Restore GART bindings, if saved */
	if (reg_ctx->gmm_state && context->mod_dispatch.gmm_restore) {
		context->mod_dispatch.gmm_restore(context, reg_ctx->gmm_state);
		reg_ctx->gmm_state = NULL;
		restored = 1;
	}

	/* restore previously saved state */

	/*
	 * NAPA class seems to work much better all the display stuff is
	 * turned off prior to restoring the registers.
	 */
	context->mod_dispatch.mode_reset_plane_pipe_ports(context);


	reg_ctx->dispatch->reg_restore(context, reg_ctx->device_state,
		reg_ctx->dispatch->platform_context);
	reg_ctx->dispatch->reg_free(context, reg_ctx->device_state,
		reg_ctx->dispatch->platform_context);

	reg_ctx->device_state = reg_state;

	/* Free GART bindings, if not already saved */
	if (!restored && !reg_ctx->gmm_state &&
		context->mod_dispatch.gmm_save) {
		context->mod_dispatch.gmm_save(context, &(reg_ctx->gmm_state));
	}

	return 0;
}

/*!
 * This function calls reg_save() to save the state of the register
 * 
 * @param driver_handle our driver context
 * @param flags specifies which states to save
 *
 * @return 0 on success
 * @return -IGD_INVAL on failure
 */
static int igd_driver_save(igd_driver_h driver_handle,
	const unsigned long flags)
{
	igd_context_t *context = (igd_context_t *)driver_handle;
	reg_buffer_t *reg_state;

	EMGD_ASSERT(context, "Null driver_handle!\n", -IGD_ERROR_INVAL);

	/* Save current state */
	reg_state = reg_ctx->dispatch->reg_alloc(context, flags,
		reg_ctx->dispatch->platform_context);
	reg_ctx->dispatch->reg_save(context, reg_state,
		reg_ctx->dispatch->platform_context);

	reg_ctx->device_state = reg_state;

	return 0;
}
/* igd_driver_save */

/*!
 * This function calls reg_restore() to restore previously saved
 * 
 * @param driver_handle our driver context
 *
 * @return 0 on success
 * @return -IGD_INVAL on failure
 */
static int igd_driver_restore(igd_driver_h driver_handle)
{
	igd_context_t *context;

	EMGD_ASSERT(driver_handle, "Null driver_handle!\n", -IGD_ERROR_INVAL);

	context = (igd_context_t *)driver_handle;

	/*
	 * NAPA class seems to work much better all the display stuff is
	 * turned off prior to restoring the registers.
	 */
	context->mod_dispatch.mode_reset_plane_pipe_ports(context);

	/* restore previously saved state */
	reg_ctx->dispatch->reg_restore(context, reg_ctx->device_state,
		reg_ctx->dispatch->platform_context);
	reg_ctx->dispatch->reg_free(context, reg_ctx->device_state,
		reg_ctx->dispatch->platform_context);

	return 0;
}

/*!
 * 
 * @param id
 * @param state
 * @param flags
 *
 * @return 0 on success
 * @return -IGD_ERROR_INVAL on failure
 */
static int reg_get_mod_state(reg_state_id_t id, module_state_h **state,
	unsigned long **flags)
{
	if(!reg_ctx->device_state) {
		return 0;
	}

	*flags = &reg_ctx->device_state->flags;
	switch(id) {
	case REG_MODE_STATE:
		if((**flags & IGD_REG_SAVE_MODE)){
			*state = &reg_ctx->device_state->mode_buffer;
		} else {
			state = NULL;
		}
		break;
	default:
		state = NULL;
		return -IGD_ERROR_INVAL;
	}

	return 0;
}

/*!
 * Free any memory allocated for registry state info.
 * 
 * @param context
 *
 * @return void
 */
void _reg_shutdown(igd_context_t *context)
{

	EMGD_TRACE_ENTER;


	if(reg_ctx->device_state) {
		EMGD_DEBUG("Restoring register values prior to exit...");
		reg_ctx->dispatch->reg_restore(context, reg_ctx->device_state,
			reg_ctx->dispatch->platform_context);
		reg_ctx->dispatch->reg_free(context, reg_ctx->device_state,
			reg_ctx->dispatch->platform_context);

	}
	OS_MEMSET(reg_ctx, 0, sizeof(reg_context_t));

	EMGD_TRACE_EXIT;
}

/*!
 * Initialize the registry state context for registry save
 * /restore operations.
 * 
 * @param context
 * @param flags
 *
 * @return 0 on success
 * @return 1 on failure
 */
int _reg_init(igd_context_t *context, unsigned long flags)
{
	reg_dispatch_t *dispatch;

	EMGD_TRACE_ENTER;

	dispatch = (reg_dispatch_t *)dispatch_acquire(context, reg_dispatch_list);
	if(!dispatch) {
		return -IGD_ERROR_NODEV;
	}

	OS_MEMSET(reg_ctx, 0, sizeof(reg_context_t));

	reg_ctx->dispatch = dispatch;

	/*
	 * Hook up functions in dispatch table.
	 */
	context->dispatch.driver_save_restore = igd_driver_save_restore;
	context->dispatch.driver_save         = igd_driver_save;
	context->dispatch.driver_restore      = igd_driver_restore;
	context->mod_dispatch.reg_get_mod_state = reg_get_mod_state;
	context->mod_dispatch.reg_alloc   = reg_alloc;
	context->mod_dispatch.reg_free    = reg_free;
	context->mod_dispatch.reg_save    = reg_save;
	context->mod_dispatch.reg_restore = reg_restore;
	context->mod_dispatch.reg_shutdown = _reg_shutdown;

	if(flags & IGD_DRIVER_SAVE_RESTORE) {
		reg_ctx->flags |= IGD_DRIVER_SAVE_RESTORE;
		EMGD_DEBUG("Saving Device State");

		reg_ctx->device_state = reg_ctx->dispatch->reg_alloc(context,
			IGD_REG_SAVE_STATE|IGD_REG_SAVE_MODE,
			reg_ctx->dispatch->platform_context);
		reg_ctx->dispatch->reg_save(context, reg_ctx->device_state,
			reg_ctx->dispatch->platform_context);
	}

	EMGD_TRACE_EXIT;

	return 0;
}