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

« back to all changes in this revision

Viewing changes to drivers/dsp/syslink/multicore_ipc/ipc.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
 
 *  ipc.c
3
 
 *
4
 
 *  This module is primarily used to configure IPC-wide settings and
5
 
 *           initialize IPC at runtime
6
 
 *
7
 
 *  Copyright (C) 2009 Texas Instruments, Inc.
8
 
 *
9
 
 *  This package is free software; you can redistribute it and/or modify
10
 
 *  it under the terms of the GNU General Public License version 2 as
11
 
 *  published by the Free Software Foundation.
12
 
 *
13
 
 *  THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14
 
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15
 
 *  WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
16
 
 *  PURPOSE.
17
 
 */
18
 
 
19
 
 
20
 
/* Standard headers */
21
 
#include <linux/types.h>
22
 
#include <linux/module.h>
23
 
 
24
 
#include <syslink/atomic_linux.h>
25
 
 
26
 
/* Module headers */
27
 
#include <multiproc.h>
28
 
#include <ipc.h>
29
 
#include <platform.h>
30
 
 
31
 
#include <gatemp.h>
32
 
#include <sharedregion.h>
33
 
#include <notify.h>
34
 
#include <notify_ducatidriver.h>
35
 
#include <notify_setup_proxy.h>
36
 
 
37
 
#include <heap.h>
38
 
#include <heapbufmp.h>
39
 
#include <heapmemmp.h>
40
 
 
41
 
#include <messageq.h>
42
 
#include <nameserver.h>
43
 
#include <nameserver_remotenotify.h>
44
 
 
45
 
/* Ipu Power Management Header (ipu_pm) */
46
 
#include "../ipu_pm/ipu_pm.h"
47
 
/* =============================================================================
48
 
 * Macros
49
 
 * =============================================================================
50
 
 */
51
 
/* Macro to make a correct module magic number with ref_count */
52
 
#define IPC_MAKE_MAGICSTAMP(x)((IPC_MODULEID << 16u) | (x))
53
 
 
54
 
/* flag for starting processor synchronization */
55
 
#define IPC_PROCSYNCSTART    1
56
 
 
57
 
/* flag for finishing processor synchronization */
58
 
#define IPC_PROCSYNCFINISH   2
59
 
 
60
 
#define ROUND_UP(a, b)  (((a) + ((b) - 1)) & (~((b) - 1)))
61
 
 
62
 
/* =============================================================================
63
 
 * Enums & Structures
64
 
 * =============================================================================
65
 
 */
66
 
 
67
 
/* The structure used for reserving memory in SharedRegion */
68
 
struct ipc_reserved {
69
 
        VOLATILE u32    started_key;
70
 
        u32             *notify_sr_ptr;
71
 
        u32             *nsrn_sr_ptr;
72
 
        u32             *transport_sr_ptr;
73
 
        u32             *config_list_head;
74
 
};
75
 
 
76
 
 
77
 
/* head of the config list */
78
 
struct ipc_config_head {
79
 
        VOLATILE u32 first;
80
 
        /* Address of first config entry */
81
 
};
82
 
 
83
 
 
84
 
/*
85
 
 *  This structure captures Configuration details of a module/instance
86
 
 *  written by a slave to synchornize with a remote slave/HOST
87
 
 */
88
 
struct ipc_config_entry {
89
 
        VOLATILE u32 remote_proc_id;
90
 
        /* Remote processor identifier */
91
 
        VOLATILE u32 local_proc_id;
92
 
        /* Config Entry owner processor identifier */
93
 
        VOLATILE u32 tag;
94
 
        /* Unique tag to distinguish config from other config entries */
95
 
        VOLATILE u32 size;
96
 
        /* Size of the config pointer */
97
 
        VOLATILE u32 next;
98
 
        /* Address of next config entry (In SRPtr format) */
99
 
};
100
 
 
101
 
/*
102
 
 *  This structure defines the fields that are to be configured
103
 
 *  between the executing processor and a remote processor.
104
 
 */
105
 
struct ipc_entry {
106
 
        u16     remote_proc_id; /* the remote processor id   */
107
 
        bool    setup_notify; /* whether to setup Notify   */
108
 
        bool    setup_messageq; /* whether to setup messageq */
109
 
        bool    setup_ipu_pm; /* whether to setup ipu_pm */
110
 
};
111
 
 
112
 
/* Ipc instance structure. */
113
 
struct ipc_proc_entry {
114
 
        void                    *local_config_list;
115
 
        void                    *remote_config_list;
116
 
        void                    *user_obj;
117
 
        bool                    slave;
118
 
        struct ipc_entry        entry;
119
 
        bool                    is_attached;
120
 
};
121
 
 
122
 
 
123
 
/* Module state structure */
124
 
struct ipc_module_state {
125
 
        s32                     ref_count;
126
 
        atomic_t                start_ref_count;
127
 
        void                    *ipc_shared_addr;
128
 
        void                    *gatemp_shared_addr;
129
 
        void                    *ipu_pm_shared_addr;
130
 
        enum ipc_proc_sync      proc_sync;
131
 
        struct ipc_config       cfg;
132
 
        struct ipc_proc_entry   proc_entry[MULTIPROC_MAXPROCESSORS];
133
 
};
134
 
 
135
 
 
136
 
/* =============================================================================
137
 
 * Forward declaration
138
 
 * =============================================================================
139
 
 */
140
 
/*
141
 
 *  ======== ipc_get_master_addr() ========
142
 
 */
143
 
static void *ipc_get_master_addr(u16 remote_proc_id, void *shared_addr);
144
 
 
145
 
/*
146
 
 *  ======== ipc_get_region0_reserved_size ========
147
 
 *  Returns the amount of memory to be reserved for Ipc in SharedRegion 0.
148
 
 *
149
 
 *  This is used for synchronizing processors.
150
 
 */
151
 
static u32 ipc_get_region0_reserved_size(void);
152
 
 
153
 
/*
154
 
 *  ======== ipc_get_slave_addr() ========
155
 
 */
156
 
static void *ipc_get_slave_addr(u16 remote_proc_id, void *shared_addr);
157
 
 
158
 
/*
159
 
 *  ======== ipc_proc_sync_start ========
160
 
 *  Starts the process of synchronizing processors.
161
 
 *
162
 
 *  Shared memory in region 0 will be used.  The processor which owns
163
 
 *  SharedRegion 0 writes its reserve memory address in region 0
164
 
 *  to let the other processors know it has started.  It then spins
165
 
 *  until the other processors start.  The other processors write their
166
 
 *  reserve memory address in region 0 to let the owner processor
167
 
 *  know they've started.  The other processors then spin until the
168
 
 *  owner processor writes to let them know its finished the process
169
 
 *  of synchronization before continuing.
170
 
 */
171
 
static int ipc_proc_sync_start(u16 remote_proc_id, void *shared_addr);
172
 
 
173
 
/*
174
 
 *  ======== ipc_proc_sync_finish ========
175
 
 *  Finishes the process of synchronizing processors.
176
 
 *
177
 
 *  Each processor writes its reserve memory address in SharedRegion 0
178
 
 *  to let the other processors know its finished the process of
179
 
 *  synchronization.
180
 
 */
181
 
static int ipc_proc_sync_finish(u16 remote_proc_id, void *shared_addr);
182
 
 
183
 
/*
184
 
 *  ======== ipc_reserved_size_per_proc ========
185
 
 *  The amount of memory required to be reserved per processor.
186
 
 */
187
 
static u32 ipc_reserved_size_per_proc(void);
188
 
 
189
 
/* TODO: figure these out */
190
 
#define gate_enter_system() 0
191
 
#define gate_leave_system(key) {}
192
 
 
193
 
/* =============================================================================
194
 
 * Globals
195
 
 * =============================================================================
196
 
 */
197
 
static struct ipc_module_state ipc_module_state = {
198
 
        .proc_sync = IPC_PROCSYNC_ALL,
199
 
        .ref_count = 0,
200
 
};
201
 
static struct ipc_module_state *ipc_module = &ipc_module_state;
202
 
 
203
 
