summaryrefslogtreecommitdiff
path: root/hifi/xaf/host-apf/proxy/xf-proxy.c
blob: 0af45179c68e5454324294df89d99ca20b4b930f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

#define MODULE_TAG                      PROXY

/*******************************************************************************
 * Includes
 ******************************************************************************/

#include "xf.h"

/*******************************************************************************
 * Tracing configuration
 ******************************************************************************/

TRACE_TAG(INIT, 1);
TRACE_TAG(CMD, 1);
TRACE_TAG(EXEC, 1);
TRACE_TAG(RSP, 1);
TRACE_TAG(REG, 1);
TRACE_TAG(MEM, 1);
TRACE_TAG(GRAPH, 1);
TRACE_TAG(BUFFER, 1);

/*******************************************************************************
 * Internal functions definitions
 ******************************************************************************/

/* ...execute proxy command synchronously */
static inline int xf_proxy_cmd_exec(xf_proxy_t *proxy, xf_user_msg_t *msg)
{
    xf_proxy_msg_t  m;
    
    /* ...send command to remote proxy */
    m.id = msg->id, m.opcode = msg->opcode, m.length = msg->length;

    /* ...translate address */
    XF_CHK_ERR((m.address = xf_proxy_b2a(proxy, msg->buffer)) != XF_PROXY_BADADDR, -EINVAL);

    /* ...pass command to remote proxy */
    XF_CHK_API(xf_ipc_send(&proxy->ipc, &m, msg->buffer));

    /* ...wait for response reception indication from proxy thread */
    XF_CHK_API(xf_proxy_response_get(proxy, &m));
    
    /* ...copy parameters */
    msg->id = m.id, msg->opcode = m.opcode, msg->length = m.length;

    /* ...translate address back to virtual space */
    XF_CHK_ERR((msg->buffer = xf_proxy_a2b(proxy, m.address)) != (void *)-1, -EBADFD);
    
    TRACE(EXEC, _b("proxy[%p]: command done: [%08x:%p:%u]"), proxy, msg->opcode, msg->buffer, msg->length);

    return 0;
}

#if 0
/* ...pass command to remote DSP */
static inline int xf_proxy_cmd(xf_proxy_t *proxy, xf_handle_t *handle, xf_user_msg_t *m)
{
    xf_proxy_msg_t  msg;

    /* ...set session-id of the message */
    msg.id = __XF_MSG_ID(__XF_AP_CLIENT(proxy->core, handle->client), m->id);
    msg.opcode = m->opcode;
    msg.length = m->length;

    /* ...translate buffer pointer to shared address */
    XF_CHK_ERR((msg.address = xf_proxy_b2a(proxy, m->buffer)) != XF_PROXY_BADADDR, -EINVAL);

    /* ...submit command message to IPC layer */
    return XF_CHK_API(xf_ipc_send(&proxy->ipc, &msg, m->buffer));
}
#endif /* 0 */

/* ...allocate local client-id number */
static inline u32 xf_client_alloc(xf_proxy_t *proxy, xf_handle_t *handle)
{
    u32     client;
    
    if ((client = proxy->cmap[0].next) != 0)
    {
        /* ...pop client from free clients list */
        proxy->cmap[0].next = proxy->cmap[client].next;

        /* ...put client handle into association map */
        handle->client = client, proxy->cmap[client].handle = handle;
    }

    return client;
}

/* ...recycle local client-id number */
static inline void xf_client_free(xf_proxy_t *proxy, xf_handle_t *handle)
{
    u32     client = handle->client;
    
    /* ...push client into head of the free clients list */
    proxy->cmap[client].next = proxy->cmap[0].next;
    
    /* ...adjust head of free clients */
    proxy->cmap[0].next = client;
}

/* ...lookup client basing on its local id */
static inline xf_handle_t * xf_client_lookup(xf_proxy_t *proxy, u32 client)
{
    /* ...client index must be in proper range */
    BUG(client >= XF_CFG_PROXY_MAX_CLIENTS, _x("Invalid client index: %u"), client);
    
    /* ...check if client index is small */
    if (proxy->cmap[client].next < XF_CFG_PROXY_MAX_CLIENTS)
        return NULL;
    else
        return proxy->cmap[client].handle;
}

