virtio_pci.c revision 1.52 1 1.52 riastrad /* $NetBSD: virtio_pci.c,v 1.52 2024/06/25 14:54:55 riastradh Exp $ */
2 1.1 cherry
3 1.1 cherry /*
4 1.15 reinoud * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.15 reinoud * Copyright (c) 2012 Stefan Fritsch.
6 1.1 cherry * Copyright (c) 2010 Minoura Makoto.
7 1.1 cherry * All rights reserved.
8 1.1 cherry *
9 1.1 cherry * Redistribution and use in source and binary forms, with or without
10 1.1 cherry * modification, are permitted provided that the following conditions
11 1.1 cherry * are met:
12 1.1 cherry * 1. Redistributions of source code must retain the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer.
14 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cherry * notice, this list of conditions and the following disclaimer in the
16 1.1 cherry * documentation and/or other materials provided with the distribution.
17 1.1 cherry *
18 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 cherry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 cherry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 cherry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 cherry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 cherry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 cherry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 cherry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 cherry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 cherry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 cherry */
29 1.1 cherry
30 1.1 cherry #include <sys/cdefs.h>
31 1.52 riastrad __KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.52 2024/06/25 14:54:55 riastradh Exp $");
32 1.1 cherry
33 1.1 cherry #include <sys/param.h>
34 1.50 riastrad #include <sys/types.h>
35 1.50 riastrad
36 1.50 riastrad #include <sys/device.h>
37 1.50 riastrad #include <sys/endian.h>
38 1.50 riastrad #include <sys/interrupt.h>
39 1.4 jakllsch #include <sys/kmem.h>
40 1.5 jakllsch #include <sys/module.h>
41 1.33 yamaguch #include <sys/syslog.h>
42 1.50 riastrad #include <sys/systm.h>
43 1.1 cherry
44 1.1 cherry #include <dev/pci/pcidevs.h>
45 1.1 cherry #include <dev/pci/pcireg.h>
46 1.1 cherry #include <dev/pci/pcivar.h>
47 1.1 cherry
48 1.15 reinoud #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
49 1.15 reinoud #include <dev/pci/virtio_pcireg.h>
50 1.15 reinoud
51 1.1 cherry #define VIRTIO_PRIVATE
52 1.15 reinoud #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
53 1.1 cherry
54 1.44 thorpej #if defined(__alpha__) || defined(__sparc64__)
55 1.44 thorpej /*
56 1.44 thorpej * XXX VIRTIO_F_ACCESS_PLATFORM is required for standard PCI DMA
57 1.44 thorpej * XXX to work on these platforms, at least by Qemu.
58 1.44 thorpej * XXX
59 1.44 thorpej * XXX Generalize this later.
60 1.44 thorpej */
61 1.44 thorpej #define __NEED_VIRTIO_F_ACCESS_PLATFORM
62 1.44 thorpej #endif /* __alpha__ || __sparc64__ */
63 1.1 cherry
64 1.33 yamaguch #define VIRTIO_PCI_LOG(_sc, _use_log, _fmt, _args...) \
65 1.33 yamaguch do { \
66 1.33 yamaguch if ((_use_log)) { \
67 1.33 yamaguch log(LOG_DEBUG, "%s: " _fmt, \
68 1.33 yamaguch device_xname((_sc)->sc_dev), \
69 1.33 yamaguch ##_args); \
70 1.33 yamaguch } else { \
71 1.33 yamaguch aprint_error_dev((_sc)->sc_dev, \
72 1.33 yamaguch _fmt, ##_args); \
73 1.33 yamaguch } \
74 1.33 yamaguch } while(0)
75 1.33 yamaguch
76 1.4 jakllsch static int virtio_pci_match(device_t, cfdata_t, void *);
77 1.4 jakllsch static void virtio_pci_attach(device_t, device_t, void *);
78 1.4 jakllsch static int virtio_pci_rescan(device_t, const char *, const int *);
79 1.4 jakllsch static int virtio_pci_detach(device_t, int);
80 1.4 jakllsch
81 1.22 reinoud #define NMAPREG ((PCI_MAPREG_END - PCI_MAPREG_START) / \
82 1.22 reinoud sizeof(pcireg_t))
83 1.4 jakllsch struct virtio_pci_softc {
84 1.4 jakllsch struct virtio_softc sc_sc;
85 1.39 yamaguch bool sc_intr_pervq;
86 1.15 reinoud
87 1.15 reinoud /* IO space */
88 1.4 jakllsch bus_space_tag_t sc_iot;
89 1.4 jakllsch bus_space_handle_t sc_ioh;
90 1.4 jakllsch bus_size_t sc_iosize;
91 1.15 reinoud
92 1.15 reinoud /* BARs */
93 1.21 reinoud bus_space_tag_t sc_bars_iot[NMAPREG];
94 1.21 reinoud bus_space_handle_t sc_bars_ioh[NMAPREG];
95 1.21 reinoud bus_size_t sc_bars_iosize[NMAPREG];
96 1.15 reinoud
97 1.15 reinoud /* notify space */
98 1.15 reinoud bus_space_tag_t sc_notify_iot;
99 1.15 reinoud bus_space_handle_t sc_notify_ioh;
100 1.15 reinoud bus_size_t sc_notify_iosize;
101 1.15 reinoud uint32_t sc_notify_off_multiplier;
102 1.15 reinoud
103 1.15 reinoud /* isr space */
104 1.15 reinoud bus_space_tag_t sc_isr_iot;
105 1.15 reinoud bus_space_handle_t sc_isr_ioh;
106 1.15 reinoud bus_size_t sc_isr_iosize;
107 1.15 reinoud
108 1.15 reinoud /* generic */
109 1.4 jakllsch struct pci_attach_args sc_pa;
110 1.4 jakllsch pci_intr_handle_t *sc_ihp;
111 1.4 jakllsch void **sc_ihs;
112 1.4 jakllsch int sc_ihs_num;
113 1.15 reinoud int sc_devcfg_offset; /* for 0.9 */
114 1.4 jakllsch };
115 1.4 jakllsch
116 1.15 reinoud static int virtio_pci_attach_09(device_t, void *);
117 1.15 reinoud static void virtio_pci_kick_09(struct virtio_softc *, uint16_t);
118 1.15 reinoud static uint16_t virtio_pci_read_queue_size_09(struct virtio_softc *, uint16_t);
119 1.52 riastrad static void virtio_pci_setup_queue_09(struct virtio_softc *, uint16_t,
120 1.52 riastrad uint64_t);
121 1.15 reinoud static void virtio_pci_set_status_09(struct virtio_softc *, int);
122 1.52 riastrad static void virtio_pci_negotiate_features_09(struct virtio_softc *,
123 1.52 riastrad uint64_t);
124 1.15 reinoud
125 1.15 reinoud static int virtio_pci_attach_10(device_t, void *);
126 1.15 reinoud static void virtio_pci_kick_10(struct virtio_softc *, uint16_t);
127 1.15 reinoud static uint16_t virtio_pci_read_queue_size_10(struct virtio_softc *, uint16_t);
128 1.52 riastrad static void virtio_pci_setup_queue_10(struct virtio_softc *, uint16_t,
129 1.52 riastrad uint64_t);
130 1.15 reinoud static void virtio_pci_set_status_10(struct virtio_softc *, int);
131 1.52 riastrad static void virtio_pci_negotiate_features_10(struct virtio_softc *,
132 1.52 riastrad uint64_t);
133 1.52 riastrad static int virtio_pci_find_cap(struct virtio_pci_softc *, int, void *,
134 1.52 riastrad int);
135 1.15 reinoud
136 1.31 yamaguch static int virtio_pci_alloc_interrupts(struct virtio_softc *);
137 1.4 jakllsch static void virtio_pci_free_interrupts(struct virtio_softc *);
138 1.52 riastrad static int virtio_pci_adjust_config_region(struct virtio_pci_softc *);
139 1.52 riastrad static int virtio_pci_intr(void *);
140 1.4 jakllsch static int virtio_pci_msix_queue_intr(void *);
141 1.4 jakllsch static int virtio_pci_msix_config_intr(void *);
142 1.32 yamaguch static int virtio_pci_setup_interrupts_09(struct virtio_softc *, int);
143 1.32 yamaguch static int virtio_pci_setup_interrupts_10(struct virtio_softc *, int);
144 1.31 yamaguch static int virtio_pci_establish_msix_interrupts(struct virtio_softc *,
145 1.4 jakllsch struct pci_attach_args *);
146 1.31 yamaguch static int virtio_pci_establish_intx_interrupt(struct virtio_softc *,
147 1.4 jakllsch struct pci_attach_args *);
148 1.31 yamaguch static bool virtio_pci_msix_enabled(struct virtio_pci_softc *);
149 1.4 jakllsch
150 1.4 jakllsch #define VIRTIO_MSIX_CONFIG_VECTOR_INDEX 0
151 1.4 jakllsch #define VIRTIO_MSIX_QUEUE_VECTOR_INDEX 1
152 1.4 jakllsch
153 1.27 reinoud /*
154 1.43 rin * For big-endian aarch64/armv7 on QEMU (and most real HW), only CPU cores
155 1.43 rin * are running in big-endian mode, with all peripheral being configured to
156 1.43 rin * little-endian mode. Their default bus_space(9) functions forcibly swap
157 1.43 rin * byte-order. This guarantees that PIO'ed data from pci(4), e.g., are
158 1.43 rin * correctly handled by bus_space(9), while DMA'ed ones should be swapped
159 1.43 rin * by hand, in violation of virtio(4) specifications.
160 1.27 reinoud */
161 1.4 jakllsch
162 1.43 rin #if (defined(__aarch64__) || defined(__arm__)) && BYTE_ORDER == BIG_ENDIAN
163 1.43 rin # define READ_ENDIAN_09 BIG_ENDIAN
164 1.27 reinoud # define READ_ENDIAN_10 BIG_ENDIAN
165 1.27 reinoud # define STRUCT_ENDIAN_09 BIG_ENDIAN
166 1.27 reinoud # define STRUCT_ENDIAN_10 LITTLE_ENDIAN
167 1.27 reinoud #elif BYTE_ORDER == BIG_ENDIAN
168 1.27 reinoud # define READ_ENDIAN_09 LITTLE_ENDIAN
169 1.27 reinoud # define READ_ENDIAN_10 BIG_ENDIAN
170 1.27 reinoud # define STRUCT_ENDIAN_09 BIG_ENDIAN
171 1.27 reinoud # define STRUCT_ENDIAN_10 LITTLE_ENDIAN
172 1.27 reinoud #else /* little endian */
173 1.27 reinoud # define READ_ENDIAN_09 LITTLE_ENDIAN
174 1.27 reinoud # define READ_ENDIAN_10 LITTLE_ENDIAN
175 1.27 reinoud # define STRUCT_ENDIAN_09 LITTLE_ENDIAN
176 1.27 reinoud # define STRUCT_ENDIAN_10 LITTLE_ENDIAN
177 1.15 reinoud #endif
178 1.15 reinoud
179 1.4 jakllsch CFATTACH_DECL3_NEW(virtio_pci, sizeof(struct virtio_pci_softc),
180 1.4 jakllsch virtio_pci_match, virtio_pci_attach, virtio_pci_detach, NULL,
181 1.48 riastrad virtio_pci_rescan, NULL, 0);
182 1.4 jakllsch
183 1.15 reinoud static const struct virtio_ops virtio_pci_ops_09 = {
184 1.15 reinoud .kick = virtio_pci_kick_09,
185 1.15 reinoud .read_queue_size = virtio_pci_read_queue_size_09,
186 1.15 reinoud .setup_queue = virtio_pci_setup_queue_09,
187 1.15 reinoud .set_status = virtio_pci_set_status_09,
188 1.15 reinoud .neg_features = virtio_pci_negotiate_features_09,
189 1.31 yamaguch .alloc_interrupts = virtio_pci_alloc_interrupts,
190 1.15 reinoud .free_interrupts = virtio_pci_free_interrupts,
191 1.31 yamaguch .setup_interrupts = virtio_pci_setup_interrupts_09,
192 1.15 reinoud };
193 1.15 reinoud
194 1.15 reinoud static const struct virtio_ops virtio_pci_ops_10 = {
195 1.15 reinoud .kick = virtio_pci_kick_10,
196 1.15 reinoud .read_queue_size = virtio_pci_read_queue_size_10,
197 1.15 reinoud .setup_queue = virtio_pci_setup_queue_10,
198 1.15 reinoud .set_status = virtio_pci_set_status_10,
199 1.15 reinoud .neg_features = virtio_pci_negotiate_features_10,
200 1.31 yamaguch .alloc_interrupts = virtio_pci_alloc_interrupts,
201 1.4 jakllsch .free_interrupts = virtio_pci_free_interrupts,
202 1.31 yamaguch .setup_interrupts = virtio_pci_setup_interrupts_10,
203 1.4 jakllsch };
204 1.1 cherry
205 1.1 cherry static int
206 1.4 jakllsch virtio_pci_match(device_t parent, cfdata_t match, void *aux)
207 1.1 cherry {
208 1.1 cherry struct pci_attach_args *pa;
209 1.1 cherry
210 1.1 cherry pa = (struct pci_attach_args *)aux;
211 1.1 cherry switch (PCI_VENDOR(pa->pa_id)) {
212 1.1 cherry case PCI_VENDOR_QUMRANET:
213 1.34 uwe /* Transitional devices MUST have a PCI Revision ID of 0. */
214 1.15 reinoud if (((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <=
215 1.52 riastrad PCI_PRODUCT(pa->pa_id)) &&
216 1.52 riastrad (PCI_PRODUCT(pa->pa_id) <=
217 1.52 riastrad PCI_PRODUCT_QUMRANET_VIRTIO_103F)) &&
218 1.52 riastrad PCI_REVISION(pa->pa_class) == 0)
219 1.15 reinoud return 1;
220 1.34 uwe /*
221 1.34 uwe * Non-transitional devices SHOULD have a PCI Revision
222 1.34 uwe * ID of 1 or higher. Drivers MUST match any PCI
223 1.34 uwe * Revision ID value.
224 1.34 uwe */
225 1.15 reinoud if (((PCI_PRODUCT_QUMRANET_VIRTIO_1040 <=
226 1.52 riastrad PCI_PRODUCT(pa->pa_id)) &&
227 1.52 riastrad (PCI_PRODUCT(pa->pa_id) <=
228 1.52 riastrad PCI_PRODUCT_QUMRANET_VIRTIO_107F)) &&
229 1.52 riastrad /* XXX: TODO */
230 1.52 riastrad PCI_REVISION(pa->pa_class) == 1)
231 1.1 cherry return 1;
232 1.1 cherry break;
233 1.1 cherry }
234 1.1 cherry
235 1.1 cherry return 0;
236 1.1 cherry }
237 1.1 cherry
238 1.1 cherry static void
239 1.4 jakllsch virtio_pci_attach(device_t parent, device_t self, void *aux)
240 1.1 cherry {
241 1.4 jakllsch struct virtio_pci_softc * const psc = device_private(self);
242 1.4 jakllsch struct virtio_softc * const sc = &psc->sc_sc;
243 1.1 cherry struct pci_attach_args *pa = (struct pci_attach_args *)aux;
244 1.1 cherry pci_chipset_tag_t pc = pa->pa_pc;
245 1.1 cherry pcitag_t tag = pa->pa_tag;
246 1.1 cherry int revision;
247 1.15 reinoud int ret;
248 1.1 cherry pcireg_t id;
249 1.2 uwe pcireg_t csr;
250 1.1 cherry
251 1.1 cherry revision = PCI_REVISION(pa->pa_class);
252 1.15 reinoud switch (revision) {
253 1.15 reinoud case 0:
254 1.15 reinoud /* subsystem ID shows what I am */
255 1.15 reinoud id = PCI_SUBSYS_ID(pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG));
256 1.15 reinoud break;
257 1.15 reinoud case 1:
258 1.15 reinoud /* pci product number shows what I am */
259 1.15 reinoud id = PCI_PRODUCT(pa->pa_id) - PCI_PRODUCT_QUMRANET_VIRTIO_1040;
260 1.15 reinoud break;
261 1.15 reinoud default:
262 1.1 cherry aprint_normal(": unknown revision 0x%02x; giving up\n",
263 1.52 riastrad revision);
264 1.1 cherry return;
265 1.1 cherry }
266 1.15 reinoud
267 1.1 cherry aprint_normal("\n");
268 1.1 cherry aprint_naive("\n");
269 1.15 reinoud virtio_print_device_type(self, id, revision);
270 1.1 cherry
271 1.2 uwe csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
272 1.2 uwe csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE;
273 1.2 uwe pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
274 1.2 uwe
275 1.1 cherry sc->sc_dev = self;
276 1.4 jakllsch psc->sc_pa = *pa;
277 1.4 jakllsch psc->sc_iot = pa->pa_iot;
278 1.15 reinoud
279 1.15 reinoud sc->sc_dmat = pa->pa_dmat;
280 1.1 cherry if (pci_dma64_available(pa))
281 1.1 cherry sc->sc_dmat = pa->pa_dmat64;
282 1.1 cherry
283 1.15 reinoud /* attach is dependent on revision */
284 1.15 reinoud ret = 0;
285 1.15 reinoud if (revision == 1) {
286 1.15 reinoud /* try to attach 1.0 */
287 1.15 reinoud ret = virtio_pci_attach_10(self, aux);
288 1.15 reinoud }
289 1.15 reinoud if (ret == 0 && revision == 0) {
290 1.44 thorpej /*
291 1.44 thorpej * revision 0 means 0.9 only or both 0.9 and 1.0. The
292 1.44 thorpej * latter are so-called "Transitional Devices". For
293 1.44 thorpej * those devices, we want to use the 1.0 interface if
294 1.44 thorpej * possible.
295 1.44 thorpej *
296 1.44 thorpej * XXX Currently only on platforms that require 1.0
297 1.44 thorpej * XXX features, such as VIRTIO_F_ACCESS_PLATFORM.
298 1.44 thorpej */
299 1.44 thorpej #ifdef __NEED_VIRTIO_F_ACCESS_PLATFORM
300 1.44 thorpej /* First, try to attach 1.0 */
301 1.44 thorpej ret = virtio_pci_attach_10(self, aux);
302 1.44 thorpej if (ret != 0) {
303 1.44 thorpej aprint_error_dev(self,
304 1.44 thorpej "VirtIO 1.0 error = %d, falling back to 0.9\n",
305 1.44 thorpej ret);
306 1.44 thorpej /* Fall back to 0.9. */
307 1.44 thorpej ret = virtio_pci_attach_09(self, aux);
308 1.44 thorpej }
309 1.44 thorpej #else
310 1.15 reinoud ret = virtio_pci_attach_09(self, aux);
311 1.44 thorpej #endif /* __NEED_VIRTIO_F_ACCESS_PLATFORM */
312 1.15 reinoud }
313 1.15 reinoud if (ret) {
314 1.15 reinoud aprint_error_dev(self, "cannot attach (%d)\n", ret);
315 1.1 cherry return;
316 1.1 cherry }
317 1.15 reinoud KASSERT(sc->sc_ops);
318 1.15 reinoud
319 1.15 reinoud /* preset config region */
320 1.15 reinoud psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
321 1.15 reinoud if (virtio_pci_adjust_config_region(psc))
322 1.15 reinoud return;
323 1.1 cherry
324 1.15 reinoud /* generic */
325 1.1 cherry virtio_device_reset(sc);
326 1.1 cherry virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
327 1.1 cherry virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
328 1.1 cherry
329 1.15 reinoud sc->sc_childdevid = id;
330 1.1 cherry sc->sc_child = NULL;
331 1.29 thorpej virtio_pci_rescan(self, NULL, NULL);
332 1.1 cherry return;
333 1.1 cherry }
334 1.1 cherry
335 1.1 cherry /* ARGSUSED */
336 1.1 cherry static int
337 1.29 thorpej virtio_pci_rescan(device_t self, const char *ifattr, const int *locs)
338 1.1 cherry {
339 1.4 jakllsch struct virtio_pci_softc * const psc = device_private(self);
340 1.4 jakllsch struct virtio_softc * const sc = &psc->sc_sc;
341 1.1 cherry struct virtio_attach_args va;
342 1.1 cherry
343 1.1 cherry if (sc->sc_child) /* Child already attached? */
344 1.1 cherry return 0;
345 1.1 cherry
346 1.1 cherry memset(&va, 0, sizeof(va));
347 1.1 cherry va.sc_childdevid = sc->sc_childdevid;
348 1.1 cherry
349 1.30 thorpej config_found(self, &va, NULL, CFARGS_NONE);
350 1.1 cherry
351 1.15 reinoud if (virtio_attach_failed(sc))
352 1.1 cherry return 0;
353 1.1 cherry
354 1.1 cherry return 0;
355 1.1 cherry }
356 1.1 cherry
357 1.1 cherry static int
358 1.4 jakllsch virtio_pci_detach(device_t self, int flags)
359 1.1 cherry {
360 1.4 jakllsch struct virtio_pci_softc * const psc = device_private(self);
361 1.4 jakllsch struct virtio_softc * const sc = &psc->sc_sc;
362 1.46 riastrad unsigned i;
363 1.1 cherry int r;
364 1.1 cherry
365 1.40 yamaguch r = config_detach_children(self, flags);
366 1.40 yamaguch if (r != 0)
367 1.40 yamaguch return r;
368 1.1 cherry
369 1.41 riastrad /* Check that child never attached, or detached properly */
370 1.42 yamaguch KASSERT(sc->sc_child == NULL);
371 1.1 cherry KASSERT(sc->sc_vqs == NULL);
372 1.4 jakllsch KASSERT(psc->sc_ihs_num == 0);
373 1.1 cherry
374 1.46 riastrad if (sc->sc_version_1) {
375 1.46 riastrad for (i = 0; i < __arraycount(psc->sc_bars_iot); i++) {
376 1.46 riastrad if (psc->sc_bars_iosize[i] == 0)
377 1.46 riastrad continue;
378 1.46 riastrad bus_space_unmap(psc->sc_bars_iot[i],
379 1.46 riastrad psc->sc_bars_ioh[i], psc->sc_bars_iosize[i]);
380 1.46 riastrad psc->sc_bars_iosize[i] = 0;
381 1.46 riastrad }
382 1.46 riastrad } else {
383 1.46 riastrad if (psc->sc_iosize) {
384 1.46 riastrad bus_space_unmap(psc->sc_iot, psc->sc_ioh,
385 1.47 riastrad psc->sc_iosize);
386 1.46 riastrad psc->sc_iosize = 0;
387 1.46 riastrad }
388 1.46 riastrad }
389 1.1 cherry
390 1.1 cherry return 0;
391 1.1 cherry }
392 1.4 jakllsch
393 1.15 reinoud static int
394 1.15 reinoud virtio_pci_attach_09(device_t self, void *aux)
395 1.15 reinoud {
396 1.15 reinoud struct virtio_pci_softc * const psc = device_private(self);
397 1.15 reinoud struct pci_attach_args *pa = (struct pci_attach_args *)aux;
398 1.15 reinoud struct virtio_softc * const sc = &psc->sc_sc;
399 1.15 reinoud
400 1.15 reinoud /* complete IO region */
401 1.15 reinoud if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
402 1.52 riastrad &psc->sc_iot, &psc->sc_ioh, NULL, &psc->sc_iosize)) {
403 1.15 reinoud aprint_error_dev(self, "can't map i/o space\n");
404 1.15 reinoud return EIO;
405 1.15 reinoud }
406 1.15 reinoud
407 1.15 reinoud /* queue space */
408 1.15 reinoud if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
409 1.52 riastrad VIRTIO_CONFIG_QUEUE_NOTIFY, 2, &psc->sc_notify_ioh)) {
410 1.15 reinoud aprint_error_dev(self, "can't map notify i/o space\n");
411 1.15 reinoud return EIO;
412 1.15 reinoud }
413 1.15 reinoud psc->sc_notify_iosize = 2;
414 1.15 reinoud psc->sc_notify_iot = psc->sc_iot;
415 1.15 reinoud
416 1.15 reinoud /* ISR space */
417 1.15 reinoud if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
418 1.52 riastrad VIRTIO_CONFIG_ISR_STATUS, 1, &psc->sc_isr_ioh)) {
419 1.15 reinoud aprint_error_dev(self, "can't map isr i/o space\n");
420 1.15 reinoud return EIO;
421 1.15 reinoud }
422 1.15 reinoud psc->sc_isr_iosize = 1;
423 1.15 reinoud psc->sc_isr_iot = psc->sc_iot;
424 1.15 reinoud
425 1.15 reinoud /* set our version 0.9 ops */
426 1.15 reinoud sc->sc_ops = &virtio_pci_ops_09;
427 1.52 riastrad sc->sc_bus_endian = READ_ENDIAN_09;
428 1.27 reinoud sc->sc_struct_endian = STRUCT_ENDIAN_09;
429 1.15 reinoud return 0;
430 1.15 reinoud }
431 1.15 reinoud
432 1.15 reinoud static int
433 1.15 reinoud virtio_pci_attach_10(device_t self, void *aux)
434 1.4 jakllsch {
435 1.15 reinoud struct virtio_pci_softc * const psc = device_private(self);
436 1.15 reinoud struct pci_attach_args *pa = (struct pci_attach_args *)aux;
437 1.15 reinoud struct virtio_softc * const sc = &psc->sc_sc;
438 1.15 reinoud pci_chipset_tag_t pc = pa->pa_pc;
439 1.15 reinoud pcitag_t tag = pa->pa_tag;
440 1.15 reinoud
441 1.15 reinoud struct virtio_pci_cap common, isr, device;
442 1.15 reinoud struct virtio_pci_notify_cap notify;
443 1.15 reinoud int have_device_cfg = 0;
444 1.15 reinoud bus_size_t bars[NMAPREG] = { 0 };
445 1.15 reinoud int bars_idx[NMAPREG] = { 0 };
446 1.52 riastrad struct virtio_pci_cap * const caps[] =
447 1.52 riastrad { &common, &isr, &device, ¬ify.cap };
448 1.26 reinoud int i, j, ret = 0;
449 1.15 reinoud
450 1.15 reinoud if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_COMMON_CFG,
451 1.52 riastrad &common, sizeof(common)))
452 1.15 reinoud return ENODEV;
453 1.15 reinoud if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_NOTIFY_CFG,
454 1.52 riastrad ¬ify, sizeof(notify)))
455 1.15 reinoud return ENODEV;
456 1.15 reinoud if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_ISR_CFG,
457 1.52 riastrad &isr, sizeof(isr)))
458 1.15 reinoud return ENODEV;
459 1.15 reinoud if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_DEVICE_CFG,
460 1.52 riastrad &device, sizeof(device)))
461 1.15 reinoud memset(&device, 0, sizeof(device));
462 1.15 reinoud else
463 1.15 reinoud have_device_cfg = 1;
464 1.15 reinoud
465 1.15 reinoud /* Figure out which bars we need to map */
466 1.15 reinoud for (i = 0; i < __arraycount(caps); i++) {
467 1.15 reinoud int bar = caps[i]->bar;
468 1.15 reinoud bus_size_t len = caps[i]->offset + caps[i]->length;
469 1.52 riastrad
470 1.15 reinoud if (caps[i]->length == 0)
471 1.15 reinoud continue;
472 1.15 reinoud if (bars[bar] < len)
473 1.15 reinoud bars[bar] = len;
474 1.15 reinoud }
475 1.15 reinoud
476 1.26 reinoud for (i = j = 0; i < __arraycount(bars); i++) {
477 1.15 reinoud int reg;
478 1.15 reinoud pcireg_t type;
479 1.52 riastrad
480 1.15 reinoud if (bars[i] == 0)
481 1.15 reinoud continue;
482 1.35 uwe reg = PCI_BAR(i);
483 1.15 reinoud type = pci_mapreg_type(pc, tag, reg);
484 1.15 reinoud if (pci_mapreg_map(pa, reg, type, 0,
485 1.52 riastrad &psc->sc_bars_iot[j], &psc->sc_bars_ioh[j],
486 1.52 riastrad NULL, &psc->sc_bars_iosize[j])) {
487 1.15 reinoud aprint_error_dev(self, "can't map bar %u \n", i);
488 1.15 reinoud ret = EIO;
489 1.15 reinoud goto err;
490 1.15 reinoud }
491 1.17 martin aprint_debug_dev(self,
492 1.17 martin "bar[%d]: iot %p, size 0x%" PRIxBUSSIZE "\n",
493 1.17 martin j, psc->sc_bars_iot[j], psc->sc_bars_iosize[j]);
494 1.15 reinoud bars_idx[i] = j;
495 1.15 reinoud j++;
496 1.15 reinoud }
497 1.15 reinoud
498 1.15 reinoud i = bars_idx[notify.cap.bar];
499 1.15 reinoud if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
500 1.52 riastrad notify.cap.offset, notify.cap.length, &psc->sc_notify_ioh)) {
501 1.15 reinoud aprint_error_dev(self, "can't map notify i/o space\n");
502 1.15 reinoud ret = EIO;
503 1.15 reinoud goto err;
504 1.15 reinoud }
505 1.15 reinoud psc->sc_notify_iosize = notify.cap.length;
506 1.15 reinoud psc->sc_notify_iot = psc->sc_bars_iot[i];
507 1.15 reinoud psc->sc_notify_off_multiplier = le32toh(notify.notify_off_multiplier);
508 1.15 reinoud
509 1.15 reinoud if (have_device_cfg) {
510 1.15 reinoud i = bars_idx[device.bar];
511 1.52 riastrad if (bus_space_subregion(psc->sc_bars_iot[i],
512 1.52 riastrad psc->sc_bars_ioh[i], device.offset, device.length,
513 1.52 riastrad &sc->sc_devcfg_ioh)) {
514 1.15 reinoud aprint_error_dev(self, "can't map devcfg i/o space\n");
515 1.15 reinoud ret = EIO;
516 1.15 reinoud goto err;
517 1.15 reinoud }
518 1.15 reinoud aprint_debug_dev(self,
519 1.52 riastrad "device.offset = 0x%x, device.length = 0x%x\n",
520 1.52 riastrad device.offset, device.length);
521 1.15 reinoud sc->sc_devcfg_iosize = device.length;
522 1.15 reinoud sc->sc_devcfg_iot = psc->sc_bars_iot[i];
523 1.15 reinoud }
524 1.15 reinoud
525 1.15 reinoud i = bars_idx[isr.bar];
526 1.15 reinoud if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
527 1.52 riastrad isr.offset, isr.length, &psc->sc_isr_ioh)) {
528 1.15 reinoud aprint_error_dev(self, "can't map isr i/o space\n");
529 1.15 reinoud ret = EIO;
530 1.15 reinoud goto err;
531 1.15 reinoud }
532 1.15 reinoud psc->sc_isr_iosize = isr.length;
533 1.15 reinoud psc->sc_isr_iot = psc->sc_bars_iot[i];
534 1.15 reinoud
535 1.15 reinoud i = bars_idx[common.bar];
536 1.15 reinoud if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
537 1.52 riastrad common.offset, common.length, &psc->sc_ioh)) {
538 1.15 reinoud aprint_error_dev(self, "can't map common i/o space\n");
539 1.15 reinoud ret = EIO;
540 1.15 reinoud goto err;
541 1.15 reinoud }
542 1.15 reinoud psc->sc_iosize = common.length;
543 1.15 reinoud psc->sc_iot = psc->sc_bars_iot[i];
544 1.15 reinoud
545 1.15 reinoud psc->sc_sc.sc_version_1 = 1;
546 1.15 reinoud
547 1.15 reinoud /* set our version 1.0 ops */
548 1.15 reinoud sc->sc_ops = &virtio_pci_ops_10;
549 1.52 riastrad sc->sc_bus_endian = READ_ENDIAN_10;
550 1.27 reinoud sc->sc_struct_endian = STRUCT_ENDIAN_10;
551 1.15 reinoud return 0;
552 1.4 jakllsch
553 1.15 reinoud err:
554 1.45 riastrad /* undo our pci_mapreg_map()s */
555 1.23 reinoud for (i = 0; i < __arraycount(bars); i++) {
556 1.26 reinoud if (psc->sc_bars_iosize[i] == 0)
557 1.23 reinoud continue;
558 1.26 reinoud bus_space_unmap(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
559 1.49 riastrad psc->sc_bars_iosize[i]);
560 1.49 riastrad psc->sc_bars_iosize[i] = 0;
561 1.23 reinoud }
562 1.15 reinoud return ret;
563 1.4 jakllsch }
564 1.4 jakllsch
565 1.15 reinoud /* v1.0 attach helper */
566 1.15 reinoud static int
567 1.52 riastrad virtio_pci_find_cap(struct virtio_pci_softc *psc, int cfg_type, void *buf,
568 1.52 riastrad int buflen)
569 1.4 jakllsch {
570 1.15 reinoud device_t self = psc->sc_sc.sc_dev;
571 1.15 reinoud pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
572 1.15 reinoud pcitag_t tag = psc->sc_pa.pa_tag;
573 1.15 reinoud unsigned int offset, i, len;
574 1.15 reinoud union {
575 1.15 reinoud pcireg_t reg[8];
576 1.15 reinoud struct virtio_pci_cap vcap;
577 1.15 reinoud } *v = buf;
578 1.15 reinoud
579 1.15 reinoud if (buflen < sizeof(struct virtio_pci_cap))
580 1.15 reinoud return ERANGE;
581 1.15 reinoud
582 1.52 riastrad if (!pci_get_capability(pc, tag, PCI_CAP_VENDSPEC, &offset,
583 1.52 riastrad &v->reg[0]))
584 1.15 reinoud return ENOENT;
585 1.15 reinoud
586 1.15 reinoud do {
587 1.15 reinoud for (i = 0; i < 4; i++)
588 1.15 reinoud v->reg[i] =
589 1.52 riastrad le32toh(pci_conf_read(pc, tag, offset + i * 4));
590 1.15 reinoud if (v->vcap.cfg_type == cfg_type)
591 1.15 reinoud break;
592 1.15 reinoud offset = v->vcap.cap_next;
593 1.15 reinoud } while (offset != 0);
594 1.15 reinoud
595 1.15 reinoud if (offset == 0)
596 1.15 reinoud return ENOENT;
597 1.15 reinoud
598 1.15 reinoud if (v->vcap.cap_len > sizeof(struct virtio_pci_cap)) {
599 1.15 reinoud len = roundup(v->vcap.cap_len, sizeof(pcireg_t));
600 1.15 reinoud if (len > buflen) {
601 1.15 reinoud aprint_error_dev(self, "%s cap too large\n", __func__);
602 1.15 reinoud return ERANGE;
603 1.15 reinoud }
604 1.15 reinoud for (i = 4; i < len / sizeof(pcireg_t); i++)
605 1.15 reinoud v->reg[i] =
606 1.52 riastrad le32toh(pci_conf_read(pc, tag, offset + i * 4));
607 1.15 reinoud }
608 1.15 reinoud
609 1.15 reinoud /* endian fixup */
610 1.15 reinoud v->vcap.offset = le32toh(v->vcap.offset);
611 1.15 reinoud v->vcap.length = le32toh(v->vcap.length);
612 1.15 reinoud return 0;
613 1.4 jakllsch }
614 1.4 jakllsch
615 1.15 reinoud /* -------------------------------------
616 1.15 reinoud * Version 0.9 support
617 1.15 reinoud * -------------------------------------*/
618 1.15 reinoud
619 1.15 reinoud static void
620 1.15 reinoud virtio_pci_kick_09(struct virtio_softc *sc, uint16_t idx)
621 1.4 jakllsch {
622 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
623 1.15 reinoud
624 1.15 reinoud bus_space_write_2(psc->sc_notify_iot, psc->sc_notify_ioh, 0, idx);
625 1.4 jakllsch }
626 1.4 jakllsch
627 1.15 reinoud /* only applicable for v 0.9 but also called for 1.0 */
628 1.15 reinoud static int
629 1.15 reinoud virtio_pci_adjust_config_region(struct virtio_pci_softc *psc)
630 1.4 jakllsch {
631 1.37 uwe struct virtio_softc * const sc = &psc->sc_sc;
632 1.37 uwe device_t self = sc->sc_dev;
633 1.15 reinoud
634 1.15 reinoud if (psc->sc_sc.sc_version_1)
635 1.15 reinoud return 0;
636 1.15 reinoud
637 1.15 reinoud sc->sc_devcfg_iosize = psc->sc_iosize - psc->sc_devcfg_offset;
638 1.15 reinoud sc->sc_devcfg_iot = psc->sc_iot;
639 1.15 reinoud if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
640 1.52 riastrad psc->sc_devcfg_offset, sc->sc_devcfg_iosize,
641 1.52 riastrad &sc->sc_devcfg_ioh)) {
642 1.15 reinoud aprint_error_dev(self, "can't map config i/o space\n");
643 1.15 reinoud return EIO;
644 1.15 reinoud }
645 1.15 reinoud
646 1.15 reinoud return 0;
647 1.4 jakllsch }
648 1.4 jakllsch
649 1.15 reinoud static uint16_t
650 1.15 reinoud virtio_pci_read_queue_size_09(struct virtio_softc *sc, uint16_t idx)
651 1.4 jakllsch {
652 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
653 1.4 jakllsch
654 1.15 reinoud bus_space_write_2(psc->sc_iot, psc->sc_ioh,
655 1.15 reinoud VIRTIO_CONFIG_QUEUE_SELECT, idx);
656 1.15 reinoud return bus_space_read_2(psc->sc_iot, psc->sc_ioh,
657 1.15 reinoud VIRTIO_CONFIG_QUEUE_SIZE);
658 1.4 jakllsch }
659 1.4 jakllsch
660 1.4 jakllsch static void
661 1.15 reinoud virtio_pci_setup_queue_09(struct virtio_softc *sc, uint16_t idx, uint64_t addr)
662 1.4 jakllsch {
663 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
664 1.4 jakllsch
665 1.15 reinoud bus_space_write_2(psc->sc_iot, psc->sc_ioh,
666 1.15 reinoud VIRTIO_CONFIG_QUEUE_SELECT, idx);
667 1.15 reinoud bus_space_write_4(psc->sc_iot, psc->sc_ioh,
668 1.15 reinoud VIRTIO_CONFIG_QUEUE_ADDRESS, addr / VIRTIO_PAGE_SIZE);
669 1.15 reinoud
670 1.15 reinoud if (psc->sc_ihs_num > 1) {
671 1.15 reinoud int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
672 1.39 yamaguch if (psc->sc_intr_pervq)
673 1.15 reinoud vec += idx;
674 1.15 reinoud bus_space_write_2(psc->sc_iot, psc->sc_ioh,
675 1.15 reinoud VIRTIO_CONFIG_MSI_QUEUE_VECTOR, vec);
676 1.15 reinoud }
677 1.4 jakllsch }
678 1.4 jakllsch
679 1.4 jakllsch static void
680 1.15 reinoud virtio_pci_set_status_09(struct virtio_softc *sc, int status)
681 1.4 jakllsch {
682 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
683 1.15 reinoud int old = 0;
684 1.4 jakllsch
685 1.15 reinoud if (status != 0) {
686 1.52 riastrad old = bus_space_read_1(psc->sc_iot, psc->sc_ioh,
687 1.52 riastrad VIRTIO_CONFIG_DEVICE_STATUS);
688 1.15 reinoud }
689 1.15 reinoud bus_space_write_1(psc->sc_iot, psc->sc_ioh,
690 1.15 reinoud VIRTIO_CONFIG_DEVICE_STATUS, status|old);
691 1.4 jakllsch }
692 1.4 jakllsch
693 1.4 jakllsch static void
694 1.52 riastrad virtio_pci_negotiate_features_09(struct virtio_softc *sc,
695 1.52 riastrad uint64_t guest_features)
696 1.4 jakllsch {
697 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
698 1.15 reinoud uint32_t r;
699 1.15 reinoud
700 1.15 reinoud r = bus_space_read_4(psc->sc_iot, psc->sc_ioh,
701 1.15 reinoud VIRTIO_CONFIG_DEVICE_FEATURES);
702 1.15 reinoud
703 1.15 reinoud r &= guest_features;
704 1.15 reinoud
705 1.15 reinoud bus_space_write_4(psc->sc_iot, psc->sc_ioh,
706 1.15 reinoud VIRTIO_CONFIG_GUEST_FEATURES, r);
707 1.4 jakllsch
708 1.15 reinoud sc->sc_active_features = r;
709 1.4 jakllsch }
710 1.4 jakllsch
711 1.15 reinoud /* -------------------------------------
712 1.15 reinoud * Version 1.0 support
713 1.15 reinoud * -------------------------------------*/
714 1.15 reinoud
715 1.4 jakllsch static void
716 1.15 reinoud virtio_pci_kick_10(struct virtio_softc *sc, uint16_t idx)
717 1.4 jakllsch {
718 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
719 1.15 reinoud unsigned offset = sc->sc_vqs[idx].vq_notify_off *
720 1.52 riastrad psc->sc_notify_off_multiplier;
721 1.4 jakllsch
722 1.15 reinoud bus_space_write_2(psc->sc_notify_iot, psc->sc_notify_ioh, offset, idx);
723 1.4 jakllsch }
724 1.4 jakllsch
725 1.4 jakllsch static uint16_t
726 1.15 reinoud virtio_pci_read_queue_size_10(struct virtio_softc *sc, uint16_t idx)
727 1.4 jakllsch {
728 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
729 1.15 reinoud bus_space_tag_t iot = psc->sc_iot;
730 1.15 reinoud bus_space_handle_t ioh = psc->sc_ioh;
731 1.4 jakllsch
732 1.15 reinoud bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, idx);
733 1.15 reinoud return bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SIZE);
734 1.4 jakllsch }
735 1.4 jakllsch
736 1.18 reinoud /*
737 1.36 uwe * By definition little endian only in v1.0. NB: "MAY" in the text
738 1.36 uwe * below refers to "independently" (i.e. the order of accesses) not
739 1.36 uwe * "32-bit" (which is restricted by the earlier "MUST").
740 1.24 thorpej *
741 1.36 uwe * 4.1.3.1 Driver Requirements: PCI Device Layout
742 1.36 uwe *
743 1.36 uwe * For device configuration access, the driver MUST use ... 32-bit
744 1.36 uwe * wide and aligned accesses for ... 64-bit wide fields. For 64-bit
745 1.36 uwe * fields, the driver MAY access each of the high and low 32-bit parts
746 1.36 uwe * of the field independently.
747 1.20 christos */
748 1.19 christos static __inline void
749 1.24 thorpej virtio_pci_bus_space_write_8(bus_space_tag_t iot, bus_space_handle_t ioh,
750 1.52 riastrad bus_size_t offset, uint64_t value)
751 1.19 christos {
752 1.36 uwe #if _QUAD_HIGHWORD
753 1.19 christos bus_space_write_4(iot, ioh, offset, BUS_ADDR_LO32(value));
754 1.19 christos bus_space_write_4(iot, ioh, offset + 4, BUS_ADDR_HI32(value));
755 1.19 christos #else
756 1.19 christos bus_space_write_4(iot, ioh, offset, BUS_ADDR_HI32(value));
757 1.19 christos bus_space_write_4(iot, ioh, offset + 4, BUS_ADDR_LO32(value));
758 1.19 christos #endif
759 1.19 christos }
760 1.18 reinoud
761 1.4 jakllsch static void
762 1.15 reinoud virtio_pci_setup_queue_10(struct virtio_softc *sc, uint16_t idx, uint64_t addr)
763 1.4 jakllsch {
764 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
765 1.15 reinoud struct virtqueue *vq = &sc->sc_vqs[idx];
766 1.52 riastrad bus_space_tag_t iot = psc->sc_iot;
767 1.15 reinoud bus_space_handle_t ioh = psc->sc_ioh;
768 1.15 reinoud KASSERT(vq->vq_index == idx);
769 1.15 reinoud
770 1.15 reinoud bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, vq->vq_index);
771 1.15 reinoud if (addr == 0) {
772 1.15 reinoud bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_ENABLE, 0);
773 1.24 thorpej virtio_pci_bus_space_write_8(iot, ioh,
774 1.52 riastrad VIRTIO_CONFIG1_QUEUE_DESC, 0);
775 1.24 thorpej virtio_pci_bus_space_write_8(iot, ioh,
776 1.52 riastrad VIRTIO_CONFIG1_QUEUE_AVAIL, 0);
777 1.24 thorpej virtio_pci_bus_space_write_8(iot, ioh,
778 1.52 riastrad VIRTIO_CONFIG1_QUEUE_USED, 0);
779 1.15 reinoud } else {
780 1.24 thorpej virtio_pci_bus_space_write_8(iot, ioh,
781 1.52 riastrad VIRTIO_CONFIG1_QUEUE_DESC, addr);
782 1.24 thorpej virtio_pci_bus_space_write_8(iot, ioh,
783 1.52 riastrad VIRTIO_CONFIG1_QUEUE_AVAIL, addr + vq->vq_availoffset);
784 1.24 thorpej virtio_pci_bus_space_write_8(iot, ioh,
785 1.52 riastrad VIRTIO_CONFIG1_QUEUE_USED, addr + vq->vq_usedoffset);
786 1.15 reinoud bus_space_write_2(iot, ioh,
787 1.52 riastrad VIRTIO_CONFIG1_QUEUE_ENABLE, 1);
788 1.15 reinoud vq->vq_notify_off = bus_space_read_2(iot, ioh,
789 1.52 riastrad VIRTIO_CONFIG1_QUEUE_NOTIFY_OFF);
790 1.15 reinoud }
791 1.4 jakllsch
792 1.4 jakllsch if (psc->sc_ihs_num > 1) {
793 1.4 jakllsch int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
794 1.39 yamaguch if (psc->sc_intr_pervq)
795 1.4 jakllsch vec += idx;
796 1.15 reinoud bus_space_write_2(iot, ioh,
797 1.52 riastrad VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR, vec);
798 1.4 jakllsch }
799 1.4 jakllsch }
800 1.4 jakllsch
801 1.4 jakllsch static void
802 1.15 reinoud virtio_pci_set_status_10(struct virtio_softc *sc, int status)
803 1.4 jakllsch {
804 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
805 1.15 reinoud bus_space_tag_t iot = psc->sc_iot;
806 1.15 reinoud bus_space_handle_t ioh = psc->sc_ioh;
807 1.4 jakllsch int old = 0;
808 1.4 jakllsch
809 1.15 reinoud if (status)
810 1.15 reinoud old = bus_space_read_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS);
811 1.52 riastrad bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
812 1.52 riastrad status | old);
813 1.15 reinoud }
814 1.15 reinoud
815 1.15 reinoud void
816 1.52 riastrad virtio_pci_negotiate_features_10(struct virtio_softc *sc,
817 1.52 riastrad uint64_t guest_features)
818 1.15 reinoud {
819 1.15 reinoud struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
820 1.15 reinoud device_t self = sc->sc_dev;
821 1.15 reinoud bus_space_tag_t iot = psc->sc_iot;
822 1.15 reinoud bus_space_handle_t ioh = psc->sc_ioh;
823 1.15 reinoud uint64_t host, negotiated, device_status;
824 1.15 reinoud
825 1.15 reinoud guest_features |= VIRTIO_F_VERSION_1;
826 1.44 thorpej #ifdef __NEED_VIRTIO_F_ACCESS_PLATFORM
827 1.44 thorpej /* XXX This could use some work. */
828 1.44 thorpej guest_features |= VIRTIO_F_ACCESS_PLATFORM;
829 1.44 thorpej #endif /* __NEED_VIRTIO_F_ACCESS_PLATFORM */
830 1.15 reinoud /* notify on empty is 0.9 only */
831 1.15 reinoud guest_features &= ~VIRTIO_F_NOTIFY_ON_EMPTY;
832 1.15 reinoud sc->sc_active_features = 0;
833 1.15 reinoud
834 1.15 reinoud bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE_SELECT, 0);
835 1.15 reinoud host = bus_space_read_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE);
836 1.15 reinoud bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE_SELECT, 1);
837 1.52 riastrad host |= (uint64_t)bus_space_read_4(iot, ioh,
838 1.52 riastrad VIRTIO_CONFIG1_DEVICE_FEATURE) << 32;
839 1.15 reinoud
840 1.15 reinoud negotiated = host & guest_features;
841 1.15 reinoud
842 1.15 reinoud bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE_SELECT, 0);
843 1.15 reinoud bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE,
844 1.52 riastrad negotiated & 0xffffffff);
845 1.15 reinoud bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE_SELECT, 1);
846 1.15 reinoud bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE,
847 1.52 riastrad negotiated >> 32);
848 1.15 reinoud virtio_pci_set_status_10(sc, VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK);
849 1.15 reinoud
850 1.52 riastrad device_status = bus_space_read_1(iot, ioh,
851 1.52 riastrad VIRTIO_CONFIG1_DEVICE_STATUS);
852 1.15 reinoud if ((device_status & VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK) == 0) {
853 1.15 reinoud aprint_error_dev(self, "feature negotiation failed\n");
854 1.15 reinoud bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
855 1.52 riastrad VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
856 1.15 reinoud return;
857 1.15 reinoud }
858 1.15 reinoud
859 1.15 reinoud if ((negotiated & VIRTIO_F_VERSION_1) == 0) {
860 1.15 reinoud aprint_error_dev(self, "host rejected version 1\n");
861 1.15 reinoud bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
862 1.52 riastrad VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
863 1.15 reinoud return;
864 1.4 jakllsch }
865 1.15 reinoud
866 1.15 reinoud sc->sc_active_features = negotiated;
867 1.15 reinoud return;
868 1.15 reinoud }
869 1.15 reinoud
870 1.15 reinoud /* -------------------------------------
871 1.15 reinoud * Generic PCI interrupt code
872 1.15 reinoud * -------------------------------------*/
873 1.15 reinoud
874 1.15 reinoud static int
875 1.32 yamaguch virtio_pci_setup_interrupts_10(struct virtio_softc *sc, int reinit)
876 1.4 jakllsch {
877 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
878 1.15 reinoud bus_space_tag_t iot = psc->sc_iot;
879 1.15 reinoud bus_space_handle_t ioh = psc->sc_ioh;
880 1.15 reinoud int vector, ret, qid;
881 1.15 reinoud
882 1.31 yamaguch if (!virtio_pci_msix_enabled(psc))
883 1.31 yamaguch return 0;
884 1.31 yamaguch
885 1.15 reinoud vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
886 1.52 riastrad bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR, vector);
887 1.15 reinoud ret = bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR);
888 1.15 reinoud if (ret != vector) {
889 1.52 riastrad VIRTIO_PCI_LOG(sc, reinit, "can't set config msix vector\n");
890 1.15 reinoud return -1;
891 1.15 reinoud }
892 1.15 reinoud
893 1.15 reinoud for (qid = 0; qid < sc->sc_nvqs; qid++) {
894 1.15 reinoud vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
895 1.4 jakllsch
896 1.39 yamaguch if (psc->sc_intr_pervq)
897 1.15 reinoud vector += qid;
898 1.15 reinoud bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, qid);
899 1.15 reinoud bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR,
900 1.52 riastrad vector);
901 1.15 reinoud ret = bus_space_read_2(iot, ioh,
902 1.52 riastrad VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR);
903 1.15 reinoud if (ret != vector) {
904 1.33 yamaguch VIRTIO_PCI_LOG(sc, reinit, "can't set queue %d "
905 1.33 yamaguch "msix vector\n", qid);
906 1.15 reinoud return -1;
907 1.15 reinoud }
908 1.15 reinoud }
909 1.4 jakllsch
910 1.15 reinoud return 0;
911 1.4 jakllsch }
912 1.4 jakllsch
913 1.4 jakllsch static int
914 1.32 yamaguch virtio_pci_setup_interrupts_09(struct virtio_softc *sc, int reinit)
915 1.4 jakllsch {
916 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
917 1.4 jakllsch int offset, vector, ret, qid;
918 1.4 jakllsch
919 1.31 yamaguch if (!virtio_pci_msix_enabled(psc))
920 1.31 yamaguch return 0;
921 1.31 yamaguch
922 1.4 jakllsch offset = VIRTIO_CONFIG_MSI_CONFIG_VECTOR;
923 1.4 jakllsch vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
924 1.4 jakllsch
925 1.15 reinoud bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, vector);
926 1.15 reinoud ret = bus_space_read_2(psc->sc_iot, psc->sc_ioh, offset);
927 1.15 reinoud if (ret != vector) {
928 1.38 riastrad aprint_debug_dev(sc->sc_dev, "%s: expected=%d, actual=%d\n",
929 1.38 riastrad __func__, vector, ret);
930 1.33 yamaguch VIRTIO_PCI_LOG(sc, reinit,
931 1.33 yamaguch "can't set config msix vector\n");
932 1.4 jakllsch return -1;
933 1.15 reinoud }
934 1.4 jakllsch
935 1.4 jakllsch for (qid = 0; qid < sc->sc_nvqs; qid++) {
936 1.4 jakllsch offset = VIRTIO_CONFIG_QUEUE_SELECT;
937 1.15 reinoud bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, qid);
938 1.4 jakllsch
939 1.4 jakllsch offset = VIRTIO_CONFIG_MSI_QUEUE_VECTOR;
940 1.4 jakllsch vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
941 1.4 jakllsch
942 1.39 yamaguch if (psc->sc_intr_pervq)
943 1.6 yamaguch vector += qid;
944 1.6 yamaguch
945 1.15 reinoud bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, vector);
946 1.15 reinoud ret = bus_space_read_2(psc->sc_iot, psc->sc_ioh, offset);
947 1.15 reinoud if (ret != vector) {
948 1.38 riastrad aprint_debug_dev(sc->sc_dev, "%s[qid=%d]:"
949 1.38 riastrad " expected=%d, actual=%d\n",
950 1.38 riastrad __func__, qid, vector, ret);
951 1.33 yamaguch VIRTIO_PCI_LOG(sc, reinit, "can't set queue %d "
952 1.33 yamaguch "msix vector\n", qid);
953 1.4 jakllsch return -1;
954 1.15 reinoud }
955 1.4 jakllsch }
956 1.4 jakllsch
957 1.4 jakllsch return 0;
958 1.4 jakllsch }
959 1.4 jakllsch
960 1.4 jakllsch static int
961 1.31 yamaguch virtio_pci_establish_msix_interrupts(struct virtio_softc *sc,
962 1.4 jakllsch struct pci_attach_args *pa)
963 1.4 jakllsch {
964 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
965 1.4 jakllsch device_t self = sc->sc_dev;
966 1.4 jakllsch pci_chipset_tag_t pc = pa->pa_pc;
967 1.9 yamaguch struct virtqueue *vq;
968 1.4 jakllsch char intrbuf[PCI_INTRSTR_LEN];
969 1.6 yamaguch char intr_xname[INTRDEVNAMEBUF];
970 1.4 jakllsch char const *intrstr;
971 1.6 yamaguch int idx, qid, n;
972 1.4 jakllsch
973 1.4 jakllsch idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
974 1.15 reinoud if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
975 1.4 jakllsch pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
976 1.4 jakllsch
977 1.6 yamaguch snprintf(intr_xname, sizeof(intr_xname), "%s config",
978 1.6 yamaguch device_xname(sc->sc_dev));
979 1.6 yamaguch
980 1.4 jakllsch psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
981 1.6 yamaguch sc->sc_ipl, virtio_pci_msix_config_intr, sc, intr_xname);
982 1.4 jakllsch if (psc->sc_ihs[idx] == NULL) {
983 1.52 riastrad aprint_error_dev(self,
984 1.52 riastrad "couldn't establish MSI-X for config\n");
985 1.4 jakllsch goto error;
986 1.4 jakllsch }
987 1.4 jakllsch
988 1.4 jakllsch idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
989 1.39 yamaguch if (psc->sc_intr_pervq) {
990 1.6 yamaguch for (qid = 0; qid < sc->sc_nvqs; qid++) {
991 1.6 yamaguch n = idx + qid;
992 1.9 yamaguch vq = &sc->sc_vqs[qid];
993 1.6 yamaguch
994 1.6 yamaguch snprintf(intr_xname, sizeof(intr_xname), "%s vq#%d",
995 1.6 yamaguch device_xname(sc->sc_dev), qid);
996 1.6 yamaguch
997 1.15 reinoud if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE) {
998 1.6 yamaguch pci_intr_setattr(pc, &psc->sc_ihp[n],
999 1.6 yamaguch PCI_INTR_MPSAFE, true);
1000 1.6 yamaguch }
1001 1.6 yamaguch
1002 1.52 riastrad psc->sc_ihs[n] = pci_intr_establish_xname(pc,
1003 1.52 riastrad psc->sc_ihp[n], sc->sc_ipl,
1004 1.52 riastrad vq->vq_intrhand, vq->vq_intrhand_arg, intr_xname);
1005 1.6 yamaguch if (psc->sc_ihs[n] == NULL) {
1006 1.52 riastrad aprint_error_dev(self,
1007 1.52 riastrad "couldn't establish MSI-X for a vq\n");
1008 1.6 yamaguch goto error;
1009 1.6 yamaguch }
1010 1.6 yamaguch }
1011 1.6 yamaguch } else {
1012 1.52 riastrad if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE) {
1013 1.52 riastrad pci_intr_setattr(pc, &psc->sc_ihp[idx],
1014 1.52 riastrad PCI_INTR_MPSAFE, true);
1015 1.52 riastrad }
1016 1.4 jakllsch
1017 1.6 yamaguch snprintf(intr_xname, sizeof(intr_xname), "%s queues",
1018 1.6 yamaguch device_xname(sc->sc_dev));
1019 1.52 riastrad psc->sc_ihs[idx] = pci_intr_establish_xname(pc,
1020 1.52 riastrad psc->sc_ihp[idx], sc->sc_ipl,
1021 1.52 riastrad virtio_pci_msix_queue_intr, sc, intr_xname);
1022 1.6 yamaguch if (psc->sc_ihs[idx] == NULL) {
1023 1.52 riastrad aprint_error_dev(self,
1024 1.52 riastrad "couldn't establish MSI-X for queues\n");
1025 1.6 yamaguch goto error;
1026 1.6 yamaguch }
1027 1.4 jakllsch }
1028 1.4 jakllsch
1029 1.4 jakllsch idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
1030 1.52 riastrad intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf,
1031 1.52 riastrad sizeof(intrbuf));
1032 1.4 jakllsch aprint_normal_dev(self, "config interrupting at %s\n", intrstr);
1033 1.4 jakllsch idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
1034 1.39 yamaguch if (psc->sc_intr_pervq) {
1035 1.6 yamaguch kcpuset_t *affinity;
1036 1.6 yamaguch int affinity_to, r;
1037 1.6 yamaguch
1038 1.6 yamaguch kcpuset_create(&affinity, false);
1039 1.6 yamaguch
1040 1.6 yamaguch for (qid = 0; qid < sc->sc_nvqs; qid++) {
1041 1.6 yamaguch n = idx + qid;
1042 1.6 yamaguch affinity_to = (qid / 2) % ncpu;
1043 1.6 yamaguch
1044 1.6 yamaguch intrstr = pci_intr_string(pc, psc->sc_ihp[n],
1045 1.6 yamaguch intrbuf, sizeof(intrbuf));
1046 1.6 yamaguch
1047 1.6 yamaguch kcpuset_zero(affinity);
1048 1.6 yamaguch kcpuset_set(affinity, affinity_to);
1049 1.52 riastrad r = interrupt_distribute(psc->sc_ihs[n], affinity,
1050 1.52 riastrad NULL);
1051 1.6 yamaguch if (r == 0) {
1052 1.6 yamaguch aprint_normal_dev(self,
1053 1.52 riastrad "for vq #%d interrupting at %s"
1054 1.52 riastrad " affinity to %u\n",
1055 1.6 yamaguch qid, intrstr, affinity_to);
1056 1.6 yamaguch } else {
1057 1.6 yamaguch aprint_normal_dev(self,
1058 1.6 yamaguch "for vq #%d interrupting at %s\n",
1059 1.6 yamaguch qid, intrstr);
1060 1.6 yamaguch }
1061 1.6 yamaguch }
1062 1.6 yamaguch
1063 1.6 yamaguch kcpuset_destroy(affinity);
1064 1.6 yamaguch } else {
1065 1.52 riastrad intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf,
1066 1.52 riastrad sizeof(intrbuf));
1067 1.52 riastrad aprint_normal_dev(self, "queues interrupting at %s\n",
1068 1.52 riastrad intrstr);
1069 1.6 yamaguch }
1070 1.4 jakllsch
1071 1.4 jakllsch return 0;
1072 1.4 jakllsch
1073 1.4 jakllsch error:
1074 1.4 jakllsch idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
1075 1.4 jakllsch if (psc->sc_ihs[idx] != NULL)
1076 1.4 jakllsch pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
1077 1.4 jakllsch idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
1078 1.39 yamaguch if (psc->sc_intr_pervq) {
1079 1.6 yamaguch for (qid = 0; qid < sc->sc_nvqs; qid++) {
1080 1.6 yamaguch n = idx + qid;
1081 1.6 yamaguch if (psc->sc_ihs[n] == NULL)
1082 1.6 yamaguch continue;
1083 1.52 riastrad pci_intr_disestablish(psc->sc_pa.pa_pc,
1084 1.52 riastrad psc->sc_ihs[n]);
1085 1.6 yamaguch }
1086 1.6 yamaguch
1087 1.6 yamaguch } else {
1088 1.52 riastrad if (psc->sc_ihs[idx] != NULL) {
1089 1.52 riastrad pci_intr_disestablish(psc->sc_pa.pa_pc,
1090 1.52 riastrad psc->sc_ihs[idx]);
1091 1.52 riastrad }
1092 1.6 yamaguch }
1093 1.4 jakllsch
1094 1.4 jakllsch return -1;
1095 1.4 jakllsch }
1096 1.4 jakllsch
1097 1.4 jakllsch static int
1098 1.31 yamaguch virtio_pci_establish_intx_interrupt(struct virtio_softc *sc,
1099 1.4 jakllsch struct pci_attach_args *pa)
1100 1.4 jakllsch {
1101 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
1102 1.4 jakllsch device_t self = sc->sc_dev;
1103 1.4 jakllsch pci_chipset_tag_t pc = pa->pa_pc;
1104 1.4 jakllsch char intrbuf[PCI_INTRSTR_LEN];
1105 1.4 jakllsch char const *intrstr;
1106 1.4 jakllsch
1107 1.15 reinoud if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
1108 1.4 jakllsch pci_intr_setattr(pc, &psc->sc_ihp[0], PCI_INTR_MPSAFE, true);
1109 1.4 jakllsch
1110 1.4 jakllsch psc->sc_ihs[0] = pci_intr_establish_xname(pc, psc->sc_ihp[0],
1111 1.4 jakllsch sc->sc_ipl, virtio_pci_intr, sc, device_xname(sc->sc_dev));
1112 1.4 jakllsch if (psc->sc_ihs[0] == NULL) {
1113 1.4 jakllsch aprint_error_dev(self, "couldn't establish INTx\n");
1114 1.4 jakllsch return -1;
1115 1.4 jakllsch }
1116 1.4 jakllsch
1117 1.52 riastrad intrstr = pci_intr_string(pc, psc->sc_ihp[0], intrbuf,
1118 1.52 riastrad sizeof(intrbuf));
1119 1.4 jakllsch aprint_normal_dev(self, "interrupting at %s\n", intrstr);
1120 1.4 jakllsch
1121 1.4 jakllsch return 0;
1122 1.4 jakllsch }
1123 1.4 jakllsch
1124 1.4 jakllsch static int
1125 1.31 yamaguch virtio_pci_alloc_interrupts(struct virtio_softc *sc)
1126 1.4 jakllsch {
1127 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
1128 1.4 jakllsch device_t self = sc->sc_dev;
1129 1.4 jakllsch pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
1130 1.13 jakllsch pcitag_t tag = psc->sc_pa.pa_tag;
1131 1.4 jakllsch int error;
1132 1.4 jakllsch int nmsix;
1133 1.13 jakllsch int off;
1134 1.4 jakllsch int counts[PCI_INTR_TYPE_SIZE];
1135 1.4 jakllsch pci_intr_type_t max_type;
1136 1.13 jakllsch pcireg_t ctl;
1137 1.4 jakllsch
1138 1.4 jakllsch nmsix = pci_msix_count(psc->sc_pa.pa_pc, psc->sc_pa.pa_tag);
1139 1.4 jakllsch aprint_debug_dev(self, "pci_msix_count=%d\n", nmsix);
1140 1.4 jakllsch
1141 1.4 jakllsch /* We need at least two: one for config and the other for queues */
1142 1.15 reinoud if ((sc->sc_flags & VIRTIO_F_INTR_MSIX) == 0 || nmsix < 2) {
1143 1.4 jakllsch /* Try INTx only */
1144 1.4 jakllsch max_type = PCI_INTR_TYPE_INTX;
1145 1.4 jakllsch counts[PCI_INTR_TYPE_INTX] = 1;
1146 1.4 jakllsch } else {
1147 1.4 jakllsch /* Try MSI-X first and INTx second */
1148 1.39 yamaguch if (ISSET(sc->sc_flags, VIRTIO_F_INTR_PERVQ) &&
1149 1.39 yamaguch sc->sc_nvqs + VIRTIO_MSIX_QUEUE_VECTOR_INDEX <= nmsix) {
1150 1.11 yamaguch nmsix = sc->sc_nvqs + VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
1151 1.11 yamaguch } else {
1152 1.6 yamaguch nmsix = 2;
1153 1.6 yamaguch }
1154 1.6 yamaguch
1155 1.4 jakllsch max_type = PCI_INTR_TYPE_MSIX;
1156 1.6 yamaguch counts[PCI_INTR_TYPE_MSIX] = nmsix;
1157 1.4 jakllsch counts[PCI_INTR_TYPE_MSI] = 0;
1158 1.4 jakllsch counts[PCI_INTR_TYPE_INTX] = 1;
1159 1.4 jakllsch }
1160 1.4 jakllsch
1161 1.4 jakllsch retry:
1162 1.4 jakllsch error = pci_intr_alloc(&psc->sc_pa, &psc->sc_ihp, counts, max_type);
1163 1.4 jakllsch if (error != 0) {
1164 1.4 jakllsch aprint_error_dev(self, "couldn't map interrupt\n");
1165 1.4 jakllsch return -1;
1166 1.4 jakllsch }
1167 1.4 jakllsch
1168 1.4 jakllsch if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX) {
1169 1.39 yamaguch psc->sc_intr_pervq = nmsix > 2 ? true : false;
1170 1.12 jakllsch psc->sc_ihs = kmem_zalloc(sizeof(*psc->sc_ihs) * nmsix,
1171 1.4 jakllsch KM_SLEEP);
1172 1.4 jakllsch
1173 1.31 yamaguch error = virtio_pci_establish_msix_interrupts(sc, &psc->sc_pa);
1174 1.4 jakllsch if (error != 0) {
1175 1.6 yamaguch kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * nmsix);
1176 1.6 yamaguch pci_intr_release(pc, psc->sc_ihp, nmsix);
1177 1.4 jakllsch
1178 1.4 jakllsch /* Retry INTx */
1179 1.4 jakllsch max_type = PCI_INTR_TYPE_INTX;
1180 1.4 jakllsch counts[PCI_INTR_TYPE_INTX] = 1;
1181 1.4 jakllsch goto retry;
1182 1.4 jakllsch }
1183 1.4 jakllsch
1184 1.6 yamaguch psc->sc_ihs_num = nmsix;
1185 1.15 reinoud psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_MSI;
1186 1.15 reinoud virtio_pci_adjust_config_region(psc);
1187 1.4 jakllsch } else if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_INTX) {
1188 1.39 yamaguch psc->sc_intr_pervq = false;
1189 1.12 jakllsch psc->sc_ihs = kmem_zalloc(sizeof(*psc->sc_ihs) * 1,
1190 1.4 jakllsch KM_SLEEP);
1191 1.4 jakllsch
1192 1.31 yamaguch error = virtio_pci_establish_intx_interrupt(sc, &psc->sc_pa);
1193 1.4 jakllsch if (error != 0) {
1194 1.4 jakllsch kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * 1);
1195 1.4 jakllsch pci_intr_release(pc, psc->sc_ihp, 1);
1196 1.4 jakllsch return -1;
1197 1.4 jakllsch }
1198 1.4 jakllsch
1199 1.4 jakllsch psc->sc_ihs_num = 1;
1200 1.15 reinoud psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
1201 1.15 reinoud virtio_pci_adjust_config_region(psc);
1202 1.13 jakllsch
1203 1.14 jakllsch error = pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL);
1204 1.13 jakllsch if (error != 0) {
1205 1.13 jakllsch ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
1206 1.13 jakllsch ctl &= ~PCI_MSIX_CTL_ENABLE;
1207 1.13 jakllsch pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
1208 1.13 jakllsch }
1209 1.4 jakllsch }
1210 1.4 jakllsch
1211 1.39 yamaguch if (!psc->sc_intr_pervq)
1212 1.39 yamaguch CLR(sc->sc_flags, VIRTIO_F_INTR_PERVQ);
1213 1.4 jakllsch return 0;
1214 1.4 jakllsch }
1215 1.4 jakllsch
1216 1.4 jakllsch static void
1217 1.4 jakllsch virtio_pci_free_interrupts(struct virtio_softc *sc)
1218 1.4 jakllsch {
1219 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
1220 1.4 jakllsch
1221 1.4 jakllsch for (int i = 0; i < psc->sc_ihs_num; i++) {
1222 1.4 jakllsch if (psc->sc_ihs[i] == NULL)
1223 1.4 jakllsch continue;
1224 1.4 jakllsch pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[i]);
1225 1.4 jakllsch psc->sc_ihs[i] = NULL;
1226 1.4 jakllsch }
1227 1.4 jakllsch
1228 1.52 riastrad if (psc->sc_ihs_num > 0) {
1229 1.52 riastrad pci_intr_release(psc->sc_pa.pa_pc, psc->sc_ihp,
1230 1.52 riastrad psc->sc_ihs_num);
1231 1.52 riastrad }
1232 1.4 jakllsch
1233 1.4 jakllsch if (psc->sc_ihs != NULL) {
1234 1.4 jakllsch kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * psc->sc_ihs_num);
1235 1.4 jakllsch psc->sc_ihs = NULL;
1236 1.4 jakllsch }
1237 1.4 jakllsch psc->sc_ihs_num = 0;
1238 1.4 jakllsch }
1239 1.4 jakllsch
1240 1.31 yamaguch static bool
1241 1.31 yamaguch virtio_pci_msix_enabled(struct virtio_pci_softc *psc)
1242 1.31 yamaguch {
1243 1.31 yamaguch pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
1244 1.31 yamaguch
1245 1.31 yamaguch if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX)
1246 1.31 yamaguch return true;
1247 1.31 yamaguch
1248 1.31 yamaguch return false;
1249 1.31 yamaguch }
1250 1.31 yamaguch
1251 1.4 jakllsch /*
1252 1.4 jakllsch * Interrupt handler.
1253 1.4 jakllsch */
1254 1.4 jakllsch static int
1255 1.4 jakllsch virtio_pci_intr(void *arg)
1256 1.4 jakllsch {
1257 1.4 jakllsch struct virtio_softc *sc = arg;
1258 1.4 jakllsch struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
1259 1.4 jakllsch int isr, r = 0;
1260 1.4 jakllsch
1261 1.4 jakllsch /* check and ack the interrupt */
1262 1.15 reinoud isr = bus_space_read_1(psc->sc_isr_iot, psc->sc_isr_ioh, 0);
1263 1.4 jakllsch if (isr == 0)
1264 1.4 jakllsch return 0;
1265 1.4 jakllsch if ((isr & VIRTIO_CONFIG_ISR_CONFIG_CHANGE) &&
1266 1.4 jakllsch (sc->sc_config_change != NULL))
1267 1.4 jakllsch r = (sc->sc_config_change)(sc);
1268 1.4 jakllsch if (sc->sc_intrhand != NULL) {
1269 1.4 jakllsch if (sc->sc_soft_ih != NULL)
1270 1.4 jakllsch softint_schedule(sc->sc_soft_ih);
1271 1.4 jakllsch else
1272 1.4 jakllsch r |= (sc->sc_intrhand)(sc);
1273 1.4 jakllsch }
1274 1.4 jakllsch
1275 1.4 jakllsch return r;
1276 1.4 jakllsch }
1277 1.4 jakllsch
1278 1.4 jakllsch static int
1279 1.4 jakllsch virtio_pci_msix_queue_intr(void *arg)
1280 1.4 jakllsch {
1281 1.4 jakllsch struct virtio_softc *sc = arg;
1282 1.4 jakllsch int r = 0;
1283 1.4 jakllsch
1284 1.4 jakllsch if (sc->sc_intrhand != NULL) {
1285 1.4 jakllsch if (sc->sc_soft_ih != NULL)
1286 1.4 jakllsch softint_schedule(sc->sc_soft_ih);
1287 1.4 jakllsch else
1288 1.4 jakllsch r |= (sc->sc_intrhand)(sc);
1289 1.4 jakllsch }
1290 1.4 jakllsch
1291 1.4 jakllsch return r;
1292 1.4 jakllsch }
1293 1.4 jakllsch
1294 1.4 jakllsch static int
1295 1.4 jakllsch virtio_pci_msix_config_intr(void *arg)
1296 1.4 jakllsch {
1297 1.4 jakllsch struct virtio_softc *sc = arg;
1298 1.4 jakllsch int r = 0;
1299 1.4 jakllsch
1300 1.4 jakllsch if (sc->sc_config_change != NULL)
1301 1.4 jakllsch r = (sc->sc_config_change)(sc);
1302 1.4 jakllsch return r;
1303 1.4 jakllsch }
1304 1.5 jakllsch
1305 1.5 jakllsch MODULE(MODULE_CLASS_DRIVER, virtio_pci, "pci,virtio");
1306 1.5 jakllsch
1307 1.5 jakllsch #ifdef _MODULE
1308 1.5 jakllsch #include "ioconf.c"
1309 1.5 jakllsch #endif
1310 1.5 jakllsch
1311 1.5 jakllsch static int
1312 1.5 jakllsch virtio_pci_modcmd(modcmd_t cmd, void *opaque)
1313 1.5 jakllsch {
1314 1.5 jakllsch int error = 0;
1315 1.5 jakllsch
1316 1.5 jakllsch #ifdef _MODULE
1317 1.5 jakllsch switch (cmd) {
1318 1.5 jakllsch case MODULE_CMD_INIT:
1319 1.5 jakllsch error = config_init_component(cfdriver_ioconf_virtio_pci,
1320 1.5 jakllsch cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
1321 1.5 jakllsch break;
1322 1.5 jakllsch case MODULE_CMD_FINI:
1323 1.5 jakllsch error = config_fini_component(cfdriver_ioconf_virtio_pci,
1324 1.5 jakllsch cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
1325 1.5 jakllsch break;
1326 1.5 jakllsch default:
1327 1.5 jakllsch error = ENOTTY;
1328 1.5 jakllsch break;
1329 1.5 jakllsch }
1330 1.5 jakllsch #endif
1331 1.5 jakllsch
1332 1.5 jakllsch return error;
1333 1.5 jakllsch }
1334