/* =============================================================================
204
 
 * APIs
205
 
 * =============================================================================
206
 
 */
207
 
/*
208
 
 * ========== ipc_attach ==========
209
 
 * attaches to a remote processor
210
 
 */
211
 
int ipc_attach(u16 remote_proc_id)
212
 
{
213
 
        int status = 0;
214
 
#if 0
215
 
        u32 reserved_size = ipc_reserved_size_per_proc();
216
 
        bool cache_enabled = sharedregion_is_cache_enabled(0);
217
 
#endif
218
 
        void *notify_shared_addr;
219
 
        void *msgq_shared_addr;
220
 
        void *nsrn_shared_addr;
221
 
        u32 notify_mem_req;
222
 
        VOLATILE struct ipc_reserved *slave;
223
 
        struct ipc_proc_entry *ipc;
224
 
 
225
 
        if (remote_proc_id >= MULTIPROC_MAXPROCESSORS) {
226
 
                pr_err("Invalid remote_proc_id passed\n");
227
 
                return IPC_E_FAIL;
228
 
        }
229
 
 
230
 
        /* determine if self is master or slave */
231
 
        if (multiproc_self() < remote_proc_id)
232
 
                ipc_module->proc_entry[remote_proc_id].slave = true;
233
 
        else
234
 
                ipc_module->proc_entry[remote_proc_id].slave = false;
235
 
 
236
 
        /* determine the slave's slot */
237
 
        slave = ipc_get_slave_addr(remote_proc_id, ipc_module->ipc_shared_addr);
238
 
#if 0
239
 
        if (cache_enabled)
240
 
                Cache_inv((void *)slave, reserved_size, Cache_Type_ALL, true);
241
 
#endif
242
 
        /* get the attach paramters associated with remote_proc_id */
243
 
        ipc = &(ipc_module->proc_entry[remote_proc_id]);
244
 
 
245
 
        /* Synchronize the processors. */
246
 
        status = ipc_proc_sync_start(remote_proc_id, ipc_module->
247
 
                                                        ipc_shared_addr);
248
 
 
249
 
        if (status < 0)
250
 
                pr_err("ipc_attach : ipc_proc_sync_start "
251
 
                        "failed [0x%x]\n", status);
252
 
        else
253
 
                pr_err("ipc_proc_sync_start : status [0x%x]\n", status);
254
 
 
255
 
 
256
 
        if (status >= 0) {
257
 
                /* must be called before SharedRegion_attach */
258
 
                status = gatemp_attach(remote_proc_id, ipc_module->
259
 
                                                        gatemp_shared_addr);
260
 
                if (status < 0)
261
 
                        pr_err("ipc_attach : gatemp_attach "
262
 
                                "failed [0x%x]\n", status);
263
 
                else
264
 
                        pr_err("gatemp_attach : status [0x%x]\n", status);
265
 
 
266
 
        }
267
 
 
268
 
        /* retrieves the SharedRegion Heap handles */
269
 
        if (status >= 0) {
270
 
                status = sharedregion_attach(remote_proc_id);
271
 
                if (status < 0)
272
 
                        pr_err("ipc_attach : sharedregion_attach "
273
 
                                "failed [0x%x]\n", status);
274
 
                else
275
 
                        pr_err("sharedregion_attach : status "
276
 
                                "[0x%x]\n", status);
277
 
        }
278
 
 
279
 
        /* attach Notify if not yet attached and specified to set internal
280
 
                                                                setup */
281
 
        if (status >= 0 && !notify_is_registered(remote_proc_id, 0) &&
282
 
                (ipc->entry.setup_notify)) {
283
 
                /* call notify_attach */
284
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
285
 
                        notify_mem_req = notify_shared_mem_req(remote_proc_id,
286
 
                                ipc_module->ipc_shared_addr);
287
 
                        notify_shared_addr = sl_heap_alloc(
288
 
                                        sharedregion_get_heap(0),
289
 
                                        notify_mem_req,
290
 
                                        sharedregion_get_cache_line_size(0));
291
 
                        memset(notify_shared_addr, 0, notify_mem_req);
292
 
                        slave->notify_sr_ptr = sharedregion_get_srptr(
293
 
                                                        notify_shared_addr, 0);
294
 
                        if (slave->notify_sr_ptr ==
295
 
                                                SHAREDREGION_INVALIDSRPTR) {
296
 
                                status = IPC_E_FAIL;
297
 
                                pr_err("ipc_attach : "
298
 
                                        "sharedregion_get_srptr "
299
 
                                        "failed [0x%x]\n", status);
300
 
                        } else {
301
 
                                pr_err(
302
 
                                        "sharedregion_get_srptr : "
303
 
                                        "status [0x%x]\n", status);
304
 
                        }
305
 
                } else {
306
 
                        notify_shared_addr = sharedregion_get_ptr(slave->
307
 
                                                        notify_sr_ptr);
308
 
                        if (notify_shared_addr == NULL) {
309
 
                                status = IPC_E_FAIL;
310
 
                                pr_err("ipc_attach : "
311
 
                                        "sharedregion_get_ptr "
312
 
                                        "failed [0x%x]\n", status);
313
 
                        } else {
314
 
                                pr_err(
315
 
                                        "sharedregion_get_ptr : "
316
 
                                        "status [0x%x]\n", status);
317
 
                        }
318
 
                }
319
 
 
320
 
                if (status >= 0) {
321
 
                        status = notify_attach(remote_proc_id,
322
 
                                                        notify_shared_addr);
323
 
                        if (status < 0)
324
 
                                pr_err("ipc_attach : "
325
 
                                        "notify_attach "
326
 
                                        "failed [0x%x]\n", status);
327
 
                        else
328
 
                                        pr_err(
329
 
                                                "notify_attach : "
330
 
                                                "status [0x%x]\n", status);
331
 
                }
332
 
        }
333
 
 
334
 
        /* Must come before Notify because depends on default Notify */
335
 
        if (status >= 0 && ipc->entry.setup_notify && ipc->entry.setup_ipu_pm) {
336
 
                if (status >= 0) {
337
 
                        status = ipu_pm_attach(remote_proc_id, ipc_module->
338
 
                                                        ipu_pm_shared_addr);
339
 
                        if (status < 0)
340
 
                                pr_err("ipc_attach : "
341
 
                                        "ipu_pm_attach "
342
 
                                        "failed [0x%x]\n", status);
343
 
                        else
344
 
                                pr_err(
345
 
                                        "ipu_pm_attach : "
346
 
                                        "status [0x%x]\n", status);
347
 
                }
348
 
        }
349
 
 
350
 
        /* Must come after gatemp_start because depends on default GateMP */
351
 
        if (status >= 0 && ipc->entry.setup_notify) {
352
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
353
 
                        nsrn_shared_addr = sl_heap_alloc(
354
 
                                        sharedregion_get_heap(0),
355
 
                                        nameserver_remotenotify_shared_mem_req(
356
 
                                                                        NULL),
357
 
                                        sharedregion_get_cache_line_size(0));
358
 
 
359
 
                        slave->nsrn_sr_ptr =
360
 
                                sharedregion_get_srptr(nsrn_shared_addr, 0);
361
 
                        if (slave->nsrn_sr_ptr == SHAREDREGION_INVALIDSRPTR) {
362
 
                                status = IPC_E_FAIL;
363
 
                                pr_err("ipc_attach : "
364
 
                                        "sharedregion_get_srptr "
365
 
                                        "failed [0x%x]\n", status);
366
 
                        } else {
367
 
                                pr_err(
368
 
                                        "sharedregion_get_srptr : "
369
 
                                        "status [0x%x]\n", status);
370
 
                        }
371
 
                } else {
372
 
                        nsrn_shared_addr =
373
 
                                sharedregion_get_ptr(slave->nsrn_sr_ptr);
374
 
                        if (nsrn_shared_addr == NULL) {
375
 
                                status = IPC_E_FAIL;
376
 
                                pr_err("ipc_attach : "
377
 
                                        "sharedregion_get_ptr "
378
 
                                        "failed [0x%x]\n", status);
379
 
                        } else {
380
 
                                pr_err(
381
 
                                        "sharedregion_get_ptr : "
382
 
                                        "status [0x%x]\n", status);
383
 
                        }
384
 
                }
