virtio_pci.c revision 1.5 1 1.5 jakllsch /* $NetBSD: virtio_pci.c,v 1.5 2018/06/06 16:11:36 jakllsch Exp $ */
2 1.1 cherry
3 1.1 cherry /*
4 1.1 cherry * Copyright (c) 2010 Minoura Makoto.
5 1.1 cherry * All rights reserved.
6 1.1 cherry *
7 1.1 cherry * Redistribution and use in source and binary forms, with or without
8 1.1 cherry * modification, are permitted provided that the following conditions
9 1.1 cherry * are met:
10 1.1 cherry * 1. Redistributions of source code must retain the above copyright
11 1.1 cherry * notice, this list of conditions and the following disclaimer.
12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer in the
14 1.1 cherry * documentation and/or other materials provided with the distribution.
15 1.1 cherry *
16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 cherry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 cherry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 cherry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 cherry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 cherry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 cherry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 cherry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 cherry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 cherry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 cherry */
27 1.1 cherry
28 1.1 cherry #include <sys/cdefs.h>
29 1.5 jakllsch __KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.5 2018/06/06 16:11:36 jakllsch Exp $");
30 1.1 cherry
31 1.1 cherry #include <sys/param.h>
32 1.1 cherry #include <sys/systm.h>
33 1.4 jakllsch #include <sys/kmem.h>
34 1.5 jakllsch #include <sys/module.h>
35 1.1 cherry
36 1.1 cherry #include <sys/device.h>
37 1.1 cherry
38 1.1 cherry #include <dev/pci/pcidevs.h>
39 1.1 cherry #include <dev/pci/pcireg.h>
40 1.1 cherry #include <dev/pci/pcivar.h>
41 1.1 cherry
42 1.1 cherry #define VIRTIO_PRIVATE
43 1.1 cherry
44 1.1 cherry #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
45 1.1 cherry #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
46 1.1 cherry
47 1.4 jakllsch static int virtio_pci_match(device_t, cfdata_t, void *);
48 1.4 jakllsch static void virtio_pci_attach(device_t, device_t, void *);
49 1.4 jakllsch static int virtio_pci_rescan(device_t, const char *, const int *);
50 1.4 jakllsch static int virtio_pci_detach(device_t, int);
51 1.4 jakllsch
52 1.4 jakllsch struct virtio_pci_softc {
53 1.4 jakllsch struct virtio_softc sc_sc;
54 1.4 jakllsch bus_space_tag_t sc_iot;
55 1.4 jakllsch bus_space_handle_t sc_ioh;
56 1.4 jakllsch bus_size_t sc_iosize;
57 1.4 jakllsch struct pci_attach_args sc_pa;
58 1.4 jakllsch pci_intr_handle_t *sc_ihp;
59 1.4 jakllsch void **sc_ihs;
60 1.4 jakllsch int sc_ihs_num;
61 1.4 jakllsch int sc_config_offset;
62 1.4 jakllsch };
63 1.4 jakllsch
64 1.4 jakllsch static void virtio_pci_kick(struct virtio_softc *, uint16_t);
65 1.4 jakllsch static uint8_t virtio_pci_read_device_config_1(struct virtio_softc *, int);
66 1.4 jakllsch static uint16_t virtio_pci_read_device_config_2(struct virtio_softc *, int);
67 1.4 jakllsch static uint32_t virtio_pci_read_device_config_4(struct virtio_softc *, int);
68 1.4 jakllsch static uint64_t virtio_pci_read_device_config_8(struct virtio_softc *, int);
69 1.4 jakllsch static void virtio_pci_write_device_config_1(struct virtio_softc *, int, uint8_t);
70 1.4 jakllsch static void virtio_pci_write_device_config_2(struct virtio_softc *, int, uint16_t);
71 1.4 jakllsch static void virtio_pci_write_device_config_4(struct virtio_softc *, int, uint32_t);
72 1.4 jakllsch static void virtio_pci_write_device_config_8(struct virtio_softc *, int, uint64_t);
73 1.4 jakllsch static uint16_t virtio_pci_read_queue_size(struct virtio_softc *, uint16_t);
74 1.4 jakllsch static void virtio_pci_setup_queue(struct virtio_softc *, uint16_t, uint32_t);
75 1.4 jakllsch static void virtio_pci_set_status(struct virtio_softc *, int);
76 1.4 jakllsch static uint32_t virtio_pci_negotiate_features(struct virtio_softc *, uint32_t);
77 1.4 jakllsch static int virtio_pci_setup_interrupts(struct virtio_softc *);
78 1.4 jakllsch static void virtio_pci_free_interrupts(struct virtio_softc *);
79 1.4 jakllsch
80 1.4 jakllsch static int virtio_pci_intr(void *arg);
81 1.4 jakllsch static int virtio_pci_msix_queue_intr(void *);
82 1.4 jakllsch static int virtio_pci_msix_config_intr(void *);
83 1.4 jakllsch static int virtio_pci_setup_msix_vectors(struct virtio_softc *);
84 1.4 jakllsch static int virtio_pci_setup_msix_interrupts(struct virtio_softc *,
85 1.4 jakllsch struct pci_attach_args *);
86 1.4 jakllsch static int virtio_pci_setup_intx_interrupt(struct virtio_softc *,
87 1.4 jakllsch struct pci_attach_args *);
88 1.4 jakllsch
89 1.4 jakllsch #define VIRTIO_MSIX_CONFIG_VECTOR_INDEX 0
90 1.4 jakllsch #define VIRTIO_MSIX_QUEUE_VECTOR_INDEX 1
91 1.4 jakllsch
92 1.4 jakllsch /* we use the legacy virtio spec, so the PCI registers are host native
93 1.4 jakllsch * byte order, not PCI (i.e. LE) byte order */
94 1.4 jakllsch #if BYTE_ORDER == BIG_ENDIAN
95 1.4 jakllsch #define REG_HI_OFF 0
96 1.4 jakllsch #define REG_LO_OFF 4
97 1.4 jakllsch #ifndef __BUS_SPACE_HAS_STREAM_METHODS
98 1.4 jakllsch #define bus_space_read_stream_1 bus_space_read_1
99 1.4 jakllsch #define bus_space_write_stream_1 bus_space_write_1
100 1.4 jakllsch static inline uint16_t
101 1.4 jakllsch bus_space_read_stream_2(bus_space_tag_t t, bus_space_handle_t h,
102 1.4 jakllsch bus_size_t o)
103 1.4 jakllsch {
104 1.4 jakllsch return le16toh(bus_space_read_2(t, h, o));
105 1.4 jakllsch }
106 1.4 jakllsch static inline void
107 1.4 jakllsch bus_space_write_stream_2(bus_space_tag_t t, bus_space_handle_t h,
108 1.4 jakllsch bus_size_t o, uint16_t v)
109 1.4 jakllsch {
110 1.4 jakllsch bus_space_write_2(t, h, o, htole16(v));
111 1.4 jakllsch }
112 1.4 jakllsch static inline uint32_t
113 1.4 jakllsch bus_space_read_stream_4(bus_space_tag_t t, bus_space_handle_t h,
114 1.4 jakllsch bus_size_t o)
115 1.4 jakllsch {
116 1.4 jakllsch return le32toh(bus_space_read_4(t, h, o));
117 1.4 jakllsch }
118 1.4 jakllsch static inline void
119 1.4 jakllsch bus_space_write_stream_4(bus_space_tag_t t, bus_space_handle_t h,
120 1.4 jakllsch bus_size_t o, uint32_t v)
121 1.4 jakllsch {
122 1.4 jakllsch bus_space_write_4(t, h, o, htole32(v));
123 1.4 jakllsch }
124 1.4 jakllsch #endif
125 1.4 jakllsch #else
126 1.4 jakllsch #define REG_HI_OFF 4
127 1.4 jakllsch #define REG_LO_OFF 0
128 1.4 jakllsch #ifndef __BUS_SPACE_HAS_STREAM_METHODS
129 1.4 jakllsch #define bus_space_read_stream_1 bus_space_read_1
130 1.4 jakllsch #define bus_space_read_stream_2 bus_space_read_2
131 1.4 jakllsch #define bus_space_read_stream_4 bus_space_read_4
132 1.4 jakllsch #define bus_space_write_stream_1 bus_space_write_1
133 1.4 jakllsch #define bus_space_write_stream_2 bus_space_write_2
134 1.4 jakllsch #define bus_space_write_stream_4 bus_space_write_4
135 1.4 jakllsch #endif
136 1.4 jakllsch #endif
137 1.4 jakllsch
138 1.1 cherry
139 1.1 cherry static const char *virtio_device_name[] = {
140 1.1 cherry "Unknown (0)", /* 0 */
141 1.1 cherry "Network", /* 1 */
142 1.1 cherry "Block", /* 2 */
143 1.1 cherry "Console", /* 3 */
144 1.1 cherry "Entropy", /* 4 */
145 1.1 cherry "Memory Balloon", /* 5 */
146 1.1 cherry "I/O Memory", /* 6 */
147 1.1 cherry "Remote Processor Messaging", /* 7 */
148 1.1 cherry "SCSI", /* 8 */
149 1.1 cherry "9P Transport", /* 9 */
150 1.1 cherry "mac80211 wlan", /* 10 */
151 1.1 cherry };
152 1.1 cherry #define NDEVNAMES __arraycount(virtio_device_name)
153 1.1 cherry
154 1.4 jakllsch CFATTACH_DECL3_NEW(virtio_pci, sizeof(struct virtio_pci_softc),
155 1.4 jakllsch virtio_pci_match, virtio_pci_attach, virtio_pci_detach, NULL,
156 1.4 jakllsch virtio_pci_rescan, NULL, DVF_DETACH_SHUTDOWN);
157 1.4 jakllsch
158 1.4 jakllsch static const struct virtio_ops virtio_pci_ops = {
159 1.4 jakllsch .kick = virtio_pci_kick,
160 1.4 jakllsch .read_dev_cfg_1 = virtio_pci_read_device_config_1,
161 1.4 jakllsch .read_dev_cfg_2 = virtio_pci_read_device_config_2,
162 1.4 jakllsch .read_dev_cfg_4 = virtio_pci_read_device_config_4,
163 1.4 jakllsch .read_dev_cfg_8 = virtio_pci_read_device_config_8,
164 1.4 jakllsch .write_dev_cfg_1 = virtio_pci_write_device_config_1,
165 1.4 jakllsch .write_dev_cfg_2 = virtio_pci_write_device_config_2,
166 1.4 jakllsch .write_dev_cfg_4 = virtio_pci_write_device_config_4,
167 1.4 jakllsch .write_dev_cfg_8 = virtio_pci_write_device_config_8,
168 1.4 jakllsch .read_queue_size = virtio_pci_read_queue_size,
169 1.4 jakllsch .setup_queue = virtio_pci_setup_queue,
170 1.4 jakllsch .set_status = virtio_pci_set_status,
171 1.4 jakllsch .neg_features = virtio_pci_negotiate_features,
172 1.4 jakllsch .setup_interrupts = virtio_pci_setup_interrupts,
173 1.4 jakllsch .free_interrupts = virtio_pci_free_interrupts,
174 1.4 jakllsch };
175 1.1 cherry
176 1.1 cherry static int
177 1.4 jakllsch virtio_pci_match(device_t parent, cfdata_t match, void *aux)
178 1.1 cherry {
179 1.1 cherry struct pci_attach_args *pa;
180 1.1 cherry
181 1.1 cherry pa = (struct pci_attach_args *)aux;
182 1.1 cherry switch (PCI_VENDOR(pa->pa_id)) {
183 1.1 cherry case PCI_VENDOR_QUMRANET:
184 1.1 cherry if ((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <=
185 1.1 cherry PCI_PRODUCT(pa->pa_id)) &&
186 1.1 cherry (PCI_PRODUCT(pa->pa_id) <=
187 1.1 cherry PCI_PRODUCT_QUMRANET_VIRTIO_103F))
188 1.1 cherry return 1;
189 1.1 cherry break;
190 1.1 cherry }
191 1.1 cherry
192 1.1 cherry return 0;
193 1.1 cherry }
194 1.1 cherry
195 1.1 cherry static void
196 1.4 jakllsch virtio_pci_attach(device_t parent, device_t self, void *aux)
197 1.1 cherry {
198 1.4 jakllsch struct virtio_pci_softc * const psc = device_private(self);
199 1.4 jakllsch struct virtio_softc * const sc = &psc->sc_sc;
200 1.1 cherry struct pci_attach_args *pa = (struct pci_attach_args *)aux;
201 1.1 cherry pci_chipset_tag_t pc = pa->pa_pc;
202 1.1 cherry pcitag_t tag = pa->pa_tag;
203 1.1 cherry int revision;
204 1.1 cherry pcireg_t id;
205 1.2 uwe pcireg_t csr;
206 1.1 cherry
207 1.1 cherry revision = PCI_REVISION(pa->pa_class);
208 1.1 cherry if (revision != 0) {
209 1.1 cherry aprint_normal(": unknown revision 0x%02x; giving up\n",
210 1.1 cherry revision);
211 1.1 cherry return;
212 1.1 cherry }
213 1.1 cherry aprint_normal("\n");
214 1.1 cherry aprint_naive("\n");
215 1.1 cherry
216 1.1 cherry /* subsystem ID shows what I am */
217 1.1 cherry id = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
218 1.1 cherry aprint_normal_dev(self, "Virtio %s Device (rev. 0x%02x)\n",
219 1.1 cherry (PCI_SUBSYS_ID(id) < NDEVNAMES?
220 1.1 cherry virtio_device_name[PCI_SUBSYS_ID(id)] : "Unknown"),
221 1.1 cherry revision);
222 1.1 cherry
223 1.2 uwe csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
224 1.2 uwe csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE;
225 1.2 uwe pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
226 1.2 uwe
227 1.1 cherry sc->sc_dev = self;
228 1.4 jakllsch sc->sc_ops = &virtio_pci_ops;
229 1.4 jakllsch psc->sc_pa = *pa;
230 1.4 jakllsch psc->sc_iot = pa->pa_iot;
231 1.1 cherry if (pci_dma64_available(pa))
232 1.1 cherry sc->sc_dmat = pa->pa_dmat64;
233 1.1 cherry else
234 1.1 cherry sc->sc_dmat = pa->pa_dmat;
235 1.4 jakllsch psc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
236 1.1 cherry
237 1.1 cherry if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
238 1.4 jakllsch &psc->sc_iot, &psc->sc_ioh, NULL, &psc->sc_iosize)) {
239 1.1 cherry aprint_error_dev(self, "can't map i/o space\n");
240 1.1 cherry return;
241 1.1 cherry }
242 1.1 cherry
243 1.1 cherry virtio_device_reset(sc);
244 1.1 cherry virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
245 1.1 cherry virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
246 1.1 cherry
247 1.1 cherry sc->sc_childdevid = PCI_SUBSYS_ID(id);
248 1.1 cherry sc->sc_child = NULL;
249 1.4 jakllsch virtio_pci_rescan(self, "virtio", 0);
250 1.1 cherry return;
251 1.1 cherry }
252 1.1 cherry
253 1.1 cherry /* ARGSUSED */
254 1.1 cherry static int
255 1.4 jakllsch virtio_pci_rescan(device_t self, const char *attr, const int *scan_flags)
256 1.1 cherry {
257 1.4 jakllsch struct virtio_pci_softc * const psc = device_private(self);
258 1.4 jakllsch struct virtio_softc * const sc = &psc->sc_sc;
259 1.1 cherry struct virtio_attach_args va;
260 1.1 cherry
261 1.1 cherry if (sc->sc_child) /* Child already attached? */
262 1.1 cherry return 0;
263 1.1 cherry
264 1.1 cherry memset(&va, 0, sizeof(va));
265 1.1 cherry va.sc_childdevid = sc->sc_childdevid;
266 1.1 cherry
267 1.1 cherry config_found_ia(self, attr, &va, NULL);
268 1.1 cherry
269 1.1 cherry if (sc->sc_child == NULL) {
270 1.1 cherry aprint_error_dev(self,
271 1.1 cherry "no matching child driver; not configured\n");
272 1.1 cherry return 0;
273 1.1 cherry }
274 1.4 jakllsch
275 1.1 cherry if (sc->sc_child == VIRTIO_CHILD_FAILED) {
276 1.1 cherry aprint_error_dev(self,
277 1.1 cherry "virtio configuration failed\n");
278 1.1 cherry return 0;
279 1.1 cherry }
280 1.1 cherry
281 1.1 cherry /*
282 1.1 cherry * Make sure child drivers initialize interrupts via call
283 1.1 cherry * to virtio_child_attach_finish().
284 1.1 cherry */
285 1.4 jakllsch KASSERT(psc->sc_ihs_num != 0);
286 1.1 cherry
287 1.1 cherry return 0;
288 1.1 cherry }
289 1.1 cherry
290 1.1 cherry
291 1.1 cherry static int
292 1.4 jakllsch virtio_pci_detach(device_t self, int flags)
293 1.1 cherry {
294 1.4 jakllsch struct virtio_pci_softc * const psc = device_private(self);
295 1.4 jakllsch struct virtio_softc * const sc = &psc->sc_sc;
296 1.1 cherry int r;
297 1.1 cherry
298 1.1 cherry if (sc->sc_child != NULL) {
299 1.1 cherry r = config_detach(sc->sc_child, flags);
300 1.1 cherry if (r)
301 1.1 cherry return r;
302 1.1 cherry }
303 1.1 cherry
304 1.1 cherry /* Check that child detached properly */
305 1.1 cherry KASSERT(sc->sc_child == NULL);
306 1.1 cherry KASSERT(sc->sc_vqs == NULL);
307 1.4 jakllsch KASSERT(psc->sc_ihs_num == 0);
308 1.1 cherry
309 1.4 jakllsch if (psc->sc_iosize)
310 1.4 jakllsch bus_space_unmap(psc->sc_iot, psc->sc_ioh, psc->sc_iosize);
311 1.4 jakllsch psc->sc_iosize = 0;
312 1.1 cherry
313 1.1 cherry return 0;
314 1.1 cherry }
315 1.4 jakllsch
316 1.4 jakllsch static void
317 1.4 jakllsch virtio_pci_kick(struct virtio_softc *sc, uint16_t idx)
318 1.4 jakllsch {
319 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
320 1.4 jakllsch
321 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh,
322 1.4 jakllsch VIRTIO_CONFIG_QUEUE_NOTIFY, idx);
323 1.4 jakllsch }
324 1.4 jakllsch
325 1.4 jakllsch static uint8_t
326 1.4 jakllsch virtio_pci_read_device_config_1(struct virtio_softc *sc, int index)
327 1.4 jakllsch {
328 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
329 1.4 jakllsch return bus_space_read_stream_1(psc->sc_iot, psc->sc_ioh,
330 1.4 jakllsch psc->sc_config_offset + index);
331 1.4 jakllsch }
332 1.4 jakllsch
333 1.4 jakllsch static uint16_t
334 1.4 jakllsch virtio_pci_read_device_config_2(struct virtio_softc *sc, int index)
335 1.4 jakllsch {
336 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
337 1.4 jakllsch return bus_space_read_stream_2(psc->sc_iot, psc->sc_ioh,
338 1.4 jakllsch psc->sc_config_offset + index);
339 1.4 jakllsch }
340 1.4 jakllsch
341 1.4 jakllsch static uint32_t
342 1.4 jakllsch virtio_pci_read_device_config_4(struct virtio_softc *sc, int index)
343 1.4 jakllsch {
344 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
345 1.4 jakllsch return bus_space_read_stream_4(psc->sc_iot, psc->sc_ioh,
346 1.4 jakllsch psc->sc_config_offset + index);
347 1.4 jakllsch }
348 1.4 jakllsch
349 1.4 jakllsch static uint64_t
350 1.4 jakllsch virtio_pci_read_device_config_8(struct virtio_softc *sc, int index)
351 1.4 jakllsch {
352 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
353 1.4 jakllsch uint64_t r;
354 1.4 jakllsch
355 1.4 jakllsch r = bus_space_read_stream_4(psc->sc_iot, psc->sc_ioh,
356 1.4 jakllsch psc->sc_config_offset + index + REG_HI_OFF);
357 1.4 jakllsch r <<= 32;
358 1.4 jakllsch r |= bus_space_read_stream_4(psc->sc_iot, psc->sc_ioh,
359 1.4 jakllsch psc->sc_config_offset + index + REG_LO_OFF);
360 1.4 jakllsch
361 1.4 jakllsch return r;
362 1.4 jakllsch }
363 1.4 jakllsch
364 1.4 jakllsch static void
365 1.4 jakllsch virtio_pci_write_device_config_1(struct virtio_softc *sc, int index,
366 1.4 jakllsch uint8_t value)
367 1.4 jakllsch {
368 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
369 1.4 jakllsch
370 1.4 jakllsch bus_space_write_stream_1(psc->sc_iot, psc->sc_ioh,
371 1.4 jakllsch psc->sc_config_offset + index, value);
372 1.4 jakllsch }
373 1.4 jakllsch
374 1.4 jakllsch static void
375 1.4 jakllsch virtio_pci_write_device_config_2(struct virtio_softc *sc, int index,
376 1.4 jakllsch uint16_t value)
377 1.4 jakllsch {
378 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
379 1.4 jakllsch
380 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh,
381 1.4 jakllsch psc->sc_config_offset + index, value);
382 1.4 jakllsch }
383 1.4 jakllsch
384 1.4 jakllsch static void
385 1.4 jakllsch virtio_pci_write_device_config_4(struct virtio_softc *sc, int index,
386 1.4 jakllsch uint32_t value)
387 1.4 jakllsch {
388 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
389 1.4 jakllsch
390 1.4 jakllsch bus_space_write_stream_4(psc->sc_iot, psc->sc_ioh,
391 1.4 jakllsch psc->sc_config_offset + index, value);
392 1.4 jakllsch }
393 1.4 jakllsch
394 1.4 jakllsch static void
395 1.4 jakllsch virtio_pci_write_device_config_8(struct virtio_softc *sc, int index,
396 1.4 jakllsch uint64_t value)
397 1.4 jakllsch {
398 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
399 1.4 jakllsch
400 1.4 jakllsch bus_space_write_stream_4(psc->sc_iot, psc->sc_ioh,
401 1.4 jakllsch psc->sc_config_offset + index + REG_LO_OFF,
402 1.4 jakllsch value & 0xffffffff);
403 1.4 jakllsch bus_space_write_stream_4(psc->sc_iot, psc->sc_ioh,
404 1.4 jakllsch psc->sc_config_offset + index + REG_HI_OFF,
405 1.4 jakllsch value >> 32);
406 1.4 jakllsch }
407 1.4 jakllsch
408 1.4 jakllsch static uint16_t
409 1.4 jakllsch virtio_pci_read_queue_size(struct virtio_softc *sc, uint16_t idx)
410 1.4 jakllsch {
411 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
412 1.4 jakllsch
413 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh,
414 1.4 jakllsch VIRTIO_CONFIG_QUEUE_SELECT, idx);
415 1.4 jakllsch return bus_space_read_stream_2(psc->sc_iot, psc->sc_ioh,
416 1.4 jakllsch VIRTIO_CONFIG_QUEUE_SIZE);
417 1.4 jakllsch }
418 1.4 jakllsch
419 1.4 jakllsch static void
420 1.4 jakllsch virtio_pci_setup_queue(struct virtio_softc *sc, uint16_t idx, uint32_t addr)
421 1.4 jakllsch {
422 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
423 1.4 jakllsch
424 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh,
425 1.4 jakllsch VIRTIO_CONFIG_QUEUE_SELECT, idx);
426 1.4 jakllsch bus_space_write_stream_4(psc->sc_iot, psc->sc_ioh,
427 1.4 jakllsch VIRTIO_CONFIG_QUEUE_ADDRESS, addr);
428 1.4 jakllsch
429 1.4 jakllsch if (psc->sc_ihs_num > 1) {
430 1.4 jakllsch int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
431 1.4 jakllsch if (false) /* (for per-vq vectors) */
432 1.4 jakllsch vec += idx;
433 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh,
434 1.4 jakllsch VIRTIO_CONFIG_MSI_QUEUE_VECTOR, vec);
435 1.4 jakllsch }
436 1.4 jakllsch }
437 1.4 jakllsch
438 1.4 jakllsch static void
439 1.4 jakllsch virtio_pci_set_status(struct virtio_softc *sc, int status)
440 1.4 jakllsch {
441 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
442 1.4 jakllsch int old = 0;
443 1.4 jakllsch
444 1.4 jakllsch if (status != 0) {
445 1.4 jakllsch old = bus_space_read_stream_1(psc->sc_iot, psc->sc_ioh,
446 1.4 jakllsch VIRTIO_CONFIG_DEVICE_STATUS);
447 1.4 jakllsch }
448 1.4 jakllsch bus_space_write_stream_1(psc->sc_iot, psc->sc_ioh,
449 1.4 jakllsch VIRTIO_CONFIG_DEVICE_STATUS, status|old);
450 1.4 jakllsch }
451 1.4 jakllsch
452 1.4 jakllsch static uint32_t
453 1.4 jakllsch virtio_pci_negotiate_features(struct virtio_softc *sc, uint32_t guest_features)
454 1.4 jakllsch {
455 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
456 1.4 jakllsch uint32_t r;
457 1.4 jakllsch
458 1.4 jakllsch r = bus_space_read_stream_4(psc->sc_iot, psc->sc_ioh,
459 1.4 jakllsch VIRTIO_CONFIG_DEVICE_FEATURES);
460 1.4 jakllsch r &= guest_features;
461 1.4 jakllsch bus_space_write_stream_4(psc->sc_iot, psc->sc_ioh,
462 1.4 jakllsch VIRTIO_CONFIG_GUEST_FEATURES, r);
463 1.4 jakllsch
464 1.4 jakllsch return r;
465 1.4 jakllsch }
466 1.4 jakllsch
467 1.4 jakllsch
468 1.4 jakllsch static int
469 1.4 jakllsch virtio_pci_setup_msix_vectors(struct virtio_softc *sc)
470 1.4 jakllsch {
471 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
472 1.4 jakllsch int offset, vector, ret, qid;
473 1.4 jakllsch
474 1.4 jakllsch offset = VIRTIO_CONFIG_MSI_CONFIG_VECTOR;
475 1.4 jakllsch vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
476 1.4 jakllsch
477 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh, offset, vector);
478 1.4 jakllsch ret = bus_space_read_stream_2(psc->sc_iot, psc->sc_ioh, offset);
479 1.4 jakllsch aprint_debug_dev(sc->sc_dev, "expected=%d, actual=%d\n",
480 1.4 jakllsch vector, ret);
481 1.4 jakllsch if (ret != vector)
482 1.4 jakllsch return -1;
483 1.4 jakllsch
484 1.4 jakllsch for (qid = 0; qid < sc->sc_nvqs; qid++) {
485 1.4 jakllsch offset = VIRTIO_CONFIG_QUEUE_SELECT;
486 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh, offset, qid);
487 1.4 jakllsch
488 1.4 jakllsch offset = VIRTIO_CONFIG_MSI_QUEUE_VECTOR;
489 1.4 jakllsch vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
490 1.4 jakllsch
491 1.4 jakllsch bus_space_write_stream_2(psc->sc_iot, psc->sc_ioh, offset, vector);
492 1.4 jakllsch ret = bus_space_read_stream_2(psc->sc_iot, psc->sc_ioh, offset);
493 1.4 jakllsch aprint_debug_dev(sc->sc_dev, "expected=%d, actual=%d\n",
494 1.4 jakllsch vector, ret);
495 1.4 jakllsch if (ret != vector)
496 1.4 jakllsch return -1;
497 1.4 jakllsch }
498 1.4 jakllsch
499 1.4 jakllsch return 0;
500 1.4 jakllsch }
501 1.4 jakllsch
502 1.4 jakllsch static int
503 1.4 jakllsch virtio_pci_setup_msix_interrupts(struct virtio_softc *sc,
504 1.4 jakllsch struct pci_attach_args *pa)
505 1.4 jakllsch {
506 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
507 1.4 jakllsch device_t self = sc->sc_dev;
508 1.4 jakllsch pci_chipset_tag_t pc = pa->pa_pc;
509 1.4 jakllsch char intrbuf[PCI_INTRSTR_LEN];
510 1.4 jakllsch char const *intrstr;
511 1.4 jakllsch int idx;
512 1.4 jakllsch
513 1.4 jakllsch idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
514 1.4 jakllsch if (sc->sc_flags & VIRTIO_F_PCI_INTR_MPSAFE)
515 1.4 jakllsch pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
516 1.4 jakllsch
517 1.4 jakllsch psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
518 1.4 jakllsch sc->sc_ipl, virtio_pci_msix_config_intr, sc, device_xname(sc->sc_dev));
519 1.4 jakllsch if (psc->sc_ihs[idx] == NULL) {
520 1.4 jakllsch aprint_error_dev(self, "couldn't establish MSI-X for config\n");
521 1.4 jakllsch goto error;
522 1.4 jakllsch }
523 1.4 jakllsch
524 1.4 jakllsch idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
525 1.4 jakllsch if (sc->sc_flags & VIRTIO_F_PCI_INTR_MPSAFE)
526 1.4 jakllsch pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
527 1.4 jakllsch
528 1.4 jakllsch psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
529 1.4 jakllsch sc->sc_ipl, virtio_pci_msix_queue_intr, sc, device_xname(sc->sc_dev));
530 1.4 jakllsch if (psc->sc_ihs[idx] == NULL) {
531 1.4 jakllsch aprint_error_dev(self, "couldn't establish MSI-X for queues\n");
532 1.4 jakllsch goto error;
533 1.4 jakllsch }
534 1.4 jakllsch
535 1.4 jakllsch if (virtio_pci_setup_msix_vectors(sc) != 0) {
536 1.4 jakllsch aprint_error_dev(self, "couldn't setup MSI-X vectors\n");
537 1.4 jakllsch goto error;
538 1.4 jakllsch }
539 1.4 jakllsch
540 1.4 jakllsch idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
541 1.4 jakllsch intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf, sizeof(intrbuf));
542 1.4 jakllsch aprint_normal_dev(self, "config interrupting at %s\n", intrstr);
543 1.4 jakllsch idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
544 1.4 jakllsch intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf, sizeof(intrbuf));
545 1.4 jakllsch aprint_normal_dev(self, "queues interrupting at %s\n", intrstr);
546 1.4 jakllsch
547 1.4 jakllsch return 0;
548 1.4 jakllsch
549 1.4 jakllsch error:
550 1.4 jakllsch idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
551 1.4 jakllsch if (psc->sc_ihs[idx] != NULL)
552 1.4 jakllsch pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
553 1.4 jakllsch idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
554 1.4 jakllsch if (psc->sc_ihs[idx] != NULL)
555 1.4 jakllsch pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
556 1.4 jakllsch
557 1.4 jakllsch return -1;
558 1.4 jakllsch }
559 1.4 jakllsch
560 1.4 jakllsch static int
561 1.4 jakllsch virtio_pci_setup_intx_interrupt(struct virtio_softc *sc,
562 1.4 jakllsch struct pci_attach_args *pa)
563 1.4 jakllsch {
564 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
565 1.4 jakllsch device_t self = sc->sc_dev;
566 1.4 jakllsch pci_chipset_tag_t pc = pa->pa_pc;
567 1.4 jakllsch char intrbuf[PCI_INTRSTR_LEN];
568 1.4 jakllsch char const *intrstr;
569 1.4 jakllsch
570 1.4 jakllsch if (sc->sc_flags & VIRTIO_F_PCI_INTR_MPSAFE)
571 1.4 jakllsch pci_intr_setattr(pc, &psc->sc_ihp[0], PCI_INTR_MPSAFE, true);
572 1.4 jakllsch
573 1.4 jakllsch psc->sc_ihs[0] = pci_intr_establish_xname(pc, psc->sc_ihp[0],
574 1.4 jakllsch sc->sc_ipl, virtio_pci_intr, sc, device_xname(sc->sc_dev));
575 1.4 jakllsch if (psc->sc_ihs[0] == NULL) {
576 1.4 jakllsch aprint_error_dev(self, "couldn't establish INTx\n");
577 1.4 jakllsch return -1;
578 1.4 jakllsch }
579 1.4 jakllsch
580 1.4 jakllsch intrstr = pci_intr_string(pc, psc->sc_ihp[0], intrbuf, sizeof(intrbuf));
581 1.4 jakllsch aprint_normal_dev(self, "interrupting at %s\n", intrstr);
582 1.4 jakllsch
583 1.4 jakllsch return 0;
584 1.4 jakllsch }
585 1.4 jakllsch
586 1.4 jakllsch static int
587 1.4 jakllsch virtio_pci_setup_interrupts(struct virtio_softc *sc)
588 1.4 jakllsch {
589 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
590 1.4 jakllsch device_t self = sc->sc_dev;
591 1.4 jakllsch pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
592 1.4 jakllsch int error;
593 1.4 jakllsch int nmsix;
594 1.4 jakllsch int counts[PCI_INTR_TYPE_SIZE];
595 1.4 jakllsch pci_intr_type_t max_type;
596 1.4 jakllsch
597 1.4 jakllsch nmsix = pci_msix_count(psc->sc_pa.pa_pc, psc->sc_pa.pa_tag);
598 1.4 jakllsch aprint_debug_dev(self, "pci_msix_count=%d\n", nmsix);
599 1.4 jakllsch
600 1.4 jakllsch /* We need at least two: one for config and the other for queues */
601 1.4 jakllsch if ((sc->sc_flags & VIRTIO_F_PCI_INTR_MSIX) == 0 || nmsix < 2) {
602 1.4 jakllsch /* Try INTx only */
603 1.4 jakllsch max_type = PCI_INTR_TYPE_INTX;
604 1.4 jakllsch counts[PCI_INTR_TYPE_INTX] = 1;
605 1.4 jakllsch } else {
606 1.4 jakllsch /* Try MSI-X first and INTx second */
607 1.4 jakllsch max_type = PCI_INTR_TYPE_MSIX;
608 1.4 jakllsch counts[PCI_INTR_TYPE_MSIX] = 2;
609 1.4 jakllsch counts[PCI_INTR_TYPE_MSI] = 0;
610 1.4 jakllsch counts[PCI_INTR_TYPE_INTX] = 1;
611 1.4 jakllsch }
612 1.4 jakllsch
613 1.4 jakllsch retry:
614 1.4 jakllsch error = pci_intr_alloc(&psc->sc_pa, &psc->sc_ihp, counts, max_type);
615 1.4 jakllsch if (error != 0) {
616 1.4 jakllsch aprint_error_dev(self, "couldn't map interrupt\n");
617 1.4 jakllsch return -1;
618 1.4 jakllsch }
619 1.4 jakllsch
620 1.4 jakllsch if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX) {
621 1.4 jakllsch psc->sc_ihs = kmem_alloc(sizeof(*psc->sc_ihs) * 2,
622 1.4 jakllsch KM_SLEEP);
623 1.4 jakllsch
624 1.4 jakllsch error = virtio_pci_setup_msix_interrupts(sc, &psc->sc_pa);
625 1.4 jakllsch if (error != 0) {
626 1.4 jakllsch kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * 2);
627 1.4 jakllsch pci_intr_release(pc, psc->sc_ihp, 2);
628 1.4 jakllsch
629 1.4 jakllsch /* Retry INTx */
630 1.4 jakllsch max_type = PCI_INTR_TYPE_INTX;
631 1.4 jakllsch counts[PCI_INTR_TYPE_INTX] = 1;
632 1.4 jakllsch goto retry;
633 1.4 jakllsch }
634 1.4 jakllsch
635 1.4 jakllsch psc->sc_ihs_num = 2;
636 1.4 jakllsch psc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_MSI;
637 1.4 jakllsch } else if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_INTX) {
638 1.4 jakllsch psc->sc_ihs = kmem_alloc(sizeof(*psc->sc_ihs) * 1,
639 1.4 jakllsch KM_SLEEP);
640 1.4 jakllsch
641 1.4 jakllsch error = virtio_pci_setup_intx_interrupt(sc, &psc->sc_pa);
642 1.4 jakllsch if (error != 0) {
643 1.4 jakllsch kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * 1);
644 1.4 jakllsch pci_intr_release(pc, psc->sc_ihp, 1);
645 1.4 jakllsch return -1;
646 1.4 jakllsch }
647 1.4 jakllsch
648 1.4 jakllsch psc->sc_ihs_num = 1;
649 1.4 jakllsch psc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
650 1.4 jakllsch }
651 1.4 jakllsch
652 1.4 jakllsch return 0;
653 1.4 jakllsch }
654 1.4 jakllsch
655 1.4 jakllsch static void
656 1.4 jakllsch virtio_pci_free_interrupts(struct virtio_softc *sc)
657 1.4 jakllsch {
658 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
659 1.4 jakllsch
660 1.4 jakllsch for (int i = 0; i < psc->sc_ihs_num; i++) {
661 1.4 jakllsch if (psc->sc_ihs[i] == NULL)
662 1.4 jakllsch continue;
663 1.4 jakllsch pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[i]);
664 1.4 jakllsch psc->sc_ihs[i] = NULL;
665 1.4 jakllsch }
666 1.4 jakllsch
667 1.4 jakllsch if (psc->sc_ihs_num > 0)
668 1.4 jakllsch pci_intr_release(psc->sc_pa.pa_pc, psc->sc_ihp, psc->sc_ihs_num);
669 1.4 jakllsch
670 1.4 jakllsch if (psc->sc_ihs != NULL) {
671 1.4 jakllsch kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * psc->sc_ihs_num);
672 1.4 jakllsch psc->sc_ihs = NULL;
673 1.4 jakllsch }
674 1.4 jakllsch psc->sc_ihs_num = 0;
675 1.4 jakllsch }
676 1.4 jakllsch
677 1.4 jakllsch /*
678 1.4 jakllsch * Interrupt handler.
679 1.4 jakllsch */
680 1.4 jakllsch static int
681 1.4 jakllsch virtio_pci_intr(void *arg)
682 1.4 jakllsch {
683 1.4 jakllsch struct virtio_softc *sc = arg;
684 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
685 1.4 jakllsch int isr, r = 0;
686 1.4 jakllsch
687 1.4 jakllsch /* check and ack the interrupt */
688 1.4 jakllsch isr = bus_space_read_stream_1(psc->sc_iot, psc->sc_ioh,
689 1.4 jakllsch VIRTIO_CONFIG_ISR_STATUS);
690 1.4 jakllsch if (isr == 0)
691 1.4 jakllsch return 0;
692 1.4 jakllsch if ((isr & VIRTIO_CONFIG_ISR_CONFIG_CHANGE) &&
693 1.4 jakllsch (sc->sc_config_change != NULL))
694 1.4 jakllsch r = (sc->sc_config_change)(sc);
695 1.4 jakllsch if (sc->sc_intrhand != NULL) {
696 1.4 jakllsch if (sc->sc_soft_ih != NULL)
697 1.4 jakllsch softint_schedule(sc->sc_soft_ih);
698 1.4 jakllsch else
699 1.4 jakllsch r |= (sc->sc_intrhand)(sc);
700 1.4 jakllsch }
701 1.4 jakllsch
702 1.4 jakllsch return r;
703 1.4 jakllsch }
704 1.4 jakllsch
705 1.4 jakllsch static int
706 1.4 jakllsch virtio_pci_msix_queue_intr(void *arg)
707 1.4 jakllsch {
708 1.4 jakllsch struct virtio_softc *sc = arg;
709 1.4 jakllsch int r = 0;
710 1.4 jakllsch
711 1.4 jakllsch if (sc->sc_intrhand != NULL) {
712 1.4 jakllsch if (sc->sc_soft_ih != NULL)
713 1.4 jakllsch softint_schedule(sc->sc_soft_ih);
714 1.4 jakllsch else
715 1.4 jakllsch r |= (sc->sc_intrhand)(sc);
716 1.4 jakllsch }
717 1.4 jakllsch
718 1.4 jakllsch return r;
719 1.4 jakllsch }
720 1.4 jakllsch
721 1.4 jakllsch static int
722 1.4 jakllsch virtio_pci_msix_config_intr(void *arg)
723 1.4 jakllsch {
724 1.4 jakllsch struct virtio_softc *sc = arg;
725 1.4 jakllsch int r = 0;
726 1.4 jakllsch
727 1.4 jakllsch if (sc->sc_config_change != NULL)
728 1.4 jakllsch r = (sc->sc_config_change)(sc);
729 1.4 jakllsch return r;
730 1.4 jakllsch }
731 1.5 jakllsch
732 1.5 jakllsch MODULE(MODULE_CLASS_DRIVER, virtio_pci, "pci,virtio");
733 1.5 jakllsch
734 1.5 jakllsch #ifdef _MODULE
735 1.5 jakllsch #include "ioconf.c"
736 1.5 jakllsch #endif
737 1.5 jakllsch
738 1.5 jakllsch static int
739 1.5 jakllsch virtio_pci_modcmd(modcmd_t cmd, void *opaque)
740 1.5 jakllsch {
741 1.5 jakllsch int error = 0;
742 1.5 jakllsch
743 1.5 jakllsch #ifdef _MODULE
744 1.5 jakllsch switch (cmd) {
745 1.5 jakllsch case MODULE_CMD_INIT:
746 1.5 jakllsch error = config_init_component(cfdriver_ioconf_virtio_pci,
747 1.5 jakllsch cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
748 1.5 jakllsch break;
749 1.5 jakllsch case MODULE_CMD_FINI:
750 1.5 jakllsch error = config_fini_component(cfdriver_ioconf_virtio_pci,
751 1.5 jakllsch cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
752 1.5 jakllsch break;
753 1.5 jakllsch default:
754 1.5 jakllsch error = ENOTTY;
755 1.5 jakllsch break;
756 1.5 jakllsch }
757 1.5 jakllsch #endif
758 1.5 jakllsch
759 1.5 jakllsch return error;
760 1.5 jakllsch }
761 1.5 jakllsch
762