summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/core/xf-io.c
blob: 09f8e3e644e8f2553f3330ec5b57d038c6cdc284 (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
/*******************************************************************************
* 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.

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

/*******************************************************************************
 * xf-io.c
 *
 * Generic input/output ports handling
 *
 ******************************************************************************/

#define MODULE_TAG                      IO

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

#include "xf.h"

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

TRACE_TAG(INIT, 1);
TRACE_TAG(INPUT, 1);
TRACE_TAG(OUTPUT, 1);
TRACE_TAG(ROUTE, 1);

/*******************************************************************************
 * Input port API
 ******************************************************************************/

/* ...initialize input port structure */
int xf_input_port_init(xf_input_port_t *port, u32 size, u32 align, u32 core)
{
    /* ...allocate local internal buffer of particular size and alignment */
    if (size)
    {
        /* ...internal buffer is used */
        XF_CHK_ERR(port->buffer = xf_mem_alloc(size, align, core, 0), -ENOMEM);
    }
    else
    {
        /* ...no internal buffering is used */
        port->buffer = NULL;
    }
    
    /* ...initialize message queue */
    xf_msg_queue_init(&port->queue);
    
    /* ...set buffer size */
    port->length = size;
    
    /* ...enable input by default */
    port->flags = XF_INPUT_FLAG_ENABLED | XF_INPUT_FLAG_CREATED;

    /* ...mark buffer is empty */
    port->filled = 0, port->access = NULL;
    
    TRACE(INIT, _b("input-port[%p] created - %p@%u[%u]"), port, port->buffer, align, size);

    return 0;
}

/* ...put message into input port queue; return non-zero if queue was empty */
int xf_input_port_put(xf_input_port_t *port, xf_message_t *m)
{
    /* ...check if input is enabled */
    if ((port->flags & XF_INPUT_FLAG_ENABLED) == 0)
    {
        /* ...input disabled; this is an error condition, likely */
        TRACE(INPUT, _b("input-port[%p] disabled"), port);
        
        /* ...release the message instantly */
        xf_response_ok(m);

        /* ...buffer has not been accepted - no actions to take */
        return 0;
    }
    else if (m->length == 0)
    {
        /* ...it is forbidden to pass more than one zero-length message */
        BUG(port->flags & XF_INPUT_FLAG_EOS, _x("invalid state: %x"), port->flags);

        /* ...received a message with zero-length; mark end-of-stream condition */
        port->flags ^= XF_INPUT_FLAG_ENABLED | XF_INPUT_FLAG_EOS;

        /* ...still enqueue that zero-length message; it will be processed afterwards */
        TRACE(INPUT, _b("input-port[%p]: zero-length buffer received"), port);
    }
    else
    {
        TRACE(INPUT, _b("input-port[%p]: buffer received - %u bytes"), port, m->length);
    }

    /* ...enqueue message and set access pointer as needed */
    if (xf_msg_enqueue(&port->queue, m))
    {
        /* ...first message put - set access pointer and length */
        port->access = m->buffer, port->remaining = m->length;

        /* ...if first message is empty, mark port is done */
        (!port->access ? port->flags ^= XF_INPUT_FLAG_EOS | XF_INPUT_FLAG_DONE : 0);

        /* ...return non-zero to indicate the first buffer is placed into port */
        return 1;
    }
    else
    {
        /* ...subsequent message placed into buffer */
        return 0;
    }
}

/* ...internal helper - input message completion */
static inline int xf_input_port_complete(xf_input_port_t *port)
{
    /* ...dequeue message from queue */
    xf_message_t   *m = xf_msg_dequeue(&port->queue);

    /* ...message cannot be NULL */
    BUG(m == NULL, _x("invalid port state"));

    /* ...complete current message (EMPTY-THIS-BUFFER always; no length adjustment) */
    xf_response(m);

    /* ...set up next head */
    if ((m = xf_msg_queue_head(&port->queue)) != NULL)
    {
        /* ...set new access pointers */
        port->access = m->buffer, port->remaining = m->length;

        /* ...return indication that there is an input message */
        return 1;
    }
    else
    {
        /* ...no more messages; reset access pointer */
        port->access = NULL;

        /* ...return indication that input port has no data available */
        return 0;
    }
}

/* ...fill-in required amount of data into input port buffer */
int xf_input_port_fill(xf_input_port_t *port)
{
    u32     filled = port->filled;
    u32     remaining = port->remaining;
    u32     copied = 0;
    s32     n;

    /* ...function shall not be called if no internal buffering is used */
    BUG(xf_input_port_bypass(port), _x("Invalid transaction"));

    /* ...if there is no message pending, bail out */
    if (!xf_msg_queue_head(&port->queue))
    {
        TRACE(INPUT, _b("No message ready"));
        return 0;
    }
    
    /* ...calculate total amount of bytes we need to copy */
    n = (s32)(port->length - filled);
    
    /* ...get at most "n" bytes from input message(s) buffer(s) */
    while (n > 0)
    {
        u32     k;
        
        /* ...determine the size of the chunk to copy */
        ((k = remaining) > n ? k = n : 0);

        /* ...process zero-length input message separately */
        if (k == 0)
        {
            /* ...end-of-stream condition must be set */
            BUG((port->flags & XF_INPUT_FLAG_EOS) == 0, _x("port[%p]: invalid state: %x"), port, port->flags);

            /* ...mark stream is completed */
            port->flags ^= XF_INPUT_FLAG_EOS | XF_INPUT_FLAG_DONE;

            /* ...reset total amount of bytes to fill */
            n = 0;

            /* ...do not release message yet */
            TRACE(INPUT, _b("input-port[%p] done"), port);
            
            /* ...and break the loop */
            break;
        }

        /* ...buffer must be set */
        BUG(!port->access, _x("invalid port state"));
            
        /* ...get required amount from input buffer */
        memcpy(port->buffer + filled, port->access, k), port->access += k;
        
        /* ...advance buffer positions */
        filled += k, copied += k, n -= k;
        
        /* ...check if input buffer is processed completely */
        if ((remaining -= k) == 0)
        {
            if (!xf_input_port_complete(port))
            {
                /* ...no more input messages; break the loop */
                break;
            }
            else
            {
                /* ...update remaining counter */
                remaining = port->remaining;
            }
        }
    }

    /* ...update buffer positions */
    port->filled = filled, port->remaining = remaining;
    
    /* ...return indicator whether input buffer is prefilled */
    return (n == 0);
}

/* ...pad input buffer with given pattern */
void xf_input_port_pad(xf_input_port_t *port, u8 pad)
{
    u32     filled = port->filled;
    s32     k;
    
    /* ...do padding if port buffer is not filled */
    if ((k = port->length - filled) > 0)
    {
        memset(port->buffer + filled, pad, k);

        /* ...indicate port is filled */
        port->filled = port->length;
    }
}

/* ...consume input buffer data */
void xf_input_port_consume(xf_input_port_t *port, u32 n)
{
    /* ...check whether input port is in bypass mode */
    if (xf_input_port_bypass(port))
    {
        /* ...port is in bypass mode; advance access pointer */
        if ((port->remaining -= n) == 0)
        {
            /* ...complete message and try to rearm input port */
            xf_input_port_complete(port);

            /* ...check if end-of-stream flag is set */
            if (xf_msg_queue_head(&port->queue) && !port->access)
            {
                BUG((port->flags & XF_INPUT_FLAG_EOS) == 0, _x("port[%p]: invalid state: %x"), port, port->flags);

                /* ...mark stream is completed */
                port->flags ^= XF_INPUT_FLAG_EOS | XF_INPUT_FLAG_DONE;
                
                TRACE(INPUT, _b("input-port[%p] done"), port);
            }
        }
        else
        {
            /* ...advance message buffer pointer */
            port->access += n;
        }
    }
    else if (port->filled > n)
    {
        u32     k = port->filled - n;
        
        /* ...move tail of buffer to the head (safe to use memcpy) */
        memcpy(port->buffer, port->buffer + n, k);        

        /* ...adjust filled position */
        port->filled = k;
    }
    else
    {
        /* ...entire buffer is consumed; reset fill level */
        port->filled = 0;
    }
}

/* ...purge input port queue */
void xf_input_port_purge(xf_input_port_t *port)
{
    xf_message_t   *m;

    /* ...bail out early if port is not created */
    if (!xf_input_port_created(port))   return;
    
    /* ...free all queued messages with generic "ok" response */
    while ((m = xf_msg_dequeue(&port->queue)) != NULL)
    {
        xf_response_ok(m);
    }

    /* ...reset internal buffer position */
    port->filled = 0, port->access = NULL;
    
    /* ...reset port flags */
    port->flags = (port->flags & ~__XF_INPUT_FLAGS(~0)) | XF_INPUT_FLAG_ENABLED | XF_INPUT_FLAG_CREATED;
    
    TRACE(INPUT, _b("input-port[%p] purged"), port);
}

/* ...save flow-control message for propagated input port purging sequence */
void xf_input_port_control_save(xf_input_port_t *port, xf_message_t *m)
{
    /* ...make sure purging sequence is not active */
    BUG(port->flags & XF_INPUT_FLAG_PURGING, _x("invalid state: %x"), port->flags);

    /* ...place message into internal queue */
    xf_msg_enqueue(&port->queue, m);
    
    /* ...set port purging flag */
    port->flags ^= XF_INPUT_FLAG_PURGING;

    TRACE(INPUT, _b("port[%p] start purge sequence"), port);
}

/* ...mark flushing sequence is completed */
void xf_input_port_purge_done(xf_input_port_t *port)
{
    /* ...make sure flushing sequence is ongoing */
    BUG((port->flags & XF_INPUT_FLAG_PURGING) == 0, _x("invalid state: %x"), port->flags);

    /* ...complete saved flow-control message */
    xf_response_ok(xf_msg_dequeue(&port->queue));
    
    /* ...clear port purging flag */
    port->flags ^= XF_INPUT_FLAG_PURGING;

    TRACE(INPUT, _b("port[%p] purge sequence completed"), port);
}

/* ...destroy input port data */
void xf_input_port_destroy(xf_input_port_t *port, u32 core)
{
    /* ...bail out earlier if port is not created */
    if (!xf_input_port_created(port))   return;
    
    /* ...deallocate input buffer if needed */
    (port->buffer ? xf_mem_free(port->buffer, port->length, core, 0), port->buffer = NULL : 0);

    /* ...reset input port flags */
    port->flags = 0;
    
    TRACE(INIT, _b("input-port[%p] destroyed"), port);
}

/*******************************************************************************
 * Output port API
 ******************************************************************************/

/* ...initialize output port (structure must be zero-initialized) */
int xf_output_port_init(xf_output_port_t *port, u32 size)
{
    /*  ...initialize message queue */
    xf_msg_queue_init(&port->queue);
    
    /* ...set output buffer length */
    port->length = size;

    /* ...mark port is created */
    port->flags = XF_OUTPUT_FLAG_CREATED;

    TRACE(INIT, _b("output-port[%p] initialized"), port);

    return 0;
}

/* ...route output port */
int xf_output_port_route(xf_output_port_t *port, u32 id, u32 n, u32 length, u32 align)
{
    u32             core = XF_MSG_DST_CORE(id);
    u32             shared = XF_MSG_SHARED(id);
    xf_message_t   *m;
    u32             i;
    
    /* ...allocate message pool for a port; extra message for control */
    XF_CHK_API(xf_msg_pool_init(&port->pool, n + 1, core));

    /* ...allocate required amount of buffers */
    for (i = 1; i <= n; i++)
    {
        /* ...get message from pool (directly; bypass that "get" interface) */
        m = xf_msg_pool_item(&port->pool, i);

        /* ...wipe out message link pointer (debugging) */
        m->next = NULL;
        
        /* ...set message parameters */
        m->id = id;
        m->opcode = XF_FILL_THIS_BUFFER;
        m->length = length;
        m->buffer = xf_mem_alloc(length, align, core, shared);

        /* ...if allocation failed, do a cleanup */
        if (!m->buffer)     goto error;

        /* ...place message into output port */
        xf_msg_enqueue(&port->queue, m);
    }

    /* ...setup flow-control message */
    m = xf_output_port_control_msg(port);
    m->id = id;
    m->length = 0;
    m->buffer = NULL;

    /* ...wipe out message link pointer (debugging) */
    m->next = NULL;
    
    /* ...save port length */
    port->length = length;

    /* ...mark port is routed */
    port->flags |= XF_OUTPUT_FLAG_ROUTED | XF_OUTPUT_FLAG_IDLE;

    TRACE(ROUTE, _b("output-port[%p] routed: %x -> %x"), port, XF_MSG_DST(id), XF_MSG_SRC(id));

    return 0;

error:
    /* ...allocation failed; do a cleanup */
    while (--i)
    {
        m = xf_msg_pool_item(&port->pool, i);

        /* ...free item */
        xf_mem_free(m->buffer, length, core, shared);
    }

    /* ...destroy pool data */
    xf_msg_pool_destroy(&port->pool, core);
    
    return -ENOMEM;
}

/* ...start output port unrouting sequence */
void xf_output_port_unroute_start(xf_output_port_t *port, xf_message_t *m)
{
    /* ...port must be routed */
    BUG(!xf_output_port_routed(port), _x("invalid state: %x"), port->flags);

    /* ...save message in the queue */
    port->unroute = m;
    
    /* ...put port unrouting flag */
    port->flags |= XF_OUTPUT_FLAG_UNROUTING;
}

/* ...complete port unrouting sequence */
void xf_output_port_unroute_done(xf_output_port_t *port)
{
    xf_message_t   *m;
    
    /* ...make sure we have an outstanding port unrouting sequence */
    BUG(!xf_output_port_unrouting(port), _x("invalid state: %x"), port->flags);

    /* ...retrieve enqueued control-flow message */
    m = port->unroute, port->unroute = NULL;
    
    /* ...destroy port buffers */
    xf_output_port_unroute(port);
    
    /* ...and pass response to the caller */
    xf_response_ok(m);
}

/* ...unroute output port and destroy all memory buffers allocated */
void xf_output_port_unroute(xf_output_port_t *port)
{
    xf_message_t   *m = xf_output_port_control_msg(port);
    u32             core = XF_MSG_DST_CORE(m->id);
    u32             shared = XF_MSG_SHARED(m->id);   
    u32             n = port->pool.n - 1;
    u32             i;
    
    /* ...free all messages (we are running on "dst" core) */
    for (i = 1; i <= n; i++)
    {
        /* ...directly obtain message item */
        m = xf_msg_pool_item(&port->pool, i);

        /* ...free message buffer (must exist) */
        xf_mem_free(m->buffer, port->length, core, shared);
    }

    /* ...destroy pool data */
    xf_msg_pool_destroy(&port->pool, core);

    /* ...reset all flags */
    port->flags = XF_OUTPUT_FLAG_CREATED;

    /* ...reset message queue (it is empty again) */
    xf_msg_queue_init(&port->queue);

    TRACE(ROUTE, _b("output-port[%p] unrouted"), port);
}

/* ...put next message to the port */
int xf_output_port_put(xf_output_port_t *port, xf_message_t *m)
{
    /* ...in case of port unrouting sequence the flag returned will always be 0 */
    return xf_msg_enqueue(&port->queue, m);
}

/* ...retrieve next message from the port */
void * xf_output_port_data(xf_output_port_t *port)
{
    xf_message_t   *m = xf_msg_queue_head(&port->queue);

    /* ...bail out if there is nothing enqueued */
    if (m == NULL)      return NULL;

    /* ...it is not permitted to access port data when port is being unrouted */
    BUG(xf_output_port_unrouting(port), _x("invalid transaction"));

    /* ...make sure message length is valid */
    BUG(m->length < port->length, _x("Insufficient buffer length: %u < %u"), m->length, port->length);
        
    /* ...return access buffer pointer */
    return m->buffer;
}

/* ...produce output message marking amount of bytes produced */
int xf_output_port_produce(xf_output_port_t *port, u32 n)
{
    xf_message_t   *m = xf_msg_dequeue(&port->queue);
    
    /* ...message cannot be NULL */
    BUG(m == NULL, _x("Invalid transaction"));

    /* ...it is not permitted to invoke this when port is being unrouted (or flushed - tbd) */
    BUG(xf_output_port_unrouting(port), _x("invalid transaction"));

    /* ...complete message with specified amount of bytes produced */
    xf_response_data(m, n);

    /* ...clear port idle flag (technically, not needed for unrouted port) */
    port->flags &= ~XF_OUTPUT_FLAG_IDLE;
    
    /* ...return indication of pending message availability */
    return (xf_msg_queue_head(&port->queue) != NULL);
}

/* ...flush output port */
int xf_output_port_flush(xf_output_port_t *port, u32 opcode)
{
    xf_message_t   *m;

    /* ...if port is routed, we shall pass flush command to sink port */
    if (xf_output_port_routed(port))
    {
        /* ...if port is idle, satisfy immediately */
        if (port->flags & XF_OUTPUT_FLAG_IDLE)     return 1;
        
        /* ...start flushing sequence if not already started */
        if ((port->flags & XF_OUTPUT_FLAG_FLUSHING) == 0)
        {
            /* ...put flushing flag */
            port->flags ^= XF_OUTPUT_FLAG_FLUSHING;

            /* ...get control message from associated pool */
            m = xf_output_port_control_msg(port);

            /* ...set flow-control operation */
            m->opcode = opcode;
        
            /* ...message is a command, but source and destination are swapped */
            xf_response(m);
        }
        
        /* ...zero-result indicates the flushing is in progress */
        return 0;
    }
    else
    {
        /* ...for non-routed port just complete all queued messages */
        while ((m = xf_msg_dequeue(&port->queue)) != NULL)
        {
            /* ...pass generic zero-length "okay" response - tbd */
            xf_response_ok(m);
        }

        /* ...non-zero result indicates the flushing is done */
        return 1;
    }
}

/* ...mark flushing sequence is completed */
void xf_output_port_flush_done(xf_output_port_t *port)
{
    /* ...make sure flushing sequence is ongoing */
    BUG((port->flags & XF_OUTPUT_FLAG_FLUSHING) == 0, _x("invalid state: %x"), port->flags);
    
    /* ...clear flushing flag and set idle flag */
    port->flags ^= XF_OUTPUT_FLAG_IDLE | XF_OUTPUT_FLAG_FLUSHING;

    TRACE(OUTPUT, _b("port[%p] flush sequence completed"), port);
}

/* ...destroy output port */
void xf_output_port_destroy(xf_output_port_t *port, u32 core)
{
    /* ...check if port is routed */
    if (xf_output_port_routed(port))
    {
        /* ...port must be in idle state */
        BUG(!xf_output_port_idle(port), _x("destroying non-idle port[%p]"), port);

        /* ...unroute port */
        xf_output_port_unroute(port);
    }

    /* ...reset port flags */
    port->flags = 0;

    TRACE(INIT, _b("output-port[%p] destroyed"), port);
}