385
 
 
386
 
                if (status >= 0) {
387
 
                        /* create the nameserver_remotenotify instances */
388
 
                        status = nameserver_remotenotify_attach(remote_proc_id,
389
 
                                                        nsrn_shared_addr);
390
 
 
391
 
                        if (status < 0)
392
 
                                pr_err("ipc_attach : "
393
 
                                        "nameserver_remotenotify_attach "
394
 
                                        "failed [0x%x]\n", status);
395
 
                        else
396
 
                                pr_err(
397
 
                                        "nameserver_remotenotify_attach : "
398
 
                                        "status [0x%x]\n", status);
399
 
                }
400
 
        }
401
 
 
402
 
        /* Must come after gatemp_start because depends on default GateMP */
403
 
        if (status >= 0 && ipc->entry.setup_messageq) {
404
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
405
 
                        msgq_shared_addr = sl_heap_alloc
406
 
                                        (sharedregion_get_heap(0),
407
 
                                        messageq_shared_mem_req(ipc_module->
408
 
                                                        ipc_shared_addr),
409
 
                                        sharedregion_get_cache_line_size(0));
410
 
 
411
 
                        slave->transport_sr_ptr =
412
 
                                sharedregion_get_srptr(msgq_shared_addr, 0);
413
 
                        if (slave->transport_sr_ptr ==
414
 
                                        SHAREDREGION_INVALIDSRPTR) {
415
 
                                status = IPC_E_FAIL;
416
 
                                pr_err("ipc_attach : "
417
 
                                        "sharedregion_get_srptr "
418
 
                                        "failed [0x%x]\n", status);
419
 
                        } else {
420
 
                                pr_err(
421
 
                                        "sharedregion_get_srptr : "
422
 
                                        "status [0x%x]\n", status);
423
 
                        }
424
 
                } else {
425
 
                        msgq_shared_addr = sharedregion_get_ptr(slave->
426
 
                                                        transport_sr_ptr);
427
 
                        if (msgq_shared_addr == NULL) {
428
 
                                status = IPC_E_FAIL;
429
 
                                pr_err("ipc_attach : "
430
 
                                        "sharedregion_get_ptr "
431
 
                                        "failed [0x%x]\n", status);
432
 
                        } else {
433
 
                                pr_err(
434
 
                                        "sharedregion_get_ptr : "
435
 
                                        "status [0x%x]\n", status);
436
 
                        }
437
 
                }
438
 
 
439
 
                if (status >= 0) {
440
 
                        /* create the messageq Transport instances */
441
 
                        status = messageq_attach(remote_proc_id,
442
 
                                                        msgq_shared_addr);
443
 
                        if (status < 0)
444
 
                                pr_err("ipc_attach : "
445
 
                                        "messageq_attach "
446
 
                                        "failed [0x%x]\n", status);
447
 
                        else
448
 
                                pr_err(
449
 
                                        "messageq_attach : "
450
 
                                        "status [0x%x]\n", status);
451
 
                }
452
 
        }
453
 
#if 0
454
 
        if (cache_enabled) {
455
 
                if (ipc_module->proc_entry[remote_proc_id].slave)
456
 
                        Cache_wbInv((void *)slave, reserved_size,
457
 
                                                Cache_Type_ALL, true);
458
 
        }
459
 
#endif
460
 
 
461
 
        if (status >= 0) {
462
 
                /* Finish the processor synchronization */
463
 
                status = ipc_proc_sync_finish(remote_proc_id,
464
 
                                                ipc_module->ipc_shared_addr);
465
 
                if (status < 0)
466
 
                        pr_err("ipc_attach : "
467
 
                                "ipc_proc_sync_finish "
468
 
                                "failed [0x%x]\n", status);
469
 
                else
470
 
                        pr_err(
471
 
                                "ipc_proc_sync_finish : "
472
 
                                "status [0x%x]\n", status);
473
 
        }
474
 
 
475
 
        if (status >= 0)
476
 
                ipc->is_attached = true;
477
 
        else
478
 
                pr_err("ipc_attach failed! status = 0x%x\n", status);
479
 
 
480
 
        return status;
481
 
}
482
 
 
483
 
 
484
 
/*
485
 
 * ============= ipc_detach ==============
486
 
 * detaches from a remote processor
487
 
 */
488
 
int ipc_detach(u16 remote_proc_id)
489
 
