]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - packages/ti/ipc/family/omap54xx/VirtQueue.c
OMAP5: trivial comment fix
[ipc/ipcdev.git] / packages / ti / ipc / family / omap54xx / VirtQueue.c
1 /*
2  * Copyright (c) 2011-2013, 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/family/omap54xx/VirtQueue.h>
52  *  @endcode
53  *
54  */
56 #include <xdc/std.h>
57 #include <xdc/runtime/System.h>
58 #include <xdc/runtime/Error.h>
59 #include <xdc/runtime/Memory.h>
60 #include <xdc/runtime/Log.h>
61 #include <xdc/runtime/Diags.h>
63 #include <ti/sysbios/hal/Hwi.h>
64 #include <ti/sysbios/knl/Semaphore.h>
65 #include <ti/sysbios/knl/Clock.h>
66 #include <ti/sysbios/BIOS.h>
67 #include <ti/sysbios/hal/Cache.h>
69 #include <ti/ipc/MultiProc.h>
71 #include <ti/ipc/rpmsg/virtio_ring.h>
72 #include <ti/pm/IpcPower.h>
73 #include <string.h>
75 #include <ti/ipc/rpmsg/_VirtQueue.h>
77 #include "InterruptProxy.h"
78 #include "VirtQueue.h"
81 /* Used for defining the size of the virtqueue registry */
82 #define NUM_QUEUES              4
84 /* Predefined device addresses */
85 #ifndef DSPC674
86 #define IPC_MEM_VRING0          0xA0000000
87 #define IPC_MEM_VRING1          0xA0004000
88 #else
89 #define IPC_MEM_VRING0          0x9FB00000
90 #define IPC_MEM_VRING1          0x9FB04000
91 #endif
92 #define IPC_MEM_VRING2          0xA0008000
93 #define IPC_MEM_VRING3          0xA000c000
95 /*
96  * Sizes of the virtqueues (expressed in number of buffers supported,
97  * and must be power of two)
98  */
99 #define VQ0_SIZE                256
100 #define VQ1_SIZE                256
101 #define VQ2_SIZE                256
102 #define VQ3_SIZE                256
104 /*
105  * enum - Predefined Mailbox Messages
106  *
107  * @RP_MSG_MBOX_READY: informs the M3's that we're up and running. will be
108  * followed by another mailbox message that carries the A9's virtual address
109  * of the shared buffer. This would allow the A9's drivers to send virtual
110  * addresses of the buffers.
111  *
112  * @RP_MSG_MBOX_STATE_CHANGE: informs the receiver that there is an inbound
113  * message waiting in its own receive-side vring. please note that currently
114  * this message is optional: alternatively, one can explicitly send the index
115  * of the triggered virtqueue itself. the preferred approach will be decided
116  * as we progress and experiment with those design ideas.
117  *
118  * @RP_MSG_MBOX_CRASH: this message indicates that the BIOS side is unhappy
119  *
120  * @RP_MBOX_ECHO_REQUEST: this message requests the remote processor to reply
121  * with RP_MBOX_ECHO_REPLY
122  *
123  * @RP_MBOX_ECHO_REPLY: this is a reply that is sent when RP_MBOX_ECHO_REQUEST
124  * is received.
125  *
126  * @RP_MBOX_ABORT_REQUEST:  tells the M3 to crash on demand
127  *
128  * @RP_MBOX_BOOTINIT_DONE: this message indicates the BIOS side has reached a
129  * certain state during the boot process. This message is used to inform the
130  * host that the basic BIOS initialization is done, and lets the host use this
131  * notification to perform certain actions.
132  */
133 enum {
134     RP_MSG_MBOX_READY           = (Int)0xFFFFFF00,
135     RP_MSG_MBOX_STATE_CHANGE    = (Int)0xFFFFFF01,
136     RP_MSG_MBOX_CRASH           = (Int)0xFFFFFF02,
137     RP_MBOX_ECHO_REQUEST        = (Int)0xFFFFFF03,
138     RP_MBOX_ECHO_REPLY          = (Int)0xFFFFFF04,
139     RP_MBOX_ABORT_REQUEST       = (Int)0xFFFFFF05,
140     RP_MSG_FLUSH_CACHE          = (Int)0xFFFFFF06,
141     RP_MSG_BOOTINIT_DONE        = (Int)0xFFFFFF07,
142     RP_MSG_HIBERNATION          = (Int)0xFFFFFF10,
143     RP_MSG_HIBERNATION_FORCE    = (Int)0xFFFFFF11,
144     RP_MSG_HIBERNATION_ACK      = (Int)0xFFFFFF12,
145     RP_MSG_HIBERNATION_CANCEL   = (Int)0xFFFFFF13
146 };
148 #define DIV_ROUND_UP(n,d)   (((n) + (d) - 1) / (d))
149 #define RP_MSG_NUM_BUFS     (VQ0_SIZE) /* must be power of two */
150 #define RP_MSG_BUF_SIZE     (512)
151 #define RP_MSG_BUFS_SPACE   (RP_MSG_NUM_BUFS * RP_MSG_BUF_SIZE * 2)
153 #define PAGE_SIZE           (4096)
154 /*
155  * The alignment to use between consumer and producer parts of vring.
156  * Note: this is part of the "wire" protocol. If you change this, you need
157  * to update your BIOS image as well
158  */
159 #define RP_MSG_VRING_ALIGN  (4096)
161 /* With 256 buffers, our vring will occupy 3 pages */
162 #define RP_MSG_RING_SIZE    ((DIV_ROUND_UP(vring_size(RP_MSG_NUM_BUFS, \
163                             RP_MSG_VRING_ALIGN), PAGE_SIZE)) * PAGE_SIZE)
165 /* The total IPC space needed to communicate with a remote processor */
166 #define RPMSG_IPC_MEM   (RP_MSG_BUFS_SPACE + 2 * RP_MSG_RING_SIZE)
168 #define ID_SYSM3_TO_A9      ID_SELF_TO_A9
169 #define ID_A9_TO_SYSM3      ID_A9_TO_SELF
170 #define ID_DSP_TO_A9        ID_SELF_TO_A9
171 #define ID_A9_TO_DSP        ID_A9_TO_SELF
172 #define ID_APPM3_TO_A9      200
173 #define ID_A9_TO_APPM3      201
175 typedef struct VirtQueue_Object {
176     /* Id for this VirtQueue_Object */
177     UInt16                  id;
179     /* The function to call when buffers are consumed (can be NULL) */
180     VirtQueue_callback      callback;
182     /* Shared state */
183     struct vring            vring;
185     /* Number of free buffers */
186     UInt16                  num_free;
188     /* Last available index; updated by VirtQueue_getAvailBuf */
189     UInt16                  last_avail_idx;
191     /* Last available index; updated by VirtQueue_addUsedBuf */
192     UInt16                  last_used_idx;
194     /* Will eventually be used to kick remote processor */
195     UInt16                  procId;
196 } VirtQueue_Object;
198 static struct VirtQueue_Object *queueRegistry[NUM_QUEUES] = {NULL};
200 static UInt16 hostProcId;
201 #ifndef SMP
202 static UInt16 dspProcId;
203 static UInt16 sysm3ProcId;
204 static UInt16 appm3ProcId;
205 #endif
207 #if defined(M3_ONLY) && !defined(SMP)
208 extern Void OffloadM3_init();
209 extern Int OffloadM3_processSysM3Tasks(UArg msg);
210 #endif
212 static inline Void * mapPAtoVA(UInt pa)
214 #ifndef DSPC674
215     return (Void *)((pa & 0x000fffffU) | 0xa0000000U);
216 #else
217     return (Void *)((pa & 0x000fffffU) | 0x9fb00000U);
218 #endif
221 static inline UInt mapVAtoPA(Void * va)
223     return ((UInt)va & 0x000fffffU) | 0x9cf00000U;
226 /*!
227  * ======== VirtQueue_kick ========
228  */
229 Void VirtQueue_kick(VirtQueue_Handle vq)
231     /* For now, simply interrupt remote processor */
232     if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) {
233         Log_print0(Diags_USER1,
234                 "VirtQueue_kick: no kick because of VRING_AVAIL_F_NO_INTERRUPT\n");
235         return;
236     }
238     Log_print2(Diags_USER1,
239             "VirtQueue_kick: Sending interrupt to proc %d with payload 0x%x\n",
240             (IArg)vq->procId, (IArg)vq->id);
241     InterruptProxy_intSend(vq->procId, vq->id);
244 /*!
245  * ======== VirtQueue_addUsedBuf ========
246  */
247 Int VirtQueue_addUsedBuf(VirtQueue_Handle vq, Int16 head, Int len)
249     struct vring_used_elem *used;
251     if ((head > vq->vring.num) || (head < 0)) {
252         Error_raise(NULL, Error_E_generic, 0, 0);
253     }
255     /*
256     * The virtqueue contains a ring of used buffers.  Get a pointer to the
257     * next entry in that used ring.
258     */
259     used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num];
260     used->id = head;
261     used->len = len;
263     vq->vring.used->idx++;
265     return (0);
268 /*!
269  * ======== VirtQueue_addAvailBuf ========
270  */
271 Int VirtQueue_addAvailBuf(VirtQueue_Object *vq, Void *buf)
273     UInt16 avail;
275     if (vq->num_free == 0) {
276         /* There's no more space */
277         Error_raise(NULL, Error_E_generic, 0, 0);
278     }
280     vq->num_free--;
282     avail =  vq->vring.avail->idx++ % vq->vring.num;
284     vq->vring.desc[avail].addr = mapVAtoPA(buf);
285     vq->vring.desc[avail].len = RP_MSG_BUF_SIZE;
287     return (vq->num_free);
290 /*!
291  * ======== VirtQueue_getUsedBuf ========
292  */
293 Void *VirtQueue_getUsedBuf(VirtQueue_Object *vq)
295     UInt16 head;
296     Void *buf;
298     /* There's nothing available? */
299     if (vq->last_used_idx == vq->vring.used->idx) {
300         return (NULL);
301     }
303     head = vq->vring.used->ring[vq->last_used_idx % vq->vring.num].id;
304     vq->last_used_idx++;
306     buf = mapPAtoVA(vq->vring.desc[head].addr);
308     return (buf);
311 /*!
312  * ======== VirtQueue_getAvailBuf ========
313  */
314 Int16 VirtQueue_getAvailBuf(VirtQueue_Handle vq, Void **buf, Int *len)
316     UInt16 head;
318     Log_print6(Diags_USER1, "getAvailBuf vq: 0x%x %d %d %d 0x%x 0x%x\n",
319         (IArg)vq, vq->last_avail_idx, vq->vring.avail->idx, vq->vring.num,
320         (IArg)&vq->vring.avail, (IArg)vq->vring.avail);
322     /* There's nothing available? */
323     if (vq->last_avail_idx == vq->vring.avail->idx) {
324         /* We need to know about added buffers */
325         vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY;
327         return (-1);
328     }
329     /*
330      * Grab the next descriptor number they're advertising, and increment
331      * the index we've seen.
332      */
333     head = vq->vring.avail->ring[vq->last_avail_idx++ % vq->vring.num];
335     *buf = mapPAtoVA(vq->vring.desc[head].addr);
336     *len = vq->vring.desc[head].len;
338     return (head);
341 /*!
342  * ======== VirtQueue_disableCallback ========
343  */
344 Void VirtQueue_disableCallback(VirtQueue_Object *vq)
346     //TODO
347     Log_print0(Diags_USER1, "VirtQueue_disableCallback called.");
350 /*!
351  * ======== VirtQueue_enableCallback ========
352  */
353 Bool VirtQueue_enableCallback(VirtQueue_Object *vq)
355     Log_print0(Diags_USER1, "VirtQueue_enableCallback called.");
357     //TODO
358     return (FALSE);
361 /*!
362  * ======== VirtQueue_isr ========
363  * Note 'arg' is ignored: it is the Hwi argument, not the mailbox argument.
364  */
365 Void VirtQueue_isr(UArg msg)
367     VirtQueue_Object *vq;
369     Log_print1(Diags_USER1, "VirtQueue_isr received msg = 0x%x\n", msg);
371 #ifndef SMP
372     if (MultiProc_self() == sysm3ProcId || MultiProc_self() == dspProcId) {
373 #endif
374         switch(msg) {
375             case (UInt)RP_MSG_MBOX_READY:
376                 return;
378             case (UInt)RP_MBOX_ECHO_REQUEST:
379                 InterruptProxy_intSend(hostProcId, (UInt)(RP_MBOX_ECHO_REPLY));
380                 return;
382             case (UInt)RP_MBOX_ABORT_REQUEST:
383                 {
384                     Fxn f = (Fxn)0x0;
385                     Log_print0(Diags_USER1, "Crash on demand ...\n");
386                     f();
387                 }
388                 return;
390             case (UInt)RP_MSG_FLUSH_CACHE:
391                 Cache_wbAll();
392                 return;
394 #ifndef DSPC674
395             case (UInt)RP_MSG_HIBERNATION:
396                 if (IpcPower_canHibernate() == FALSE) {
397                     InterruptProxy_intSend(hostProcId,
398                                         (UInt)RP_MSG_HIBERNATION_CANCEL);
399                     return;
400                 }
402             /* Fall through */
403             case (UInt)RP_MSG_HIBERNATION_FORCE:
404 #ifndef SMP
405                 /* Core0 should notify Core1 */
406                 if (MultiProc_self() == sysm3ProcId) {
407                     InterruptProxy_intSend(appm3ProcId,
408                                            (UInt)(RP_MSG_HIBERNATION));
409                 }
410 #endif
411                 /* Ack request */
412                 InterruptProxy_intSend(hostProcId,
413                                     (UInt)RP_MSG_HIBERNATION_ACK);
414                 IpcPower_suspend();
415                 return;
416 #endif
417             default:
418 #if defined(M3_ONLY) && !defined(SMP)
419                 /* Check and process any Inter-M3 Offload messages */
420                 if (OffloadM3_processSysM3Tasks(msg))
421                     return;
422 #endif
424                 /*
425                  *  If the message isn't one of the above, it's either part of the
426                  *  2-message synchronization sequence or it a virtqueue message
427                  */
428                 break;
429         }
430 #ifndef SMP
431     }
432 #ifndef DSPC674
433     else if (msg & 0xFFFF0000) {
434         if (msg == (UInt)RP_MSG_HIBERNATION) {
435             IpcPower_suspend();
436         }
437         return;
438     }
440     if (MultiProc_self() == sysm3ProcId && (msg == ID_A9_TO_APPM3 || msg == ID_APPM3_TO_A9)) {
441         InterruptProxy_intSend(appm3ProcId, (UInt)msg);
442     }
443     else {
444 #endif
445 #endif
446         /* Don't let unknown messages to pass as a virtqueue index */
447         if (msg >= NUM_QUEUES) {
448             /* Adding print here deliberately, we should never see this */
449             System_printf("VirtQueue_isr: Invalid mailbox message 0x%x "
450                           "received\n", msg);
451             return;
452         }
454         vq = queueRegistry[msg];
455         if (vq) {
456             vq->callback(vq);
457         }
458 #ifndef SMP
459 #ifndef DSPC674
460     }
461 #endif
462 #endif
466 /*!
467  * ======== VirtQueue_create ========
468  */
469 VirtQueue_Handle VirtQueue_create(UInt16 remoteProcId, VirtQueue_Params *params,
470                                   Error_Block *eb)
472     VirtQueue_Object *vq;
473     Void *vringAddr;
475     vq = Memory_alloc(NULL, sizeof(VirtQueue_Object), 0, eb);
476     if (!vq) {
477         return (NULL);
478     }
480     vq->callback = params->callback;
481     vq->id = params->vqId;
482     vq->procId = remoteProcId;
483     vq->last_avail_idx = 0;
485 #ifndef SMP
486     if (MultiProc_self() == appm3ProcId) {
487         /* vqindices that belong to AppM3 should be big so they don't
488          * collide with SysM3's virtqueues */
489         vq->id += 200;
490     }
491 #endif
493     switch (vq->id) {
494         /* IPC transport vrings */
495         case ID_SELF_TO_A9:
496             /* IPU/DSP -> A9 */
497             vringAddr = (struct vring *) IPC_MEM_VRING0;
498             break;
499         case ID_A9_TO_SELF:
500             /* A9 -> IPU/DSP */
501             vringAddr = (struct vring *) IPC_MEM_VRING1;
502             break;
503 #ifndef SMP
504         case ID_APPM3_TO_A9:
505             /* APPM3 -> A9 */
506             vringAddr = (struct vring *) IPC_MEM_VRING2;
507             break;
508         case ID_A9_TO_APPM3:
509             /* A9 -> APPM3 */
510             vringAddr = (struct vring *) IPC_MEM_VRING3;
511             break;
512 #endif
513     }
515     Log_print3(Diags_USER1,
516             "vring: %d 0x%x (0x%x)\n", vq->id, (IArg)vringAddr,
517             RP_MSG_RING_SIZE);
519     vring_init(&(vq->vring), RP_MSG_NUM_BUFS, vringAddr, RP_MSG_VRING_ALIGN);
521     /*
522      *  Don't trigger a mailbox message every time MPU makes another buffer
523      *  available
524      */
525     if (vq->procId == hostProcId) {
526         vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY;
527     }
529     queueRegistry[vq->id] = vq;
531     return (vq);
534 /*!
535  * ======== VirtQueue_startup ========
536  */
537 Void VirtQueue_startup()
539     hostProcId      = MultiProc_getId("HOST");
540 #ifndef SMP
541     dspProcId       = MultiProc_getId("DSP");
542     sysm3ProcId     = MultiProc_getId("CORE0");
543     appm3ProcId     = MultiProc_getId("CORE1");
544 #endif
546 #ifndef DSPC674
547     /* Initilize the IpcPower module */
548     IpcPower_init();
549 #endif
551 #if defined(M3_ONLY) && !defined(SMP)
552     if (MultiProc_self() == sysm3ProcId) {
553         OffloadM3_init();
554     }
555 #endif
557     InterruptProxy_intRegister(VirtQueue_isr);
560 /*!
561  * ======== VirtQueue_postCrashToMailbox ========
562  */
563 Void VirtQueue_postCrashToMailbox(Void)
565     InterruptProxy_intSend(0, (UInt)RP_MSG_MBOX_CRASH);
568 /*!
569  * ======== VirtQueue_postInitDone ========
570  */
571 Void VirtQueue_postInitDone(Void)
573     InterruptProxy_intSend(0, (UInt)RP_MSG_BOOTINIT_DONE);
576 #define CACHE_WB_TICK_PERIOD    5
578 /*!
579  * ======== VirtQueue_cacheWb ========
580  *
581  * Used for flushing SysMin trace buffer.
582  */
583 Void VirtQueue_cacheWb()
585     static UInt32 oldticks = 0;
586     UInt32 newticks;
588     newticks = Clock_getTicks();
589     if (newticks - oldticks < (UInt32)CACHE_WB_TICK_PERIOD) {
590         /* Don't keep flushing cache */
591         return;
592     }
594     oldticks = newticks;
596     /* Flush the cache */
597     Cache_wbAll();