summaryrefslogtreecommitdiff
path: root/linux/src/daemon/GateMP_daemon.c
blob: afb1bbd52688c333964efb1caaede7380c58b5c6 (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * Copyright (c) 2013-2014, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  ======== GateMP_daemon.c ========
 */


/* Standard headers */
#include <ti/ipc/Std.h>

/* Linux specific header files */
#include <pthread.h>

/* System headers */
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>

/* Module level headers */
#include <ti/ipc/GateMP.h>
#include <ti/ipc/NameServer.h>
#include <ti/ipc/MultiProc.h>
#include <_MultiProc.h>
#include <GateMP_config.h>
#include <_GateMP.h>

#include <IGateProvider.h>
#include <_GateMP_daemon.h>
#include <_lad.h>

#if defined (__cplusplus)
extern "C" {
#endif

#define NUM_INFO_FIELDS       6    /* Number of fields in info entry */

/* Values used to populate the resource 'inUse' arrays */
#define UNUSED          ((UInt8)0)
#define USED            ((UInt8)1)
#define RESERVED        ((UInt8)-1)

/* Name of GateMP's nameserver */
#define GateMP_NAMESERVER  "GateMP"

#define PAGE_ALIGN(size, psz)  (((size) + psz - 1) & ~(psz -1))

/* =============================================================================
 * Structures & Enums
 * =============================================================================
 */

/* structure for GateMP module state */
typedef struct {
    Int               numRemoteSystem;
    Int               numRemoteCustom1;
    Int               numRemoteCustom2;
    UInt8 *           remoteSystemInUse;
    UInt8 *           remoteCustom1InUse;
    UInt8 *           remoteCustom2InUse;
    GateMP_Handle     defaultGate;
    NameServer_Handle nameServer;
    Bool              isSetup;
} GateMP_ModuleObject;

/* Internal functions */
static Int GateMP_openDefaultGate(GateMP_Handle *handlePtr);
static Int GateMP_closeDefaultGate(GateMP_Handle *handlePtr);

/* =============================================================================
 *  Globals
 * =============================================================================
 */
/*
 * GateMP_state
 */
static GateMP_ModuleObject GateMP_state = {
    .numRemoteSystem                 = 0,
    .numRemoteCustom1                = 0,
    .numRemoteCustom2                = 0,
    .remoteSystemInUse               = NULL,
    .remoteCustom1InUse              = NULL,
    .remoteCustom2InUse              = NULL,
    .defaultGate                     = NULL,
    .nameServer                      = NULL,
    .isSetup                         = FALSE
};

static GateMP_ModuleObject * GateMP_module = &GateMP_state;

/* =============================================================================
 * APIS
 * =============================================================================
 */

/* Function to setup the gatemp module. */
Int GateMP_setup(Void)
{
    Int               status = GateMP_S_SUCCESS;
    NameServer_Params params;
    UInt32            nsValue[NUM_INFO_FIELDS];
    UInt32            len;
    UInt32            size;
    UInt32            alignDiff;
    Int32             fdMem;

    NameServer_Params_init(&params);
    params.maxRuntimeEntries = MAX_RUNTIME_ENTRIES;
    params.maxNameLen = MAX_NAME_LEN;

    /* Assume info entry has more fields than other entries */
    params.maxValueLen = NUM_INFO_FIELDS * sizeof(UInt32);

    GateMP_module->nameServer =
                NameServer_create(GateMP_NAMESERVER, &params);

    if (GateMP_module->nameServer == NULL) {
        status = GateMP_E_FAIL;
        LOG0("GateMP_setup: NameServer_create failed\n");
    }

    if (status == GateMP_S_SUCCESS) {
        do {
            sleep(1);   /* Give the slaves some time to get NameServer ready */
            status = GateMP_openDefaultGate(&GateMP_module->defaultGate);
        } while (status == GateMP_E_NOTFOUND);


        if (status < 0) {
            LOG0("GateMP_setup: failed to open default gate\n");
            status = GateMP_E_FAIL;
        }
    }

    if (status == GateMP_S_SUCCESS) {
        /* Process global info NameServer entry */
        len = sizeof(nsValue);

        status = NameServer_get(GateMP_module->nameServer, "_GateMP_TI_info",
            &nsValue, &len, NULL);

        if (status < 0) {
            LOG0("GateMP_setup: failed to find info entry\n");
            status = GateMP_E_NOTFOUND;
        }
        else {
            fdMem = open ("/dev/mem", O_RDWR | O_SYNC);

            if (fdMem < 0){
                LOG0("GateMP_setup: failed to open the /dev/mem!\n");
                status = GateMP_E_FAIL;
                goto cleanup;
            }

            GateMP_module->numRemoteSystem = nsValue[3];
            GateMP_module->numRemoteCustom1 = nsValue[4];
            GateMP_module->numRemoteCustom2 = nsValue[5];

            /* Map InUse arrays to daemon's address space */
            size = GateMP_module->numRemoteSystem * sizeof (UInt8) +
                (nsValue[0] & (sysconf(_SC_PAGE_SIZE) - 1));
            size = PAGE_ALIGN(size, sysconf(_SC_PAGE_SIZE));
            GateMP_module->remoteSystemInUse = mmap(NULL, size,
                (PROT_READ|PROT_WRITE), (MAP_SHARED), fdMem,
                (off_t)nsValue[0] & ~(sysconf(_SC_PAGE_SIZE) - 1));
            if (GateMP_module->remoteSystemInUse == MAP_FAILED) {
                 GateMP_module->remoteSystemInUse = NULL;
                 status = GateMP_E_MEMORY;
                 LOG1("Failed to map remoteSystemInUse=0x%p to host address" \
                      "  space!", GateMP_module->remoteSystemInUse);
            }
            else {
                alignDiff = (off_t)nsValue[0] -
                    ((off_t)nsValue[0] & ~(sysconf(_SC_PAGE_SIZE) - 1));
                GateMP_module->remoteSystemInUse =
                    GateMP_module->remoteSystemInUse + alignDiff;
            }

            size = GateMP_module->numRemoteCustom1 * sizeof (UInt8) +
                (nsValue[0] & (sysconf(_SC_PAGE_SIZE) - 1));
            size = PAGE_ALIGN(size, sysconf(_SC_PAGE_SIZE));
            if (status == GateMP_S_SUCCESS) {
                GateMP_module->remoteCustom1InUse = mmap(NULL, size,
                    (PROT_READ|PROT_WRITE), (MAP_SHARED), fdMem,
                    (off_t)nsValue[1] & ~(sysconf(_SC_PAGE_SIZE) - 1));
                if (GateMP_module->remoteCustom1InUse == MAP_FAILED) {
                    GateMP_module->remoteCustom1InUse = NULL;
                    status = GateMP_E_MEMORY;
                    LOG1("Failed to map remoteCustom1InUse=%p to host address" \
                        " space!", GateMP_module->remoteCustom1InUse);
                }
                else {
                    alignDiff = (off_t)nsValue[1] -
                        ((off_t)nsValue[1] & ~(sysconf(_SC_PAGE_SIZE) - 1));
                    GateMP_module->remoteCustom1InUse =
                        GateMP_module->remoteCustom1InUse + alignDiff;
                }
            }

            size = GateMP_module->numRemoteCustom2 * sizeof (UInt8) +
                (nsValue[0] & (sysconf(_SC_PAGE_SIZE) - 1));
            size = PAGE_ALIGN(size, sysconf(_SC_PAGE_SIZE));
            if (status == GateMP_S_SUCCESS) {
                GateMP_module->remoteCustom2InUse = mmap(NULL, size,
                    (PROT_READ|PROT_WRITE), (MAP_SHARED), fdMem,
                    (off_t)nsValue[2] & ~(sysconf(_SC_PAGE_SIZE) - 1));
                if (GateMP_module->remoteCustom2InUse == MAP_FAILED) {
                    GateMP_module->remoteCustom2InUse = NULL;
                    status = GateMP_E_MEMORY;
                    LOG1("Failed to map remoteCustom2InUse=%p to host address" \
                        " space!", GateMP_module->remoteCustom2InUse);
                }
                else {
                    alignDiff = (off_t)nsValue[2] -
                        ((off_t)nsValue[2] & ~(sysconf(_SC_PAGE_SIZE) - 1));
                    GateMP_module->remoteCustom2InUse =
                        GateMP_module->remoteCustom2InUse + alignDiff;
                }
            }
        }
    }

    /* TODO: setup the proxy map */

cleanup:
    /* clean up if error */
    if (status < 0) {
        GateMP_destroy();
    }

    GateMP_module->isSetup = TRUE;

    return (status);
}

Void GateMP_destroy(Void)
{
    if (GateMP_module->remoteSystemInUse) {
        munmap((unsigned int *)GateMP_module->remoteSystemInUse,
            GateMP_module->numRemoteSystem * sizeof (UInt8));
        GateMP_module->remoteSystemInUse = NULL;
    }

    if (GateMP_module->remoteCustom1InUse) {
        munmap((unsigned int *)GateMP_module->remoteCustom1InUse,
            GateMP_module->numRemoteCustom1 * sizeof (UInt8));
        GateMP_module->remoteCustom1InUse = NULL;
    }

    if (GateMP_module->remoteCustom2InUse) {
        munmap((unsigned int *)GateMP_module->remoteCustom2InUse,
            GateMP_module->numRemoteCustom2 * sizeof (UInt8));
        GateMP_module->remoteCustom2InUse = NULL;
    }

    if (GateMP_module->defaultGate) {
        GateMP_closeDefaultGate(&GateMP_module->defaultGate);
    }

    if (GateMP_module->nameServer) {
        NameServer_delete(&GateMP_module->nameServer);
        GateMP_module->nameServer = NULL;
    }

    GateMP_module->isSetup = FALSE;

    return;
}

/* Open default gate during GateMP_setup. Should only be called once */
static Int GateMP_openDefaultGate(GateMP_Handle *handlePtr)
{
    Int             status = GateMP_S_SUCCESS;
    UInt32          len;
    UInt32          nsValue[4];
    GateMP_Object * obj = NULL;
    UInt32          arg;
    UInt32          mask;
    UInt32          creatorProcId;

    GateMP_RemoteSystemProxy_Params     systemParams;

    /* assert that a valid pointer has been supplied */
    if (handlePtr == NULL) {
        LOG0("GateMP_open: argument cannot be null\n");
        status = GateMP_E_INVALIDARG;
    }

    if (status == GateMP_S_SUCCESS) {
        len = sizeof(nsValue);

        status = NameServer_get(GateMP_module->nameServer, "_GateMP_TI_dGate",
            &nsValue, &len, NULL);

        if (status < 0) {
            *handlePtr = NULL;
            status = GateMP_E_NOTFOUND;
        }
        else {
            arg = nsValue[2];
            mask = nsValue[3];
            creatorProcId = nsValue[1] >> 16;
        }
    }

    if (status == GateMP_S_SUCCESS) {
        /* allocate the instance object */
        obj = (GateMP_Object *)calloc(1, sizeof (GateMP_Object));
        if (obj != NULL) {
            obj->localGate  = NULL;  /* TODO: create the local gate instance */
            obj->localProtect  = GETLOCAL(mask);
            obj->remoteProtect = GETREMOTE(mask);
            obj->nsKey         = 0;
            obj->numOpens      = 1;
            obj->objType       = Ipc_ObjType_OPENDYNAMIC;
            obj->resourceId    = arg;

            assert(obj->remoteProtect == GateMP_RemoteProtect_SYSTEM);

            /* create the proxy object */
            GateMP_RemoteSystemProxy_Params_init(&systemParams);
            systemParams.resourceId = obj->resourceId;
            systemParams.openFlag = TRUE;

            /*
             * TODO: Currently passing in localProtect instead of localGate,
             * since GateHWSpinlock owns the local gate
             */
            obj->gateHandle = (IGateProvider_Handle)
                GateMP_RemoteSystemProxy_create(obj->localProtect,
                    &systemParams);

            if (obj->gateHandle == NULL) {
                LOG0("GateMP_openDefaultGate: failed to create proxy\n");
                free(obj);
                obj = NULL;
            }
        }
        else {
            LOG0("GateMP_openDefaultGate: Memory allocation failed")
        }

        if (obj == NULL) {
            status = GateMP_E_FAIL;
        }
    }

    /* Return the "opened" GateMP instance  */
    *handlePtr = (GateMP_Handle)obj;

    return status;
}

static Int GateMP_closeDefaultGate(GateMP_Handle *handlePtr)
{
    Int status = GateMP_S_SUCCESS;
    GateMP_Object * obj = *(GateMP_Object **)handlePtr;

    if (obj->gateHandle != NULL) {
        /* Default gate is always of type System when more than 1 processor */
        GateMP_RemoteSystemProxy_delete(
            (GateMP_RemoteSystemProxy_Handle *)&obj->gateHandle);
    }

    free(*handlePtr);
    *handlePtr = NULL;

    return(status);
}

Int GateMP_getFreeResource(GateMP_RemoteProtect type)
{
    IArg   key;
    Bool   flag = FALSE;
    Int    resourceId = -1;
    UInt8* inUse = NULL;
    Int    num = 0;

    /* Remote case */
    switch (type) {
        /* TODO: currently only support System proxy */
        case GateMP_RemoteProtect_SYSTEM:
        case GateMP_RemoteProtect_CUSTOM1:
        case GateMP_RemoteProtect_CUSTOM2:
            inUse = GateMP_module->remoteSystemInUse;
            num = GateMP_module->numRemoteSystem;
            break;

        default:
            LOG0("GateMP_getFreeResource: Invalid remote protection type\n");
            break;
    }

    if (inUse != NULL) {
        assert(GateMP_module->defaultGate != NULL);
        key = GateMP_enter(GateMP_module->defaultGate);

        /*
         *  Find a free resource id. Note: zero is reserved on the
         *  system proxy for the default gate.
         */
        for (resourceId = 0; resourceId < num; resourceId++) {
            /*
             *  If not in-use, set the inUse to TRUE to prevent other
             *  creates from getting this one.
             */
            if (inUse[resourceId] == UNUSED) {
                flag = TRUE;

                /* Denote in shared memory that the resource is used */
                inUse[resourceId] = USED;
                break;
            }
        }

        GateMP_leave(GateMP_module->defaultGate, key);
    }

    if (flag == FALSE) {
        resourceId = -1;
    }

    return (resourceId);
}

Int GateMP_releaseResource(UInt id, GateMP_RemoteProtect type)
{
    Int    status = GateMP_S_SUCCESS;
    IArg   key;
    UInt8* inUse = NULL;
    Int    num = 0;

    /* Remote case */
    switch (type) {
        /* TODO: currently only support System proxy */
        case GateMP_RemoteProtect_SYSTEM:
        case GateMP_RemoteProtect_CUSTOM1:
        case GateMP_RemoteProtect_CUSTOM2:
            inUse = GateMP_module->remoteSystemInUse;
            num = GateMP_module->numRemoteSystem;
            break;

        default:
            LOG0("GateMP_releaseResource: Invalid remote protection type\n");
            status = GateMP_E_FAIL;
            break;
    }

    if ((inUse != NULL) && (id < num)) {
        assert(GateMP_module->defaultGate != NULL);
        key = GateMP_enter(GateMP_module->defaultGate);
        inUse[id] = UNUSED;
        GateMP_leave(GateMP_module->defaultGate, key);
    }
    else {
        /* Should not happen if module is properly setup */
        status = GateMP_E_FAIL;
    }

    return (status);
}

Int GateMP_getNumResources(GateMP_RemoteProtect type)
{
    Int   num = -1;

    /* Remote case */
    switch (type) {
        /* TODO: currently only support System proxy */
        case GateMP_RemoteProtect_SYSTEM:
        case GateMP_RemoteProtect_CUSTOM1:
        case GateMP_RemoteProtect_CUSTOM2:
            num = GateMP_module->numRemoteSystem;
            break;

        default:
            LOG0("GateMP_getNumResources: Invalid remote protection type\n");
            break;
    }

    return (num);
}

NameServer_Handle GateMP_getNameServer(Void)
{
    return (GateMP_module->nameServer);
}

Bool GateMP_isSetup(Void)
{
    return (GateMP_module->isSetup);
}

IArg GateMP_enter(GateMP_Handle handle)
{
    GateMP_Object * obj;
    IArg            key;

    obj = (GateMP_Object *)handle;
    key = IGateProvider_enter(obj->gateHandle);

    return(key);
}

Void GateMP_leave(GateMP_Handle handle, IArg key)
{
    GateMP_Object *obj;

    obj = (GateMP_Object *)handle;
    IGateProvider_leave(obj->gateHandle, key);
}