{
490
 
        int status = 0;
491
 
#if 0
492
 
        u32 reserved_size = ipc_reserved_size_per_proc();
493
 
#endif
494
 
        bool cache_enabled = sharedregion_is_cache_enabled(0);
495
 
        void *notify_shared_addr;
496
 
        void *nsrn_shared_addr;
497
 
        void *msgq_shared_addr;
498
 
        VOLATILE struct ipc_reserved *slave;
499
 
        VOLATILE struct ipc_reserved *master;
500
 
        struct ipc_proc_entry *ipc;
501
 
        u32 nsrn_mem_req = nameserver_remotenotify_shared_mem_req(NULL);
502
 
                /* prefetching into local variable because of
503
 
                                        later space restrictions */
504
 
 
505
 
        /* get the paramters associated with remote_proc_id */
506
 
        ipc = &(ipc_module->proc_entry[remote_proc_id]);
507
 
 
508
 
        if (ipc->is_attached == false) {
509
 
                status = IPC_E_INVALIDSTATE;
510
 
                goto exit;
511
 
        }
512
 
 
513
 
        /* determine the slave's slot */
514
 
        slave = ipc_get_slave_addr(remote_proc_id, ipc_module->
515
 
                                                        ipc_shared_addr);
516
 
 
517
 
        if (slave != NULL) {
518
 
#if 0
519
 
                if (unlikely(cache_enabled))
520
 
                        Cache_inv((void *) slave, reserved_size,
521
 
                                                Cache_Type_ALL, true);
522
 
#endif
523
 
                if (ipc->entry.setup_messageq) {
524
 
                        /* call messageq_detach for remote processor */
525
 
                        status = messageq_detach(remote_proc_id);
526
 
                        if (status < 0)
527
 
                                pr_err("ipc_detach : "
528
 
                                        "messageq_detach "
529
 
                                        "failed [0x%x]\n", status);
530
 
                        else
531
 
                                pr_err(
532
 
                                        "messageq_detach : "
533
 
                                        "status [0x%x]\n", status);
534
 
 
535
 
                        /* free the memory if slave processor */
536
 
                        if (ipc_module->proc_entry[remote_proc_id].slave) {
537
 
                                /* get the pointer to messageq transport
538
 
                                                        instance */
539
 
                                msgq_shared_addr = sharedregion_get_ptr(
540
 
                                                slave->transport_sr_ptr);
541
 
 
542
 
                                if (msgq_shared_addr != NULL) {
543
 
                                        /* free the memory back to sharedregion
544
 
                                                                0 heap */
545
 
                                        sl_heap_free(sharedregion_get_heap(0),
546
 
                                                msgq_shared_addr,
547
 
                                                messageq_shared_mem_req(
548
 
                                                        msgq_shared_addr));
549
 
                                }
550
 
 
551
 
                                /* set the pointer for messageq transport
552
 
                                                instance back to invalid */
553
 
                                slave->transport_sr_ptr =
554
 
                                                SHAREDREGION_INVALIDSRPTR;
555
 
                        }
556
 
                }
557
 
 
558
 
                if (ipc->entry.setup_notify) {
559
 
                        /* call nameserver_remotenotify_detach for
560
 
                                                        remote processor */
561
 
                        status = nameserver_remotenotify_detach(
562
 
                                                        remote_proc_id);
563
 
                        if (status < 0)
564
 
                                pr_err("ipc_detach : "
565
 
                                        "nameserver_remotenotify_detach "
566
 
                                        "failed [0x%x]\n", status);
567
 
                        else
568
 
                                pr_err(
569
 
                                        "nameserver_remotenotify_detach : "
570
 
                                        "status [0x%x]\n", status);
571
 
 
572
 
                        /* free the memory if slave processor */
573
 
                        if (ipc_module->proc_entry[remote_proc_id].slave) {
574
 
                                /* get the pointer to NSRN instance */
575
 
                                nsrn_shared_addr = sharedregion_get_ptr(
576
 
                                                        slave->nsrn_sr_ptr);
577
 
 
578
 
                                if (nsrn_shared_addr != NULL)
579
 
                                        /* free the memory back to
580
 
                                                        SharedRegion 0 heap */
581
 
                                        sl_heap_free(sharedregion_get_heap(0),
582
 
                                                nsrn_shared_addr,
583
 
                                                nsrn_mem_req);
584
 
 
585
 
                                /* set the pointer for NSRN instance back
586
 
                                                                to invalid */
587
 
                                slave->nsrn_sr_ptr =
588
 
                                                SHAREDREGION_INVALIDSRPTR;
589
 
                        }
590
 
                }
591
 
 
592
 
                if (ipc->entry.setup_notify && ipc->entry.setup_ipu_pm) {
593
 
                        /* call ipu_pm_detach for remote processor */
594
 
                        status = ipu_pm_detach(remote_proc_id);
595
 
                        if (status < 0)
596
 
                                pr_err("ipc_detach : "
597
 
                                        "ipu_pm_detach "
598
 
                                        "failed [0x%x]\n", status);
599
 
                        else
600
 
                                pr_err(
601
 
                                        "ipu_pm_detach : "
602
 
                                        "status [0x%x]\n", status);
603
 
                }
604
 
 
605
 
                if (ipc->entry.setup_notify) {
606
 
                        /* call notify_detach for remote processor */
607
 
                        status = notify_detach(remote_proc_id);
608
 
                        if (status < 0)
609
 
                                pr_err("ipc_detach : "
610
 
                                        "notify_detach "
611
 
                                        "failed [0x%x]\n", status);
612
 
                        else
613
 
                                pr_err(
614
 
                                        "notify_detach : "
615
 
                                        "status [0x%x]\n", status);
616
 
 
617
 
                        /* free the memory if slave processor */
618
 
                        if (ipc_module->proc_entry[remote_proc_id].slave) {
619
 
                                /* get the pointer to Notify instance */
620
 
                                notify_shared_addr = sharedregion_get_ptr(
621
 
                                                        slave->notify_sr_ptr);
622
 
 
623
 
                                if (notify_shared_addr != NULL) {
624
 
                                        /* free the memory back to
625
 
                                                        SharedRegion 0 heap */
626
 
                                        sl_heap_free(sharedregion_get_heap(0),
627
 
                                                notify_shared_addr,
628
 
                                                notify_shared_mem_req(
629
 
                                                        remote_proc_id,
630
 
                                                        notify_shared_addr));
631
 
                                }
632
 
 
633
 
                                /* set the pointer for Notify instance
634
 
                                                        back to invalid */
635
 
                                slave->notify_sr_ptr =
636
 
                                                SHAREDREGION_INVALIDSRPTR;
637
 
                        }
638
 
                }
639
 
 
640
 
                if (unlikely(cache_enabled)) {
641
 
                        if (ipc_module->proc_entry[remote_proc_id].slave) {
642
 
                                slave->started_key = 0;
643
 
                                slave->config_list_head =
644
 
                                                SHAREDREGION_INVALIDSRPTR;
645
 
#if 0
646
 
                                Cache_wbInv((void *)slave, reserved_size,
647
 
                                                        Cache_Type_ALL, true);
648
 
#endif
649
 
                        } else {
650
 
                                /* determine the master's slot */
651
 
                                master = ipc_get_master_addr(remote_proc_id,
652
 
                                                ipc_module->ipc_shared_addr);
653
 
 
654
 
                                if (master != NULL) {
655
 
                                        master->started_key = 0;
656
 
                                        master->config_list_head =
657
 
                                                SHAREDREGION_INVALIDSRPTR;
658
 
#if 0
659
 
                                        Cache_wbInv((void *) master,
660
 
                                                                reserved_size,
661
 
                                                                Cache_Type_ALL,
662
 
                                                                true);
663
 
#endif
664
 
                                }
665
 
                        }
666
 
                }
667
 
 
668
 
                /* Now detach the SharedRegion */
669
 
                status = sharedregion_detach(remote_proc_id);
670
 
                BUG_ON(status < 0);
671
 
 
672
 
                /* Now detach the GateMP */
673
 
                status = gatemp_detach(remote_proc_id, ipc_module->
674
 
                                                        gatemp_shared_addr);
675
 
                BUG_ON(status < 0);
676
 
        }
677
 
        ipc->is_attached = false;
678
 
 
679
 
exit:
680
 
        if (status < 0)
681
 
                pr_err("ipc_detach failed with status [0x%x]\n", status);
682
 
        return status;
683
 
}
684
 
 
685
 
 
686
 
/*
687
 
 * ========= ipc_control ==========
688
 
 * Function to destroy an ipc instance for a slave
689
 
 */
690
 
int
691
 
ipc_control(u16 proc_id, u32 cmd_id, void *arg)
692
 
{
693
 
        int status = IPC_S_SUCCESS;
694
 
 
695
 
        switch (cmd_id) {
696
 
        case IPC_CONTROLCMD_LOADCALLBACK:
697
 
        {
698
 
#if defined CONFIG_SYSLINK_USE_SYSMGR
699
 
                status = platform_load_callback(proc_id, arg);
700
 
                if (status < 0)
701
 
                        pr_err("ipc_control : platform_load_callback "
702
 
                                "failed [0x%x]\n", status);
703
 
#endif
704
 
        }
705
 
        break;
706
 
 
707
 
        case IPC_CONTROLCMD_STARTCALLBACK:
708
 
        {
709
 
#if defined CONFIG_SYSLINK_USE_SYSMGR
710
 
                status = platform_start_callback(proc_id, arg);
711
 
                if (status < 0)
712
 
                        pr_err("ipc_control : platform_start_callback"
713
 
                                " failed [0x%x]\n", status);
714
 
#endif
715
 
        }
716
 
        break;
717
 
 
718
 
        case IPC_CONTROLCMD_STOPCALLBACK:
719
 
        {
720
 
#if defined CONFIG_SYSLINK_USE_SYSMGR
721
 
                status = platform_stop_callback(proc_id, arg);
722
 
                if (status < 0)
723
 
                        pr_err("ipc_control : platform_stop_callback"
724
 
                                " failed [0x%x]\n", status);
725
 
#endif
726
 
        }
727
 
        break;
728
 
 
729
 
        default:
730
 
        {
731
 
                status = -EINVAL;
732
 
                pr_err("ipc_control : invalid "
733
 
                                " command code [0x%x]\n", cmd_id);
734
 
        }
735
 
        break;
736
 
        }
737
 
 
738
 
        return status;
739
 
}
740
 
 
741
 
 
742
 
/*
743
 
 *  ======== ipc_get_master_addr ========
744
 
 */
745
 
static void *ipc_get_master_addr(u16 remote_proc_id, void *shared_addr)
746
 
{
747
 
        u32 reserved_size = ipc_reserved_size_per_proc();
748
 
        int slot;
749
 
        u16 master_id;
750
 
        VOLATILE struct ipc_reserved *master;
751
 
 
752
 
        /* determine the master's proc_id and slot */
753
 
        if (multiproc_self() < remote_proc_id) {
754
 
                master_id = remote_proc_id;
755
 
                slot = multiproc_self();
756
 
        } else {
757
 
                master_id = multiproc_self();
758
 
                slot = remote_proc_id;
759
 
        }
760
 
 
761
 
        /* determine the reserve address for master between self and remote */
762
 
        master = (struct ipc_reserved *)((u32)shared_addr +
763
 
                        ((master_id * reserved_size) +
764
 
                        (slot * sizeof(struct ipc_reserved))));
765
 
 
766
 
        return (void *)master;
767
 
}
768
 
 
769
 
/*
770
 
 *  ======== ipc_get_region0_reserved_size ========
771
 
 */
772
 
static u32 ipc_get_region0_reserved_size(void)
773
 