/* ...create new client on remote core */
static inline int xf_client_register(xf_proxy_t *proxy, xf_handle_t *handle, xf_id_t id, u32 core)
{
    void           *b = xf_handle_aux(handle);
    xf_user_msg_t   msg;
    
    /* ...set session-id: source is local proxy, destination is remote proxy */
    msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), __XF_DSP_PROXY(core));
    msg.opcode = XF_REGISTER;
    msg.buffer = b;
    msg.length = strlen(id) + 1;

    /* ...copy component identifier */
    strncpy(b, id, xf_buffer_length(handle->aux));

    /* ...execute command synchronously */
    XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg));
    
    /* ...check operation is successfull */
    XF_CHK_ERR(msg.opcode == (u32) XF_REGISTER, -EFAULT);
    
    /* ...save received component global client-id */
    handle->id = XF_MSG_SRC(msg.id);  

    TRACE(REG, _b("[%p]=[%s:%u:%u]"), handle, id, XF_PORT_CORE(handle->id), XF_PORT_CLIENT(handle->id));

    return 0;
}

/* ...unregister client from remote proxy */
static inline int xf_client_unregister(xf_proxy_t *proxy, xf_handle_t *handle)
{
    xf_user_msg_t   msg;

    /* ...make sure the client is consistent */
    BUG(proxy->cmap[handle->client].handle != handle, _x("Invalid handle: %p"), handle);
    
    /* ...set message parameters */
    msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), handle->id);
    msg.opcode = XF_UNREGISTER;
    msg.buffer = NULL;
    msg.length = 0;

    /* ...synchronously execute command on remote proxy */
    XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg));
    
    /* ...opcode must be XF_UNREGISTER - tbd */
    BUG(msg.opcode != XF_UNREGISTER, _x("Invalid opcode: %X"), msg.opcode);
    
    TRACE(REG, _b("%p[%u:%u] unregistered"), handle, XF_PORT_CORE(handle->id), XF_PORT_CLIENT(handle->id));

    return 0;
}

/* ...allocate shared buffer */
static inline int xf_proxy_buffer_alloc(xf_proxy_t *proxy, u32 length, void **buffer)
{
    u32             core = proxy->core;
    xf_user_msg_t   msg;
    
    /* ...prepare command parameters */
    msg.id = __XF_MSG_ID(__XF_AP_PROXY(core), __XF_DSP_PROXY(core));
    msg.opcode = XF_ALLOC;
    msg.length = length;
    msg.buffer = NULL;

    /* ...synchronously execute command on remote proxy */
    XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg));

    /* ...check if response is valid */
    XF_CHK_ERR(msg.opcode == XF_ALLOC, -EBADFD);

    /* ...check if allocation is successful */
    XF_CHK_ERR(msg.buffer != NULL, -ENOMEM);

    /* ...save output parameter */
    *buffer = msg.buffer;
    
    TRACE(MEM, _b("proxy-%u: allocated [%p:%u]"), core, *buffer, length);

    return 0;
}

/* ...free shared AP-DSP memory */
static inline int xf_proxy_buffer_free(xf_proxy_t *proxy, void *buffer, u32 length)
{
    u32             core = proxy->core;
    xf_user_msg_t   msg;

    /* ...prepare command parameters */
    msg.id = __XF_MSG_ID(__XF_AP_PROXY(core), __XF_DSP_PROXY(core));
    msg.opcode = XF_FREE;
    msg.length = length;
    msg.buffer = buffer;
    
    /* ...synchronously execute command on remote proxy */
    XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg));

    /* ...check if response is valid */
    XF_CHK_ERR(msg.opcode == XF_FREE, -EBADFD);

    TRACE(MEM, _b("proxy-%u: free [%p:%u]"), core, buffer, length);

    return 0;
}

/*******************************************************************************
 * Proxy interface asynchronous receiving thread
 ******************************************************************************/

static void * xf_proxy_thread(void *arg)
{
    xf_proxy_t     *proxy = arg;
    xf_handle_t    *client;
    int             r;
    
    /* ...start polling thread */
    while (1)
    {
        xf_proxy_msg_t  m;
        xf_user_msg_t   msg;
        
        /* ...wait for response from remote proxy (infinite timeout) */
        if ((r = xf_ipc_wait(&proxy->ipc, 0)) < 0)
            break;

        /* ...retrieve all responses received */
        while ((r = xf_ipc_recv(&proxy->ipc, &m, &msg.buffer)) == sizeof(m))
        {
            /* ...make sure we have proper core identifier of SHMEM interface */
            BUG(XF_MSG_DST_CORE(m.id) != proxy->core, _x("Invalid session-id: %X (core=%u)"), m.id, proxy->core);

            /* ...make sure translation is successful */
            BUG(msg.buffer == (void *)-1, _x("Invalid buffer address: %08x"), m.address);        

            /* ...retrieve information fields */
            msg.id = XF_MSG_SRC(m.id), msg.opcode = m.opcode, msg.length = m.length;           
        
            TRACE(RSP, _b("R[%08x]:(%08x,%u,%08x)"), m.id, m.opcode, m.length, m.address);

            /* ...lookup component basing on destination port specification */
            if (XF_AP_CLIENT(m.id) == 0)
            {
                /* ...put proxy response to local IPC queue */
                xf_proxy_response_put(proxy, &m);
            }
            else if ((client = xf_client_lookup(proxy, XF_AP_CLIENT(m.id))) != NULL)
            {
                /* ...client is found; invoke its response callback (must be non-blocking) */
                client->response(client, &msg);
            }
            else
            {
                /* ...client has been disconnected already; drop message */
                TRACE(RSP, _b("Client look-up failed - drop message"));
            }
        }

        /* ...if result code is negative; terminate thread operation */
        if (r < 0)
        {
            TRACE(ERROR, _x("abnormal proxy[%p] thread termination: %d"), proxy, r);
            break;
        }
    }
    
    TRACE(INIT, _b("IPC proxy[%p] thread terminated: %d"), proxy, r);
    
    return (void *)(intptr_t)r;
}

