~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/dsp/syslink/multicore_ipc/transportshm.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  transportshm.c
3
 
 *
4
 
 *  Shared Memory Transport module
5
 
 *
6
 
 *  Copyright (C) 2009 Texas Instruments, Inc.
7
 
 *
8
 
 *  This package is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License version 2 as
10
 
 *  published by the Free Software Foundation.
11
 
 *
12
 
 *  THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13
 
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14
 
 *  WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
15
 
 *  PURPOSE.
16
 
 */
17
 
 
18
 
/* Standard headers */
19
 
#include <linux/types.h>
20
 
 
21
 
/* Utilities headers */
22
 
#include <linux/string.h>
23
 
#include <linux/slab.h>
24
 
 
25
 
/* Syslink headers */
26
 
#include <syslink/atomic_linux.h>
27
 
/* Module level headers */
28
 
#include <multiproc.h>
29
 
#include <sharedregion.h>
30
 
#include <nameserver.h>
31
 
#include <gatepeterson.h>
32
 
#include <notify.h>
33
 
#include <messageq.h>
34
 
#include <listmp.h>
35
 
#include <gatemp.h>
36
 
#include <transportshm.h>
37
 
 
38
 
/* =============================================================================
39
 
 * Globals
40
 
 * =============================================================================
41
 
 */
42
 
 
43
 
/* Indicates that the transport is up. */
44
 
#define TRANSPORTSHM_UP 0xBADC0FFE
45
 
 
46
 
/* transportshm Version. */
47
 
#define TRANSPORTSHM_VERSION    1
48
 
 
49
 
/*!
50
 
 *  @brief  Macro to make a correct module magic number with refCount
51
 
 */
52
 
#define TRANSPORTSHM_MAKE_MAGICSTAMP(x)                                \
53
 
                                ((TRANSPORTSHM_MODULEID << 12u) | (x))
54
 
 
55
 
#define ROUND_UP(a, b)  (((a) + ((b) - 1)) & (~((b) - 1)))
56
 
 
57
 
typedef void (*transportshm_err_fxn)(enum transportshm_reason reason,
58
 
                                        void *handle,
59
 
                                        void *msg,
60
 
                                        u32 info);
61
 
 
62
 
/* =============================================================================
63
 
 * Structures & Enums
64
 
 * =============================================================================
65
 
 */
66
 
/*
67
 
 *  Defines the transportshm state object, which contains all the
68
 
 *           module specific information.
69
 
 */
70
 
struct transportshm_module_object {
71
 
        atomic_t ref_count;
72
 
        /* Flag to indicate initialization state of transportshr */
73
 
        struct transportshm_config cfg;
74
 
        /* transportshm configuration structure */
75
 
        struct transportshm_config def_cfg;
76
 
        /* Default module configuration */
77
 
        struct transportshm_params def_inst_params;
78
 
        /* Default instance parameters */
79
 
        void *gate_handle;
80
 
        /* Handle to the gate for local thread safety */
81
 
        void *transports
82
 
                [MULTIPROC_MAXPROCESSORS][MESSAGEQ_NUM_PRIORITY_QUEUES];
83
 
        /* Transport to be set in messageq_register_transport */
84
 
        transportshm_err_fxn err_fxn;
85
 
        /* Error function */
86
 
 
87
 
};
88
 
 
89
 
/*
90
 
 * Structure of attributes in shared memory
91
 
 */
92
 
struct transportshm_attrs {
93
 
        VOLATILE u32 flag;              /* flag */
94
 
        VOLATILE u32 creator_proc_id;   /* Creator processor ID */
95
 
        VOLATILE u32 notify_event_id;   /* Notify event number */
96
 
        VOLATILE u16 priority;          /* priority */
97
 
        VOLATILE u32 *gatemp_addr;      /* gatemp shared memory srptr */
98
 
};
99
 
 
100
 
/*
101
 
 * Structure defining config parameters for the MessageQ transport
102
 
 *  instances.
103
 
 */
104
 
struct transportshm_object {
105
 
        VOLATILE struct transportshm_attrs *self;
106
 
        /* Attributes for local processor in shared memory */
107
 
        VOLATILE struct transportshm_attrs *other;
108
 
        /* Attributes for remote processor in shared memory */
109
 
        void *local_list;
110
 
        /* List for this processor      */
111
 
        void *remote_list;
112
 
        /* List for remote processor    */
113
 
        VOLATILE int status;
114
 
        /* Current status                */
115
 
        u32 alloc_size;
116
 
        /* Shared memory allocated       */
117
 
        bool cache_enabled;
118
 
        /* Whether to cache calls        */
119
 
        int notify_event_id;
120
 
        /* Notify event to be used      */
121
 
        u16 region_id;
122
 
        /* The shared region id         */
123
 
        u16 remote_proc_id;
124
 
        /* dst proc id                  */
125
 
        u32 priority;
126
 
        /* Priority of messages supported by this transport */
127
 
        void *gate;
128
 
        /* Gate for critical regions    */
129
 
        struct transportshm_params params;
130
 
        /* Instance specific parameters  */
131
 
};
132
 
 
133
 
/* =============================================================================
134
 
 *  Globals
135
 
 * =============================================================================
136
 
 */
137
 
/*
138
 
 *  @var    transportshm_state
139
 
 *
140
 
 * transportshm state object variable
141
 
 */
142
 
static struct transportshm_module_object transportshm_state = {
143
 
        .gate_handle = NULL,
144
 
        .def_cfg.err_fxn = NULL,
145
 
        .def_inst_params.gate = NULL,
146
 
        .def_inst_params.shared_addr = NULL,
147
 
        .def_inst_params.notify_event_id = (u32)(-1),
148
 