{
774
 
        u32 reserved_size = ipc_reserved_size_per_proc();
775
 
 
776
 
        /* Calculate the total amount to reserve */
777
 
        reserved_size = reserved_size * multiproc_get_num_processors();
778
 
 
779
 
        return reserved_size;
780
 
}
781
 
 
782
 
/*
783
 
 *  ======== Ipc_getSlaveAddr ========
784
 
 */
785
 
static void *ipc_get_slave_addr(u16 remote_proc_id, void *shared_addr)
786
 
{
787
 
        u32 reserved_size = ipc_reserved_size_per_proc();
788
 
        int slot;
789
 
        u16 slave_id;
790
 
        VOLATILE struct ipc_reserved *slave;
791
 
 
792
 
        /* determine the slave's proc_id and slot */
793
 
        if (multiproc_self() < remote_proc_id) {
794
 
                slave_id = multiproc_self();
795
 
                slot = remote_proc_id - 1;
796
 
        } else {
797
 
                slave_id = remote_proc_id;
798
 
                slot = multiproc_self() - 1;
799
 
        }
800
 
 
801
 
        /* determine the reserve address for slave between self and remote */
802
 
        slave = (struct ipc_reserved *)((u32)shared_addr +
803
 
                        ((slave_id * reserved_size) +
804
 
                        (slot * sizeof(struct ipc_reserved))));
805
 
 
806
 
        return (void *)slave;
807
 
}
808
 
 
809
 
/*
810
 
 *  ======== Ipc_proc_syncStart ========
811
 
 *  The owner of SharedRegion 0 writes to its reserve memory address
812
 
 *  in region 0 to let the other processors know it has started.
813
 
 *  It then spins until the other processors start.
814
 
 *  The other processors write their reserve memory address in
815
 
 *  region 0 to let the owner processor know they've started.
816
 
 *  The other processors then spin until the owner processor writes
817
 
 *  to let them know that its finished the process of synchronization
818
 
 *  before continuing.
819
 
 */
820
 
static int ipc_proc_sync_start(u16 remote_proc_id, void *shared_addr)
821
 
{
822
 
#if 0
823
 
        u32 reserved_size = ipc_reserved_size_per_proc();
824
 
        bool cache_enabled = sharedregion_is_cache_enabled(0);
825
 
#endif
826
 
        int status = 0;
827
 
        VOLATILE struct ipc_reserved *self;
828
 
        VOLATILE struct ipc_reserved *remote;
829
 
        struct ipc_proc_entry *ipc;
830
 
 
831
 
        /* don't do any synchronization if proc_sync is NONE */
832
 
        if (ipc_module->proc_sync != IPC_PROCSYNC_NONE) {
833
 
                /* determine self and remote pointers */
834
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
835
 
                        self = ipc_get_slave_addr(remote_proc_id, shared_addr);
836
 
                        remote = ipc_get_master_addr(remote_proc_id,
837
 
                                                                shared_addr);
838
 
                } else {
839
 
                        self = ipc_get_master_addr(remote_proc_id, shared_addr);
840
 
                        remote = ipc_get_slave_addr(remote_proc_id,
841
 
                                                                shared_addr);
842
 
                }
843
 
 
844
 
                /* construct the config list */
845
 
                ipc = &(ipc_module->proc_entry[remote_proc_id]);
846
 
 
847
 
                ipc->local_config_list = (void *)&self->config_list_head;
848
 
                ipc->remote_config_list = (void *)&remote->config_list_head;
849
 
 
850
 
                ((struct ipc_config_head *)ipc->local_config_list)->first =
851
 
                                                (u32)SHAREDREGION_INVALIDSRPTR;
852
 
#if 0
853
 
                if (cache_enabled) {
854
 
                        Cache_wbInv(ipc->local_config_list,
855
 
                                         reserved_size,
856
 
                                         Cache_Type_ALL,
857
 
                                         true);
858
 
                }
859
 
#endif
860
 
 
861
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
862
 
                        /* set my processor's reserved key to start */
863
 
                        self->started_key = IPC_PROCSYNCSTART;
864
 
#if 0
865
 
                        /* write back my processor's reserve key */
866
 
                        if (cache_enabled)
867
 
                                Cache_wbInv((void *)self, reserved_size,
868
 
                                                        Cache_Type_ALL, true);
869
 
 
870
 
                        /* wait for remote processor to start */
871
 
                        if (cache_enabled)
872
 
                                Cache_inv((void *)remote, reserved_size,
873
 
                                                        Cache_Type_ALL, true);
874
 
#endif
875
 
                        if (remote->started_key != IPC_PROCSYNCSTART)
876
 
                                status = IPC_E_FAIL;
877
 
                        goto exit;
878
 
                }
879
 
 
880
 
#if 0
881
 
                /*  wait for remote processor to start */
882
 
                Cache_inv((void *)remote, reserved_size, Cache_Type_ALL, true);
883
 
#endif
884
 
                if ((self->started_key != IPC_PROCSYNCSTART) &&
885
 
                        (remote->started_key != IPC_PROCSYNCSTART)) {
886
 
                        status = IPC_E_FAIL;
887
 
                        goto exit;
888
 
                }
889
 
 
890
 
                if (status >= 0) {
891
 
                        /* set my processor's reserved key to start */
892
 
                        self->started_key = IPC_PROCSYNCSTART;
893
 
#if 0
894
 
                        /* write my processor's reserve key back */
895
 
                        if (cache_enabled)
896
 
                                Cache_wbInv((void *)self, reserved_size,
897
 
                                                        Cache_Type_ALL, true);
898
 
 
899
 
                        /* wait for remote processor to finish */
900
 
                        Cache_inv((void *)remote, reserved_size,
901
 
                                                        Cache_Type_ALL, true);
902
 
#endif
903
 
                        if (remote->started_key != IPC_PROCSYNCFINISH) {
904
 
                                status = IPC_E_FAIL;
905
 
                                goto exit;
906
 
                        }
907
 
                }
908
 
        }
909
 
exit:
910
 
        if (status < 0)
911
 
                pr_err("ipc_proc_sync_start failed: status [0x%x]\n", status);
912
 
        else
913
 
                pr_err("ipc_proc_sync_start done\n");
914
 
 
915
 
        return status;
916
 
}
917
 
 
918
 
/*
919
 
 *  ======== Ipc_proc_syncFinish ========
920
 
 *  Each processor writes its reserve memory address in SharedRegion 0
921
 
 *  to let the other processors know its finished the process of
922
 
 *  synchronization.
923
 
 */
924
 
static int ipc_proc_sync_finish(u16 remote_proc_id, void *shared_addr)
925
 
{
926
 
#if 0
927
 
        u32 reserved_size = ipc_reserved_size_per_proc();
928
 
        bool cache_enabled = sharedregion_is_cache_enabled(0);
929
 
#endif
930
 
        VOLATILE struct ipc_reserved *self;
931
 
        VOLATILE struct ipc_reserved *remote;
932
 
 
933
 
        /* don't do any synchronization if proc_sync is NONE */
934
 
        if (ipc_module->proc_sync != IPC_PROCSYNC_NONE) {
935
 
                /* determine self pointer */
936
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
937
 
                        self = ipc_get_slave_addr(remote_proc_id, shared_addr);
938
 
                        remote = ipc_get_master_addr(remote_proc_id,
939
 
                                        shared_addr);
940
 
                } else {
941
 
                        self = ipc_get_master_addr(remote_proc_id,
942
 
                                                                shared_addr);
943
 
                        remote = ipc_get_slave_addr(remote_proc_id,
944
 
                                                                shared_addr);
945
 
                }
946
 
                /* set my processor's reserved key to finish */
947
 
                self->started_key = IPC_PROCSYNCFINISH;
948
 
#if 0
949
 
                /* write back my processor's reserve key */
950
 
                if (cache_enabled)
951
 
                        Cache_wbInv((void *)self, reserved_size,
952
 
                                                Cache_Type_ALL, true);
953
 
#endif
954
 
                /* if slave processor, wait for remote to finish sync */
955
 
                if (ipc_module->proc_entry[remote_proc_id].slave) {
956
 
                        /* wait for remote processor to finish */
957
 
                        do {
958
 
#if 0
959
 
                                if (cacheEnabled) {
960
 
                                        Cache_inv((Ptr)remote, reservedSize,
961
 
                                                Cache_Type_ALL, TRUE);
962
 
                                }
963
 
#endif
964
 
                        } while (remote->started_key != IPC_PROCSYNCFINISH);
965
 
                }
966
 
        }
