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