summaryrefslogtreecommitdiff
path: root/linux-core/xgi_cmdlist.c
blob: f8aacea29975253e7fbfa693aca3200a69c438f6 (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
347
348
349
350
351

/****************************************************************************
 * 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_types.h"
#include "xgi_linux.h"
#include "xgi_drv.h"
#include "xgi_regs.h"
#include "xgi_misc.h"
#include "xgi_cmdlist.h"

struct xgi_cmdring_info s_cmdring;

static void addFlush2D(struct xgi_info * info);
static U32 getCurBatchBeginPort(struct xgi_cmd_info * pCmdInfo);
static void triggerHWCommandList(struct xgi_info * info, U32 triggerCounter);
static void xgi_cmdlist_reset(void);

int xgi_cmdlist_initialize(struct xgi_info * info, U32 size)
{
	//struct xgi_mem_req mem_req;
	struct xgi_mem_alloc mem_alloc;

	//mem_req.size = size;

	xgi_pcie_alloc(info, size, PCIE_2D, &mem_alloc);

	if ((mem_alloc.size == 0) && (mem_alloc.hw_addr == 0)) {
		return -1;
	}

	s_cmdring._cmdRingSize = mem_alloc.size;
	s_cmdring._cmdRingBuffer = mem_alloc.hw_addr;
	s_cmdring._cmdRingBusAddr = mem_alloc.bus_addr;
	s_cmdring._lastBatchStartAddr = 0;
	s_cmdring._cmdRingOffset = 0;

	return 1;
}

void xgi_submit_cmdlist(struct xgi_info * info, struct xgi_cmd_info * pCmdInfo)
{
	U32 beginPort;
    /** XGI_INFO("Jong-xgi_submit_cmdlist-Begin \n"); **/

	/* Jong 05/25/2006 */
	/* return; */

	beginPort = getCurBatchBeginPort(pCmdInfo);
	XGI_INFO("Jong-xgi_submit_cmdlist-After getCurBatchBeginPort() \n");

	/* Jong 05/25/2006 */
	/* return; */

	if (s_cmdring._lastBatchStartAddr == 0) {
		U32 portOffset;

		/* Jong 06/13/2006; remove marked for system hang test */
		/* xgi_waitfor_pci_idle(info); */

		/* Jong 06132006; BASE_3D_ENG=0x2800 */
		/* beginPort: 2D: 0x30 */
		portOffset = BASE_3D_ENG + beginPort;

		// Enable PCI Trigger Mode
		XGI_INFO("Jong-xgi_submit_cmdlist-Enable PCI Trigger Mode \n");

		/* Jong 05/25/2006 */
		/* return; */

		/* Jong 06/13/2006; M2REG_AUTO_LINK_SETTING_ADDRESS=0x10 */
		XGI_INFO("Jong-M2REG_AUTO_LINK_SETTING_ADDRESS=0x%lx \n",
			 M2REG_AUTO_LINK_SETTING_ADDRESS);
		XGI_INFO("Jong-M2REG_CLEAR_COUNTERS_MASK=0x%lx \n",
			 M2REG_CLEAR_COUNTERS_MASK);
		XGI_INFO
		    ("Jong-(M2REG_AUTO_LINK_SETTING_ADDRESS << 22)=0x%lx \n",
		     (M2REG_AUTO_LINK_SETTING_ADDRESS << 22));
		XGI_INFO("Jong-M2REG_PCI_TRIGGER_MODE_MASK=0x%lx \n\n",
			 M2REG_PCI_TRIGGER_MODE_MASK);

		/* Jong 06/14/2006; 0x400001a */
		XGI_INFO
		    ("Jong-(M2REG_AUTO_LINK_SETTING_ADDRESS << 22)|M2REG_CLEAR_COUNTERS_MASK|0x08|M2REG_PCI_TRIGGER_MODE_MASK=0x%lx \n",
		     (M2REG_AUTO_LINK_SETTING_ADDRESS << 22) |
		     M2REG_CLEAR_COUNTERS_MASK | 0x08 |
		     M2REG_PCI_TRIGGER_MODE_MASK);
		dwWriteReg(BASE_3D_ENG + M2REG_AUTO_LINK_SETTING_ADDRESS,
			   (M2REG_AUTO_LINK_SETTING_ADDRESS << 22) |
			   M2REG_CLEAR_COUNTERS_MASK | 0x08 |
			   M2REG_PCI_TRIGGER_MODE_MASK);

		/* Jong 05/25/2006 */
		XGI_INFO("Jong-xgi_submit_cmdlist-After dwWriteReg() \n");
		/* return; *//* OK */

		/* Jong 06/14/2006; 0x400000a */
		XGI_INFO
		    ("Jong-(M2REG_AUTO_LINK_SETTING_ADDRESS << 22)|0x08|M2REG_PCI_TRIGGER_MODE_MASK=0x%lx \n",
		     (M2REG_AUTO_LINK_SETTING_ADDRESS << 22) | 0x08 |
		     M2REG_PCI_TRIGGER_MODE_MASK);
		dwWriteReg(BASE_3D_ENG + M2REG_AUTO_LINK_SETTING_ADDRESS,
			   (M2REG_AUTO_LINK_SETTING_ADDRESS << 22) | 0x08 |
			   M2REG_PCI_TRIGGER_MODE_MASK);

		// Send PCI begin command
		XGI_INFO("Jong-xgi_submit_cmdlist-Send PCI begin command \n");
		/* return; */

		XGI_INFO("Jong-xgi_submit_cmdlist-portOffset=%d \n",
			 portOffset);
		XGI_INFO("Jong-xgi_submit_cmdlist-beginPort=%d \n", beginPort);

		/* beginPort = 48; */
		/* 0xc100000 */
		dwWriteReg(portOffset,
			   (beginPort << 22) + (BEGIN_VALID_MASK) +
			   pCmdInfo->_curDebugID);
		XGI_INFO("Jong-(beginPort<<22)=0x%lx \n", (beginPort << 22));
		XGI_INFO("Jong-(BEGIN_VALID_MASK)=0x%lx \n", BEGIN_VALID_MASK);
		XGI_INFO("Jong- pCmdInfo->_curDebugID=0x%lx \n",
			 pCmdInfo->_curDebugID);
		XGI_INFO
		    ("Jong- (beginPort<<22) + (BEGIN_VALID_MASK) + pCmdInfo->_curDebugID=0x%lx \n",
		     (beginPort << 22) + (BEGIN_VALID_MASK) +
		     pCmdInfo->_curDebugID);
		XGI_INFO
		    ("Jong-xgi_submit_cmdlist-Send PCI begin command- After \n");
		/* return; *//* OK */

		/* 0x80000024 */
		dwWriteReg(portOffset + 4,
			   BEGIN_LINK_ENABLE_MASK + pCmdInfo->_firstSize);
		XGI_INFO("Jong- BEGIN_LINK_ENABLE_MASK=0x%lx \n",
			 BEGIN_LINK_ENABLE_MASK);
		XGI_INFO("Jong- pCmdInfo->_firstSize=0x%lx \n",
			 pCmdInfo->_firstSize);
		XGI_INFO
		    ("Jong-  BEGIN_LINK_ENABLE_MASK + pCmdInfo->_firstSize=0x%lx \n",
		     BEGIN_LINK_ENABLE_MASK + pCmdInfo->_firstSize);
		XGI_INFO("Jong-xgi_submit_cmdlist-dwWriteReg-1 \n");

		/* 0x1010000 */
		dwWriteReg(portOffset + 8, (pCmdInfo->_firstBeginAddr >> 4));
		XGI_INFO("Jong-  pCmdInfo->_firstBeginAddr=0x%lx \n",
			 pCmdInfo->_firstBeginAddr);
		XGI_INFO("Jong-  (pCmdInfo->_firstBeginAddr >> 4)=0x%lx \n",
			 (pCmdInfo->_firstBeginAddr >> 4));
		XGI_INFO("Jong-xgi_submit_cmdlist-dwWriteReg-2 \n");

		/* Jong 06/13/2006 */
		xgi_dump_register(info);

		/* Jong 06/12/2006; system hang; marked for test */
		dwWriteReg(portOffset + 12, 0);
		XGI_INFO("Jong-xgi_submit_cmdlist-dwWriteReg-3 \n");

		/* Jong 06/13/2006; remove marked for system hang test */
		/* xgi_waitfor_pci_idle(info); */
	} else {
		U32 *lastBatchVirtAddr;

		XGI_INFO
		    ("Jong-xgi_submit_cmdlist-s_cmdring._lastBatchStartAddr != 0 \n");

		/* Jong 05/25/2006 */
		/* return; */

		if (pCmdInfo->_firstBeginType == BTYPE_3D) {
			addFlush2D(info);
		}

		lastBatchVirtAddr =
		    (U32 *) xgi_find_pcie_virt(info,
					       s_cmdring._lastBatchStartAddr);

		/* lastBatchVirtAddr should *never* be NULL.  However, there
		 * are currently some bugs that cause this to happen.  The
		 * if-statement here prevents some fatal (i.e., hard lock
		 * requiring the reset button) oopses.
		 */
		if (lastBatchVirtAddr) {
			lastBatchVirtAddr[1] =
				BEGIN_LINK_ENABLE_MASK + pCmdInfo->_firstSize;
			lastBatchVirtAddr[2] = pCmdInfo->_firstBeginAddr >> 4;
			lastBatchVirtAddr[3] = 0;
			//barrier();
			lastBatchVirtAddr[0] =
				(beginPort << 22) + (BEGIN_VALID_MASK) +
				(0xffff & pCmdInfo->_curDebugID);

			/* Jong 06/12/2006; system hang; marked for test */
			triggerHWCommandList(info, pCmdInfo->_beginCount);
		}

		XGI_INFO
		    ("Jong-xgi_submit_cmdlist-s_cmdring._lastBatchStartAddr != 0 - End\n");
	}

	s_cmdring._lastBatchStartAddr = pCmdInfo->_lastBeginAddr;
	XGI_INFO("Jong-xgi_submit_cmdlist-End \n");
}

