summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/common/gsl_driver.c
blob: b8c5170a1425655a2dbb7e164cc66c306b35c83d (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
/* Copyright (c) 2008-2010, Advanced Micro Devices. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * 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.
 *
 */

#include "gsl.h"
#include "gsl_hal.h"


//////////////////////////////////////////////////////////////////////////////
//  defines
//////////////////////////////////////////////////////////////////////////////
#define GSL_PROCESSID_NONE          0x00000000

#define GSL_DRVFLAGS_EXTERNAL       0x10000000
#define GSL_DRVFLAGS_INTERNAL       0x20000000


//////////////////////////////////////////////////////////////////////////////
// globals
//////////////////////////////////////////////////////////////////////////////
#ifndef KGSL_USER_MODE
static gsl_flags_t  gsl_driver_initialized = 0;
gsl_driver_t        gsl_driver;
#else
extern gsl_flags_t  gsl_driver_initialized;
extern gsl_driver_t gsl_driver;
#endif


//////////////////////////////////////////////////////////////////////////////
// functions
//////////////////////////////////////////////////////////////////////////////

int
kgsl_driver_init0(gsl_flags_t flags, gsl_flags_t flags_debug)
{
    int  status = GSL_SUCCESS;

    if (!(gsl_driver_initialized & GSL_FLAGS_INITIALIZED0))
    {
#ifdef GSL_LOG
        // Uncomment these to enable logging.
        //kgsl_log_init();
        //kgsl_log_open_stdout( KGSL_LOG_GROUP_ALL | KGSL_LOG_LEVEL_ALL | KGSL_LOG_TIMESTAMP
        //                      | KGSL_LOG_THREAD_ID | KGSL_LOG_PROCESS_ID );
        //kgsl_log_open_file( "c:\\kgsl_log.txt", KGSL_LOG_GROUP_ALL | KGSL_LOG_LEVEL_ALL | KGSL_LOG_TIMESTAMP
        //                      | KGSL_LOG_THREAD_ID | KGSL_LOG_PROCESS_ID );
#endif
        kos_memset(&gsl_driver, 0, sizeof(gsl_driver_t));

        GSL_API_MUTEX_CREATE();
    }

#ifdef _DEBUG
    // set debug flags on every entry, and prior to hal initialization
    gsl_driver.flags_debug |= flags_debug;
#else
    (void) flags_debug;     // unref formal parameter
#endif // _DEBUG


    KGSL_DEBUG(GSL_DBGFLAGS_DUMPX,
    {
        KGSL_DEBUG_DUMPX_OPEN("dumpx.tb", 0);
        KGSL_DEBUG_DUMPX( BB_DUMP_ENABLE, 0, 0, 0, " ");
    });

    KGSL_DEBUG_TBDUMP_OPEN("tbdump.txt");

    if (!(gsl_driver_initialized & GSL_FLAGS_INITIALIZED0))
    {
        GSL_API_MUTEX_LOCK();

        // init hal
        status = kgsl_hal_init();

        if (status == GSL_SUCCESS)
        {
            gsl_driver_initialized |= flags;
            gsl_driver_initialized |= GSL_FLAGS_INITIALIZED0;
        }

        GSL_API_MUTEX_UNLOCK();
    }

    return (status);
}

//----------------------------------------------------------------------------

int
kgsl_driver_close0(gsl_flags_t flags)
{
    int  status = GSL_SUCCESS;

    if ((gsl_driver_initialized & GSL_FLAGS_INITIALIZED0) && (gsl_driver_initialized & flags))
    {
        GSL_API_MUTEX_LOCK();

        // close hall
        status = kgsl_hal_close();

        GSL_API_MUTEX_UNLOCK();

        GSL_API_MUTEX_FREE();

#ifdef GSL_LOG
        kgsl_log_close();
#endif

        gsl_driver_initialized &= ~flags;
        gsl_driver_initialized &= ~GSL_FLAGS_INITIALIZED0;

        KGSL_DEBUG(GSL_DBGFLAGS_DUMPX,
        {
            KGSL_DEBUG_DUMPX_CLOSE();
        });

        KGSL_DEBUG_TBDUMP_CLOSE();
    }

    return (status);
}

//----------------------------------------------------------------------------

KGSL_API int
kgsl_driver_init()
{
    // only an external (platform specific device driver) component should call this

    return(kgsl_driver_init0(GSL_DRVFLAGS_EXTERNAL, 0));
}

//----------------------------------------------------------------------------

KGSL_API int
kgsl_driver_close()
{
    // only an external (platform specific device driver) component should call this

    return(kgsl_driver_close0(GSL_DRVFLAGS_EXTERNAL));
}

//----------------------------------------------------------------------------

KGSL_API int
kgsl_driver_entry(gsl_flags_t flags)
{
    int           status = GSL_FAILURE;
    int           index, i;
    unsigned int  pid;

    if (kgsl_driver_init0(GSL_DRVFLAGS_INTERNAL, flags) != GSL_SUCCESS)
    {
        return (GSL_FAILURE);
    }

    kgsl_log_write( KGSL_LOG_GROUP_DRIVER | KGSL_LOG_LEVEL_TRACE, "--> int kgsl_driver_entry( gsl_flags_t flags=%d )\n", flags );

    GSL_API_MUTEX_LOCK();

    pid = GSL_CALLER_PROCESSID_GET();

    // if caller process has not already opened access
    status = kgsl_driver_getcallerprocessindex(pid, &index);
    if (status != GSL_SUCCESS)
    {
        // then, add caller pid to process table
        status = kgsl_driver_getcallerprocessindex(GSL_PROCESSID_NONE, &index);
        if (status == GSL_SUCCESS)
        {
            gsl_driver.callerprocess[index] = pid;
            gsl_driver.refcnt++;
        }
    }

    if (status == GSL_SUCCESS)
    {
        if (!(gsl_driver_initialized & GSL_FLAGS_INITIALIZED))
        {
            // init memory apertures
            status = kgsl_sharedmem_init(&gsl_driver.shmem);
            if (status == GSL_SUCCESS)
            {
                // init devices
		status = GSL_FAILURE;
                for (i = 0; i < GSL_DEVICE_MAX; i++)
                {
		    if (kgsl_device_init(&gsl_driver.device[i], (gsl_deviceid_t)(i + 1)) == GSL_SUCCESS) {
			status = GSL_SUCCESS;
		    }
                }
            }

            if (status == GSL_SUCCESS)
            {
                gsl_driver_initialized |= GSL_FLAGS_INITIALIZED;
            }
        }

        // walk through process attach callbacks
        if (status == GSL_SUCCESS)
        {
            for (i = 0; i < GSL_DEVICE_MAX; i++)
            {
                status = kgsl_device_attachcallback(&gsl_driver.device[i], pid);
                if (status != GSL_SUCCESS)
                {
                    break;
                }
            }
        }

        // if something went wrong
        if (status != GSL_SUCCESS)
        {
            // then, remove caller pid from process table
            if (kgsl_driver_getcallerprocessindex(pid, &index) == GSL_SUCCESS)
            {
                gsl_driver.callerprocess[index] = GSL_PROCESSID_NONE;
                gsl_driver.refcnt--;
            }
        }
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DRIVER | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_driver_entry. Return value: %B\n", status );

    return (status);
}

//----------------------------------------------------------------------------

int
kgsl_driver_exit0(unsigned int pid)
{
    int  status = GSL_SUCCESS;
    int  index, i;

    GSL_API_MUTEX_LOCK();

    if (gsl_driver_initialized & GSL_FLAGS_INITIALIZED)
    {
        if (kgsl_driver_getcallerprocessindex(pid, &index) == GSL_SUCCESS)
        {
            // walk through process detach callbacks
            for (i = 0; i < GSL_DEVICE_MAX; i++)
            {
                // Empty the freememqueue of this device
                kgsl_cmdstream_memqueue_drain(&gsl_driver.device[i]);

                // Detach callback
                status = kgsl_device_detachcallback(&gsl_driver.device[i], pid);
                if (status != GSL_SUCCESS)
                {
                    break;
                }
            }

            // last running caller process
            if (gsl_driver.refcnt - 1 == 0)
            {
                // close devices
                for (i = 0; i < GSL_DEVICE_MAX; i++)
                {
                    kgsl_device_close(&gsl_driver.device[i]);
                }

                // shutdown memory apertures
                kgsl_sharedmem_close(&gsl_driver.shmem);

                gsl_driver_initialized &= ~GSL_FLAGS_INITIALIZED;
            }

            // remove caller pid from process table
            gsl_driver.callerprocess[index] = GSL_PROCESSID_NONE;
            gsl_driver.refcnt--;
        }
    }

    GSL_API_MUTEX_UNLOCK();

    if (!(gsl_driver_initialized & GSL_FLAGS_INITIALIZED))
    {
        kgsl_driver_close0(GSL_DRVFLAGS_INTERNAL);
    }

    return (status);
}

//----------------------------------------------------------------------------

KGSL_API int
kgsl_driver_exit(void)
{
    int status;

    kgsl_log_write( KGSL_LOG_GROUP_DRIVER | KGSL_LOG_LEVEL_TRACE, "--> int kgsl_driver_exit()\n" );

    status = kgsl_driver_exit0(GSL_CALLER_PROCESSID_GET());

    kgsl_log_write( KGSL_LOG_GROUP_DRIVER | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_driver_exit(). Return value: %B\n", status );

    return (status);
}

//----------------------------------------------------------------------------

KGSL_API int
kgsl_driver_destroy(unsigned int pid)
{
    return (kgsl_driver_exit0(pid));
}