summaryrefslogtreecommitdiff
path: root/src/xg47_cmdlist.c
blob: 2bb504041ed4f81d9dd997d17fe8cba8f94d4117 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/***************************************************************************
 * 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.						   *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xf86.h"
#include "xgi.h"
#include "xgi_regs.h"
#include "xg47_regs.h"
#include "xgi_driver.h"
#include "xg47_cmdlist.h"
#include "xgi_misc.h"
#include "xgi_debug.h"

/**
 * Track a batch of commands to be sent to hardware.
 */
struct xg47_batch {
    enum xgi_batch_type type;   /**< Command engine to receive commands. */
    unsigned int request_size;  /**< Number of 32-bit words requested for this
				 * batch. 
				 */
    uint32_t *  begin;          /**< Pointer to first command of batch. */
    uint32_t *  end;            /**< Pointer for writing next coomand */
};


/**
 * Track buffer allocated from DRM for sending commands to hardware.
 */
struct xg47_buffer {
    uint32_t *    ptr;          /**< CPU pointer to buffer base. */
    uint32_t      hw_addr;      /**< Buffer base address from hardware point
				 * of view. 
				 */
    unsigned long bus_addr;     /**< Handle used to release buffer. */
    unsigned int  size;         /**< Size of buffer in 32-bit words. */
};

struct xg47_CmdList
{
    struct xg47_batch current;  /**< Active command buffer. */
    struct xg47_batch previous; /**< Previous buffer sent to hardware. */
    struct xg47_buffer command; /**< Memory allocated from DRM. */

    /**
     * \name 2D Command bunch
     * 
     * Buffer indiviual 2D command register writes.
     */
    /*@{*/
    unsigned bunch_count;       /**< Number of commands pending in \c bunch. */
    uint32_t bunch[4];          /**< Register write data. */
    /*@}*/


    int		_fd;            /**< DRM file handle. */
    
    struct _drmFence  top_fence;
    int top_fence_set;
    struct _drmFence  bottom_fence;
    int bottom_fence_set;
};


struct xg47_CmdList *
xg47_Initialize(ScrnInfoPtr pScrn, unsigned int cmdBufSize, int fd)
{
    struct xg47_CmdList *list = xnfcalloc(sizeof(struct xg47_CmdList), 1);
    int ret;

    list->command.size = cmdBufSize;
    list->_fd = fd;

    if (!XGIPcieMemAllocate(pScrn,
                            list->command.size * sizeof(uint32_t),
                            & list->command.bus_addr,
                            & list->command.hw_addr,
                            (void **) & list->command.ptr)) {
        XGIDebug(DBG_ERROR, "[DBG Error]Allocate CmdList buffer error!\n");
        goto err;
    }

    XGIDebug(DBG_CMDLIST, "cmdBuf VAddr=0x%p  HAddr=0x%p buffsize=0x%x\n",
             list->command.ptr, list->command.hw_addr, list->command.size);

    ret = drmFenceCreate(list->_fd, 0, 0, DRM_FENCE_TYPE_EXE,
                         & list->top_fence);
    if (ret) {
        xf86DrvMsg(0, X_ERROR, "Unable to create top-half fence (%s, %d)!\n",
                   strerror(-ret), -ret);
        goto err;
    }

    drmFenceCreate(list->_fd, 0, 0, DRM_FENCE_TYPE_EXE,
                   & list->bottom_fence);
    if (ret) {
        xf86DrvMsg(0, X_ERROR, "Unable to create bottom-half fence "
                   "(%s, %d)!\n", strerror(-ret), -ret);
        goto err;
    }

    xg47_Reset(list);

    return list;

err:
    xg47_Cleanup(pScrn, list);
    return NULL;
}

void xg47_Cleanup(ScrnInfoPtr pScrn, struct xg47_CmdList *s_pCmdList)
{
    if (s_pCmdList) {
        if (s_pCmdList->top_fence_set) {
            drmFenceWait(s_pCmdList->_fd, 0, & s_pCmdList->top_fence, 0);
        }

        if (s_pCmdList->bottom_fence_set) {
            drmFenceWait(s_pCmdList->_fd, 0, & s_pCmdList->bottom_fence, 0);
        }

        drmFenceUnreference(s_pCmdList->_fd, & s_pCmdList->top_fence);
        drmFenceUnreference(s_pCmdList->_fd, & s_pCmdList->bottom_fence);

        if (s_pCmdList->command.bus_addr) {
            XGIDebug(DBG_CMDLIST, "[DBG Free]cmdBuf VAddr=0x%x  HAddr=0x%x\n",
                     s_pCmdList->command.ptr,
                     s_pCmdList->command.hw_addr);

            XGIPcieMemFree(pScrn, s_pCmdList->command.size * sizeof(uint32_t),
                           s_pCmdList->command.bus_addr,
                           s_pCmdList->command.ptr);
        }

        xfree(s_pCmdList);
    }
}