967
 
 
968
 
        return IPC_S_SUCCESS;
969
 
}
970
 
 
971
 
/*
972
 
 * ======== ipc_read_config ========
973
 
 */
974
 
int ipc_read_config(u16 remote_proc_id, u32 tag, void *cfg, u32 size)
975
 
{
976
 
#if 0
977
 
        bool cache_enabled = sharedregion_is_cache_enabled(0);
978
 
#endif
979
 
        int status = IPC_E_FAIL;
980
 
        VOLATILE struct ipc_config_entry *entry;
981
 
 
982
 
        if (ipc_module->ref_count == 0) {
983
 
                status = -ENODEV;
984
 
                goto exit;
985
 
        }
986
 
 
987
 
        if (ipc_module->proc_entry[remote_proc_id].is_attached == false) {
988
 
                status = -ENODEV;
989
 
                goto exit;
990
 
        }
991
 
 
992
 
 
993
 
#if 0
994
 
        if (cache_enabled) {
995
 
                Cache_inv(ipc_module->proc_entry[remote_proc_id].
996
 
                                                remote_config_list,
997
 
                                sharedregion_get_cache_line_size(0),
998
 
                                Cache_Type_ALL,
999
 
                                true);
1000
 
        }
1001
 
#endif
1002
 
        entry = (struct ipc_config_entry *)((struct ipc_config_head *)
1003
 
                ipc_module->proc_entry[remote_proc_id].remote_config_list)->
1004
 
                                                                        first;
1005
 
 
1006
 
        while ((u32 *)entry != SHAREDREGION_INVALIDSRPTR) {
1007
 
                entry = (struct ipc_config_entry *)
1008
 
                                sharedregion_get_ptr((u32 *)entry);
1009
 
                if (entry == NULL) {
1010
 
                        status = IPC_E_FAIL;
1011
 
                        goto exit;
1012
 
                }
1013
 
#if 0
1014
 
                /* Traverse the list to find the tag */
1015
 
                if (cache_enabled) {
1016
 
                        Cache_inv((void *)entry,
1017
 
                                size + sizeof(struct ipc_config_entry),
1018
 
                                Cache_Type_ALL,
1019
 
                                true);
1020
 
                }
1021
 
#endif
1022
 
 
1023
 
                if ((entry->remote_proc_id == multiproc_self()) &&
1024
 
                        (entry->local_proc_id == remote_proc_id) &&
1025
 
                        (entry->tag == tag)) {
1026
 
 
1027
 
                        if (size == entry->size)
1028
 
                                memcpy(cfg,
1029
 
                                        (void *)((u32)entry + sizeof(struct
1030
 
                                                        ipc_config_entry)),
1031
 
                                        entry->size);
1032
 
                        else
1033
 
                                status = IPC_E_FAIL;
1034
 
                } else {
1035
 
                        entry = (struct ipc_config_entry *)entry->next;
1036
 
                }
1037
 
        }
1038
 
 
1039
 
 
1040
 
exit:
1041
 
        if (status < 0)
1042
 
                pr_err("ipc_read_config failed: status [0x%x]\n", status);
1043
 
 
1044
 
        return status;
1045
 
}
1046
 
 
1047
 
/*
1048
 
 *  ======== ipc_reserved_size_per_proc ========
1049
 
 */
1050
 
static u32 ipc_reserved_size_per_proc(void)
1051
 
{
1052
 
        u32 reserved_size = sizeof(struct ipc_reserved) *
1053
 
                                        multiproc_get_num_processors();
1054
 
        u32 cache_line_size = sharedregion_get_cache_line_size(0);
1055
 
 
1056
 
        /* Calculate amount to reserve per processor */
1057
 
        if (cache_line_size > reserved_size)
1058
 
                /* Use cache_line_size if larger than reserved_size */
1059
 
                reserved_size = cache_line_size;
1060
 
        else
1061
 
                /* Round reserved_size to cache_line_size */
1062
 
                reserved_size = ROUND_UP(reserved_size, cache_line_size);
1063
 
 
1064
 
        return reserved_size;
1065
 
}
1066
 
 
1067
 
/*!
1068
 
 *  ======== ipc_write_config ========
1069
 
 */
1070
 
int ipc_write_config(u16 remote_proc_id, u32 tag, void *cfg, u32 size)
1071
 
{
1072
 
#if 0
1073
 
        bool cache_enabled = sharedregion_is_cache_enabled(0);
1074
 
#endif
1075
 
        u32 cache_line_size = sharedregion_get_cache_line_size(0);
1076
 
        int status = IPC_S_SUCCESS;
1077
 
        struct ipc_config_entry *entry;
1078
 
 
1079
 
        if (ipc_module->ref_count == 0) {
1080
 
                status = -ENODEV;
1081
 
                goto exit;
1082
 
        }
1083
 
 
1084
 
        if (ipc_module->proc_entry[remote_proc_id].is_attached == false) {
1085
 
                status = -ENODEV;
1086
 
                goto exit;
1087
 
        }
1088
 
 
1089
 
        /* Allocate memory from the shared heap (System Heap) */
1090
 
        entry = sl_heap_alloc(sharedregion_get_heap(0),
1091
 
                                size + sizeof(struct ipc_config_entry),
1092
 
                                cache_line_size);
1093
 
 
1094
 
        if (entry == NULL) {
1095
 
                status = IPC_E_FAIL;
1096
 
                goto exit;
1097
 
        }
1098
 
 
1099
 
        entry->remote_proc_id = remote_proc_id;
1100
 
        entry->local_proc_id = multiproc_self();
1101
 
        entry->tag = tag;
1102
 
        entry->size = size;
1103
 
        memcpy((void *)((u32)entry + sizeof(struct ipc_config_entry)),
1104
 
                                                                cfg, size);
1105
 
 
1106
 
        /* Create a linked-list of config */
1107
 
        if (((struct ipc_config_head *)ipc_module->
1108
 
                proc_entry[remote_proc_id].local_config_list)->first
1109
 
                == (u32)SHAREDREGION_INVALIDSRPTR) {
1110
 
 
1111
 
                entry->next = (u32)SHAREDREGION_INVALIDSRPTR;
1112
 
                ((struct ipc_config_head *)ipc_module->
1113
 
                        proc_entry[remote_proc_id].local_config_list)->first =
1114
 
                                (u32)sharedregion_get_srptr(entry, 0);
1115
 
 
1116
 
                if (((struct ipc_config_head *)ipc_module->
1117
 
                        proc_entry[remote_proc_id].local_config_list)->first
1118
 
                        == (u32)SHAREDREGION_INVALIDSRPTR)
1119
 
                        status = IPC_E_FAIL;
1120
 
        } else {
1121
 
                entry->next = ((struct ipc_config_head *)ipc_module->
1122
 
                        proc_entry[remote_proc_id].local_config_list)->first;
1123
 
 
1124
 
                ((struct ipc_config_head *)ipc_module->
1125
 
                        proc_entry[remote_proc_id].local_config_list)->first =
1126
 
                        (u32)sharedregion_get_srptr(entry, 0);
1127
 
                if (((struct ipc_config_head *)ipc_module->
1128
 
                        proc_entry[remote_proc_id].local_config_list)->first
1129
 
                        == (u32)SHAREDREGION_INVALIDSRPTR)
1130
 
                        status = IPC_E_FAIL;
1131
 
        }
1132
 
#if 0
1133
 
        if (cache_enabled) {
1134
 
                Cache_wbInv(ipc_module->proc_entry[remote_proc_id].
1135
 
                                                        local_config_list,
1136
 
                        sharedregion_get_cache_line_size(0),
1137
 
                        Cache_Type_ALL,
1138
 
                        false);
1139
 
 
1140
 
                Cache_wbInv(entry,
1141
 
                        size + sizeof(struct ipc_config_entry),
1142
 
                        Cache_Type_ALL,
1143
 
                        true);
1144
 
        }
1145
 
#endif
1146
 
 
1147
 
exit:
1148
 
        if (status < 0)
1149
 
                pr_err("ipc_write_config failed: status [0x%x]\n", status);
1150
 
 
1151
 
        return status;
1152
 
}
1153
 
 
1154
 
 
1155
 
