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 #define MODULE_TAG                      FIO
24 
25 /*******************************************************************************
26  * Includes
27  ******************************************************************************/
28 
29 #include "xf.h"
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 
33 /*******************************************************************************
34  * Tracing configuration
35  ******************************************************************************/
36 
37 TRACE_TAG(INIT, 1);
38 TRACE_TAG(CMD, 1);
39 TRACE_TAG(RSP, 1);
40 
41 /*******************************************************************************
42  * Local constants - tbd
43  ******************************************************************************/
44 
45 /* ...proxy setup ioctl */
46 #define XF_PROXY_SETUP_IOCTL            _IO('P', 0x0)
47 
48 /* ...proxy close ioctl */
49 #define XF_PROXY_CLOSE_IOCTL            _IO('P', 0x1)
50 
51 #define HIFI_DSP_MISC_DRIVER "/dev/hifi_misc"
52 #ifndef GJB_COMMENT
53 #define HIFI_MISC_IOCTL_XAF_IPC_MSG_SEND _IOW('A',  0x7c, xf_proxy_message_driv_t)
54 #define HIFI_MISC_IOCTL_XAF_IPC_MSG_RECV _IOR('A', 0x7d, xf_proxy_message_driv_t)
55 #define HIFI_MISC_IOCTL_XAF_IPC_VMSG_PTR _IOR('A', 0x7e, xf_proxy_message_driv_t)
56 #endif
57 //u8 remote_ipc_pool[XF_CFG_REMOTE_IPC_POOL_SIZE];
58 /*******************************************************************************
59  * Internal IPC API implementation
60  ******************************************************************************/
61 
62 /* ...pass command to remote DSP */
xf_ipc_send(xf_proxy_ipc_data_t * ipc,xf_proxy_msg_t * msg,void * b)63 int xf_ipc_send(xf_proxy_ipc_data_t *ipc, xf_proxy_msg_t *msg, void *b)
64 {
65     /* ...unused arg */
66     (void) b;
67 
68     int     fd = ipc->fd;
69     int ret;
70 #ifdef GJB_COMMENT
71     TRACE(CMD, _b("C[%08x]:(%x,%08x,%u)"), msg->id, msg->opcode, msg->address, msg->length);
72 
73     /* ...pass message to kernel driver */
74     XF_CHK_ERR(write(fd, msg, sizeof(*msg)) == sizeof(*msg), -errno);
75 #else
76 	ret = ioctl(fd, HIFI_MISC_IOCTL_XAF_IPC_MSG_SEND, msg);// GJB:-Verify th return value with driver implementation.
77 #endif
78 
79     /* ...communication mutex is still locked! */
80     return 0;
81 }
82 
83 /* ...wait for response availability */
xf_ipc_wait(xf_proxy_ipc_data_t * ipc,u32 timeout)84 int xf_ipc_wait(xf_proxy_ipc_data_t *ipc, u32 timeout)
85 {
86     int             fd = ipc->fd;
87     fd_set          rfds;
88     struct timeval  tv;
89 
90     /* ...specify waiting set */
91     FD_ZERO(&rfds);
92     FD_SET(fd, &rfds);
93 
94     /* ...set timeout value if given */
95     (timeout ? tv.tv_sec = timeout / 1000, tv.tv_usec = (timeout % 1000) * 1000 : 0);
96 
97     /* ...wait until there is a data in file */
98 //    XF_CHK_ERR(select(fd + 1, &rfds, NULL, NULL, (timeout ? &tv : NULL)) >= 0, -errno);
99 	select(fd+1,&rfds,NULL,NULL,(timeout? &tv: NULL));
100 
101     /* ...check if descriptor is set */
102     return (FD_ISSET(fd, &rfds) ? 0 : -ETIMEDOUT);
103 }
104 
105 /* ...read response from proxy */
xf_ipc_recv(xf_proxy_ipc_data_t * ipc,xf_proxy_msg_t * msg,void ** buffer)106 int xf_ipc_recv(xf_proxy_ipc_data_t *ipc, xf_proxy_msg_t *msg, void **buffer)
107 {
108     int     fd = ipc->fd;
109     int     r;
110     xf_proxy_msg_t temp;
111 #ifdef GJB_COMMENT
112     /* ...get message header from file */
113     if ((r = read(fd, msg, sizeof(*msg))) == sizeof(*msg))
114     {
115         TRACE(RSP, _b("R[%08x]:(%x,%u,%08x)"), msg->id, msg->opcode, msg->length, msg->address);
116 
117         /* ...translate shared address into local pointer */
118         XF_CHK_ERR((*buffer = xf_ipc_a2b(ipc, msg->address)) != (void *)-1, -EBADFD);
119 
120         /* ...return positive result indicating the message has been received */
121         return sizeof(*msg);
122     }
123 #else
124     if ((r = ioctl(fd, HIFI_MISC_IOCTL_XAF_IPC_MSG_RECV, &temp)) == sizeof(temp))
125     {
126         msg->id = temp.id;
127         msg->opcode = temp.opcode;
128         msg->length = temp.length;
129         *buffer = xf_ipc_a2b(ipc, temp.address);
130         /* ...translate shared address into local pointer */
131         XF_CHK_ERR((*buffer = xf_ipc_a2b(ipc, temp.address)) != (void *)-1, -EBADFD);
132         msg->address = temp.address;
133         return sizeof(*msg);
134     }
135 #endif
136     else
137     {
138         /* ...if no response is available, return 0 result */
139         return XF_CHK_API(errno == EAGAIN ? 0 : -errno);
140     }
141 }
142 
143 /*******************************************************************************
144  * Internal API functions implementation
145  ******************************************************************************/
146 
147 /* ...open proxy interface on proper DSP partition */
xf_ipc_open(xf_proxy_ipc_data_t * ipc,u32 core,void * p_shmem)148 int xf_ipc_open(xf_proxy_ipc_data_t *ipc, u32 core, void *p_shmem)
149 {
150     //XF_CHK_ERR((p_shmem != NULL), -errno);
151     //size_t xf_cfg_remote_ipc_pool_size = *(size_t *)p_shmem;//user configured shmem pool size: minimum 256 KB
152     /* ...unused arg */
153     (void) p_shmem;
154 #ifdef GJB_COMMENT
155     /* ...open file handle */
156     XF_CHK_ERR((ipc->fd = open("/dev/xtensa-proxy", O_RDWR)) >= 0, -errno);
157 
158     /* ...pass shread memory core for this proxy instance */
159     XF_CHK_ERR(ioctl(ipc->fd, XF_PROXY_SETUP_IOCTL, core) >= 0, -errno);
160 #else
161 	XF_CHK_ERR((ipc->fd = open(HIFI_DSP_MISC_DRIVER, O_RDWR,0)) >= 0, -errno);
162 #endif
163     /* ...create pipe for asynchronous response delivery */
164     XF_CHK_ERR(pipe(ipc->pipe) == 0, -errno);
165 
166     /* ...map entire shared memory region (not too good - tbd) */
167 //	ipc->shmem = remote_ipc_pool;
168 //	ioctl(ipc->fd, HIFI_MISC_IOCTL_XAF_IPC_VMSG_PTR, ipc->shmem);
169 #if 1
170     //allocate 256 KB constant size
171     XF_CHK_ERR((ipc->shmem = mmap(NULL, XF_CFG_REMOTE_IPC_POOL_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, ipc->fd, 0)) != MAP_FAILED, -errno);
172 #else
173     XF_CHK_ERR((ipc->shmem = mmap(NULL, xf_cfg_remote_ipc_pool_size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc->fd, 0)) != MAP_FAILED, -errno);
174 #endif
175     TRACE(INIT, _b("proxy-%u interface opened"), core);
176     return 0;
177 }
178 
179 /* ...close proxy handle */
xf_ipc_close(xf_proxy_ipc_data_t * ipc,u32 core)180 void xf_ipc_close(xf_proxy_ipc_data_t *ipc, u32 core)
181 {
182     /* ...unmap shared memory region */
183 //    (void)munmap(ipc->shmem, XF_CFG_REMOTE_IPC_POOL_SIZE);
184 
185     /* ...close asynchronous response delivery pipe */
186     close(ipc->pipe[0]), close(ipc->pipe[1]);
187 
188     /* ...close proxy file handle */
189     close(ipc->fd);
190 
191     TRACE(INIT, _b("proxy-%u interface closed"), core);
192 }
193 
194