/*
    state:      0 - console
                1 - graphic
                2 - fb
                3 - logout
*/
void xgi_state_change(struct xgi_info * info, struct xgi_state_info * pStateInfo)
{
#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 ((pStateInfo->_fromState == STATE_GRAPHIC)
	    && (pStateInfo->_toState == STATE_CONSOLE)) {
		XGI_INFO("[kd] I see, now is to leaveVT\n");
		// stop to received batch
	} else if ((pStateInfo->_fromState == STATE_CONSOLE)
		   && (pStateInfo->_toState == STATE_GRAPHIC)) {
		XGI_INFO("[kd] I see, now is to enterVT\n");
		xgi_cmdlist_reset();
	} else if ((pStateInfo->_fromState == STATE_GRAPHIC)
		   && ((pStateInfo->_toState == STATE_LOGOUT)
		       || (pStateInfo->_toState == STATE_REBOOT)
		       || (pStateInfo->_toState == STATE_SHUTDOWN))) {
		XGI_INFO("[kd] I see, not is to exit from X\n");
		// stop to received batch
	} else {
		XGI_ERROR("[kd] Should not happen\n");
	}

}

void xgi_cmdlist_reset(void)
{
	s_cmdring._lastBatchStartAddr = 0;
	s_cmdring._cmdRingOffset = 0;
}

