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/family/omap54xx/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.omap54xx.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/MultiProc.h>
76 #include <ti/ipc/rpmsg/virtio_ring.h>
77 #include <ti/pm/IpcPower.h>
78 #include <string.h>
80 #include <ti/ipc/remoteproc/Resource.h>
81 #include <ti/ipc/remoteproc/rsc_types.h>
82 #include <ti/ipc/rpmsg/_VirtQueue.h>
84 #include "InterruptProxy.h"
85 #include "VirtQueue.h"
88 /*
89 * The following three VIRTIO_* defines must match those in
90 * <Linux_kernel>/include/uapi/linux/virtio_config.h
91 */
92 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
93 #define VIRTIO_CONFIG_S_DRIVER 2
94 #define VIRTIO_CONFIG_S_DRIVER_OK 4
96 #define VRING_BUFS_PRIMED (VIRTIO_CONFIG_S_ACKNOWLEDGE | \
97 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK)
99 /* Used for defining the size of the virtqueue registry */
100 #define NUM_QUEUES 2
102 /* Predefined device addresses */
103 #define IPC_MEM_VRING0 0xA0000000
104 #define IPC_MEM_VRING1 0xA0004000
106 /*
107 * Size of the virtqueues (expressed in number of buffers supported,
108 * and must be power of two)
109 */
110 #define VQ_SIZE 256
112 /*
113 * enum - Predefined Mailbox Messages
114 *
115 * @RP_MSG_MBOX_READY: informs the slave that we're up and running. will be
116 * followed by another mailbox message that carries the HOST's virtual address
117 * of the shared buffer. This would allow the HOST's drivers to send virtual
118 * addresses of the buffers.
119 *
120 * @RP_MSG_MBOX_STATE_CHANGE: informs the receiver that there is an inbound
121 * message waiting in its own receive-side vring. please note that currently
122 * this message is optional: alternatively, one can explicitly send the index
123 * of the triggered virtqueue itself. the preferred approach will be decided
124 * as we progress and experiment with those design ideas.
125 *
126 * @RP_MSG_MBOX_CRASH: this message indicates that the BIOS side is unhappy
127 *
128 * @RP_MBOX_ECHO_REQUEST: this message requests the remote processor to reply
129 * with RP_MBOX_ECHO_REPLY
130 *
131 * @RP_MBOX_ECHO_REPLY: this is a reply that is sent when RP_MBOX_ECHO_REQUEST
132 * is received.
133 *
134 * @RP_MBOX_ABORT_REQUEST: tells the M3 to crash on demand
135 *
136 * @RP_MBOX_BOOTINIT_DONE: this message indicates the BIOS side has reached a
137 * certain state during the boot process. This message is used to inform the
138 * host that the basic BIOS initialization is done, and lets the host use this
139 * notification to perform certain actions.
140 */
141 enum {
142 RP_MSG_MBOX_READY = (Int)0xFFFFFF00,
143 RP_MSG_MBOX_STATE_CHANGE = (Int)0xFFFFFF01,
144 RP_MSG_MBOX_CRASH = (Int)0xFFFFFF02,
145 RP_MBOX_ECHO_REQUEST = (Int)0xFFFFFF03,
146 RP_MBOX_ECHO_REPLY = (Int)0xFFFFFF04,
147 RP_MBOX_ABORT_REQUEST = (Int)0xFFFFFF05,
148 RP_MSG_FLUSH_CACHE = (Int)0xFFFFFF06,
149 RP_MSG_BOOTINIT_DONE = (Int)0xFFFFFF07,
150 RP_MSG_HIBERNATION = (Int)0xFFFFFF10,
151 RP_MSG_HIBERNATION_FORCE = (Int)0xFFFFFF11,
152 RP_MSG_HIBERNATION_ACK = (Int)0xFFFFFF12,
153 RP_MSG_HIBERNATION_CANCEL = (Int)0xFFFFFF13
154 };
156 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
157 #define RP_MSG_NUM_BUFS (VQ_SIZE) /* must be power of two */
158 #define RP_MSG_BUF_SIZE (512)
159 #define RP_MSG_BUFS_SPACE (RP_MSG_NUM_BUFS * RP_MSG_BUF_SIZE * 2)
161 #define PAGE_SIZE (4096)
162 /*
163 * The alignment to use between consumer and producer parts of vring.
164 * Note: this is part of the "wire" protocol. If you change this, you need
165 * to update your BIOS image as well
166 */
167 #define RP_MSG_VRING_ALIGN (4096)
169 /* With 256 buffers, our vring will occupy 3 pages */
170 #define RP_MSG_RING_SIZE ((DIV_ROUND_UP(vring_size(RP_MSG_NUM_BUFS, \
171 RP_MSG_VRING_ALIGN), PAGE_SIZE)) * PAGE_SIZE)
173 /* The total IPC space needed to communicate with a remote processor */
174 #define RPMSG_IPC_MEM (RP_MSG_BUFS_SPACE + 2 * RP_MSG_RING_SIZE)
176 typedef struct VirtQueue_Object {
177 /* Id for this VirtQueue_Object */
178 UInt16 id;
180 /* The function to call when buffers are consumed (can be NULL) */
181 VirtQueue_callback callback;
183 /* Shared state */
184 struct vring vring;
186 /* Number of free buffers */
187 UInt16 num_free;
189 /* Last available index; updated by VirtQueue_getAvailBuf */
190 UInt16 last_avail_idx;
192 /* Will eventually be used to kick remote processor */
193 UInt16 procId;
195 /* Gate to protect from multiple threads */
196 GateHwi_Handle gateH;
197 } VirtQueue_Object;
199 /* module diags mask */
200 Registry_Desc Registry_CURDESC;
202 static struct VirtQueue_Object *queueRegistry[NUM_QUEUES] = {NULL};
204 static UInt16 hostProcId;
206 /*!
207 * ======== _VirtQueue_init ========
208 *
209 * This function adds the VirtQueue "module" to the Registry so that
210 * DIAGS will work with this non-XDC module.
211 * Since VirtQueue_init is not called by XDC-VirtQueue module clients, this
212 * function is called in the first VirtQueue fxn called: VirtQueue_create.
213 */
214 static Void _VirtQueue_init()
215 {
216 static int initialized = 0;
218 if (!initialized) {
219 Registry_Result result;
221 /* register with xdc.runtime to get a diags mask */
222 result = Registry_addModule(&Registry_CURDESC, MODULE_NAME);
223 Assert_isTrue(result == Registry_SUCCESS, (Assert_Id)NULL);
225 initialized = 1;
226 }
227 }
229 static inline Void * mapPAtoVA(UInt pa)
230 {
231 return (Void *)((pa & 0x000fffffU) | IPC_MEM_VRING0);
232 }
234 /*!
235 * ======== VirtQueue_kick ========
236 */
237 Void VirtQueue_kick(VirtQueue_Handle vq)
238 {
239 /* For now, simply interrupt remote processor */
240 if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) {
241 Log_print0(Diags_USER1,
242 "VirtQueue_kick: no kick because of VRING_AVAIL_F_NO_INTERRUPT\n");
243 return;
244 }
246 Log_print2(Diags_USER1,
247 "VirtQueue_kick: Sending interrupt to proc %d with payload 0x%x\n",
248 (IArg)vq->procId, (IArg)vq->id);
249 InterruptProxy_intSend(vq->procId, vq->id);
250 }
252 /*!
253 * ======== VirtQueue_addUsedBuf ========
254 */
255 Int VirtQueue_addUsedBuf(VirtQueue_Handle vq, Int16 head, Int len)
256 {
257 struct vring_used_elem *used;
258 IArg key;
260 key = GateHwi_enter(vq->gateH);
261 if ((head > vq->vring.num) || (head < 0)) {
262 GateHwi_leave(vq->gateH, key);
263 Error_raise(NULL, Error_E_generic, 0, 0);
264 }
266 /*
267 * The virtqueue contains a ring of used buffers. Get a pointer to the
268 * next entry in that used ring.
269 */
270 used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num];
271 used->id = head;
272 used->len = len;
274 vq->vring.used->idx++;
275 GateHwi_leave(vq->gateH, key);
277 return (0);
278 }
280 /*!
281 * ======== VirtQueue_getAvailBuf ========
282 */
283 Int16 VirtQueue_getAvailBuf(VirtQueue_Handle vq, Void **buf, Int *len)
284 {
285 Int16 head;
286 IArg key;
288 key = GateHwi_enter(vq->gateH);
289 Log_print6(Diags_USER1, "getAvailBuf vq: 0x%x %d %d %d 0x%x 0x%x\n",
290 (IArg)vq, vq->last_avail_idx, vq->vring.avail->idx, vq->vring.num,
291 (IArg)&vq->vring.avail, (IArg)vq->vring.avail);
293 /* Clear flag here to avoid race condition with remote processor.
294 * This is a negative flag, clearing it means that we want to
295 * receive an interrupt when a buffer has been added to the pool.
296 */
297 vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY;
299 /* There's nothing available? */
300 if (vq->last_avail_idx == vq->vring.avail->idx) {
301 head = (-1);
302 }
303 else {
304 /* No need to be kicked about added buffers anymore */
305 vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY;
307 /*
308 * Grab the next descriptor number they're advertising, and increment
309 * the index we've seen.
310 */
311 head = vq->vring.avail->ring[vq->last_avail_idx++ % vq->vring.num];
313 *buf = mapPAtoVA(vq->vring.desc[head].addr);
314 *len = vq->vring.desc[head].len;
315 }
316 GateHwi_leave(vq->gateH, key);
318 return (head);
319 }
321 /*!
322 * ======== VirtQueue_disableCallback ========
323 */
324 Void VirtQueue_disableCallback(VirtQueue_Object *vq)
325 {
326 /* TODO */
327 Log_print0(Diags_USER1, "VirtQueue_disableCallback called.");
328 }
330 /*!
331 * ======== VirtQueue_enableCallback ========
332 */
333 Bool VirtQueue_enableCallback(VirtQueue_Object *vq)
334 {
335 Log_print0(Diags_USER1, "VirtQueue_enableCallback called.");
337 /* TODO */
338 return (FALSE);
339 }
341 /*!
342 * ======== VirtQueue_isr ========
343 * Note 'arg' is ignored: it is the Hwi argument, not the mailbox argument.
344 */
345 Void VirtQueue_isr(UArg msg)
346 {
347 VirtQueue_Object *vq;
349 Log_print1(Diags_USER1, "VirtQueue_isr received msg = 0x%x\n", msg);
351 switch(msg) {
352 case (UInt)RP_MSG_MBOX_READY:
353 return;
355 case (UInt)RP_MBOX_ECHO_REQUEST:
356 InterruptProxy_intSend(hostProcId, (UInt)(RP_MBOX_ECHO_REPLY));
357 return;
359 case (UInt)RP_MBOX_ABORT_REQUEST:
360 {
361 /* Suppress Coverity Error: FORWARD_NULL: */
362 /* coverity[assign_zero] */
363 Fxn f = (Fxn)0x0;
364 Log_print0(Diags_USER1, "Crash on demand ...\n");
365 /* coverity[var_deref_op] */
366 f();
367 }
368 return;
370 case (UInt)RP_MSG_FLUSH_CACHE:
371 Cache_wbAll();
372 return;
374 case (UInt)RP_MSG_HIBERNATION:
375 if (IpcPower_canHibernate() == FALSE) {
376 InterruptProxy_intSend(hostProcId,
377 (UInt)RP_MSG_HIBERNATION_CANCEL);
378 return;
379 }
381 /* Fall through */
382 case (UInt)RP_MSG_HIBERNATION_FORCE:
383 /* Ack request */
384 InterruptProxy_intSend(hostProcId,
385 (UInt)RP_MSG_HIBERNATION_ACK);
386 IpcPower_suspend();
387 return;
389 default:
390 /*
391 * If the message isn't one of the above, it's either part of the
392 * 2-message synchronization sequence or it a virtqueue message
393 */
394 break;
395 }
397 /* Don't let unknown messages to pass as a virtqueue index */
398 if (msg >= NUM_QUEUES) {
399 /* Adding print here deliberately, we should never see this */
400 System_printf("VirtQueue_isr: Invalid mailbox message 0x%x "
401 "received\n", msg);
402 return;
403 }
405 vq = queueRegistry[msg];
406 if (vq) {
407 vq->callback(vq);
408 }
409 }
412 /*!
413 * ======== VirtQueue_create ========
414 */
415 VirtQueue_Handle VirtQueue_create(UInt16 remoteProcId, VirtQueue_Params *params,
416 Error_Block *eb)
417 {
418 VirtQueue_Object *vq;
419 Void *vringAddr;
421 /* Perform initialization we can't do in Instance_init (being non-XDC): */
422 _VirtQueue_init();
424 vq = Memory_alloc(NULL, sizeof(VirtQueue_Object), 0, eb);
425 if (NULL == vq) {
426 return (NULL);
427 }
429 /* Create the thread protection gate */
430 vq->gateH = GateHwi_create(NULL, eb);
431 if (Error_check(eb)) {
432 Log_error0("VirtQueue_create: could not create gate object");
433 Memory_free(NULL, vq, sizeof(VirtQueue_Object));
434 return (NULL);
435 }
437 vq->callback = params->callback;
438 vq->id = params->vqId;
439 vq->procId = remoteProcId;
440 vq->last_avail_idx = 0;
442 switch (vq->id) {
443 /* IPC transport vrings */
444 case ID_SELF_TO_HOST:
445 /* slave -> HOST */
446 vringAddr = (struct vring *) IPC_MEM_VRING0;
447 break;
448 case ID_HOST_TO_SELF:
449 /* HOST -> slave */
450 vringAddr = (struct vring *) IPC_MEM_VRING1;
451 break;
452 default:
453 GateHwi_delete(&vq->gateH);
454 Memory_free(NULL, vq, sizeof(VirtQueue_Object));
455 return (NULL);
456 }
458 Log_print3(Diags_USER1,
459 "vring: %d 0x%x (0x%x)\n", vq->id, (IArg)vringAddr,
460 RP_MSG_RING_SIZE);
462 /* See coverity related comment in vring_init() */
463 /* coverity[overrun-call] */
464 vring_init(&(vq->vring), RP_MSG_NUM_BUFS, vringAddr, RP_MSG_VRING_ALIGN);
466 /*
467 * Don't trigger a mailbox message every time MPU makes another buffer
468 * available
469 */
470 if (vq->procId == hostProcId) {
471 vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY;
472 }
474 queueRegistry[vq->id] = vq;
476 return (vq);
477 }
479 /*!
480 * ======== VirtQueue_startup ========
481 */
482 Void VirtQueue_startup()
483 {
484 hostProcId = MultiProc_getId("HOST");
486 /* Initilize the IpcPower module */
487 IpcPower_init();
489 /*
490 * Wait for HLOS (Virtio device) to indicate that priming of host's receive
491 * buffers is complete, indicating that host is ready to send.
492 *
493 * Though this is a Linux Virtio configuration status, it must be
494 * implemented by each non-Linux HLOS as well.
495 */
496 Log_print1(Diags_USER1, "VirtQueue_startup: VDEV status: 0x%x\n",
497 Resource_getVdevStatus(VIRTIO_ID_RPMSG));
498 Log_print0(Diags_USER1, "VirtQueue_startup: Polling VDEV status...\n");
499 while (Resource_getVdevStatus(VIRTIO_ID_RPMSG) != VRING_BUFS_PRIMED);
500 Log_print1(Diags_USER1, "VirtQueue_startup: VDEV status: 0x%x\n",
501 Resource_getVdevStatus(VIRTIO_ID_RPMSG));
503 InterruptProxy_intRegister(VirtQueue_isr);
504 Log_print0(Diags_USER1, "Passed VirtQueue_startup\n");
505 }
507 /*!
508 * ======== VirtQueue_postCrashToMailbox ========
509 */
510 Void VirtQueue_postCrashToMailbox(Void)
511 {
512 InterruptProxy_intSend(0, (UInt)RP_MSG_MBOX_CRASH);
513 }
515 #define CACHE_WB_TICK_PERIOD 5
517 /*!
518 * ======== VirtQueue_cacheWb ========
519 *
520 * Used for flushing SysMin trace buffer.
521 */
522 Void VirtQueue_cacheWb()
523 {
524 static UInt32 oldticks = 0;
525 UInt32 newticks;
527 newticks = Clock_getTicks();
528 if (newticks - oldticks < (UInt32)CACHE_WB_TICK_PERIOD) {
529 /* Don't keep flushing cache */
530 return;
531 }
533 oldticks = newticks;
535 /* Flush the cache */
536 Cache_wbAll();
537 }