/*
1156
 
 *  ======== ipc_start ========
1157
 
 */
1158
 
int ipc_start(void)
1159
 
{
1160
 
        int status = 0;
1161
 
        int i;
1162
 
        struct sharedregion_entry entry;
1163
 
        void *ipc_shared_addr;
1164
 
        void *gatemp_shared_addr;
1165
 
        void *ipu_pm_shared_addr;
1166
 
        struct gatemp_params gatemp_params;
1167
 
        bool line_available;
1168
 
 
1169
 
        /* This sets the ref_count variable is not initialized, upper 16 bits
1170
 
         * is written with module Id to ensure correctness of ref_count
1171
 
         * variable.
1172
 
         */
1173
 
        atomic_cmpmask_and_set(&(ipc_module->start_ref_count),
1174
 
                                                IPC_MAKE_MAGICSTAMP(0),
1175
 
                                                IPC_MAKE_MAGICSTAMP(0));
1176
 
        if (atomic_inc_return(&(ipc_module->start_ref_count))
1177
 
                != IPC_MAKE_MAGICSTAMP(1u)) {
1178
 
                status = IPC_S_SUCCESS;
1179
 
                goto exit;
1180
 
        }
1181
 
 
1182
 
        /* get region 0 information */
1183
 
        sharedregion_get_entry(0, &entry);
1184
 
 
1185
 
        /* if entry is not valid then return */
1186
 
        if (entry.is_valid == false) {
1187
 
                status = IPC_E_FAIL;
1188
 
                goto exit;
1189
 
        }
1190
 
        /*
1191
 
         *  Need to reserve memory in region 0 for processor synchronization.
1192
 
         *  This must done before SharedRegion_start().
1193
 
         */
1194
 
        ipc_shared_addr = sharedregion_reserve_memory(0,
1195
 
                                        ipc_get_region0_reserved_size());
1196
 
 
1197
 
        /* must reserve memory for gatemp before sharedregion_start() */
1198
 
        gatemp_shared_addr = sharedregion_reserve_memory(0,
1199
 
                                        gatemp_get_region0_reserved_size());
1200
 
 
1201
 
        /* Init params for default gate(must match those in gatemp_start() */
1202
 
        gatemp_params_init(&gatemp_params);
1203
 
        gatemp_params.local_protect = GATEMP_LOCALPROTECT_TASKLET;
1204
 
 
1205
 
        if (multiproc_get_num_processors() > 1)
1206
 
                gatemp_params.remote_protect = GATEMP_REMOTEPROTECT_SYSTEM;
1207
 
        else
1208
 
                gatemp_params.remote_protect = GATEMP_REMOTEPROTECT_NONE;
1209
 
 
1210
 
        /* reserve memory for default gate before SharedRegion_start() */
1211
 
        sharedregion_reserve_memory(0, gatemp_shared_mem_req(&gatemp_params));
1212
 
 
1213
 
        /* reserve memory for PM region */
1214
 
        ipu_pm_shared_addr = sharedregion_reserve_memory(0,
1215
 
                                                ipu_pm_mem_req(NULL));
1216
 
 
1217
 
        /* clear the reserved memory */
1218
 
        sharedregion_clear_reserved_memory();
1219
 
 
1220
 
        /* Set shared addresses */
1221
 
        ipc_module->ipc_shared_addr = ipc_shared_addr;
1222
 
        ipc_module->gatemp_shared_addr = gatemp_shared_addr;
1223
 
        ipc_module->ipu_pm_shared_addr = ipu_pm_shared_addr;
1224
 
 
1225
 
        /* create default GateMP, must be called before sharedregion_start */
1226
 
        status = gatemp_start(ipc_module->gatemp_shared_addr);
1227
 
        if (status < 0) {
1228
 
                status = IPC_E_FAIL;
1229
 
                goto exit;
1230
 
        }
1231
 
 
1232
 
        /* create HeapMemMP in each SharedRegion */
1233
 
        status = sharedregion_start();
1234
 
        if (status < 0) {
1235
 
                status = IPC_E_FAIL;
1236
 
                goto exit;
1237
 
        }
1238
 
 
1239
 
        /* Call attach for all procs if proc_sync is ALL */
1240
 
        if (ipc_module->proc_sync == IPC_PROCSYNC_ALL) {
1241
 
                /* Must attach to owner first to get default GateMP and
1242
 
                 * HeapMemMP */
1243
 
                if (multiproc_self() != entry.owner_proc_id) {
1244
 
                        do {
1245
 
                                status = ipc_attach(entry.owner_proc_id);
1246
 
                        } while (status < 0);
1247
 
                }
1248
 
 
1249
 
                /* Loop to attach to all other processors */
1250
 
                for (i = 0; i < multiproc_get_num_processors(); i++) {
1251
 
                        if ((i == multiproc_self())
1252
 
                                        || (i == entry.owner_proc_id))
1253
 
                                continue;
1254
 
                        line_available =
1255
 
                                notify_setup_proxy_int_line_available(i);
1256
 
                        if (!line_available)
1257
 
                                continue;
1258
 
                        /* call Ipc_attach for every remote processor */
1259
 
                        do {
1260
 
                                status = ipc_attach(i);
1261
 
                        } while (status < 0);
1262
 
                }
1263
 
        }
1264
 
 
1265
 
exit:
1266
 
        if (status < 0)
1267
 
                pr_err("ipc_start failed: status [0x%x]\n", status);
1268
 
 
1269
 
        return status;
1270
 
}
1271
 
 
1272
 
 
1273
 
/*
1274
 
 *  ======== ipc_stop ========
1275
 
 */
1276
 
int ipc_stop(void)
1277
 
{
1278
 
        int status = IPC_S_SUCCESS;
1279
 
        int tmp_status = IPC_S_SUCCESS;
1280
 
        struct sharedregion_entry entry;
1281
 
        struct gatemp_params gatemp_params;
1282
 
 
1283
 
        if (unlikely(atomic_cmpmask_and_lt(&(ipc_module->start_ref_count),
1284
 
                                   IPC_MAKE_MAGICSTAMP(0),
1285
 
                                   IPC_MAKE_MAGICSTAMP(1)) == true)) {
1286
 
                status = IPC_E_FAIL;
1287
 
                goto exit;
1288
 
        }
1289
 
 
1290
 
        if (likely(atomic_dec_return(&ipc_module->start_ref_count)
1291
 
                                == IPC_MAKE_MAGICSTAMP(0))) {
1292
 
                /* get region 0 information */
1293
 
                sharedregion_get_entry(0, &entry);
1294
 
 
1295
 
                /* if entry is not valid then return */
1296
 
                if (entry.is_valid == false) {
1297
 
                        status = IPC_E_FAIL;
1298
 
                        goto exit;
1299
 
                }
1300
 
 
1301
 
 
1302
 
                /*
1303
 
                *  Need to unreserve memory in region 0 for processor
1304
 
                *  synchronization. This must done before sharedregion_stop().
1305
 
                */
1306
 
                sharedregion_unreserve_memory(0,
1307
 
                                        ipc_get_region0_reserved_size());
1308
 
 
1309
 
                /* must unreserve memory for GateMP before
1310
 
                                                        sharedregion_stop() */
1311
 
                sharedregion_unreserve_memory(0,
1312
 
                                        gatemp_get_region0_reserved_size());
1313
 
 
1314
 
                /* Init params for default gate (must match those
1315
 
                                                in gatemp_stop() */
1316
 
                gatemp_params_init(&gatemp_params);
1317
 
                gatemp_params.local_protect = GATEMP_LOCALPROTECT_TASKLET;
1318
 
 
1319
 
                if (multiproc_get_num_processors() > 1)
1320
 
                        gatemp_params.remote_protect =
1321
 
                                        GATEMP_REMOTEPROTECT_SYSTEM;
1322
 
                else
1323
 
                        gatemp_params.remote_protect =
1324
 
                                        GATEMP_REMOTEPROTECT_NONE;
1325
 
 
1326
 
                /* unreserve memory for default gate before
1327
 
                                        sharedregion_stop() */
1328
 
                sharedregion_unreserve_memory(0,
1329
 
                        gatemp_shared_mem_req(&gatemp_params));
1330
 
 
1331
 
                /* must unreserve memory for PM before sharedregion_stop() */
1332
 
                sharedregion_unreserve_memory(0, ipu_pm_mem_req(NULL));
1333
 
 
1334
 
                /* Delete heapmemmp in each sharedregion */
1335
 
                status = sharedregion_stop();
1336
 
                if (status < 0) {
1337
 
                        status = IPC_E_FAIL;
1338
 
                        goto exit;
1339
 
                }
1340
 
 
1341
 
                /* delete default gatemp, must be called after
1342
 
                 * sharedregion_stop
1343
 
                 */
1344
 
                tmp_status = gatemp_stop();
1345
 
                if ((tmp_status < 0) && (status >= 0)) {
1346
 
                        status = IPC_E_FAIL;
1347
 
                        goto exit;
1348
 
                }
1349
 
 
1350
 
                ipc_module->ipu_pm_shared_addr = NULL;
1351
 
                ipc_module->gatemp_shared_addr = NULL;
1352
 
                ipc_module->ipc_shared_addr = NULL;
1353
 
        }
1354
 
exit:
1355
 
        if (status < 0)
1356
 
                pr_err("ipc_stop failed: status [0x%x]\n", status);
1357
 
 
1358
 
        return status;
1359
 
}
1360
 
 
1361
 
 
1362
 
