1 /*******************************************************************************
2 * Copyright (C) 2018 Cadence Design Systems, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to use this Software with Cadence processor cores only and
7 * not with any other processors and platforms, subject to
8 * the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 ******************************************************************************/
22
23 /*******************************************************************************
24 * xf-msg.c
25 *
26 * Message/message pool handling
27 *
28 ******************************************************************************/
29
30 #define MODULE_TAG MSG
31
32 /*******************************************************************************
33 * Includes
34 ******************************************************************************/
35
36 #include "xf.h"
37
38 /*******************************************************************************
39 * Entry points
40 ******************************************************************************/
41
42 /* ...allocate message pool */
xf_msg_pool_init(xf_msg_pool_t * pool,u32 n,u32 core)43 int xf_msg_pool_init(xf_msg_pool_t *pool, u32 n, u32 core)
44 {
45 u32 i;
46
47 /* ...allocate shared memory from global pool */
48 XF_CHK_ERR(pool->p = xf_mem_alloc(XF_MM(sizeof(*pool->p) * n), XF_PROXY_ALIGNMENT, core, 1), -ENOMEM);
49
50 /* ...place all messages into single-liked list */
51 for (pool->head = &pool->p[i = 0]; i < n - 1; i++)
52 {
53 /* ...set message pointer to next message in the pool */
54 xf_msg_pool_item(pool, i)->next = xf_msg_pool_item(pool, i + 1);
55 }
56
57 /* ...set tail of the list */
58 xf_msg_pool_item(pool, i)->next = NULL;
59
60 /* ...save pool size */
61 pool->n = n;
62
63 return 0;
64 }
65
66 /* ...destroy memory pool */
xf_msg_pool_destroy(xf_msg_pool_t * pool,u32 core)67 void xf_msg_pool_destroy(xf_msg_pool_t *pool, u32 core)
68 {
69 /* ...release pool memory (from shared local-IPC memory) */
70 xf_mem_free(pool->p, XF_MM(sizeof(*pool->p) * pool->n), core, 1);
71 }
72
73 /* ...allocate message from a pool (no concurrent access from other cores) */
xf_msg_pool_get(xf_msg_pool_t * pool)74 xf_message_t * xf_msg_pool_get(xf_msg_pool_t *pool)
75 {
76 __xf_message_t *_m;
77
78 /* ...pop message from the head of the pool */
79 XF_CHK_ERR(_m = pool->head, NULL);
80
81 /* ...advance list head */
82 pool->head = (__xf_message_t *)(((xf_message_t *) _m)->next);
83
84 /* ...debug - wipe out message "next" pointer */
85 ((xf_message_t *) _m)->next = NULL;
86
87 /* ...return properly aligned message pointer */
88 return (xf_message_t *) _m;
89 }
90
91 /* ...return message back to the pool (no concurrent access from other cores) */
xf_msg_pool_put(xf_msg_pool_t * pool,xf_message_t * m)92 void xf_msg_pool_put(xf_msg_pool_t *pool, xf_message_t *m)
93 {
94 __xf_message_t *_m = (__xf_message_t *) m;
95
96 /* ...make sure the message is properly aligned object */
97 BUG(!XF_IS_ALIGNED(_m), _x("Corrupted message pointer: %p"), _m);
98
99 /* ...make sure it is returned to the same pool (need a length for that - tbd) */
100 BUG(!xf_msg_from_pool(pool, m) < 0, _x("Bad pool/message: %p/%p"), pool->p, _m);
101
102 /* ...place message into the head */
103 m->next = (xf_message_t *) pool->head;
104
105 /* ...advance pool head */
106 pool->head = _m;
107 }
108