/*******************************************************************************
 * HiFi proxy API
 ******************************************************************************/

/* ...open HiFi proxy */
int xf_proxy_init(xf_proxy_t *proxy, u32 core, void *p_shmem)
{
    u32             i;
    int             r;
    
    /* ...initialize proxy lock */
    __xf_lock_init(&proxy->lock);
   
    /* ...open proxy IPC interface */
    XF_CHK_API(xf_ipc_open(&proxy->ipc, core, p_shmem));

    /* ...save proxy core - hmm, too much core identifiers - tbd */
    proxy->core = core;
    
    /* ...line-up all clients into single-linked list */
    for (i = 0; i < XF_CFG_PROXY_MAX_CLIENTS - 1; i++)
    {
        proxy->cmap[i].next = i + 1;
    }
    
    /* ...tail of the list points back to head (list terminator) */
    proxy->cmap[i].next = 0;

    /* ...initialize thread attributes (joinable, with minimal stack) */
    if ((r = __xf_thread_create(&proxy->thread, xf_proxy_thread, proxy)) < 0)
    {
        TRACE(ERROR, _x("Failed to create polling thread: %d"), r);
        xf_ipc_close(&proxy->ipc, core);
        return r;
    }
        
    TRACE(INIT, _b("proxy-%u[%p] opened"), core, proxy);
    
    return 0;
}

/* ...close proxy handle */
void xf_proxy_close(xf_proxy_t *proxy)
{
    u32     core = proxy->core;
    
    /* ...terminate proxy thread */
    __xf_thread_destroy(&proxy->thread);

    /* ...close proxy IPC interface */
    xf_ipc_close(&proxy->ipc, core);
    
    TRACE(INIT, _b("proxy-%u[%p] closed"), core, proxy);
}

/*******************************************************************************
 * HiFi component API
 ******************************************************************************/

/* ...open component handle */
int xf_open(xf_proxy_t *proxy, xf_handle_t *handle, xf_id_t id, u32 core, xf_response_cb response)
{
    int     r;
    
    /* ...retrieve auxiliary control buffer from proxy - need I */
    XF_CHK_ERR(handle->aux = xf_buffer_get(proxy->aux), -EBUSY);

    /* ...initialize IPC data */
    XF_CHK_API(xf_ipc_data_init(&handle->ipc));    

    /* ...register client in interlocked fashion */
    xf_proxy_lock(proxy);

    /* ...allocate local client */
    if (xf_client_alloc(proxy, handle) == 0)
    {
        TRACE(ERROR, _x("client allocation failed"));
        r = -EBUSY;
    }
    else if ((r = xf_client_register(proxy, handle, id, core)) < 0)
    {
        TRACE(ERROR, _x("client registering failed"));
        xf_client_free(proxy, handle);
    }
    
    xf_proxy_unlock(proxy);

    /* ...if failed, release buffer handle */
    if (r < 0)
    {
        /* ...operation failed; return buffer back to proxy pool */
        xf_buffer_put(handle->aux), handle->aux = NULL;
    }
    else
    {
        /* ...operation completed successfully; assign handle data */
        handle->response = response;
        handle->proxy = proxy;

        TRACE(INIT, _b("component[%p]:(id=%s,core=%u) created"), handle, id, core);
    }
    
    return XF_CHK_API(r);
}