/*
1363
 
 *  ======== ipc_get_config ========
1364
 
 */
1365
 
void ipc_get_config(struct ipc_config *cfg_params)
1366
 
{
1367
 
        int key;
1368
 
        int status = 0;
1369
 
 
1370
 
        BUG_ON(cfg_params == NULL);
1371
 
 
1372
 
        if (cfg_params == NULL) {
1373
 
                status = -EINVAL;
1374
 
                goto exit;
1375
 
        }
1376
 
 
1377
 
 
1378
 
        key = gate_enter_system();
1379
 
        if (ipc_module->ref_count == 0)
1380
 
                cfg_params->proc_sync = IPC_PROCSYNC_ALL;
1381
 
        else
1382
 
                memcpy((void *) cfg_params, (void *) &ipc_module->cfg,
1383
 
                                                sizeof(struct ipc_config));
1384
 
 
1385
 
        gate_leave_system(key);
1386
 
 
1387
 
exit:
1388
 
        if (status < 0)
1389
 
                pr_err("ipc_get_config failed: status [0x%x]\n", status);
1390
 
}
1391
 
 
1392
 
 
1393
 
/* Sets up ipc for this processor. */
1394
 
int ipc_setup(const struct ipc_config *cfg)
1395
 
{
1396
 
        int status = IPC_S_SUCCESS;
1397
 
        struct ipc_config tmp_cfg;
1398
 
        int key;
1399
 
        int i;
1400
 
 
1401
 
        key = gate_enter_system();
1402
 
        ipc_module->ref_count++;
1403
 
 
1404
 
        /* This sets the ref_count variable is not initialized, upper 16 bits is
1405
 
         * written with module Id to ensure correctness of ref_count variable.
1406
 
         */
1407
 
        if (ipc_module->ref_count > 1) {
1408
 
                status = IPC_S_ALREADYSETUP;
1409
 
                gate_leave_system(key);
1410
 
                goto exit;
1411
 
        }
1412
 
 
1413
 
        gate_leave_system(key);
1414
 
        if (cfg == NULL) {
1415
 
                ipc_get_config(&tmp_cfg);
1416
 
                cfg = &tmp_cfg;
1417
 
        }
1418
 
 
1419
 
        /* Copy the cfg */
1420
 
        memcpy(&ipc_module->cfg, cfg, sizeof(struct ipc_config));
1421
 
 
1422
 
        ipc_module->proc_sync = cfg->proc_sync;
1423
 
 
1424
 
        status = platform_setup();
1425
 
        if (status < 0) {
1426
 
                key = gate_enter_system();
1427
 
                ipc_module->ref_count--;
1428
 
                gate_leave_system(key);
1429
 
                status = IPC_E_FAIL;
1430
 
                goto exit;
1431
 
        }
1432
 
 
1433
 
        /* Following can be done regardless of status */
1434
 
        for (i = 0; i < multiproc_get_num_processors(); i++)
1435
 
                ipc_module->proc_entry[i].is_attached = false;
1436
 
 
1437
 
exit:
1438
 
        if (status < 0)
1439
 
                pr_err("ipc_setup failed: status [0x%x]\n", status);
1440
 
 
1441
 
        return status;
1442
 
}
1443
 
 
1444
 
 
1445
 
/*
1446
 
 * =========== ipc_destroy ==========
1447
 
 * Destroys ipc for this processor.
1448
 
 */
1449
 
int ipc_destroy(void)
1450
 
{
1451
 
        int status = IPC_S_SUCCESS;
1452
 
        int key;
1453
 
 
1454
 
        key = gate_enter_system();
1455
 
        ipc_module->ref_count--;
1456
 
 
1457
 
        if (ipc_module->ref_count < 0) {
1458
 
                gate_leave_system(key);
1459
 
                status = IPC_E_INVALIDSTATE;
1460
 
                goto exit;
1461
 
        }
1462
 
 
1463
 
        if (ipc_module->ref_count == 0) {
1464
 
                gate_leave_system(key);
1465
 
                if (unlikely(atomic_cmpmask_and_lt(
1466
 
                                        &(ipc_module->start_ref_count),
1467
 
                                        IPC_MAKE_MAGICSTAMP(0),
1468
 
                                        IPC_MAKE_MAGICSTAMP(1)) == false)) {
1469
 
                        /*
1470
 
                         * ipc_start was called, but ipc_stop never happened.
1471
 
                         * Need to call ipc_stop here.
1472
 
                         */
1473
 
                        /* Set the count to 1 so only need to call stop once. */
1474
 
                        atomic_set(&ipc_module->start_ref_count,
1475
 
                                        IPC_MAKE_MAGICSTAMP(1));
1476
 
                        ipc_stop();
1477
 
                }
1478
 
                status = platform_destroy();
1479
 
                if (status < 0) {
1480
 
                        status = IPC_E_FAIL;
1481
 
                        goto exit;
1482
 
                }
1483
 
        } else
1484
 
                gate_leave_system(key);
1485
 
 
1486
 
exit:
1487
 
        if (status < 0)
1488
 
                pr_err("ipc_destroy failed: status [0x%x]\n", status);
1489
 
 
1490
 
        return status;
1491
 
}
1492
 
 
1493
 
 
1494
 
/*
1495
 
 * ====== ipc_create =======
1496
 
 *  Creates a IPC.
1497
 
 */
1498
 
int ipc_create(u16 remote_proc_id, struct ipc_params *params)
1499
 
{
1500
 
        ipc_module->proc_entry[remote_proc_id].entry.setup_messageq =
1501
 
                                                        params->setup_messageq;
1502
 
        ipc_module->proc_entry[remote_proc_id].entry.setup_notify =
1503
 
                                                        params->setup_notify;
1504
 
        ipc_module->proc_entry[remote_proc_id].entry.setup_ipu_pm =
1505
 
                                                        params->setup_ipu_pm;
1506
 
        ipc_module->proc_entry[remote_proc_id].entry.remote_proc_id =
1507
 
                                                        remote_proc_id;
1508
 
 
1509
 
        /* Assert that the proc_sync is same as configured for the module. */
1510
 
        BUG_ON(ipc_module->proc_sync != params->proc_sync);
1511
 
 
1512
 
        return IPC_S_SUCCESS;
1513
 
}