        .def_inst_params.priority = MESSAGEQ_NORMALPRI
149
 
};
150
 
 
151
 
/* Pointer to module state */
152
 
static struct transportshm_module_object *transportshm_module =
153
 
                                                        &transportshm_state;
154
 
 
155
 
/* =============================================================================
156
 
 * Forward declarations of internal functions
157
 
 * =============================================================================
158
 
 */
159
 
/* Callback function registered with the Notify module. */
160
 
static void _transportshm_notify_fxn(u16 proc_id,
161
 
                                     u16 line_id,
162
 
                                     u32 event_id,
163
 
                                     uint *arg,
164
 
                                     u32 payload);
165
 
 
166
 
/* Function to create/open the handle. */
167
 
static int _transportshm_create(struct transportshm_object **handle_ptr,
168
 
                                u16 proc_id,
169
 
                                const struct transportshm_params *params,
170
 
                                bool create_flag);
171
 
 
172
 
/* =============================================================================
173
 
 * APIs called directly by applications
174
 
 * =============================================================================
175
 
 */
176
 
/*
177
 
 * ======== transportshm_get_config ========
178
 
 *  Purpose:
179
 
 *  Get the default configuration for the transportshm
180
 
 *  module.
181
 
 *
182
 
 *  This function can be called by the application to get their
183
 
 *  configuration parameter to transportshm_setup filled in
184
 
 *  by the transportshm module with the default parameters.
185
 
 *  If the user does not wish to make any change in the default
186
 
 *  parameters, this API is not required to be called.
187
 
 */
188
 
void transportshm_get_config(struct transportshm_config *cfg)
189
 
{
190
 
        if (WARN_ON(cfg == NULL))
191
 
                goto exit;
192
 
 
193
 
        if (atomic_cmpmask_and_lt(&(transportshm_module->ref_count),
194
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
195
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true) {
196
 
                memcpy(cfg, &(transportshm_module->def_cfg),
197
 
                        sizeof(struct transportshm_config));
198
 
        } else {
199
 
                memcpy(cfg, &(transportshm_module->cfg),
200
 
                        sizeof(struct transportshm_config));
201
 
        }
202
 
        return;
203
 
 
204
 
exit:
205
 
        pr_err("transportshm_get_config: Argument of type"
206
 
                "(struct transportshm_config *) passed is null!\n");
207
 
}
208
 
 
209
 
 
210
 
/*
211
 
 * ======== transportshm_setup ========
212
 
 *  Purpose:
213
 
 *  Setup the transportshm module.
214
 
 *
215
 
 *  This function sets up the transportshm module. This
216
 
 *  function must be called before any other instance-level APIs can
217
 
 *  be invoked.
218
 
 *  Module-level configuration needs to be provided to this
219
 
 *  function. If the user wishes to change some specific config
220
 
 *  parameters, then transportshm_getconfig can be called
221
 
 *  to get the configuration filled with the default values. After
222
 
 *  this, only the required configuration values can be changed. If
223
 
 *  the user does not wish to make any change in the default
224
 
 *  parameters, the application can simply call
225
 
 *  transportshm_setup with NULL parameters. The default
226
 
 *  parameters would get automatically used.
227
 
 */
228
 
int transportshm_setup(const struct transportshm_config *cfg)
229
 
{
230
 
        int status = TRANSPORTSHM_SUCCESS;
231
 
        struct transportshm_config tmpCfg;
232
 
 
233
 
        /* This sets the refCount variable is not initialized, upper 16 bits is
234
 
        * written with module Id to ensure correctness of refCount variable.
235
 
        */
236
 
        atomic_cmpmask_and_set(&transportshm_module->ref_count,
237
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
238
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0));
239
 
 
240
 
        if (atomic_inc_return(&transportshm_module->ref_count)
241
 
                != TRANSPORTSHM_MAKE_MAGICSTAMP(1u)) {
242
 
                return 1;
243
 
        }
244
 
 
245
 
        if (cfg == NULL) {
246
 
                transportshm_get_config(&tmpCfg);
247
 
                cfg = &tmpCfg;
248
 
        }
249
 
 
250
 
        transportshm_module->gate_handle = \
251
 
                                kmalloc(sizeof(struct mutex), GFP_KERNEL);
252
 
        if (transportshm_module->gate_handle == NULL) {
253
 
                /* @retval TRANSPORTSHM_E_FAIL Failed to create
254
 
                GateMutex! */
255
 
                status = TRANSPORTSHM_E_FAIL;
256
 
                pr_err("transportshm_setup: Failed to create mutex!\n");
257
 
                atomic_set(&transportshm_module->ref_count,
258
 
                                TRANSPORTSHM_MAKE_MAGICSTAMP(0));
259
 
                goto exit;
260
 
        }
261
 
        mutex_init(transportshm_module->gate_handle);
262
 
 
263
 
        /* Copy the user provided values into the state object. */
264
 
        memcpy(&transportshm_module->cfg, cfg,
265
 
                        sizeof(struct transportshm_config));
266
 
        memset(&(transportshm_module->transports), 0, (sizeof(void *) * \
267
 
                MULTIPROC_MAXPROCESSORS * MESSAGEQ_NUM_PRIORITY_QUEUES));
268
 
        return status;
269
 
 
270
 
exit:
271
 
        pr_err("transportshm_setup failed: status = 0x%x", status);
272
 
        return status;
273
 
}
274
 
 
275
 
 
276
 
/*
277
 
 * ======== transportshm_destroy ========
278
 
 *  Purpose:
279
 
 *  Destroy the transportshm module.
280
 
 *
281
 
 *  Once this function is called, other transportshm module
282
 
 *  APIs, except for the transportshm_getConfig API cannot
283
 
 *  be called anymore.
284
 
 */
