summaryrefslogtreecommitdiff
path: root/drivers/mxc/gpu-viv/hal/kernel/gc_hal_kernel_async_command.c
blob: 2a0c5547215eebbef9afa49522e881a8da4fb864 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/****************************************************************************
*
*    The MIT License (MIT)
*
*    Copyright (c) 2014 - 2018 Vivante Corporation
*
*    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
*    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 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 NONINFRINGEMENT. IN NO EVENT SHALL THE
*    AUTHORS OR COPYRIGHT HOLDERS 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.
*
*****************************************************************************
*
*    The GPL License (GPL)
*
*    Copyright (C) 2014 - 2018 Vivante Corporation
*
*    This program is free software; you can redistribute it and/or
*    modify it under the terms of the GNU General Public License
*    as published by the Free Software Foundation; either version 2
*    of the License, or (at your option) any later version.
*
*    This program is distributed in the hope that 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*****************************************************************************
*
*    Note: This software is released under dual MIT and GPL licenses. A
*    recipient may use this file under the terms of either the MIT license or
*    GPL License. If you wish to use only one license not the other, you can
*    indicate your decision by deleting one of the above license notices in your
*    version of this file.
*
*****************************************************************************/


#include "gc_hal_kernel_precomp.h"
#include "gc_hal_kernel_context.h"

#define _GC_OBJ_ZONE            gcvZONE_ASYNC_COMMAND

static gceSTATUS
_HandlePatchList(
    IN gckASYNC_COMMAND Command,
    IN gcoCMDBUF CommandBuffer,
    IN gctBOOL NeedCopy
    )
{
    gceSTATUS status;
    gcsPATCH_LIST * uList;
    gcsPATCH_LIST * previous;
    gcsPATCH_LIST * kList;

    gcmkHEADER_ARG(
        "Command=0x%x CommandBuffer=0x%x NeedCopy=%d",
        Command, CommandBuffer, NeedCopy
        );

    uList = gcmUINT64_TO_PTR(CommandBuffer->patchHead);

    while (uList)
    {
        gctUINT i;

        kList = gcvNULL;
        previous = uList;

        gcmkONERROR(gckKERNEL_OpenUserData(
            Command->kernel,
            NeedCopy,
            Command->kList,
            uList,
            gcmSIZEOF(gcsPATCH_LIST),
            (gctPOINTER *)&kList
            ));

        for (i = 0; i < kList->count; i++)
        {
            gcsPATCH * patch = &kList->patch[i];

            /* Touch video memory node. */
            gcmkVERIFY_OK(gckVIDMEM_SetCommitStamp(Command->kernel, gcvENGINE_BLT, patch->handle, Command->commitStamp));
        }

        uList = kList->next;

        gcmkVERIFY_OK(gckKERNEL_CloseUserData(
            Command->kernel,
            NeedCopy,
            gcvFALSE,
            previous,
            gcmSIZEOF(gcsPATCH_LIST),
            (gctPOINTER *)&kList
            ));
    }

    gcmkFOOTER_NO();
    return gcvSTATUS_OK;

OnError:
    if (kList)
    {
        gcmkVERIFY_OK(gckKERNEL_CloseUserData(
            Command->kernel,
            NeedCopy,
            gcvFALSE,
            previous,
            gcmSIZEOF(gcsPATCH_LIST),
            (gctPOINTER *)&kList
            ));
    }

    gcmkFOOTER();
    return status;
}


gceSTATUS
gckASYNC_COMMAND_Construct(
    IN gckKERNEL Kernel,
    OUT gckASYNC_COMMAND * Command
    )
{
    gceSTATUS status;
    gckASYNC_COMMAND command;
    gckOS os = Kernel->os;

    gcmkHEADER();

    /* Allocate gckASYNC_COMMAND object. */
    gcmkONERROR(gckOS_Allocate(os, gcmSIZEOF(gcsASYNC_COMMAND), (gctPOINTER *)&command));

    gckOS_ZeroMemory(command, gcmSIZEOF(gcsASYNC_COMMAND));

    /* Mutex to protect gckFE. */
    gcmkONERROR(gckOS_CreateMutex(os, &command->mutex));

    /* Initialize gckFE. */
    gckFE_Initialize(Kernel->hardware, &command->fe);

    /* Initialize gckASYNC_COMMAND object. */
    command->os = os;
    command->kernel = Kernel;
    command->hardware = Kernel->hardware;

    gcmkVERIFY_OK(gckHARDWARE_QueryCommandBuffer(
        Kernel->hardware,
        gcvENGINE_BLT,
        gcvNULL,
        gcvNULL,
        &command->reservedTail
        ));

    gcmkONERROR(gckFENCE_Create(
        os, Kernel, &command->fence
        ));

    gcmkONERROR(gckOS_Allocate(os, gcmSIZEOF(gcsPATCH_LIST), &command->kList));

    /* Commit stamp start from 1. */
    command->commitStamp = 1;

    *Command = command;

    gcmkFOOTER_NO();
    return gcvSTATUS_OK;

OnError:
    /* Rollback. */
    gckASYNC_COMMAND_Destroy(command);

    gcmkFOOTER();
    return status;
}

