1 /*
2 * Copyright (c) 2011-2014, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /** ============================================================================
33 * @file VirtQueue.c
34 *
35 * @brief Virtio Queue implementation for BIOS
36 *
37 * Differences between BIOS version and Linux kernel (include/linux/virtio.h):
38 * - Renamed module from virtio.h to VirtQueue_Object.h to match the API prefixes;
39 * - BIOS (XDC) types and CamelCasing used;
40 * - virtio_device concept removed (i.e, assumes no containing device);
41 * - simplified scatterlist from Linux version;
42 * - VirtQueue_Objects are created statically here, so just added a VirtQueue_Object_init()
43 * fxn to take the place of the Virtio vring_new_virtqueue() API;
44 * - The notify function is implicit in the implementation, and not provided
45 * by the client, as it is in Linux virtio.
46 *
47 * All VirtQueue operations can be called in any context.
48 *
49 * The virtio header should be included in an application as follows:
50 * @code
51 * #include <ti/ipc/rpmsg/VirtQueue.h>
52 * @endcode
53 *
54 */
56 #include <xdc/std.h>
57 #include <xdc/runtime/System.h>
58 #include <xdc/runtime/Assert.h>
59 #include <xdc/runtime/Error.h>
60 #include <xdc/runtime/Memory.h>
61 #include <xdc/runtime/Log.h>
62 #include <xdc/runtime/Diags.h>
63 #include <xdc/runtime/SysMin.h>
64 #include <ti/sysbios/gates/GateAll.h>
66 #include <ti/sysbios/knl/Clock.h>
67 #include <ti/sysbios/family/c66/Cache.h>
68 #include <ti/sysbios/knl/Swi.h>
70 #include <ti/sdo/ipc/notifyDrivers/IInterrupt.h>
71 #include <ti/ipc/family/tci6638/Interrupt.h>
72 #include <ti/ipc/remoteproc/Resource.h>
74 #include <ti/ipc/MultiProc.h>
76 #include "package/internal/VirtQueue.xdc.h"
78 #include <string.h>
80 #include <ti/ipc/rpmsg/_VirtQueue.h>
81 #include <ti/ipc/rpmsg/virtio_ring.h>
83 /* Used for defining the size of the virtqueue registry */
84 #define NUM_QUEUES 2
86 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
87 #define RP_MSG_BUFS_SPACE (VirtQueue_RP_MSG_NUM_BUFS * RPMSG_BUF_SIZE * 2)
89 /* With 256 buffers, our vring will occupy 3 pages */
90 #define RP_MSG_RING_SIZE ((DIV_ROUND_UP(vring_size(VirtQueue_RP_MSG_NUM_BUFS, \
91 VirtQueue_RP_MSG_VRING_ALIGN), VirtQueue_PAGE_SIZE)) * VirtQueue_PAGE_SIZE)
93 /* The total IPC space needed to communicate with a remote processor */
94 #define RPMSG_IPC_MEM (RP_MSG_BUFS_SPACE + 2 * RP_MSG_RING_SIZE)
96 #define ID_DSP_TO_A9 0
97 #define ID_A9_TO_DSP 1
99 extern volatile cregister UInt DNUM;
101 static VirtQueue_Object *queueRegistry[NUM_QUEUES] = {NULL};
103 static inline Void * mapPAtoVA(UInt pa)
104 {
105 return (Void *)(pa | 0x80000000);
106 }
108 static inline UInt mapVAtoPA(Void * va)
109 {
110 return ((UInt)va & 0x000fffffU) | 0xe1000000U;
111 }
113 /*
114 * ======== VirtQueue_init ========
115 */
116 Void VirtQueue_init()
117 {
118 extern cregister volatile UInt DNUM;
119 UInt16 procId;
121 /*
122 * VirtQueue_init() must be called before MultiProcSetup_init().
123 * (Check the xdc_runtime_Startup_firstFxns__A in the XDC generated code)
124 * Abort if the procId has already been set. We must set it!
125 */
126 if (MultiProc_self() != MultiProc_INVALIDID) {
127 System_abort("VirtQueue_init(): MultiProc_self already set!");
128 return;
129 }
131 procId = DNUM + 1;
133 /* Set the local ID */
134 MultiProc_setLocalId(procId);
135 }
137 /*
138 * ======== VirtQueue_Instance_init ========
139 */
140 Int VirtQueue_Instance_init(VirtQueue_Object *vq, UInt16 remoteProcId,
141 const VirtQueue_Params *params, Error_Block *eb)
142 {
143 void *vringAddr = NULL;
144 UInt32 marValue;
146 VirtQueue_module->traceBufPtr = Resource_getTraceBufPtr();
147 /* Create the thread protection gate */
148 vq->gateH = GateAll_create(NULL, eb);
149 if (Error_check(eb)) {
150 Log_error0("VirtQueue_create: could not create gate object");
151 Error_raise(NULL, Error_E_generic, 0, 0);
152 return(0);
153 }
155 vq->vringPtr = Memory_calloc(NULL, sizeof(struct vring), 0, eb);
156 Assert_isTrue((vq->vringPtr != NULL), NULL);
158 vq->callback = params->callback;
159 vq->id = params->vqId;
160 vq->procId = remoteProcId;
161 vq->last_avail_idx = 0;
162 vq->last_used_idx = 0;
163 vq->num_free = VirtQueue_RP_MSG_NUM_BUFS;
164 vq->swiHandle = params->swiHandle;
166 switch (vq->id) {
167 case ID_DSP_TO_A9:
168 case ID_A9_TO_DSP:
169 vringAddr = (struct vring *)Resource_getVringDA(vq->id);
170 Assert_isTrue(vringAddr != NULL, NULL);
171 /* Add per core offset: must match on host side: */
172 vringAddr = (struct vring *)((UInt32)vringAddr +
173 (DNUM * VirtQueue_VRING_OFFSET));
175 /* Also, assert that the vring address is non-cached: */
176 marValue = Cache_getMar((Ptr)vringAddr);
177 Log_print1(Diags_USER1, "Vring cache is %s",
178 (IArg)(marValue & 0x1 ? "enabled" : "disabled"));
179 Assert_isTrue(!(marValue & 0x1), NULL);
180 break ;
182 default:
183 Log_error1("VirtQueue_create: invalid vq->id: %d", vq->id);
184 GateAll_delete(&vq->gateH);
185 Memory_free(NULL, vq->vringPtr, sizeof(struct vring));
186 Error_raise(NULL, Error_E_generic, 0, 0);
187 return(0);
188 }
190 Log_print3(Diags_USER1,
191 "vring: %d 0x%x (0x%x)\n", vq->id, (IArg)vringAddr,
192 RP_MSG_RING_SIZE);
194 vring_init(vq->vringPtr, VirtQueue_RP_MSG_NUM_BUFS, vringAddr, VirtQueue_RP_MSG_VRING_ALIGN);
196 queueRegistry[vq->id] = vq;
197 return(0);
198 }
200 /*
201 * ======== VirtQueue_kick ========
202 */
203 Void VirtQueue_kick(VirtQueue_Handle vq)
204 {
205 struct vring *vring = vq->vringPtr;
206 IInterrupt_IntInfo intInfo;
208 /* For now, simply interrupt remote processor */
209 if (vring->avail->flags & VRING_AVAIL_F_NO_INTERRUPT) {
210 Log_print0(Diags_USER1,
211 "VirtQueue_kick: no kick because of VRING_AVAIL_F_NO_INTERRUPT\n");
212 return;
213 }
215 Log_print2(Diags_USER1,
216 "VirtQueue_kick: Sending interrupt to proc %d with payload 0x%x\n",
217 (IArg)vq->procId, (IArg)vq->id);
219 intInfo.localIntId = Interrupt_SRCS_BITPOS_CORE0;
220 Interrupt_intSend(vq->procId, &intInfo, vq->id);
221 }
223 /*
224 * ======== VirtQueue_addUsedBuf ========
225 */
226 Int VirtQueue_addUsedBuf(VirtQueue_Handle vq, Int16 head, Int len)
227 {
228 struct vring_used_elem *used;
229 struct vring *vring = vq->vringPtr;
230 IArg key;
232 key = GateAll_enter(vq->gateH);
233 if ((head > vring->num) || (head < 0)) {
234 Error_raise(NULL, Error_E_generic, 0, 0);
235 }
236 else {
237 /*
238 * The virtqueue contains a ring of used buffers. Get a pointer to the
239 * next entry in that used ring.
240 */
241 used = &vring->used->ring[vring->used->idx % vring->num];
242 used->id = head;
243 used->len = len;
245 vring->used->idx++;
246 }
247 GateAll_leave(vq->gateH, key);
249 return (0);
250 }
252 /*
253 * ======== VirtQueue_addAvailBuf ========
254 */
255 Int VirtQueue_addAvailBuf(VirtQueue_Object *vq, Void *buf)
256 {
257 UInt16 avail;
258 struct vring *vring = vq->vringPtr;
259 IArg key;
261 key = GateAll_enter(vq->gateH);
262 if (vq->num_free == 0) {
263 /* There's no more space */
264 Error_raise(NULL, Error_E_generic, 0, 0);
265 }
266 else {
267 vq->num_free--;
269 avail = vring->avail->idx++ % vring->num;
271 vring->desc[avail].addr = mapVAtoPA(buf);
272 vring->desc[avail].len = RPMSG_BUF_SIZE;
273 }
274 GateAll_leave(vq->gateH, key);
276 return (vq->num_free);
277 }
279 /*
280 * ======== VirtQueue_getUsedBuf ========
281 */
282 Void *VirtQueue_getUsedBuf(VirtQueue_Object *vq)
283 {
284 UInt16 head;
285 Void *buf;
286 struct vring *vring = vq->vringPtr;
287 IArg key;
289 key = GateAll_enter(vq->gateH);
290 /* There's nothing available? */
291 if (vq->last_used_idx == vring->used->idx) {
292 buf = NULL;
293 }
294 else {
295 head = vring->used->ring[vq->last_used_idx % vring->num].id;
296 vq->last_used_idx++;
297 vq->num_free++;
299 buf = mapPAtoVA(vring->desc[head].addr);
300 }
301 GateAll_leave(vq->gateH, key);
303 return (buf);
304 }
306 /*
307 * ======== VirtQueue_getAvailBuf ========
308 */
309 Int16 VirtQueue_getAvailBuf(VirtQueue_Handle vq, Void **buf, Int *len)
310 {
311 Int16 head;
312 struct vring *vring = vq->vringPtr;
313 IArg key;
315 key = GateAll_enter(vq->gateH);
316 Log_print6(Diags_USER1, "getAvailBuf vq: 0x%x %d %d %d 0x%x 0x%x\n",
317 (IArg)vq,
318 vq->last_avail_idx, vring->avail->idx, vring->num,
319 (IArg)&vring->avail, (IArg)vring->avail);
321 /* Clear flag here to avoid race condition with remote processor.
322 * This is a negative flag, clearing it means that we want to
323 * receive an interrupt when a buffer has been added to the pool.
324 */
325 vring->used->flags &= ~VRING_USED_F_NO_NOTIFY;
327 /* There's nothing available? */
328 if (vq->last_avail_idx == vring->avail->idx) {
329 head = (-1);
330 }
331 else {
332 /* No need to be kicked about added buffers anymore */
333 vring->used->flags |= VRING_USED_F_NO_NOTIFY;
335 /*
336 * Grab the next descriptor number they're advertising, and increment
337 * the index we've seen.
338 */
339 head = vring->avail->ring[vq->last_avail_idx++ % vring->num];
341 *buf = mapPAtoVA(vring->desc[head].addr);
342 *len = vring->desc[head].len;
343 }
344 GateAll_leave(vq->gateH, key);
346 return (head);
347 }
349 /*
350 * ======== VirtQueue_isr ========
351 * Note 'msg' is ignored: it is only used where there is a mailbox payload.
352 */
353 Void VirtQueue_isr(UArg msg)
354 {
355 VirtQueue_Object *vq;
357 Log_print1(Diags_USER1, "VirtQueue_isr received msg = 0x%x\n", msg);
359 vq = queueRegistry[0];
360 if (vq) {
361 vq->callback(vq);
362 }
363 vq = queueRegistry[1];
364 if (vq) {
365 vq->callback(vq);
366 }
367 }
369 /*
370 * ======== VirtQueue_startup ========
371 */
372 Void VirtQueue_startup(UInt16 remoteProcId, Bool isHost)
373 {
374 IInterrupt_IntInfo intInfo;
376 intInfo.intVectorId = Interrupt_DSPINT;
377 intInfo.localIntId = Interrupt_SRCS_BITPOS_HOST;
380 /*
381 * Wait for first kick from host, which happens to coincide with the
382 * priming of host's receive buffers, indicating host is ready to send.
383 * Since interrupt is cleared, we throw away this first kick, which is
384 * OK since we don't process this in the ISR anyway.
385 */
386 Log_print0(Diags_USER1, "VirtQueue_startup: Polling for host int...\n");
387 while (!Interrupt_checkAndClear(remoteProcId, &intInfo));
389 Interrupt_intRegister(remoteProcId, &intInfo, (Fxn)VirtQueue_isr, 0);
391 Log_print0(Diags_USER1, "Passed VirtQueue_startup\n");
392 }
394 /* By convention, Host VirtQueues host are the even number in the pair */
395 Bool VirtQueue_isSlave(VirtQueue_Handle vq)
396 {
397 return (vq->id & 0x1);
398 }
400 Bool VirtQueue_isHost(VirtQueue_Handle vq)
401 {
402 return (~(vq->id & 0x1));
403 }
405 UInt16 VirtQueue_getId(VirtQueue_Handle vq)
406 {
407 return (vq->id);
408 }
410 Swi_Handle VirtQueue_getSwiHandle(VirtQueue_Handle vq)
411 {
412 return (vq->swiHandle);
413 }
415 #define CACHE_WB_TICK_PERIOD 5
417 /*
418 * ======== VirtQueue_cacheWb ========
419 *
420 * Used for flushing SysMin trace buffer.
421 */
422 Void VirtQueue_cacheWb()
423 {
424 static UInt32 oldticks = 0;
425 UInt32 newticks;
427 newticks = Clock_getTicks();
428 if (newticks - oldticks < (UInt32)CACHE_WB_TICK_PERIOD) {
429 /* Don't keep flushing cache */
430 return;
431 }
433 oldticks = newticks;
435 /* Flush the cache of the SysMin buffer only: */
436 Assert_isTrue((VirtQueue_module->traceBufPtr != NULL), NULL);
437 Cache_wb(VirtQueue_module->traceBufPtr, SysMin_bufSize, Cache_Type_ALL,
438 FALSE);
439 }