285
 
int transportshm_destroy(void)
286
 
{
287
 
        struct transportshm_object *obj = NULL;
288
 
        int status = 0;
289
 
        u16 i;
290
 
        u16 j;
291
 
 
292
 
        if (WARN_ON(atomic_cmpmask_and_lt(
293
 
                        &(transportshm_module->ref_count),
294
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
295
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
296
 
                status = -ENODEV;
297
 
                goto exit;
298
 
        }
299
 
 
300
 
        if (!(atomic_dec_return(&transportshm_module->ref_count)
301
 
                                == TRANSPORTSHM_MAKE_MAGICSTAMP(0))) {
302
 
                status = 1;
303
 
                goto exit;
304
 
        }
305
 
 
306
 
        /* Temporarily increment ref_count here. */
307
 
        atomic_set(&transportshm_module->ref_count,
308
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1));
309
 
 
310
 
        /* Delete any Transports that have not been deleted so far. */
311
 
        for (i = 0; i < MULTIPROC_MAXPROCESSORS; i++) {
312
 
                for (j = 0 ; j < MESSAGEQ_NUM_PRIORITY_QUEUES; j++) {
313
 
                        if (transportshm_module->transports[i][j] != \
314
 
                                NULL) {
315
 
                                obj = (struct transportshm_object *)
316
 
                                        transportshm_module->transports[i][j];
317
 
                                if (obj->self != NULL) {
318
 
                                        if (obj->self->creator_proc_id
319
 
                                                        == multiproc_self())
320
 
                                                transportshm_delete(
321
 
                                                        &(transportshm_module->
322
 
                                                        transports[i][j]));
323
 
                                        else
324
 
                                                transportshm_close(
325
 
                                                        &(transportshm_module->
326
 
                                                        transports[i][j]));
327
 
                                }
328
 
                        }
329
 
                }
330
 
        }
331
 
 
332
 
        /* Decrease the ref_count */
333
 
        atomic_set(&transportshm_module->ref_count,
334
 
                                TRANSPORTSHM_MAKE_MAGICSTAMP(0));
335
 
 
336
 
        if (transportshm_module->gate_handle != NULL) {
337
 
                kfree(transportshm_module->gate_handle);
338
 
                transportshm_module->gate_handle = NULL;
339
 
        }
340
 
        return 0;
341
 
 
342
 
exit:
343
 
        if (status < 0)
344
 
                pr_err("transportshm_destroy failed: status = 0x%x\n", status);
345
 
        return status;
346
 
}
347
 
 
348
 
 
349
 
/*
350
 
 * ======== transportshm_params_init ========
351
 
 *  Purpose:
352
 
 *  Get Instance parameters
353
 
 */
354
 
void transportshm_params_init(struct transportshm_params *params)
355
 
{
356
 
        if (WARN_ON(atomic_cmpmask_and_lt(
357
 
                        &(transportshm_module->ref_count),
358
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
359
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
360
 
                pr_err("transportshm_params_init: Module was "
361
 
                                " not initialized\n");
362
 
                goto exit;
363
 
        }
364
 
 
365
 
        if (WARN_ON(params == NULL)) {
366
 
                pr_err("transportshm_params_init: Argument of"
367
 
                                " type (struct transportshm_params *) "
368
 
                                "is NULL!");
369
 
                goto exit;
370
 
        }
371
 
 
372
 
        memcpy(params, &(transportshm_module->def_inst_params),
373
 
                        sizeof(struct transportshm_params));
374
 
 
375
 
exit:
376
 
        return;
377
 
}
378
 
 
379
 
/*
380
 
 * ======== transportshm_create ========
381
 
 *  Purpose:
382
 
 *  Create a transport instance. This function waits for the remote
383
 
 *  processor to complete its transport creation. Hence it must be
384
 
 *  called only after the remote processor is running.
385
 
 */
386
 
void *transportshm_create(u16 proc_id,
387
 
                        const struct transportshm_params *params)
388
 
{
389
 
        struct transportshm_object *handle = NULL;
390
 
        int status = 0;
391
 
 
392
 
        BUG_ON(params == NULL);
393
 
        BUG_ON(!(proc_id < multiproc_get_num_processors()));
394
 
        if (WARN_ON(atomic_cmpmask_and_lt(
395
 
                        &(transportshm_module->ref_count),
396
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
397
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
398
 
                status = -ENODEV;
399
 
                goto exit;
400
 
        }
401
 
 
402
 
        if (WARN_ON(params == NULL)) {
403
 
                status = -EINVAL;
404
 
                goto exit;
405
 
        }
406
 
        if (transportshm_module->transports[proc_id][params->priority] \
407
 
                != NULL) {
408
 
                /* Specified transport is already registered. */
409
 
                status = MESSAGEQ_E_ALREADYEXISTS;
410
 
                goto exit;
411
 
        }
412
 
 
413
 
        status = _transportshm_create(&handle, proc_id, params, true);
414
 
 
415
 
        if (status < 0) {
416
 
                status = -EFAULT;
417
 
                goto exit;
418
 
        }
419
 
 
420
 
        return handle;
421
 
 
422
 
exit:
423
 
        pr_err("transportshm_create failed: status = 0x%x\n", status);
424
 
        return handle;
425
 
}
426
 
 
427
 
 
428
 
/*
429
 
 * ======== transportshm_delete ========
430
 
 *  Purpose:
431
 
 *  Delete instance
432
 
 */
433
 
int transportshm_delete(void **handle_ptr)
434
 
{
435
 
        int status = 0;
436
 
        int tmp_status = 0;
437
 
        struct transportshm_object *handle;
438
 
        u16 proc_id;
439
 
        u32 key;
440
 
 
441
 
        if (WARN_ON(atomic_cmpmask_and_lt(
442
 
                        &(transportshm_module->ref_count),
443
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
444
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
445
 
                status = -ENODEV;
446
 
                goto exit;
447
 
        }
448
 
        if (WARN_ON(handle_ptr == NULL)) {
449
 
                status = -EINVAL;
450
 
                goto exit;
451
 
        }
452
 
        if (WARN_ON(*handle_ptr == NULL)) {
453
 
                status = -EINVAL;
454
 
                pr_warn("transportshm_delete: Invalid NULL"
455
 
                        " mqtshm_handle specified! status = 0x%x\n", status);
456
 
                goto exit;
457
 
        }
458
 
 
459
 
        key = mutex_lock_interruptible(transportshm_state.gate_handle);
460
 
        if (key < 0)
461
 
                goto exit;
462
 
 
463
 
        handle = (struct transportshm_object *) (*handle_ptr);
464
 
        if (handle != NULL) {
465
 
                if (handle->self != NULL) {
466
 
                        proc_id = handle->self->creator_proc_id;
467
 
                        /* Clear handle in the local array */
468
 
                        transportshm_module->
469
 
                                transports[proc_id][handle->priority] = NULL;
470
 
                        /* clear the self flag */
471
 
                        handle->self->flag = 0;
472
 
#if 0
473
 
                        if (EXPECT_FALSE(handle->cache_enabled)) {
474
 
                                Cache_wbinv((Ptr)&(handle->self->flag),
475
 
                                        sharedregion_get_cache_line_size(
476
 
                                                handle->region_id),
477
 
                                                Cache_Type_ALL,
478
 
                                                true);
479
 
                        }
480
 
#endif
481
 
                }
482
 
 
483
 
                if (handle->local_list != NULL) {
484
 
                        status = listmp_delete(&handle->local_list);
485
 
                        if (status < 0)
486
 
                                pr_warn("transportshm_delete: "
487
 
                                                "Failed to delete local listmp "
488
 
                                                "instance!\n");
489
 
                }
490
 
 
491
 
                if (handle->remote_list != NULL) {
492
 
                        tmp_status = listmp_close(&handle->remote_list);
493
 
                        if ((tmp_status < 0) && (status >= 0)) {
494
 
                                status = tmp_status;
495
 
                                pr_warn("transportshm_delete: "
496
 
                                                "Failed to close remote listmp "
497
 
                                                "instance!\n");
498
 
                        }
499
 
                }
500
 
 
501
 
                messageq_unregister_transport(handle->
502
 
                                remote_proc_id, handle->params.priority);
503
 
 
504
 
                tmp_status = notify_unregister_event_single(handle->
505
 
                                                remote_proc_id,
506
 
                                                0,
507
 
                                                handle->notify_event_id);
508
 
                if (tmp_status < 0) {
509
 
                        status = tmp_status;
510
 
                        pr_warn("transportshm_delete: Failed to "
511
 
                                        "unregister notify event!\n");
512
 
                }
513
 
 
514
 
                kfree(handle);
515
 
                *handle_ptr = NULL;
516
 
        }
517
 
        mutex_unlock(transportshm_state.gate_handle);
518
 
        return status;
519
 
 
520
 
exit:
521
 
        if (status < 0)
522
 
                pr_err("transportshm_delete failed: "
523
 
                        "status = 0x%x\n", status);
524
 
        return status;
525
 
}
526
 
 
527
 
/*
528
 
 * ========== transportshm_open_by_addr ===========
529
 
 * Open a transport instance
530
 
 */
531
 
int
532
 
transportshm_open_by_addr(void *shared_addr, void **handle_ptr)
533
 
{
534
 
        int status = 0;
535
 
        struct transportshm_attrs *attrs = NULL;
536
 
        struct transportshm_params params;
537
 
        u16 id;
538
 
 
539
 
        BUG_ON(shared_addr == NULL);
540
 
        BUG_ON(handle_ptr == NULL);
541
 
 
542
 
        if (WARN_ON(atomic_cmpmask_and_lt(
543
 
                        &(transportshm_module->ref_count),
544
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
545
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
546
 
                status = -ENODEV;
547
 
                goto exit;
548
 
        }
549
 
 
550
 
        if (shared_addr == NULL) {
551
 
                status = -EINVAL;
552
 
                goto exit;
553
 
        }
554
 
 
555
 
        if (handle_ptr == NULL) {
556
 
                status = -EINVAL;
557
 
                goto exit;
558
 
        }
559
 
 
560
 
        attrs = (struct transportshm_attrs *) shared_addr;
561
 
        id = sharedregion_get_id(shared_addr);
562
 
 
563
 
        if (id == SHAREDREGION_INVALIDREGIONID) {
564
 
                status = -EFAULT;
565
 
                goto exit;
566
 
        }
567
 
        if (((u32) shared_addr % sharedregion_get_cache_line_size(id) != 0)) {
568
 
                status = -EFAULT;
569
 
                goto exit;
570
 
        }
571
 
 
572
 
#if 0
573
 
        /* invalidate the attrs before using it */
574
 
        if (EXPECT_FALSE(SharedRegion_isCacheEnabled(id))) {
575
 
                Cache_inv((Ptr) attrs,
576
 
                        sizeof(struct transportshm_attrs),
577
 
                        Cache_Type_ALL,
578
 
                        true);
579
 
        }
580
 
#endif
581
 
        transportshm_params_init(&params);
582
 
        /* set params field */
583
 
        params.shared_addr = shared_addr;
584
 
        params.notify_event_id = attrs->notify_event_id | \
585
 
                                                (NOTIFY_SYSTEMKEY << 16);
586
 
        params.priority = attrs->priority;
587
 
 
588
 
        if (unlikely(attrs->flag != TRANSPORTSHM_UP)) {
589
 
                status = -EFAULT;
590
 
                *handle_ptr = NULL;
591
 
                goto exit;
592
 
        }
593
 
 
594
 
        /* Create the object */
595
 
        status = _transportshm_create((struct transportshm_object **)
596
 
                                        handle_ptr,
597
 
                                        attrs->creator_proc_id, &params, false);
598
 
        if (status < 0)
599
 
                goto exit;
600
 
 
601
 
        return status;
602
 
 
603
 
exit:
604
 
        pr_err("transportshm_open_by_addr failed: status = 0x%x\n", status);
605
 
        return status;
606
 
}
607
 
 
608
 
/*
609
 
 * ========== transportshm_close ===========
610
 
 * Close an opened transport instance
611
 
 */
612
 
int
613
 
transportshm_close(void **handle_ptr)
614
 
{
615
 
        int status = 0;
616
 
        int tmp_status = 0;
617
 
        struct transportshm_object *obj;
618
 
 
619
 
        if (WARN_ON(atomic_cmpmask_and_lt(
620
 
                        &(transportshm_module->ref_count),
621
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
622
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
623
 
                status = -ENODEV;
624
 
                goto exit;
625
 
        }
626
 
        if (WARN_ON(handle_ptr == NULL)) {
627
 
                status = -EINVAL;
628
 
                goto exit;
629
 
        }
630
 
        if (WARN_ON(*handle_ptr == NULL)) {
631
 
                status = -EINVAL;
632
 
                goto exit;
633
 
        }
634
 
 
635
 
        obj = (struct transportshm_object *)(*handle_ptr);
636
 
        transportshm_module->transports[obj->remote_proc_id]
637
 
                                        [obj->params.priority] = NULL;
638
 
 
639
 
        if (obj->other != NULL) {
640
 
                /* other flag was set by remote proc */
641
 
                obj->other->flag = 0;
642
 
#if 0
643
 
                if (EXPECT_FALSE(obj->cache_enabled)) {
644
 
                        Cache_wbInv(&(obj->other->flag),
645
 
                                 sharedregion_get_cache_line_size
646
 
                                                (obj->region_id),
647
 
                                 Cache_Type_ALL,
648
 
                                 TRUE);
649
 
                }
650
 
#endif
651
 
        }
652
 
 
653
 
        if (obj->gate != NULL) {
654
 
                status = gatemp_close(&obj->gate);
655
 
                if (status < 0) {
656
 
                        status = TRANSPORTSHM_E_FAIL;
657
 
                        pr_err("transportshm_close: "
658
 
                                "gatemp_close failed, status [0x%x]\n",
659
 
                                status);
660
 
                }
661
 
        }
662
 
 
663
 
        if (obj->local_list != NULL) {
664
 
                tmp_status = listmp_close(&obj->local_list);
665
 
                if ((tmp_status < 0) && (status >= 0)) {
666
 
                        status = TRANSPORTSHM_E_FAIL;
667
 
                        pr_err("transportshm_close: "
668
 
                                "listmp_close(local_list) failed, "
669
 
                                "status [0x%x]\n", status);
670
 
                }
671
 
        }
672
 
 
673
 
        if (obj->remote_list != NULL) {
674
 
                tmp_status = listmp_close(&obj->remote_list);
675
 
                if ((tmp_status < 0) && (status >= 0)) {
676
 
                        status = TRANSPORTSHM_E_FAIL;
677
 
                        pr_err("transportshm_close: "
678
 
                                "listmp_close(remote_list) failed, "
679
 
                                "status [0x%x]\n", status);
680
 
                }
681
 
        }
682
 
 
683
 
        messageq_unregister_transport(obj->remote_proc_id,
684
 
                                        obj->params.priority);
685
 
 
686
 
        tmp_status = notify_unregister_event_single(obj->remote_proc_id, 0,
687
 
                        (obj->notify_event_id | (NOTIFY_SYSTEMKEY << 16)));
688
 
        if ((tmp_status < 0) && (status >= 0))
689
 
                status = TRANSPORTSHM_E_FAIL;
690
 
 
691
 
        kfree(obj);
692
 
        *handle_ptr = NULL;
693
 
exit:
694
 
        if (status < 0)
695
 
                pr_err("transportshm_close failed: status = 0x%x\n", status);
696
 
        return status;
697
 
}
698
 
 
699
 
 
700
 
/*
701
 
 * ======== transportshm_put ========
702
 
 *  Purpose:
703
 
 *  Put msg to remote list
704
 
*/
705
 
int transportshm_put(void *handle, void *msg)
706
 
{
707
 
        int status = 0;
708
 
        struct transportshm_object *obj = NULL;
709
 
        /*int *key;*/
710
 
 
711
 
        if (WARN_ON(atomic_cmpmask_and_lt(
712
 
                        &(transportshm_module->ref_count),
713
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(0),
714
 
                        TRANSPORTSHM_MAKE_MAGICSTAMP(1)) == true)) {
715
 
                status = -ENODEV;
716
 
                goto exit;
717
 
        }
718
 
        if (WARN_ON(handle == NULL)) {
719
 
                status = -EINVAL;
720
 
                goto exit;
721
 
        }
722
 
        if (WARN_ON(msg == NULL)) {
723
 
                status = -EINVAL;
724
 
                goto exit;
725
 
        }
726
 
 
727
 
        obj = (struct transportshm_object *)handle;
728
 
#if 0
729
 
        /* writeback invalidate the message */
730
 
        if (EXPECT_FALSE(obj->cache_enabled)) {
731
 
                Cache_wbinv((Ptr) msg,
732
 
                 ((MessageQ_Msg)(msg))->msgSize,
733
 
                 Cache_Type_ALL,
734
 
                 true);
735
 
        }
736
 
#endif
737
 
        /* make sure ListMP_put and sendEvent are done before remote executes */
738
 
        /*key = gatemp_enter(obj->gate);*/
739
 
        status = listmp_put_tail(obj->remote_list, (struct listmp_elem *) msg);
740
 
        if (status < 0) {
741
 
                pr_err("transportshm_put: Failed to put "
742
 
                        "message in the shared list! status = 0x%x\n", status);
743
 
                goto exit_with_gate;
744
 
        }
745
 
 
746
 
        status = notify_send_event(obj->remote_proc_id, 0,
747
 
                                obj->notify_event_id, 0, false);
748
 
        if (status < 0)
749
 
                goto notify_send_fail;
750
 
        else
751
 
                goto exit_with_gate;
752
 
 
753
 
 
754
 
notify_send_fail:
755
 
        pr_err("transportshm_put: Notification to remote "
756
 
                "processor failed, status = 0x%x\n", status);
757
 
        /* If sending the event failed, then remove the element from the */
758
 
        /* list.  Ignore the status of remove. */
759
 
        listmp_remove(obj->remote_list, (struct listmp_elem *) msg);
760
 
 
761
 
exit_with_gate:
762
 
        /*gatemp_leave(obj->gate, key);*/
763
 
exit:
764
 
        if (status < 0)
765
 
                pr_err("transportshm_put failed: status = 0x%x\n", status);
766
 
        return status;
767
 
}
768
 
 
769
 
/*
770
 
 * ======== transportshm_control ========
771
 
 *  Purpose:
772
 
 *  Control Function
773
 
*/
774
 
int transportshm_control(void *handle, u32 cmd, u32 *cmd_arg)
775
 
{
776
 
        BUG_ON(handle == NULL);
777
 
 
778
 
        pr_err("transportshm_control not supported!\n");
779
 
        return TRANSPORTSHM_E_NOTSUPPORTED;
780
 
}
781
 
 
782
 
/*
783
 
 * ======== transportshm_get_status ========
784
 
 *  Purpose:
785
 
 *  Get status
786
 
 */
787
 
enum transportshm_status transportshm_get_status(void *handle)
788
 
{
789
 
        struct transportshm_object *obj = \
790
 
                        (struct transportshm_object *) handle;
791
 
 
792
 
        BUG_ON(obj == NULL);
793
 
 
794
 
        return obj->status;
795
 
}
796
 
 
797
 
/*
798
 
 * ======== transportshm_put ========
799
 
 *  Purpose:
800
 
 *  Get shared memory requirements.
801
 
 */
802
 
u32 transportshm_shared_mem_req(const struct transportshm_params *params)
803
 
{
804
 
        u32 mem_req = 0;
805
 
        s32 min_align;
806
 
        u16 region_id;
807
 
        struct listmp_params list_params;
808
 
        s32 status = 0;
809
 
 
810
 
        if (params == NULL) {
811
 
                status = -EINVAL;
812
 
                goto exit;
813
 
        }
814
 
        region_id = sharedregion_get_id(params->shared_addr);
815
 
 
816
 
        if (region_id == SHAREDREGION_INVALIDREGIONID)  {
817
 
                status = -EFAULT;
818
 
                goto exit;
819
 
        }
820
 
 
821
 
        min_align = 4; /*memory_get_max_default_type_align(); */
822
 
        if (sharedregion_get_cache_line_size(region_id) > min_align)
823
 
                min_align = sharedregion_get_cache_line_size(region_id);
824
 
 
825
 
        /* for the Attrs structure */
826
 
        mem_req = ROUND_UP(sizeof(struct transportshm_attrs), min_align);
827
 
 
828
 
        /* for the second Attrs structure */
829
 
        mem_req += ROUND_UP(sizeof(struct transportshm_attrs), min_align);
830
 
 
831
 
        listmp_params_init(&list_params);
832
 
        list_params.region_id = region_id;
833
 
        /* for local listMP */
834
 
        mem_req += listmp_shared_mem_req(&list_params);
835
 
 
836
 
        /* for remote listMP */
837
 
        mem_req += listmp_shared_mem_req(&list_params);
838
 
 
839
 
exit:
840
 
        if (status < 0)
841
 
                pr_err("transportshm_shared_mem_req failed: "
842
 
                        "status = 0x%x\n", status);
843
 
 
844
 
        return mem_req;
845
 
}
846
 
 
847
 
 
848
 
/* =============================================================================
849
 
 * internal functions
850
 
 * =============================================================================
851
 
 */
852
 
/*
853
 
 * ======== _transportshm_notify_fxn ========
854
 
 *  Purpose:
855
 
 *  Callback function registered with the Notify module.
856
 
 */
857
 
static void _transportshm_notify_fxn(u16 proc_id, u16 line_id, u32 event_no,
858
 
                                        uint *arg, u32 payload)
859
 
{
860
 
        struct transportshm_object *obj = NULL;
861
 
        messageq_msg msg = NULL;
862
 
        u32 queue_id;
863
 
        u32 key;
864
 
 
865
 
        key = mutex_lock_interruptible(transportshm_state.gate_handle);
866
 
 
867
 
        if (key < 0)
868
 
                goto mutex_fail;
869
 
 
870
 
        if (WARN_ON(arg == NULL))
871
 
                goto exit;
872
 
 
873
 
        obj = (struct transportshm_object *)arg;
874
 
        /*  While there is are messages, get them out and send them to
875
 
         *  their final destination. */
876
 
        if (obj->local_list)
877
 
                msg = (messageq_msg) listmp_get_head(obj->local_list);
878
 
        else
879
 
                goto exit;
880
 
        while (msg != NULL) {
881
 
                /* Get the destination message queue Id */
882
 
                queue_id = messageq_get_dst_queue(msg);
883
 
 
884
 
                /* put the message to the destination queue */
885
 
                messageq_put(queue_id, msg);
886
 
                if (obj->local_list)
887
 
                        msg = (messageq_msg)
888
 
                                listmp_get_head(obj->local_list);
889
 
                else
890
 
                        msg = NULL;
891
 
        }
892
 
        mutex_unlock(transportshm_state.gate_handle);
893
 
        return;
894
 
 
895
 
exit:
896
 
        mutex_unlock(transportshm_state.gate_handle);
897
 
mutex_fail:
898
 
        pr_err("transportshm_notify_fxn: argument passed is NULL!\n");
899
 
        return;
900
 
}
901
 
 
902
 
 
903
 
/*
904
 
 * ======== transportshm_delete ========
905
 
 *  Purpose:
906
 
 *  This will set the asynchronous error function for the transport module
907
 
 */
908
 
void transportshm_set_err_fxn(  void (*err_fxn)(
909
 
                                enum transportshm_reason reason,
910
 
                                void *handle,
911
 
                                void *msg,
912
 
                                u32 info))
913
 
{
914
 
        int key;
915
 
 
916
 
        key = mutex_lock_interruptible(transportshm_module->gate_handle);
917
 
        if (key < 0)
918
 
                goto exit;
919
 
 
920
 
        transportshm_module->cfg.err_fxn = err_fxn;
921
 
        mutex_unlock(transportshm_module->gate_handle);
922
 
 
923
 
exit:
924
 
        return;
925
 
}
926
 
 
927
 
 
928
 
/*
929
 
 * ========= _transportshm_create =========
930
 
 *  Purpose:
931
 
 *  Internal function for create()/open()
932
 
 */
933
 
static int _transportshm_create(struct transportshm_object **handle_ptr,
934
 
                u16 proc_id, const struct transportshm_params *params,
935
 
                bool create_flag)
936
 
{
937
 
        int status = 0;
938
 
        struct transportshm_object *handle = NULL;
939
 
        void *local_addr = NULL;
940
 
        int local_index;
941
 
        int remote_index;
942
 
        u32 min_align;
943
 
        struct listmp_params listmp_params[2];
944
 
 
945
 
        BUG_ON(handle_ptr == NULL);
946
 
        BUG_ON(params == NULL);
947
 
        BUG_ON(proc_id >= multiproc_get_num_processors());
948
 
 
949
 
        /*
950
 
         *  Determine who gets the '0' slot and who gets the '1' slot
951
 
         *  The '0' slot is given to the lower multiproc id.
952
 
         */
953
 
        if (multiproc_self() < proc_id) {
954
 
                local_index = 0;
955
 
                remote_index = 1;
956
 
        } else {
957
 
                local_index = 1;
958
 
                remote_index = 0;
959
 
        }
960
 
 
961
 
        handle = kzalloc(sizeof(struct transportshm_object), GFP_KERNEL);
962
 
        if (handle == NULL) {
963
 
                status = -ENOMEM;
964
 
                goto exit;
965
 
        }
966
 
        *handle_ptr = handle;
967
 
 
968
 
        if (create_flag == false) {
969
 
                /* Open by shared addr */
970
 
                handle->self = (struct transportshm_attrs *)
971
 
                                                        params->shared_addr;
972
 
                handle->region_id = sharedregion_get_id(params->shared_addr);
973
 
                BUG_ON(handle->region_id == SHAREDREGION_INVALIDREGIONID);
974
 
 
975
 
                handle->cache_enabled = sharedregion_is_cache_enabled(handle->
976
 
                                                                region_id);
977
 
 
978
 
                local_addr = sharedregion_get_ptr((u32 *)handle->self->
979
 
                                                                gatemp_addr);
980
 
                BUG_ON(local_addr == NULL);
981
 
 
982
 
                status = gatemp_open_by_addr(local_addr, &handle->gate);
983
 
                if (status < 0) {
984
 
                        status = -EFAULT;
985
 
                        goto exit;
986
 
                }
987
 
        } else {
988
 
                /* Init the gate for ListMP create below */
989
 
                if (params->gate != NULL)
990
 
                        handle->gate = params->gate;
991
 
                else
992
 
                        handle->gate = gatemp_get_default_remote();
993
 
 
994
 
                if (handle->gate == NULL) {
995
 
                        status = -EFAULT;
996
 
                        goto exit;
997
 
                }
998
 
                memcpy(&(handle->params), params,
999
 
                                sizeof(struct transportshm_params));
1000
 
                handle->region_id = sharedregion_get_id(params->shared_addr);
1001
 
 
1002
 
                /* Assert that the buffer is in a valid shared
1003
 
                 * region
1004
 
                 */
1005
 
                if (handle->region_id == SHAREDREGION_INVALIDREGIONID) {
1006
 
                        status = -EFAULT;
1007
 
                        goto exit;
1008
 
                }
1009
 
                if (((u32)params->shared_addr
1010
 
                        % sharedregion_get_cache_line_size(handle->region_id)
1011
 
                        != 0)) {
1012
 
                        status = -EFAULT;
1013
 
                        goto exit;
1014
 
                }
1015
 
 
1016
 
                /* set handle's cache_enabled, type, attrs */
1017
 
                handle->cache_enabled = sharedregion_is_cache_enabled(
1018
 
                                                        handle->region_id);
1019
 
                handle->self = (struct transportshm_attrs *)
1020
 
                                                        params->shared_addr;
1021
 
        }
1022
 
 
1023
 
        /* Determine the minimum alignment to align to */
1024
 
        min_align = 4; /*memory_get_max_default_type_align(); */
1025
 
        if (sharedregion_get_cache_line_size(handle->region_id) > min_align)
1026
 
                min_align = sharedregion_get_cache_line_size(handle->
1027
 
                                                                region_id);
1028
 
        /*
1029
 
         *  Carve up the shared memory.
1030
 
         *  If cache is enabled, these need to be on separate cache
1031
 
         *  lines. This is done with min_align and ROUND_UP function.
1032
 
         */
1033
 
 
1034
 
        handle->other = (struct transportshm_attrs *)(((u32)handle->self) +
1035
 
                (ROUND_UP(sizeof(struct transportshm_attrs), min_align)));
1036
 
 
1037
 
 
1038
 
        listmp_params_init(&(listmp_params[0]));
1039
 
        listmp_params[0].gatemp_handle = handle->gate;
1040
 
        listmp_params[0].shared_addr = (void *)(((u32)handle->other)
1041
 
                 + (ROUND_UP(sizeof(struct transportshm_attrs), min_align)));
1042
 
 
1043
 
        listmp_params_init(&listmp_params[1]);
1044
 
        listmp_params[1].gatemp_handle = handle->gate;
1045
 
        listmp_params[1].shared_addr = (void *)
1046
 
                        (((u32)listmp_params[0].shared_addr)
1047
 
                        + listmp_shared_mem_req(&listmp_params[0]));
1048
 
 
1049
 
        handle->notify_event_id = params->notify_event_id;
1050
 
        handle->priority = params->priority;
1051
 
        handle->remote_proc_id = proc_id;
1052
 
 
1053
 
        if (create_flag == true) {
1054
 
                handle->local_list =
1055
 
                                listmp_create(&(listmp_params[local_index]));
1056
 
                if (handle->local_list == NULL) {
1057
 
                        status = -EFAULT;
1058
 
                        goto exit;
1059
 
                }
1060
 
                handle->remote_list = listmp_create(
1061
 
                                        &(listmp_params[remote_index]));
1062
 
                if (handle->remote_list == NULL) {
1063
 
                        status = -EFAULT;
1064
 
                        goto exit;
1065
 
                }
1066
 
        } else {
1067
 
                /* Open the local ListMP instance */
1068
 
                status = listmp_open_by_addr(
1069
 
                                listmp_params[local_index].shared_addr,
1070
 
                                &(handle->local_list));
1071
 
                if (status < 0) {
1072
 
                        status = -EFAULT;
1073
 
                        goto exit;
1074
 
                }
1075
 
 
1076
 
                status = listmp_open_by_addr(
1077
 
                                listmp_params[remote_index].shared_addr,
1078
 
                                &(handle->remote_list));
1079
 
                if (status < 0) {
1080
 
                        status = -EFAULT;
1081
 
                        goto exit;
1082
 
                }
1083
 
        }
1084
 
 
1085
 
        status = notify_register_event_single(proc_id,
1086
 
                                                0, /* lineId */
1087
 
                                                params->notify_event_id,
1088
 
                                                _transportshm_notify_fxn,
1089
 
                                                handle);
1090
 
        if (status < 0) {
1091
 
                status = -EFAULT;
1092
 
                goto exit;
1093
 
        }
1094
 
 
1095
 
        if (create_flag == true) {
1096
 
                handle->self->creator_proc_id = multiproc_self();
1097
 
                handle->self->notify_event_id = handle->notify_event_id;
1098
 
                handle->self->priority = handle->priority;
1099
 
 
1100
 
                /* Store the GateMP shared_addr in the Attrs */
1101
 
                handle->self->gatemp_addr =
1102
 
                                        gatemp_get_shared_addr(handle->gate);
1103
 
                handle->self->flag = TRANSPORTSHM_UP;
1104
 
#if 0
1105
 
                if (EXPECT_FALSE(handle->cache_enabled)) {
1106
 
                        Cache_wbinv((Ptr) handle->self,
1107
 
                                     sizeof(struct transportshm_attrs),
1108
 
                                     Cache_Type_ALL,
1109
 
                                     true);
1110
 
                }
1111
 
#endif
1112
 
        } else {
1113
 
                handle->other->flag = TRANSPORTSHM_UP;
1114
 
#if 0
1115
 
                if (EXPECT_FALSE(handle->cache_enabled)) {
1116
 
                        Cache_wb((Ptr)&(handle->other->flag),
1117
 
                                min_align,
1118
 
                                Cache_Type_ALL,
1119
 
                                true);
1120
 
                }
1121
 
#endif
1122
 
        }
1123
 
 
1124
 
        /* Register the transport with MessageQ */
1125
 
        status = messageq_register_transport(handle, proc_id,
1126
 
                                                        params->priority);
1127
 
        if (status < 0) {
1128
 
                status = -EFAULT;
1129
 
                goto exit;
1130
 
        }
1131
 
        handle->status = TRANSPORTSHM_UP;
1132
 
        /* Set handle in the local array. */
1133
 
        transportshm_module->transports[handle->remote_proc_id]
1134
 
                [handle->params.priority] = handle;
1135
 
 
1136
 
        return status;
1137
 
 
1138
 
exit:
1139
 
        /* Cleanup in case of error. */
1140
 
        if (create_flag == true)
1141
 
                transportshm_delete((void **)handle_ptr);
1142
 
        else
1143
 
                transportshm_close((void **)handle_ptr);
1144
 
 
1145
 
        return status;
1146
 
}