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 #warning "VIRTIO_CACHED_RSC_TABLE is deprecated, please use VIRTIO_USE_DCACHE"
30 #endif
31 #if defined(VIRTIO_CACHED_RSC_TABLE) || defined(VIRTIO_USE_DCACHE)
32 #define RSC_TABLE_FLUSH(x, s)		CACHE_FLUSH(x, s)
33 #define RSC_TABLE_INVALIDATE(x, s)	CACHE_INVALIDATE(x, s)
34 #else
35 #define RSC_TABLE_FLUSH(x, s)		do { } while (0)
36 #define RSC_TABLE_INVALIDATE(x, s)	do { } while (0)
37 #endif /* VIRTIO_CACHED_RSC_TABLE || VIRTIO_USE_DCACHE */
38 
39 /* define vdev notification function user should implement */
40 typedef int (*rpvdev_notify_func)(void *priv, uint32_t id);
41 
42 /** @brief Virtio structure for remoteproc instance */
43 struct remoteproc_virtio {
44 	/** Pointer to private data */
45 	void *priv;
46 
47 	/** Address of vdev resource */
48 	void *vdev_rsc;
49 
50 	/** Metal I/O region of vdev_info, can be NULL */
51 	struct metal_io_region *vdev_rsc_io;
52 
53 	/** Notification function */
54 	rpvdev_notify_func notify;
55 
56 	/** Virtio device */
57 	struct virtio_device vdev;
58 
59 	/** List node */
60 	struct metal_list node;
61 };
62 
63 /**
64  * @brief Create rproc virtio vdev
65  *
66  * @param role		VIRTIO_DEV_DRIVER or VIRTIO_DEV_DEVICE
67  * @param notifyid	Virtio device notification id
68  * @param rsc		Pointer to the virtio device resource
69  * @param rsc_io	Pointer to the virtio device resource I/O region
70  * @param priv		Pointer to the private data
71  * @param notify	vdev and virtqueue notification function
72  * @param rst_cb	Reset virtio device callback
73  *
74  * @return pointer to the created virtio device for success,
75  * NULL for failure.
76  */
77 struct virtio_device *
78 rproc_virtio_create_vdev(unsigned int role, unsigned int notifyid,
79 			 void *rsc, struct metal_io_region *rsc_io,
80 			 void *priv,
81 			 rpvdev_notify_func notify,
82 			 virtio_dev_reset_cb rst_cb);
83 
84 /**
85  * @brief Remove rproc virtio vdev
86  *
87  * @param vdev	Pointer to the virtio device
88  */
89 void rproc_virtio_remove_vdev(struct virtio_device *vdev);
90 
91 /**
92  * @brief Initialize rproc virtio vring
93  *
94  * @param vdev		Pointer to the virtio device
95  * @param index		vring index in the virtio device
96  * @param notifyid	remoteproc vring notification id
97  * @param va		vring virtual address
98  * @param io		Pointer to vring I/O region
99  * @param num_descs	Number of descriptors
100  * @param align		vring alignment
101  *
102  * @return 0 for success, negative value for failure.
103  */
104 int rproc_virtio_init_vring(struct virtio_device *vdev, unsigned int index,
105 			    unsigned int notifyid, void *va,
106 			    struct metal_io_region *io,
107 			    unsigned int num_descs, unsigned int align);
108 
109 /**
110  * @brief remoteproc virtio is got notified
111  *
112  * @param vdev		Pointer to the virtio device
113  * @param notifyid	Notify id
114  *
115  * @return 0 for successful, negative value for failure
116  */
117 int rproc_virtio_notified(struct virtio_device *vdev, uint32_t notifyid);
118 
119 /**
120  * @brief Blocking function, waiting for the remote core is ready to start
121  * communications.
122  *
123  * @param vdev	Pointer to the virtio device
124  *
125  * @return true when remote processor is ready.
126  */
127 void rproc_virtio_wait_remote_ready(struct virtio_device *vdev);
128 
129 #if defined __cplusplus
130 }
131 #endif
132 
133 #endif /* REMOTEPROC_VIRTIO_H */
134