summaryrefslogtreecommitdiff
path: root/linux-core/xgi_cmdlist.c
blob: a728c0eff6b718c6eac64acd4e417ed7509f6c4f (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
/****************************************************************************
 * Copyright (C) 2003-2006 by XGI Technology, Taiwan.
 *
 * All Rights Reserved.
 *
 * 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 on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, 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
 * XGI 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.
 ***************************************************************************/

#include "xgi_drv.h"
#include "xgi_regs.h"
#include "xgi_misc.h"
#include "xgi_cmdlist.h"

static void xgi_emit_flush(struct xgi_info * info, bool link);
static unsigned int get_batch_command(enum xgi_batch_type type);
static void triggerHWCommandList(struct xgi_info * info);
static void xgi_cmdlist_reset(struct xgi_info * info);


/**
 * Graphic engine register (2d/3d) acessing interface
 */
static inline void dwWriteReg(struct drm_map * map, u32 addr, u32 data)
{
#ifdef XGI_MMIO_DEBUG
	DRM_INFO("mmio_map->handle = 0x%p, addr = 0x%x, data = 0x%x\n",
		 map->handle, addr, data);
#endif
	DRM_WRITE32(map, addr, data);
}


int xgi_cmdlist_initialize(struct xgi_info * info, size_t size,
			   struct drm_file * filp)
{
	struct xgi_mem_alloc mem_alloc = {
		.location = XGI_MEMLOC_NON_LOCAL,
		.size = size,
	};
	int err;

	err = xgi_alloc(info, &mem_alloc, filp);
	if (err) {
		return err;
	}

	info->cmdring.ptr = xgi_find_pcie_virt(info, mem_alloc.hw_addr);
	info->cmdring.size = mem_alloc.size;
	info->cmdring.ring_hw_base = mem_alloc.hw_addr;
	info->cmdring.last_ptr = NULL;
	info->cmdring.ring_offset = 0;

	return 0;
}


/**
 * get_batch_command - Get the command ID for the current begin type.
 * @type: Type of the current batch
 *
 * See section 3.2.2 "Begin" (page 15) of the 3D SPG.
 * 
 * This function assumes that @type is on the range [0,3].
 */
unsigned int get_batch_command(enum xgi_batch_type type)
{
	static const unsigned int ports[4] = {
		0x30 >> 2, 0x40 >> 2, 0x50 >> 2, 0x20 >> 2
	};
	
	return ports[type];
}


int xgi_submit_cmdlist(struct drm_device * dev, void * data,
		       struct drm_file * filp)
{
	struct xgi_info *const info = dev->dev_private;
	const struct xgi_cmd_info *const pCmdInfo =
		(struct xgi_cmd_info *) data;
	const unsigned int cmd = get_batch_command(pCmdInfo->type);
	u32 begin[4];


	begin[0] = (cmd << 24) | BEGIN_VALID_MASK
		| (BEGIN_BEGIN_IDENTIFICATION_MASK & pCmdInfo->id);
	begin[1] = BEGIN_LINK_ENABLE_MASK | pCmdInfo->size;
	begin[2] = pCmdInfo->hw_addr >> 4;
	begin[3] = 0;

	if (info->cmdring.last_ptr == NULL) {
		const unsigned int portOffset = BASE_3D_ENG + (cmd << 2);


		/* Enable PCI Trigger Mode
		 */
		dwWriteReg(info->mmio_map,
			   BASE_3D_ENG + M2REG_AUTO_LINK_SETTING_ADDRESS,
			   (M2REG_AUTO_LINK_SETTING_ADDRESS << 22) |
			   M2REG_CLEAR_COUNTERS_MASK | 0x08 |
			   M2REG_PCI_TRIGGER_MODE_MASK);

		dwWriteReg(info->mmio_map,
			   BASE_3D_ENG + M2REG_AUTO_LINK_SETTING_ADDRESS,
			   (M2REG_AUTO_LINK_SETTING_ADDRESS << 22) | 0x08 |
			   M2REG_PCI_TRIGGER_MODE_MASK);


		/* Send PCI begin command
		 */
		dwWriteReg(info->mmio_map, portOffset,      begin[0]);
		dwWriteReg(info->mmio_map, portOffset +  4, begin[1]);
		dwWriteReg(info->mmio_map, portOffset +  8, begin[2]);
		dwWriteReg(info->mmio_map, portOffset + 12, begin[3]);
	} else {
		DRM_DEBUG("info->cmdring.last_ptr != NULL\n");

		if (pCmdInfo->type == BTYPE_3D) {
			xgi_emit_flush(info, TRUE);
		}

		info->cmdring.last_ptr[1] = begin[1];
		info->cmdring.last_ptr[2] = begin[2];
		info->cmdring.last_ptr[3] = begin[3];
		wmb();
		info->cmdring.last_ptr[0] = begin[0];

		triggerHWCommandList(info);
	}

	info->cmdring.last_ptr = xgi_find_pcie_virt(info, pCmdInfo->hw_addr);
	return 0;
}


/*
    state:      0 - console
                1 - graphic
                2 - fb
                3 - logout
*/
int xgi_state_change(struct xgi_info * info, unsigned int to, 
		     unsigned int from)
{
#define STATE_CONSOLE   0
#define STATE_GRAPHIC   1
#define STATE_FBTERM    2
#define STATE_LOGOUT    3
#define STATE_REBOOT    4
#define STATE_SHUTDOWN  5

	if ((from == STATE_GRAPHIC) && (to == STATE_CONSOLE)) {
		DRM_INFO("Leaving graphical mode (probably VT switch)\n");
	} else if ((from == STATE_CONSOLE) && (to == STATE_GRAPHIC)) {
		DRM_INFO("Entering graphical mode (probably VT switch)\n");
		xgi_cmdlist_reset(info);
	} else if ((from == STATE_GRAPHIC)
		   && ((to == STATE_LOGOUT)
		       || (to == STATE_REBOOT)
		       || (to == STATE_SHUTDOWN))) {
		DRM_INFO("Leaving graphical mode (probably X shutting down)\n");
	} else {
		DRM_ERROR("Invalid state change.\n");
		return -EINVAL;
	}

	return 0;
}


int xgi_state_change_ioctl(struct drm_device * dev, void * data,
			   struct drm_file * filp)
{
	struct xgi_state_info *const state =
		(struct xgi_state_info *) data;
	struct xgi_info *info = dev->dev_private;


	return xgi_state_change(info, state->_toState, state->_fromState);
}


void xgi_cmdlist_reset(struct xgi_info * info)
{
	info->cmdring.last_ptr = NULL;
	info->cmdring.ring_offset = 0;
}


void xgi_cmdlist_cleanup(struct xgi_info * info)
{
	if (info->cmdring.ring_hw_base != 0) {
		/* If command lists have been issued, terminate the command
		 * list chain with a flush command.
		 */
		if (info->cmdring.last_ptr != NULL) {
			xgi_emit_flush(info, FALSE);
			xgi_waitfor_pci_idle(info);
		}

		(void) memset(&info->cmdring, 0, sizeof(info->cmdring));
	}
}

static void triggerHWCommandList(struct xgi_info * info)
{
	static unsigned int s_triggerID = 1;

	dwWriteReg(info->mmio_map,
		   BASE_3D_ENG + M2REG_PCI_TRIGGER_REGISTER_ADDRESS,
		   0x05000000 + (0x0ffff & s_triggerID++));
}


/**
 * Emit a flush to the CRTL command stream.
 * @info XGI info structure
 * @link Emit (or don't emit) link information at start of flush command.
 *
 * This function assumes info->cmdring.ptr is non-NULL.
 */
static void xgi_emit_flush(struct xgi_info * info, bool link)
{
	static const u32 flush_command[8] = {
		(0x10 << 24),
		BEGIN_LINK_ENABLE_MASK | (0x00004),
		0x00000000, 0x00000000,

		/* Flush everything with the default 32 clock delay.
		 */
		0x003fffff, 0x003fffff, 0x003fffff, 0x003fffff
	};
	const unsigned int base = (link) ? 0 : 4;
	const unsigned int flush_size = (8 - base) * sizeof(u32);
	u32 *batch_addr;
	u32 hw_addr;

	/* check buf is large enough to contain a new flush batch */
	if ((info->cmdring.ring_offset + flush_size) >= info->cmdring.size) {
		info->cmdring.ring_offset = 0;
	}

	hw_addr = info->cmdring.ring_hw_base 
		+ info->cmdring.ring_offset;
	batch_addr = info->cmdring.ptr 
		+ (info->cmdring.ring_offset / 4);

	(void) memcpy(batch_addr, & flush_command[base], flush_size);

	info->cmdring.last_ptr[1] = BEGIN_LINK_ENABLE_MASK | (flush_size / 4);
	info->cmdring.last_ptr[2] = hw_addr >> 4;
	info->cmdring.last_ptr[3] = 0;
	wmb();
	info->cmdring.last_ptr[0] = (get_batch_command(BTYPE_CTRL) << 24) 
		| (BEGIN_VALID_MASK);

	triggerHWCommandList(info);

	info->cmdring.ring_offset += flush_size;
	info->cmdring.last_ptr = (link) ? batch_addr : NULL;
}