gceSTATUS
gckASYNC_COMMAND_Destroy(
    IN gckASYNC_COMMAND Command
    )
{
    gcmkHEADER();

    if (Command)
    {
        if (Command->mutex)
        {
            gcmkVERIFY_OK(gckOS_DeleteMutex(Command->os, Command->mutex));
        }

        if (Command->fence)
        {
            gcmkVERIFY_OK(gckFENCE_Destory(Command->os, Command->fence));
        }

        if (Command->kList)
        {
            gcmkOS_SAFE_FREE(Command->os, Command->kList);
        }

        if (Command->fe.freeDscriptors)
        {
            gcmkOS_SAFE_FREE(Command->os, Command->fe.freeDscriptors);
        }

        gcmkOS_SAFE_FREE(Command->os, Command);
    }

    gcmkFOOTER_NO();
    return gcvSTATUS_OK;
}

gceSTATUS
gckASYNC_COMMAND_Commit(
    IN gckASYNC_COMMAND Command,
    IN gcoCMDBUF CommandBuffer,
    IN gcsQUEUE_PTR EventQueue
    )
{
    gceSTATUS       status;
    gctBOOL         available = gcvFALSE;
    gctBOOL         acquired = gcvFALSE;
    gcoCMDBUF       commandBufferObject = gcvNULL;
    struct _gcoCMDBUF _commandBufferObject;
    gctUINT8_PTR    commandBufferLogical;
    gctUINT8_PTR    commandBufferTail;
    gctUINT         commandBufferSize;
    gctUINT32       commandBufferAddress;
    gcsFEDescriptor descriptor;
    gctUINT32       skipFlushBytes;
    gctUINT32       fenceBytes;
    gctBOOL         needCopy;
    gctUINT32       oldValue;
    gctUINT32       flushBytes;

    gcmkHEADER();

    gckOS_QueryNeedCopy(Command->os, 0, &needCopy);

    gcmkVERIFY_OK(_HandlePatchList(Command, CommandBuffer, needCopy));

    /* Open user passed gcoCMDBUF object. */
    gcmkONERROR(gckKERNEL_OpenUserData(
        Command->kernel,
        needCopy,
        &_commandBufferObject,
        CommandBuffer,
        gcmSIZEOF(struct _gcoCMDBUF),
        (gctPOINTER *)&commandBufferObject
        ));

    gcmkVERIFY_OBJECT(commandBufferObject, gcvOBJ_COMMANDBUFFER);

    gckHARDWARE_FlushAsyncMMU(Command->hardware, gcvNULL, &flushBytes);

    gcmkONERROR(gckOS_AtomicExchange(Command->os,
                                     Command->hardware->pageTableDirty[gcvENGINE_BLT],
                                     0,
                                     &oldValue));

    if (oldValue)
    {
        commandBufferLogical
            = (gctUINT8_PTR) gcmUINT64_TO_PTR(commandBufferObject->logical)
            +                commandBufferObject->startOffset;

        gckHARDWARE_FlushAsyncMMU(Command->hardware, commandBufferLogical, &flushBytes);

        skipFlushBytes = 0;
    }
    else
    {
        skipFlushBytes = flushBytes;
    }

    /* Compute the command buffer entry and the size. */
    commandBufferLogical
        = (gctUINT8_PTR) gcmUINT64_TO_PTR(commandBufferObject->logical)
        +                commandBufferObject->startOffset
        +                skipFlushBytes;

    commandBufferSize
        = commandBufferObject->offset
        + Command->reservedTail
        - commandBufferObject->startOffset
        - skipFlushBytes;

    commandBufferTail
        = commandBufferLogical
        + commandBufferSize
        - Command->reservedTail;

    /* Get the hardware address. */
    if (Command->kernel && Command->kernel->virtualCommandBuffer)
    {
        gckKERNEL kernel = Command->kernel;
        gckVIRTUAL_COMMAND_BUFFER_PTR virtualCommandBuffer
            = gcmNAME_TO_PTR(commandBufferObject->physical);

        if (virtualCommandBuffer == gcvNULL)
        {
            gcmkONERROR(gcvSTATUS_INVALID_ARGUMENT);
        }

        gcmkONERROR(gckKERNEL_GetGPUAddress(
            Command->kernel,
            commandBufferLogical,
            gcvTRUE,
            virtualCommandBuffer,
            &commandBufferAddress
            ));
    }
    else
    {
        gcmkONERROR(gckHARDWARE_ConvertLogical(
            Command->hardware,
            commandBufferLogical,
            gcvTRUE,
            &commandBufferAddress
            ));
    }

    gcmkONERROR(gckHARDWARE_Fence(
        Command->hardware,
        gcvENGINE_BLT,
        commandBufferTail,
        Command->fence->address,
        Command->commitStamp,
        &fenceBytes
        ));

    descriptor.start = commandBufferAddress;
    descriptor.end   = commandBufferAddress + commandBufferSize;

    gcmkDUMPCOMMAND(
        Command->os,
        commandBufferLogical,
        commandBufferSize,
        gcvDUMP_BUFFER_USER,
        gcvFALSE
        );

    gckOS_AcquireMutex(Command->os, Command->mutex, gcvINFINITE);
    acquired = gcvTRUE;

    /* Acquire a slot. */
    for(;;)
    {
        gcmkONERROR(gckFE_ReserveSlot(Command->hardware, &Command->fe, &available));

        if (available)
        {
            break;
        }
        else
        {
            gcmkTRACE_ZONE(gcvLEVEL_INFO, _GC_OBJ_ZONE, "No available slot, have to wait");

            gckOS_Delay(Command->os, 1);
        }
    }

    /* Send descriptor. */
    gckFE_Execute(Command->hardware, &Command->fe, &descriptor);

    Command->commitStamp++;

    gckOS_ReleaseMutex(Command->os, Command->mutex);
    acquired = gcvFALSE;

    gcmkVERIFY_OK(gckKERNEL_CloseUserData(
        Command->kernel,
        needCopy,
        gcvFALSE,
        CommandBuffer,
        gcmSIZEOF(struct _gcoCMDBUF),
        (gctPOINTER *)&commandBufferObject
        ));

    gcmkFOOTER_NO();
    return gcvSTATUS_OK;

OnError:
    if (acquired)
    {
        gckOS_ReleaseMutex(Command->os, Command->mutex);
    }

    if (commandBufferObject)
    {
        gcmkVERIFY_OK(gckKERNEL_CloseUserData(
            Command->kernel,
            needCopy,
            gcvFALSE,
            CommandBuffer,
            gcmSIZEOF(struct _gcoCMDBUF),
            (gctPOINTER *)&commandBufferObject
            ));
    }

    gcmkFOOTER();
    return status;
}

gceSTATUS
gckASYNC_COMMAND_EnterCommit(
    IN gckASYNC_COMMAND Command
    )
{
    return gckOS_AcquireMutex(Command->os, Command->mutex, gcvINFINITE);
}


gceSTATUS
gckASYNC_COMMAND_ExitCommit(
    IN gckASYNC_COMMAND Command
    )
{
    return gckOS_ReleaseMutex(Command->os, Command->mutex);
}

gceSTATUS
gckASYNC_COMMAND_Execute(
    IN gckASYNC_COMMAND Command,
    IN gctUINT32 Start,
    IN gctUINT32 End
    )
{
    gceSTATUS status;
    gcsFEDescriptor descriptor;
    gctBOOL available;

    descriptor.start = Start;
    descriptor.end   = End;

    /* Acquire a slot. */
    for(;;)
    {
        gcmkONERROR(gckFE_ReserveSlot(Command->hardware, &Command->fe, &available));

        if (available)
        {
            break;
        }
        else
        {
            gckOS_Delay(Command->os, 1);
        }
    }

    /* Send descriptor. */
    gckFE_Execute(Command->hardware, &Command->fe, &descriptor);

    return gcvSTATUS_OK;

OnError:
    return status;
}