void xg47_Reset(struct xg47_CmdList *s_pCmdList)
{
    s_pCmdList->previous.begin = s_pCmdList->command.ptr;
    s_pCmdList->previous.end = s_pCmdList->command.ptr;
    s_pCmdList->current.end = 0;
}

/* Implementation Part*/
static void emit_bunch(struct xg47_CmdList *pCmdList);
static void reset_bunch(struct xg47_CmdList *pCmdList);

#ifdef DUMP_COMMAND_BUFFER
static void dumpCommandBuffer(struct xg47_CmdList * pCmdList);
#endif

uint32_t s_emptyBegin[AGPCMDLIST_BEGIN_SIZE] =
{
    0x10000000,     /* 3D Type Begin, Invalid */
    0x80000004,     /* Length = 4;  */
    0x00000000,
    0x00000000
};


/**
 * Reserve space in the command buffer
 * 
 * \param pCmdList  pointer to the command list structure
 * \param size      Size, in DWORDS, of the command
 *
 * \returns
 * 1 -- success 0 -- false
 */
int xg47_BeginCmdListType(struct xg47_CmdList *pCmdList, unsigned req_size,
                          unsigned type)
{
    /* Pad the commmand list to 128-bit alignment and add the begin header.
     */
    const unsigned size = ((req_size + 0x3) & ~0x3) + AGPCMDLIST_BEGIN_SIZE;
    const uint32_t *const mid_point =
        pCmdList->command.ptr + (pCmdList->command.size / 2);
    const uint32_t *const end_point = 
        pCmdList->command.ptr + pCmdList->command.size;
    uint32_t * begin_cmd = pCmdList->previous.end;
    uint32_t *const end_cmd = pCmdList->previous.end + size;


    XGIDebug(DBG_CMDLIST, "[DEBUG] Enter beginCmdList.\n");

    if (size >= pCmdList->command.size) {
        return 0;
    }


    /* If the command spills into the bottom half of the command buffer,
     * wait on the bottom half's fence.
     */
    if ((begin_cmd < mid_point) && (end_cmd > mid_point)) {
        if (pCmdList->bottom_fence_set) {
            drmFenceWait(pCmdList->_fd, 0, & pCmdList->bottom_fence, 0);
            pCmdList->bottom_fence_set = 0;
        }
    } else {
        /* If the command won't fit at the end of the list and we need to wrap
         * back to the top half of the command buffer, wait on the top half's
         * fence.
         *
         * After waiting on the top half's fence, emit the bottom half's
         * fence.
         */
        if (end_cmd > end_point) {
            begin_cmd = pCmdList->command.ptr;

            if (pCmdList->top_fence_set) {
                drmFenceWait(pCmdList->_fd, 0, & pCmdList->top_fence, 0);
                pCmdList->top_fence_set = 0;
            }

            drmFenceEmit(pCmdList->_fd, 0, & pCmdList->bottom_fence, 0);
            pCmdList->bottom_fence_set = 1;
        }
    }


    /* Prepare the begin address of next batch. */
    pCmdList->current.begin = begin_cmd;
    pCmdList->current.end = pCmdList->current.begin;
    pCmdList->current.request_size = size;
    pCmdList->current.type = type;


    /* Prepare next begin */
    xg47_EmitRawCommand(pCmdList, s_emptyBegin);
    reset_bunch(pCmdList);


    XGIDebug(DBG_CMDLIST, "[DEBUG] Leave beginCmdList.\n");
    return 1;
}


/**
 * Copy raw command data into the command buffer.
 *
 * \param pCmdList  Command list control structure.
 * \param cmd       Pointer to 4 32-bit commands.
 *
 * \note
 * This function performs byte-swapping on each 32-bit value on big-endian
 * architectures.
 */
void xg47_EmitRawCommand(struct xg47_CmdList *pCmdList, const uint32_t *cmd)
{
    pCmdList->current.end[0] = BE_SWAP32(cmd[0]);
    pCmdList->current.end[1] = BE_SWAP32(cmd[1]);
    pCmdList->current.end[2] = BE_SWAP32(cmd[2]);
    pCmdList->current.end[3] = BE_SWAP32(cmd[3]);
    pCmdList->current.end += 4;
}


/**
 * Reset the 2D register-write data.
 *
 * \param pCmdList  Command list control structure.
 */
