1 /*
2  * Remoteproc Virtio Framework
3  *
4  * Copyright(c) 2018 Xilinx Ltd.
5  * Copyright(c) 2011 Texas Instruments, Inc.
6  * Copyright(c) 2011 Google, Inc.
7  * All rights reserved.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  */
11 
12 #ifndef REMOTEPROC_VIRTIO_H
13 #define REMOTEPROC_VIRTIO_H
14 
15 #include <metal/io.h>
16 #include <metal/list.h>
17 #include <openamp/virtio.h>
18 #include <metal/cache.h>
19 
20 #if defined __cplusplus
21 extern "C" {
22 #endif
23 
24 /* maximum number of vring descriptors for a vdev limited by 16-bit data type */
25 #define	RPROC_MAX_VRING_DESC	USHRT_MAX
26 
27 /* cache invalidation helpers for resource table */
28 #ifdef VIRTIO_CACHED_RSC_TABLE
29 #define RSC_TABLE_FLUSH(x, s)		metal_cache_flush(x, s)
30 #define RSC_TABLE_INVALIDATE(x, s)	metal_cache_invalidate(x, s)
31 #else
32 #define RSC_TABLE_FLUSH(x, s)		do { } while (0)
33 #define RSC_TABLE_INVALIDATE(x, s)	do { } while (0)
34 #endif /* VIRTIO_CACHED_RSC_TABLE */
35 
36 /* define vdev notification function user should implement */
37 typedef int (*rpvdev_notify_func)(void *priv, uint32_t id);
38 
39 /**
40  * struct remoteproc_virtio
41  * @priv pointer to private data
42  * @vdev_rsc address of vdev resource
43  * @vdev_rsc_io metal I/O region of vdev_info, can be NULL
44  * @notify notification function
45  * @vdev virtio device
46  * @node list node
47  */
48 struct remoteproc_virtio {
49 	void *priv;
50 	void *vdev_rsc;
51 	struct metal_io_region *vdev_rsc_io;
52 	rpvdev_notify_func notify;
53 	struct virtio_device vdev;
54 	struct metal_list node;
55 };
56 
57 /**
58  * @brief Create rproc virtio vdev
59  *
60  * @param role		VIRTIO_DEV_DRIVER or VIRTIO_DEV_DEVICE
61  * @param notifyid	Virtio device notification id
62  * @param rsc		Pointer to the virtio device resource
63  * @param rsc_io	Pointer to the virtio device resource I/O region
64  * @param priv		Pointer to the private data
65  * @param notify	vdev and virtqueue notification function
66  * @param rst_cb	Reset virtio device callback
67  *
68  * @return pointer to the created virtio device for success,
69  * NULL for failure.
70  */
71 struct virtio_device *
72 rproc_virtio_create_vdev(unsigned int role, unsigned int notifyid,
73 			 void *rsc, struct metal_io_region *rsc_io,
74 			 void *priv,
75 			 rpvdev_notify_func notify,
76 			 virtio_dev_reset_cb rst_cb);
77 
78 /**
79  * @brief Remove rproc virtio vdev
80  *
81  * @param vdev	Pointer to the virtio device
82  */
83 void rproc_virtio_remove_vdev(struct virtio_device *vdev);
84 
85 /**
86  * @brief Initialize rproc virtio vring
87  *
88  * @param vdev		Pointer to the virtio device
89  * @param index		vring index in the virtio device
90  * @param notifyid	remoteproc vring notification id
91  * @param va		vring virtual address
92  * @param io		Pointer to vring I/O region
93  * @param num_descs	Number of descriptors
94  * @param align		vring alignment
95  *
96  * @return 0 for success, negative value for failure.
97  */
98 int rproc_virtio_init_vring(struct virtio_device *vdev, unsigned int index,
99 			    unsigned int notifyid, void *va,
100 			    struct metal_io_region *io,
101 			    unsigned int num_descs, unsigned int align);
102 
103 /**
104  * @brief remoteproc virtio is got notified
105  *
106  * @param vdev		Pointer to the virtio device
107  * @param notifyid	Notify id
108  *
109  * @return 0 for successful, negative value for failure
110  */
111 int rproc_virtio_notified(struct virtio_device *vdev, uint32_t notifyid);
112 
113 /**
114  * @brief Blocking function, waiting for the remote core is ready to start
115  * communications.
116  *
117  * @param vdev	Pointer to the virtio device
118  *
119  * @return true when remote processor is ready.
120  */
121 void rproc_virtio_wait_remote_ready(struct virtio_device *vdev);
122 
123 #if defined __cplusplus
124 }
125 #endif
126 
127 #endif /* REMOTEPROC_VIRTIO_H */
128