56a5fdb95f912a5d4eef9400ce0a617d87547b6f
1 /*
2 * Copyright (c) 2011-2015, 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/vayu/VirtQueue.h>
52 * @endcode
53 *
54 */
56 /* this define must precede inclusion of any xdc header file */
57 #define Registry_CURDESC ti_ipc_family_vayu__Desc
58 #define MODULE_NAME "ti.ipc.family.vayu.VirtQueue"
60 #include <xdc/std.h>
61 #include <xdc/runtime/System.h>
62 #include <xdc/runtime/Assert.h>
63 #include <xdc/runtime/Error.h>
64 #include <xdc/runtime/Memory.h>
65 #include <xdc/runtime/Registry.h>
66 #include <xdc/runtime/Log.h>
67 #include <xdc/runtime/Diags.h>
69 #include <ti/sysbios/hal/Hwi.h>
70 #include <ti/sysbios/knl/Clock.h>
71 #include <ti/sysbios/gates/GateHwi.h>
72 #include <ti/sysbios/hal/Cache.h>
74 #include <ti/ipc/remoteproc/Resource.h>
75 #include <ti/ipc/MultiProc.h>
77 #include <ti/ipc/rpmsg/virtio_ring.h>
78 #include <ti/pm/IpcPower.h>
80 #include <string.h>
82 #include <ti/ipc/remoteproc/Resource.h>
83 #include <ti/ipc/remoteproc/rsc_types.h>
84 #include <ti/ipc/rpmsg/_VirtQueue.h>
86 #include <ti/sdo/ipc/notifyDrivers/IInterrupt.h>
87 #include "InterruptProxy.h"
88 #include "VirtQueue.h"
91 /*
92 * The following three VIRTIO_* defines must match those in
93 * <Linux_kernel>/include/uapi/linux/virtio_config.h
94 */
95 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
96 #define VIRTIO_CONFIG_S_DRIVER 2
97 #define VIRTIO_CONFIG_S_DRIVER_OK 4
99 #define VRING_BUFS_PRIMED (VIRTIO_CONFIG_S_ACKNOWLEDGE | \
100 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK)
102 /* Used for defining the size of the virtqueue registry */
103 #define NUM_QUEUES 2
105 /*
106 * Size of the virtqueues (expressed in number of buffers supported,
107 * and must be power of two)
108 */
109 #define VQ_SIZE 256
111 /*
112 * enum - Predefined Mailbox Messages
113 *
114 * @RP_MSG_MBOX_READY: informs the slave that we're up and running. will be
115 * followed by another mailbox message that carries the HOST's virtual address
116 * of the shared buffer. This would allow the HOST's drivers to send virtual
117 * addresses of the buffers.
118 *
119 * @RP_MSG_MBOX_STATE_CHANGE: informs the receiver that there is an inbound
120 * message waiting in its own receive-side vring. please note that currently
121 * this message is optional: alternatively, one can explicitly send the index
122 * of the triggered virtqueue itself. the preferred approach will be decided
123 * as we progress and experiment with those design ideas.
124 *
125 * @RP_MSG_MBOX_CRASH: this message indicates that the BIOS side is unhappy
126 *
127 * @RP_MBOX_ECHO_REQUEST: this message requests the remote processor to reply
128 * with RP_MBOX_ECHO_REPLY
129 *
130 * @RP_MBOX_ECHO_REPLY: this is a reply that is sent when RP_MBOX_ECHO_REQUEST
131 * is received.
132 *
133 * @RP_MBOX_ABORT_REQUEST: tells the M3 to crash on demand
134 *
135 * @RP_MBOX_BOOTINIT_DONE: this message indicates the BIOS side has reached a
136 * certain state during the boot process. This message is used to inform the
137 * host that the basic BIOS initialization is done, and lets the host use this
138 * notification to perform certain actions.
139 */
140 enum {
141 RP_MSG_MBOX_READY = (Int)0xFFFFFF00,
142 RP_MSG_MBOX_STATE_CHANGE = (Int)0xFFFFFF01,
143 RP_MSG_MBOX_CRASH = (Int)0xFFFFFF02,
144 RP_MBOX_ECHO_REQUEST = (Int)0xFFFFFF03,
145 RP_MBOX_ECHO_REPLY = (Int)0xFFFFFF04,
146 RP_MBOX_ABORT_REQUEST = (Int)0xFFFFFF05,
147 RP_MSG_FLUSH_CACHE = (Int)0xFFFFFF06,
148 RP_MSG_BOOTINIT_DONE = (Int)0xFFFFFF07,
149 RP_MSG_HIBERNATION = (Int)0xFFFFFF10,
150 RP_MSG_HIBERNATION_FORCE = (Int)0xFFFFFF11,
151 RP_MSG_HIBERNATION_ACK = (Int)0xFFFFFF12,
152 RP_MSG_HIBERNATION_CANCEL = (Int)0xFFFFFF13
153 };
155 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
156 #define RP_MSG_NUM_BUFS (VQ_SIZE) /* must be power of two */
157 #define RP_MSG_BUF_SIZE (512)
158 #define RP_MSG_BUFS_SPACE (RP_MSG_NUM_BUFS * RP_MSG_BUF_SIZE * 2)
160 #define PAGE_SIZE (4096)
161 /*
162 * The alignment to use between consumer and producer parts of vring.
163 * Note: this is part of the "wire" protocol. If you change this, you need
164 * to update your BIOS image as well
165 */
166 #define RP_MSG_VRING_ALIGN (4096)
168 /* With 256 buffers, our vring will occupy 3 pages */
169 #define RP_MSG_RING_SIZE ((DIV_ROUND_UP(vring_size(RP_MSG_NUM_BUFS, \
170 RP_MSG_VRING_ALIGN), PAGE_SIZE)) * PAGE_SIZE)
172 /* The total IPC space needed to communicate with a remote processor */
173 #define RPMSG_IPC_MEM (RP_MSG_BUFS_SPACE + 2 * RP_MSG_RING_SIZE)
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 /* Will eventually be used to kick remote processor */
192 UInt16 procId;
194 /* Gate to protect from multiple threads */
195 GateHwi_Handle gateH;
197 /* Base phys addr - used for quick pa/va translations */
198 UInt32 basePa;
200 /* Base virt addr - used for quick pa/va translations */
201 UInt32 baseVa;
202 } VirtQueue_Object;
204 /* module diags mask */
205 Registry_Desc Registry_CURDESC;
207 static struct VirtQueue_Object *queueRegistry[NUM_QUEUES] = {NULL};
209 static UInt16 hostProcId;
211 #define DSPEVENTID 5
212 IInterrupt_IntInfo intInfo;
214 /*!
215 * ======== _VirtQueue_init ========
216 *
217 * This function adds the VirtQueue "module" to the Registry so that
218 * DIAGS will work with this non-XDC module.
219 * Since VirtQueue_init is not called by XDC-VirtQueue module clients, this
220 * function is called in the first VirtQueue fxn called: VirtQueue_create.
221 */
222 static Void _VirtQueue_init()
223 {
224 static int initialized = 0;
226 if (!initialized) {
227 Registry_Result result;
229 /* register with xdc.runtime to get a diags mask */
230 result = Registry_addModule(&Registry_CURDESC, MODULE_NAME);
231 Assert_isTrue(result == Registry_SUCCESS, (Assert_Id)NULL);
233 initialized = 1;
234 }
235 }
237 static inline Void * _VirtQueue_getVA(VirtQueue_Handle vq, UInt32 pa)
238 {
239 return (Void *)(pa - vq->basePa + vq->baseVa);
240 }
242 /*!
243 * ======== VirtQueue_kick ========
244 */
245 Void VirtQueue_kick(VirtQueue_Handle vq)
246 {
247 /* For now, simply interrupt remote processor */
248 if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) {
249 Log_print0(Diags_USER1,
250 "VirtQueue_kick: no kick because of VRING_AVAIL_F_NO_INTERRUPT\n");
251 return;
252 }
254 Log_print2(Diags_USER1,
255 "VirtQueue_kick: Sending interrupt to proc %d with payload 0x%x\n",
256 (IArg)vq->procId, (IArg)vq->id);
257 InterruptProxy_intSend(vq->procId, NULL, vq->id);
258 }
260 /*!
261 * ======== VirtQueue_addUsedBuf ========
262 */
263 Int VirtQueue_addUsedBuf(VirtQueue_Handle vq, Int16 head, Int len)
264 {
265 struct vring_used_elem *used;
266 IArg key;
268 key = GateHwi_enter(vq->gateH);
269 if ((head > vq->vring.num) || (head < 0)) {
270 GateHwi_leave(vq->gateH, key);
271 Error_raise(NULL, Error_E_generic, 0, 0);
272 }
274 /*
275 * The virtqueue contains a ring of used buffers. Get a pointer to the
276 * next entry in that used ring.
277 */
278 used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num];
279 used->id = head;
280 used->len = len;
282 vq->vring.used->idx++;
283 GateHwi_leave(vq->gateH, key);
285 return (0);
286 }
288 /*!
289 * ======== VirtQueue_getAvailBuf ========
290 */
291 Int16 VirtQueue_getAvailBuf(VirtQueue_Handle vq, Void **buf, Int *len)
292 {
293 Int16 head;
294 IArg key;
296 key = GateHwi_enter(vq->gateH);
297 Log_print6(Diags_USER1, "getAvailBuf vq: 0x%x %d %d %d 0x%x 0x%x\n",
298 (IArg)vq, vq->last_avail_idx, vq->vring.avail->idx, vq->vring.num,
299 (IArg)&vq->vring.avail, (IArg)vq->vring.avail);
301 /* Clear flag here to avoid race condition with remote processor.
302 * This is a negative flag, clearing it means that we want to
303 * receive an interrupt when a buffer has been added to the pool.
304 */
305 vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY;
307 /* There's nothing available? */
308 if (vq->last_avail_idx == vq->vring.avail->idx) {
309 head = (-1);
310 }
311 else {
312 /* No need to be kicked about added buffers anymore */
313 vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY;
315 /*
316 * Grab the next descriptor number they're advertising, and increment
317 * the index we've seen.
318 */
319 head = vq->vring.avail->ring[vq->last_avail_idx++ % vq->vring.num];
321 *buf = _VirtQueue_getVA(vq, vq->vring.desc[head].addr);
322 *len = vq->vring.desc[head].len;
323 }
324 GateHwi_leave(vq->gateH, key);
326 return (head);
327 }
329 /*!
330 * ======== VirtQueue_disableCallback ========
331 */
332 Void VirtQueue_disableCallback(VirtQueue_Object *vq)
333 {
334 /* TODO */
335 Log_print0(Diags_USER1, "VirtQueue_disableCallback called.");
336 }
338 /*!
339 * ======== VirtQueue_enableCallback ========
340 */
341 Bool VirtQueue_enableCallback(VirtQueue_Object *vq)
342 {
343 Log_print0(Diags_USER1, "VirtQueue_enableCallback called.");
345 /* TODO */
346 return (FALSE);
347 }
349 /*!
350 * ======== VirtQueue_isr ========
351 * Note 'arg' is ignored: it is the Hwi argument, not the mailbox argument.
352 */
353 Void VirtQueue_isr(UArg msg)
354 {
355 VirtQueue_Object *vq;
357 msg = InterruptProxy_intClear(hostProcId, &intInfo);
359 Log_print1(Diags_USER1, "VirtQueue_isr received msg = 0x%x\n", msg);
361 switch(msg) {
362 case (UInt)RP_MSG_MBOX_READY:
363 return;
365 case (UInt)RP_MBOX_ECHO_REQUEST:
366 InterruptProxy_intSend(hostProcId, NULL, (UInt)(RP_MBOX_ECHO_REPLY));
367 return;
369 case (UInt)RP_MBOX_ABORT_REQUEST:
370 {
371 /* Suppress Coverity Error: FORWARD_NULL: */
372 /* coverity[assign_zero] */
373 Fxn f = (Fxn)0x0;
374 Log_print0(Diags_USER1, "Crash on demand ...\n");
375 /* coverity[var_deref_op] */
376 f();
377 }
378 return;
380 case (UInt)RP_MSG_FLUSH_CACHE:
381 Cache_wbAll();
382 return;
384 case (UInt)RP_MSG_HIBERNATION:
385 if (IpcPower_canHibernate() == FALSE) {
386 InterruptProxy_intSend(hostProcId, NULL,
387 (UInt)RP_MSG_HIBERNATION_CANCEL);
388 return;
389 }
391 /* Fall through */
392 case (UInt)RP_MSG_HIBERNATION_FORCE:
393 /* Ack request */
394 InterruptProxy_intSend(hostProcId, NULL,
395 (UInt)RP_MSG_HIBERNATION_ACK);
396 IpcPower_suspend();
397 return;
399 default:
400 /*
401 * If the message isn't one of the above, it's either part of the
402 * 2-message synchronization sequence or it a virtqueue message
403 */
404 break;
405 }
407 /* Don't let unknown messages to pass as a virtqueue index */
408 if (msg >= NUM_QUEUES) {
409 /* Adding print here deliberately, we should never see this */
410 System_printf("VirtQueue_isr: Invalid mailbox message 0x%x "
411 "received\n", msg);
412 return;
413 }
415 vq = queueRegistry[msg];
416 if (vq) {
417 vq->callback(vq);
418 }
419 }
422 /*!
423 * ======== VirtQueue_create ========
424 */
425 VirtQueue_Handle VirtQueue_create(UInt16 remoteProcId, VirtQueue_Params *params,
426 Error_Block *eb)
427 {
428 VirtQueue_Object *vq;
429 Void *vringAddr;
430 Int result;
432 /* Perform initialization we can't do in Instance_init (being non-XDC): */
433 _VirtQueue_init();
435 vq = Memory_alloc(NULL, sizeof(VirtQueue_Object), 0, eb);
436 if (NULL == vq) {
437 return (NULL);
438 }
440 /* Create the thread protection gate */
441 vq->gateH = GateHwi_create(NULL, eb);
442 if (Error_check(eb)) {
443 Log_error0("VirtQueue_create: could not create gate object");
444 Memory_free(NULL, vq, sizeof(VirtQueue_Object));
445 return (NULL);
446 }
448 vq->callback = params->callback;
449 vq->id = params->vqId;
450 vq->procId = remoteProcId;
451 vq->last_avail_idx = 0;
453 switch (vq->id) {
454 /* IPC transport vrings */
455 case ID_SELF_TO_HOST:
456 case ID_HOST_TO_SELF:
457 vq->basePa = (UInt32)Resource_getVringDA(vq->id);
458 Assert_isTrue(vq->basePa != NULL, NULL);
460 result = Resource_physToVirt(vq->basePa, &(vq->baseVa));
461 Assert_isTrue(result == Resource_S_SUCCESS, (Assert_Id)NULL);
463 vringAddr = (Void *)vq->baseVa;
464 break;
465 default:
466 GateHwi_delete(&vq->gateH);
467 Memory_free(NULL, vq, sizeof(VirtQueue_Object));
468 return (NULL);
469 }
471 Log_print3(Diags_USER1,
472 "vring: %d 0x%x (0x%x)\n", vq->id, (IArg)vringAddr,
473 RP_MSG_RING_SIZE);
475 /* See coverity related comment in vring_init() */
476 /* coverity[overrun-call] */
477 vring_init(&(vq->vring), RP_MSG_NUM_BUFS, vringAddr, RP_MSG_VRING_ALIGN);
479 /*
480 * Don't trigger a mailbox message every time MPU makes another buffer
481 * available
482 */
483 if (vq->procId == hostProcId) {
484 vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY;
485 }
487 queueRegistry[vq->id] = vq;
489 return (vq);
490 }
492 /*!
493 * ======== VirtQueue_startup ========
494 */
495 Void VirtQueue_startup()
496 {
497 hostProcId = MultiProc_getId("HOST");
499 #ifdef DSP
500 intInfo.intVectorId = DSPEVENTID;
501 #endif
503 /* Initilize the IpcPower module */
504 IpcPower_init();
506 /*
507 * Wait for HLOS (Virtio device) to indicate that priming of host's receive
508 * buffers is complete, indicating that host is ready to send.
509 *
510 * Though this is a Linux Virtio configuration status, it must be
511 * implemented by each non-Linux HLOS as well.
512 */
513 Log_print1(Diags_USER1, "VirtQueue_startup: VDEV status: 0x%x\n",
514 Resource_getVdevStatus(VIRTIO_ID_RPMSG));
515 Log_print0(Diags_USER1, "VirtQueue_startup: Polling VDEV status...\n");
516 while (Resource_getVdevStatus(VIRTIO_ID_RPMSG) != VRING_BUFS_PRIMED);
517 Log_print1(Diags_USER1, "VirtQueue_startup: VDEV status: 0x%x\n",
518 Resource_getVdevStatus(VIRTIO_ID_RPMSG));
520 InterruptProxy_intRegister(hostProcId, &intInfo, (Fxn)VirtQueue_isr, NULL);
521 Log_print0(Diags_USER1, "Passed VirtQueue_startup\n");
522 }
524 /*!
525 * ======== VirtQueue_postCrashToMailbox ========
526 */
527 Void VirtQueue_postCrashToMailbox(Void)
528 {
529 InterruptProxy_intSend(0, NULL, (UInt)RP_MSG_MBOX_CRASH);
530 }
532 #define CACHE_WB_TICK_PERIOD 5
534 /*!
535 * ======== VirtQueue_cacheWb ========
536 *
537 * Used for flushing SysMin trace buffer.
538 */
539 Void VirtQueue_cacheWb()
540 {
541 static UInt32 oldticks = 0;
542 UInt32 newticks;
544 newticks = Clock_getTicks();
545 if (newticks - oldticks < (UInt32)CACHE_WB_TICK_PERIOD) {
546 /* Don't keep flushing cache */
547 return;
548 }
550 oldticks = newticks;
552 /* Flush the cache */
553 Cache_wbAll();
554 }