/* ...close component handle */
void xf_close(xf_handle_t *handle)
{
    xf_proxy_t *proxy = handle->proxy;

    /* ...do I need to take component lock here? guess no - tbd */

    /* ...buffers and stuff? - tbd */

    /* ...acquire global proxy lock */
    xf_proxy_lock(proxy);
    
    /* ...unregister component from remote DSP proxy (ignore result code) */
    (void) xf_client_unregister(proxy, handle);
    
    /* ...recycle client-id afterwards */
    xf_client_free(proxy, handle);

    /* ...release global proxy lock */
    xf_proxy_unlock(proxy);

    /* ...destroy IPC data */
    xf_ipc_data_destroy(&handle->ipc);
    
    /* ...clear handle data */
    xf_buffer_put(handle->aux), handle->aux = NULL;
    
    /* ...wipe out proxy pointer */
    handle->proxy = NULL;

    TRACE(INIT, _b("component[%p] destroyed"), handle);
}

/* ...port binding function */
int xf_route(xf_handle_t *src, u32 src_port, xf_handle_t *dst, u32 dst_port, u32 num, u32 size, u32 align)
{
    xf_proxy_t             *proxy = src->proxy;
    xf_buffer_t            *b;
    xf_route_port_msg_t    *m;
    xf_user_msg_t           msg;
    
    /* ...validity checks - proxy pointers are same */
    XF_CHK_ERR(proxy == dst->proxy, -EINVAL);
    
    /* ...buffer data is valid */
    XF_CHK_ERR(num && size && xf_is_power_of_two(align), -EINVAL);
    
    /* ...get control buffer */
    XF_CHK_ERR(b = xf_buffer_get(proxy->aux), -EBUSY);

    /* ...get message buffer */
    m = xf_buffer_data(b);
    
    /* ...fill-in message parameters */
    //m->src = __XF_PORT_SPEC2(src->id, src_port);
    m->dst = __XF_PORT_SPEC2(dst->id, dst_port);
    m->alloc_number = num;
    m->alloc_size = size;
    m->alloc_align = align;

    /* ...set command parameters */
    msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), __XF_PORT_SPEC2(src->id, src_port));
    msg.opcode = XF_ROUTE;
    msg.length = sizeof(*m);
    msg.buffer = m;

    /* ...synchronously execute command on remote DSP */    
    XF_CHK_API(xf_proxy_cmd_exec(proxy, &msg));

    /* ...return buffer to proxy */
    xf_buffer_put(b);

    /* ...check result is successfull */
    XF_CHK_ERR(msg.opcode == (u32) XF_ROUTE, -ENOMEM);
    
    /* ...port binding completed */
    TRACE(GRAPH, _b("[%p]:%u bound to [%p]:%u"), src, src_port, dst, dst_port);
    
    return 0;
}

/* ...port unbinding function */
int xf_unroute(xf_handle_t *src, u32 src_port)
{
    xf_proxy_t             *proxy = src->proxy;
    xf_buffer_t            *b;
    xf_unroute_port_msg_t  *m;
    xf_user_msg_t           msg;
    int                     r;
    
    /* ...get control buffer */
    XF_CHK_ERR(b = xf_buffer_get(proxy->aux), -EBUSY);

    /* ...get message buffer */
    m = xf_buffer_data(b);
    
    /* ...fill-in message parameters */
    //m->src = __XF_PORT_SPEC2(src->id, src_port);

    /* ...set command parameters */
    msg.id = __XF_MSG_ID(__XF_AP_PROXY(proxy->core), __XF_PORT_SPEC2(src->id, src_port));
    msg.opcode = XF_UNROUTE;
    msg.length = sizeof(*m);
    msg.buffer = m;

    /* ...synchronously execute command on remote DSP */    
    if ((r = xf_proxy_cmd_exec(proxy, &msg)) != 0)
    {
        TRACE(ERROR, _x("Command failed: %d"), r);
        goto out;
    }
    else if (msg.opcode != (u32) XF_UNROUTE)
    {
        TRACE(ERROR, _x("Port unbinding failed"));
        r = -EBADFD;
        goto out;
    }
    
    /* ...port binding completed */
    TRACE(GRAPH, _b("[%p]:%u unbound"), src, src_port);
    
out:
    /* ...return buffer to proxy */
    xf_buffer_put(b);
    
    return r;
}

/* ...send a command message to component */
int xf_command(xf_handle_t *handle, u32 port, u32 opcode, void *buffer, u32 length)
{
    xf_proxy_t     *proxy = handle->proxy;
    xf_proxy_msg_t  msg;
    
    /* ...fill-in message parameters */
    msg.id = __XF_MSG_ID(__XF_AP_CLIENT(proxy->core, handle->client), __XF_PORT_SPEC2(handle->id, port));
    msg.opcode = opcode;
    msg.length = length;
    XF_CHK_ERR((msg.address = xf_proxy_b2a(proxy, buffer)) != XF_PROXY_BADADDR, -EINVAL);

    TRACE(CMD, _b("[%p]:[%08x]:(%08x,%u,%p)"), handle, msg.id, opcode, length, buffer);

    /* ...pass command to IPC layer */
    return XF_CHK_API(xf_ipc_send(&proxy->ipc, &msg, buffer));
}

