xref: /freebsd-14-stable/sys/dev/beri/virtio/virtio.c (revision ed03c309908687bdb9f71dc6d9c9c8a92c54fc20)
1 /*-
2  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * BERI virtio mmio backend common methods
33  */
34 
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/cdefs.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
43 #include <sys/rman.h>
44 #include <sys/timeet.h>
45 #include <sys/timetc.h>
46 #include <sys/conf.h>
47 #include <sys/uio.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <sys/event.h>
51 #include <sys/selinfo.h>
52 #include <sys/endian.h>
53 #include <sys/rwlock.h>
54 
55 #include <machine/bus.h>
56 #include <machine/fdt.h>
57 #include <machine/cpu.h>
58 #include <machine/intr.h>
59 
60 #include <dev/fdt/fdt_common.h>
61 #include <dev/ofw/openfirm.h>
62 #include <dev/ofw/ofw_bus.h>
63 #include <dev/ofw/ofw_bus_subr.h>
64 
65 #include <dev/beri/virtio/virtio.h>
66 #include <dev/virtio/virtqueue.h>
67 #include <dev/virtio/virtio_ring.h>
68 #include <dev/altera/pio/pio.h>
69 
70 #include "pio_if.h"
71 
72 int
vq_ring_ready(struct vqueue_info * vq)73 vq_ring_ready(struct vqueue_info *vq)
74 {
75 
76 	return (vq->vq_flags & VQ_ALLOC);
77 }
78 
79 int
vq_has_descs(struct vqueue_info * vq)80 vq_has_descs(struct vqueue_info *vq)
81 {
82 
83 	return (vq_ring_ready(vq) && vq->vq_last_avail !=
84 		be16toh(vq->vq_avail->idx));
85 }
86 
87 void *
paddr_map(uint32_t offset,uint32_t phys,uint32_t size)88 paddr_map(uint32_t offset, uint32_t phys, uint32_t size)
89 {
90 	bus_space_handle_t bsh;
91 
92 	if (bus_space_map(fdtbus_bs_tag, (phys + offset),
93 			size, 0, &bsh) != 0) {
94 		panic("Couldn't map 0x%08x\n", (phys + offset));
95 	}
96 
97 	return (void *)(bsh);
98 }
99 
100 void
paddr_unmap(void * phys,uint32_t size)101 paddr_unmap(void *phys, uint32_t size)
102 {
103 
104 	bus_space_unmap(fdtbus_bs_tag, (bus_space_handle_t)phys, size);
105 }
106 
107 static inline void
_vq_record(uint32_t offs,int i,volatile struct vring_desc * vd,struct iovec * iov,int n_iov,uint16_t * flags)108 _vq_record(uint32_t offs, int i, volatile struct vring_desc *vd,
109 	struct iovec *iov, int n_iov, uint16_t *flags) {
110 	uint32_t len;
111 	uint64_t addr;
112 
113 	if (i >= n_iov)
114 		return;
115 
116 	len = atomic_load_32(&vd->len);
117 	addr = atomic_load_64(&vd->addr);
118 	iov[i].iov_base = paddr_map(offs, be64toh(addr),
119 				be32toh(len));
120 	iov[i].iov_len = be32toh(len);
121 	if (flags != NULL)
122 		flags[i] = be16toh(vd->flags);
123 }
124 
125 int
vq_getchain(uint32_t offs,struct vqueue_info * vq,struct iovec * iov,int n_iov,uint16_t * flags)126 vq_getchain(uint32_t offs, struct vqueue_info *vq,
127 	struct iovec *iov, int n_iov, uint16_t *flags)
128 {
129 	volatile struct vring_desc *vdir, *vindir, *vp;
130 	int idx, ndesc, n_indir;
131 	int head, next;
132 	int i;
133 
134 	idx = vq->vq_last_avail;
135 	ndesc = (be16toh(vq->vq_avail->idx) - idx);
136 	if (ndesc == 0)
137 		return (0);
138 
139 	head = be16toh(vq->vq_avail->ring[idx & (vq->vq_qsize - 1)]);
140 	next = head;
141 
142 	for (i = 0; i < VQ_MAX_DESCRIPTORS; next = be16toh(vdir->next)) {
143 		vdir = &vq->vq_desc[next];
144 		if ((be16toh(vdir->flags) & VRING_DESC_F_INDIRECT) == 0) {
145 			_vq_record(offs, i, vdir, iov, n_iov, flags);
146 			i++;
147 		} else {
148 			n_indir = be32toh(vdir->len) / 16;
149 			vindir = paddr_map(offs, be64toh(vdir->addr),
150 					be32toh(vdir->len));
151 			next = 0;
152 			for (;;) {
153 				vp = &vindir[next];
154 				_vq_record(offs, i, vp, iov, n_iov, flags);
155 				i+=1;
156 				if ((be16toh(vp->flags) & \
157 					VRING_DESC_F_NEXT) == 0)
158 					break;
159 				next = be16toh(vp->next);
160 			}
161 			paddr_unmap(__DEVOLATILE(void *, vindir), be32toh(vdir->len));
162 		}
163 
164 		if ((be16toh(vdir->flags) & VRING_DESC_F_NEXT) == 0)
165 			return (i);
166 	}
167 
168 	return (i);
169 }
170 
171 void
vq_relchain(struct vqueue_info * vq,struct iovec * iov,int n,uint32_t iolen)172 vq_relchain(struct vqueue_info *vq, struct iovec *iov, int n, uint32_t iolen)
173 {
174 	volatile struct vring_used_elem *vue;
175 	volatile struct vring_used *vu;
176 	uint16_t head, uidx, mask;
177 	int i;
178 
179 	mask = vq->vq_qsize - 1;
180 	vu = vq->vq_used;
181 	head = be16toh(vq->vq_avail->ring[vq->vq_last_avail++ & mask]);
182 
183 	uidx = be16toh(vu->idx);
184 	vue = &vu->ring[uidx++ & mask];
185 	vue->id = htobe32(head);
186 
187 	vue->len = htobe32(iolen);
188 	vu->idx = htobe16(uidx);
189 
190 	/* Clean up */
191 	for (i = 0; i < n; i++) {
192 		paddr_unmap((void *)iov[i].iov_base, iov[i].iov_len);
193 	}
194 }
195 
196 int
setup_pio(device_t dev,char * name,device_t * pio_dev)197 setup_pio(device_t dev, char *name, device_t *pio_dev)
198 {
199 	phandle_t pio_node;
200 	struct fdt_ic *ic;
201 	phandle_t xref;
202 	phandle_t node;
203 
204 	if ((node = ofw_bus_get_node(dev)) == -1)
205 		return (ENXIO);
206 
207 	if (OF_searchencprop(node, name, &xref,
208 		sizeof(xref)) == -1) {
209 		return (ENXIO);
210 	}
211 
212 	pio_node = OF_node_from_xref(xref);
213 	SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) {
214 		if (ic->iph == pio_node) {
215 			*pio_dev = ic->dev;
216 			return (0);
217 		}
218 	}
219 
220 	return (ENXIO);
221 }
222 
223 int
setup_offset(device_t dev,uint32_t * offset)224 setup_offset(device_t dev, uint32_t *offset)
225 {
226 	pcell_t dts_value[2];
227 	phandle_t mem_node;
228 	phandle_t xref;
229 	phandle_t node;
230 	int len;
231 
232 	if ((node = ofw_bus_get_node(dev)) == -1)
233 		return (ENXIO);
234 
235 	if (OF_searchencprop(node, "beri-mem", &xref,
236 		sizeof(xref)) == -1) {
237 		return (ENXIO);
238 	}
239 
240 	mem_node = OF_node_from_xref(xref);
241 	if ((len = OF_getproplen(mem_node, "reg")) <= 0)
242 		return (ENXIO);
243 	OF_getencprop(mem_node, "reg", dts_value, len);
244 	*offset = dts_value[0];
245 
246 	return (0);
247 }
248 
249 struct iovec *
getcopy(struct iovec * iov,int n)250 getcopy(struct iovec *iov, int n)
251 {
252 	struct iovec *tiov;
253 	int i;
254 
255 	tiov = malloc(n * sizeof(struct iovec), M_DEVBUF, M_NOWAIT);
256 	for (i = 0; i < n; i++) {
257 		tiov[i].iov_base = iov[i].iov_base;
258 		tiov[i].iov_len = iov[i].iov_len;
259 	}
260 
261 	return (tiov);
262 }
263