void reset_bunch(struct xg47_CmdList *pCmdList)
{
    pCmdList->bunch[0] = 0x7f000000;
    pCmdList->bunch[1] = 0x00000000;
    pCmdList->bunch[2] = 0x00000000;
    pCmdList->bunch[3] = 0x00000000;
    pCmdList->bunch_count = 0;
}


void emit_bunch(struct xg47_CmdList *pCmdList)
{
    /* Copy the commands from bunch to the command buffer and advance the
     * command buffer write pointer.
     */
    xg47_EmitRawCommand(pCmdList, pCmdList->bunch);

    /* Reset bunch.
     */
    reset_bunch(pCmdList);
}


void xg47_SendGECommand(struct xg47_CmdList *pCmdList, uint32_t addr,
                        uint32_t cmd)
{
    /* Encrypt the command for AGP. */
    const unsigned shift = pCmdList->bunch_count;
    const uint32_t reg = (addr & 0x00ff);

    pCmdList->bunch[0] |= (reg | 1) << (shift << 3);
    pCmdList->bunch[shift + 1]  = cmd;

    /* Bunch finished, Send to HW. */
    pCmdList->bunch_count++;
    if (pCmdList->bunch_count == 3) {
        emit_bunch(pCmdList);
    }
}


#ifdef DUMP_COMMAND_BUFFER
void dumpCommandBuffer(struct xg47_CmdList * pCmdList)
{
    const unsigned int count = pCmdList->current.end
        - pCmdList->current.begin;
    unsigned int i;

    XGIDebug(DBG_FUNCTION,"Entering dumpCommandBuffer\n");

    for (i = 0; i < count; i += 4) {
        XGIDebug(DBG_CMD_BUFFER, "%08p: %08x %08x %08x %08x\n",
                 (pCmdList->current.begin + i),
                 pCmdList->current.begin[i + 0],
                 pCmdList->current.begin[i + 1],
                 pCmdList->current.begin[i + 2],
                 pCmdList->current.begin[i + 3]);
    }

    XGIDebug(DBG_FUNCTION,"Leaving dumpCommandBuffer\n");
}
#endif /* DUMP_COMMAND_BUFFER */


void xg47_EndCmdList(struct xg47_CmdList *pCmdList)
{
    uint32_t beginHWAddr;
    struct xgi_cmd_info submitInfo;
    int err;
    size_t data_count;

    XGIDebug(DBG_FUNCTION, "%s: enter\n", __func__);

    /* If there are any pending commands in bunch, emit the whole batch.
     */
    if (pCmdList->bunch_count != 0) {
        emit_bunch(pCmdList);
    }


    /* Calculate data counter *after* flushing the 2D register-write batch as
     * the flush may change the data count.
     */
    data_count = pCmdList->current.end - pCmdList->current.begin;
    if (data_count == 0) {
        return;
    }

    if (data_count > pCmdList->current.request_size) {
        ErrorF("Too much data written written to command buffer! %u vs. %u\n",
	       data_count, pCmdList->current.request_size);
    }


    beginHWAddr = pCmdList->command.hw_addr
	+ ((intptr_t) pCmdList->current.begin
	   - (intptr_t) pCmdList->command.ptr);

    submitInfo.type = pCmdList->current.type;
    submitInfo.hw_addr = beginHWAddr;
    submitInfo.size = data_count;

    XGIDebug(DBG_FUNCTION, "%s: calling ioctl XGI_IOCTL_SUBMIT_CMDLIST\n", 
             __func__);

#ifdef DUMP_COMMAND_BUFFER
    dumpCommandBuffer(pCmdList);
#endif

    err = drmCommandWrite(pCmdList->_fd, DRM_XGI_SUBMIT_CMDLIST,
                          &submitInfo, sizeof(submitInfo));
    if (!err) {
        uint32_t *const begin_cmd = pCmdList->current.begin;
        uint32_t *const end_cmd = pCmdList->current.end;
        const uint32_t *const mid_point =
            pCmdList->command.ptr + (pCmdList->command.size / 2);

        pCmdList->previous = pCmdList->current;

        /* If the command is the last command in the top half, emit the top
         * half's fence.
         */
        if ((begin_cmd < mid_point) && (end_cmd >= mid_point)) {
            drmFenceEmit(pCmdList->_fd, 0, & pCmdList->top_fence, 0);
            pCmdList->top_fence_set = 1;
        }
    } else {
        ErrorF("[2D] ioctl -- cmdList error (%d, %s)!\n",
               -err, strerror(-err));
    }

    XGIDebug(DBG_FUNCTION, "%s: exit\n", __func__);
    return;
}