/*******************************************************************************
 * Buffer pool API
 ******************************************************************************/

/* ...allocate buffer pool */
int xf_pool_alloc(xf_proxy_t *proxy, u32 number, u32 length, xf_pool_type_t type, xf_pool_t **pool, s32 id, 
                  xaf_mem_malloc_fxn_t xaf_malloc, xaf_mem_free_fxn_t xaf_free)
{
    xf_pool_t      *p;
    xf_buffer_t    *b;
    void           *data;
    int             r;
 
    /* ...unused arg */
    (void) type;

    /* ...basic validity checks; number of buffers is positive */
    XF_CHK_ERR(number > 0, -EINVAL);

    /* ...get properly aligned buffer length */
    length = (length + XF_PROXY_ALIGNMENT - 1) & ~(XF_PROXY_ALIGNMENT - 1);

    /* ...allocate data structure */
    p = xaf_malloc(offset_of(xf_pool_t, buffer) + number * sizeof(xf_buffer_t), id);
    XF_CHK_ERR(p, -ENOMEM);

    /* ...issue memory pool allocation request to remote DSP */
    xf_proxy_lock(proxy);
    r = xf_proxy_buffer_alloc(proxy, number * length, &p->p);
    xf_proxy_unlock(proxy);
    
    /* ...if operation is failed, do cleanup */
    if (r < 0)
    {
        TRACE(ERROR, _x("failed to allocate buffer: %d"), r);
        xaf_free(p, id);
        return r;
    }
    else
    {
        /* ...set pool parameters */
        p->number = number, p->length = length;
        p->proxy = proxy;
    }
 
    /* ...create individual buffers and link them into free list */
    for (p->free = b = &p->buffer[0], data = p->p; number > 0; number--, b++)
    {
        /* ...set address of the buffer (no length there) */
        b->address = data;
            
        /* ...file buffer into the free list */
        b->link.next = b + 1;

        /* ...advance data pointer in contiguous buffer */
        data = (unsigned char *) data + length;
    }

    /* ...terminate list of buffers (not too good - tbd) */
    b[-1].link.next = NULL;

    TRACE(BUFFER, _b("[%p]: pool[%p] created: %u * %u"), proxy, p, p->number, p->length);
    
    /* ...return buffer pointer */
    *pool = p;
    
    return 0;
}

/* ...buffer pool destruction */
void xf_pool_free(xf_pool_t *pool, s32 id, xaf_mem_free_fxn_t xaf_free)
{
    xf_proxy_t     *proxy = pool->proxy;

    /* ...check buffers are all freed - tbd */

    /* ...use global proxy lock for pool operations protection */
    xf_proxy_lock(proxy);
    
    /* ...release allocated buffer on remote DSP */
    xf_proxy_buffer_free(proxy, pool->p, pool->length * pool->number);

    /* ...release global proxy lock */
    xf_proxy_unlock(proxy);
    
    /* ...deallocate pool structure itself */
    xaf_free(pool, id);

    TRACE(BUFFER, _b("[%p]::pool[%p] destroyed"), proxy, pool);
}

/* ...get new buffer from a pool */
xf_buffer_t * xf_buffer_get(xf_pool_t *pool)
{
    xf_buffer_t    *b;

    /* ...use global proxy lock for pool operations protection */
    xf_proxy_lock(pool->proxy);
    
    /* ...take buffer from a head of the free list */
    if ((b = pool->free) != NULL)
    {
        /* ...advance free list head */
        pool->free = b->link.next, b->link.pool = pool;

        TRACE(BUFFER, _b("pool[%p]::get[%p]"), pool, b);
    }

    xf_proxy_unlock(pool->proxy);
    
    return b;
}

/* ...return buffer back to pool */
void xf_buffer_put(xf_buffer_t *buffer)
{
    xf_pool_t  *pool = buffer->link.pool;
    
    /* ...use global proxy lock for pool operations protection */
    xf_proxy_lock(pool->proxy);
    
    /* ...put buffer back to a pool */
    buffer->link.next = pool->free, pool->free = buffer;
    
    TRACE(BUFFER, _b("pool[%p]::put[%p]"), pool, buffer);

    xf_proxy_unlock(pool->proxy);
}