1 /**
2  * Copyright (c) 2019, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *    * Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *    * Redistributions in binary form must reproduce the above
10  *      copyright notice, this list of conditions and the following
11  *      disclaimer in the documentation and/or other materials provided
12  *      with the distribution.
13  *    * Neither the name of The Linux Foundation nor the names of its
14  *      contributors may be used to endorse or promote products derived
15  *      from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef LISTENER_BUF_H
31 #define LISTENER_BUF_H
32 
33 #include "sbuf.h"
34 #include "remote.h"
35 #include "verify.h"
36 
pack_in_bufs(struct sbuf * buf,remote_arg * pra,int nBufs)37 static __inline void pack_in_bufs(struct sbuf* buf, remote_arg* pra, int nBufs) {
38    int ii;
39    uint32_t len;
40    C_ASSERT(sizeof(len) == 4);
41    for(ii = 0; ii < nBufs; ++ii) {
42       len = (uint32_t)pra[ii].buf.nLen;
43       sbuf_write(buf, (uint8*)&len, 4);
44       if(len) {
45          sbuf_align(buf, 8);
46          sbuf_write(buf, pra[ii].buf.pv, len);
47       }
48    }
49 }
50 
pack_out_lens(struct sbuf * buf,remote_arg * pra,int nBufs)51 static __inline void pack_out_lens(struct sbuf* buf, remote_arg* pra, int nBufs) {
52    int ii;
53    uint32_t len;
54    C_ASSERT(sizeof(len) == 4);
55    for(ii = 0; ii < nBufs; ++ii) {
56       len = (uint32_t)pra[ii].buf.nLen;
57       sbuf_write(buf, (uint8*)&len, 4);
58    }
59 }
60 
unpack_in_bufs(struct sbuf * buf,remote_arg * pra,int nBufs)61 static __inline void unpack_in_bufs(struct sbuf* buf, remote_arg* pra, int nBufs) {
62    int ii;
63    uint32_t len=0;
64    C_ASSERT(sizeof(len) == 4);
65    for(ii = 0; ii < nBufs; ++ii) {
66       sbuf_read(buf, (uint8*)&len, 4);
67       pra[ii].buf.nLen = len;
68       if(pra[ii].buf.nLen) {
69          sbuf_align(buf, 8);
70          if((int)pra[ii].buf.nLen <= sbuf_left(buf)) {
71             pra[ii].buf.pv = sbuf_head(buf);
72          }
73          sbuf_advance(buf, pra[ii].buf.nLen);
74       }
75    }
76 }
77 
unpack_out_lens(struct sbuf * buf,remote_arg * pra,int nBufs)78 static __inline void unpack_out_lens(struct sbuf* buf, remote_arg* pra, int nBufs) {
79    int ii;
80    uint32_t len=0;
81    C_ASSERT(sizeof(len) == 4);
82    for(ii = 0; ii < nBufs; ++ii) {
83       sbuf_read(buf, (uint8*)&len, 4);
84       pra[ii].buf.nLen = len;
85    }
86 }
87 
88 //map out buffers on the hlos side to the remote_arg array
89 //dst is the space required for buffers we coun't map from the adsp
pack_out_bufs(struct sbuf * buf,remote_arg * pra,int nBufs)90 static __inline void pack_out_bufs(struct sbuf* buf, remote_arg* pra, int nBufs) {
91    int ii;
92    uint32_t len;
93    C_ASSERT(sizeof(len) == 4);
94    for(ii = 0; ii < nBufs; ++ii) {
95       len = (uint32_t)pra[ii].buf.nLen;
96       sbuf_write(buf, (uint8*)&len, 4);
97       if(pra[ii].buf.nLen) {
98          sbuf_align(buf, 8);
99          if((int)pra[ii].buf.nLen <= sbuf_left(buf)) {
100             pra[ii].buf.pv = sbuf_head(buf);
101          }
102          sbuf_advance(buf, pra[ii].buf.nLen);
103       }
104    }
105 }
106 
107 //on the aDSP copy the data from buffers we had to copy to the local remote_arg structure
unpack_out_bufs(struct sbuf * buf,remote_arg * pra,int nBufs)108 static __inline int unpack_out_bufs(struct sbuf* buf, remote_arg* pra, int nBufs) {
109    int ii, nErr = 0;
110    uint32_t len;
111    C_ASSERT(sizeof(len) == 4);
112    for(ii = 0; ii < nBufs; ++ii) {
113       sbuf_read(buf, (uint8*)&len, 4);
114       VERIFY(len == pra[ii].buf.nLen);
115       if(pra[ii].buf.nLen) {
116          sbuf_align(buf, 8);
117          sbuf_read(buf, pra[ii].buf.pv, pra[ii].buf.nLen);
118       }
119    }
120 bail:
121    return nErr;
122 }
123 
124 #endif
125