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