void xgi_cmdlist_cleanup(struct xgi_info * info)
{
	if (s_cmdring._cmdRingBuffer != 0) {
		xgi_pcie_free(info, s_cmdring._cmdRingBusAddr);
		s_cmdring._cmdRingBuffer = 0;
		s_cmdring._cmdRingOffset = 0;
		s_cmdring._cmdRingSize = 0;
	}
}

static void triggerHWCommandList(struct xgi_info * info, U32 triggerCounter)
{
	static U32 s_triggerID = 1;

	//Fix me, currently we just trigger one time
	while (triggerCounter--) {
		dwWriteReg(BASE_3D_ENG + M2REG_PCI_TRIGGER_REGISTER_ADDRESS,
			   0x05000000 + (0xffff & s_triggerID++));
		// xgi_waitfor_pci_idle(info);
	}
}

static U32 getCurBatchBeginPort(struct xgi_cmd_info * pCmdInfo)
{
	// Convert the batch type to begin port ID
	switch (pCmdInfo->_firstBeginType) {
	case BTYPE_2D:
		return 0x30;
	case BTYPE_3D:
		return 0x40;
	case BTYPE_FLIP:
		return 0x50;
	case BTYPE_CTRL:
		return 0x20;
	default:
		//ASSERT(0);
		return 0xff;
	}
}

static void addFlush2D(struct xgi_info * info)
{
	U32 *flushBatchVirtAddr;
	U32 flushBatchHWAddr;

	U32 *lastBatchVirtAddr;

	/* check buf is large enough to contain a new flush batch */
	if ((s_cmdring._cmdRingOffset + 0x20) >= s_cmdring._cmdRingSize) {
		s_cmdring._cmdRingOffset = 0;
	}

	flushBatchHWAddr = s_cmdring._cmdRingBuffer + s_cmdring._cmdRingOffset;
	flushBatchVirtAddr = (U32 *) xgi_find_pcie_virt(info, flushBatchHWAddr);

	/* not using memcpy for I assume the address is discrete */
	*(flushBatchVirtAddr + 0) = 0x10000000;
	*(flushBatchVirtAddr + 1) = 0x80000004;	/* size = 0x04 dwords */
	*(flushBatchVirtAddr + 2) = 0x00000000;
	*(flushBatchVirtAddr + 3) = 0x00000000;
	*(flushBatchVirtAddr + 4) = FLUSH_2D;
	*(flushBatchVirtAddr + 5) = FLUSH_2D;
	*(flushBatchVirtAddr + 6) = FLUSH_2D;
	*(flushBatchVirtAddr + 7) = FLUSH_2D;

	// ASSERT(s_cmdring._lastBatchStartAddr != NULL);
	lastBatchVirtAddr =
	    (U32 *) xgi_find_pcie_virt(info, s_cmdring._lastBatchStartAddr);

	lastBatchVirtAddr[1] = BEGIN_LINK_ENABLE_MASK + 0x08;
	lastBatchVirtAddr[2] = flushBatchHWAddr >> 4;
	lastBatchVirtAddr[3] = 0;

	//barrier();

	// BTYPE_CTRL & NO debugID
	lastBatchVirtAddr[0] = (0x20 << 22) + (BEGIN_VALID_MASK);

	triggerHWCommandList(info, 1);

	s_cmdring._cmdRingOffset += 0x20;
	s_cmdring._lastBatchStartAddr = flushBatchHWAddr;
}