virtio_mainbus.c revision 1.1.4.2 1 1.1.4.2 thorpej /* $NetBSD: virtio_mainbus.c,v 1.1.4.2 2021/04/03 22:28:23 thorpej Exp $ */
2 1.1.4.2 thorpej
3 1.1.4.2 thorpej /*
4 1.1.4.2 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1.4.2 thorpej * All rights reserved.
6 1.1.4.2 thorpej *
7 1.1.4.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 thorpej * by Reinoud Zandijk
9 1.1.4.2 thorpej *
10 1.1.4.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 thorpej * modification, are permitted provided that the following conditions
12 1.1.4.2 thorpej * are met:
13 1.1.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.4.2 thorpej *
19 1.1.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.1.4.2 thorpej * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 1.1.4.2 thorpej * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 1.1.4.2 thorpej * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 1.1.4.2 thorpej * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 1.1.4.2 thorpej * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1.4.2 thorpej * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 1.1.4.2 thorpej * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 1.1.4.2 thorpej * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 thorpej */
31 1.1.4.2 thorpej
32 1.1.4.2 thorpej #include <sys/cdefs.h>
33 1.1.4.2 thorpej __KERNEL_RCSID(0, "$NetBSD: virtio_mainbus.c,v 1.1.4.2 2021/04/03 22:28:23 thorpej Exp $");
34 1.1.4.2 thorpej
35 1.1.4.2 thorpej #include <sys/param.h>
36 1.1.4.2 thorpej #include <sys/systm.h>
37 1.1.4.2 thorpej
38 1.1.4.2 thorpej #include <sys/device.h>
39 1.1.4.2 thorpej #include <evbmips/mipssim/autoconf.h>
40 1.1.4.2 thorpej #include <evbmips/mipssim/mipssimreg.h>
41 1.1.4.2 thorpej
42 1.1.4.2 thorpej #define VIRTIO_PRIVATE
43 1.1.4.2 thorpej #include <dev/virtio/virtio_mmiovar.h>
44 1.1.4.2 thorpej
45 1.1.4.2 thorpej
46 1.1.4.2 thorpej static int virtio_mainbus_match(device_t, cfdata_t, void *);
47 1.1.4.2 thorpej static void virtio_mainbus_attach(device_t, device_t, void *);
48 1.1.4.2 thorpej static int virtio_mainbus_rescan(device_t, const char *, const int *);
49 1.1.4.2 thorpej static int virtio_mainbus_detach(device_t, int);
50 1.1.4.2 thorpej
51 1.1.4.2 thorpej static int virtio_mainbus_setup_interrupts(struct virtio_mmio_softc *);
52 1.1.4.2 thorpej static void virtio_mainbus_free_interrupts(struct virtio_mmio_softc *);
53 1.1.4.2 thorpej
54 1.1.4.2 thorpej struct virtio_mainbus_softc {
55 1.1.4.2 thorpej struct virtio_mmio_softc sc_msc;
56 1.1.4.2 thorpej
57 1.1.4.2 thorpej int sc_irq;
58 1.1.4.2 thorpej void *sc_ih;
59 1.1.4.2 thorpej };
60 1.1.4.2 thorpej
61 1.1.4.2 thorpej
62 1.1.4.2 thorpej CFATTACH_DECL3_NEW(virtio_mainbus, sizeof(struct virtio_mainbus_softc),
63 1.1.4.2 thorpej virtio_mainbus_match, virtio_mainbus_attach,
64 1.1.4.2 thorpej virtio_mainbus_detach, NULL,
65 1.1.4.2 thorpej virtio_mainbus_rescan, (void *)voidop, DVF_DETACH_SHUTDOWN);
66 1.1.4.2 thorpej
67 1.1.4.2 thorpej
68 1.1.4.2 thorpej static int
69 1.1.4.2 thorpej virtio_mainbus_match(device_t parent, cfdata_t match, void *aux)
70 1.1.4.2 thorpej {
71 1.1.4.2 thorpej struct mainbus_attach_args *ma = aux;
72 1.1.4.2 thorpej
73 1.1.4.2 thorpej /* TODO can we check here if its present? */
74 1.1.4.2 thorpej if (strcmp(ma->ma_name, match->cf_name) == 0)
75 1.1.4.2 thorpej return (1<<1);
76 1.1.4.2 thorpej
77 1.1.4.2 thorpej return (0);
78 1.1.4.2 thorpej }
79 1.1.4.2 thorpej
80 1.1.4.2 thorpej
81 1.1.4.2 thorpej void
82 1.1.4.2 thorpej virtio_mainbus_attach(device_t parent, device_t self, void *aux)
83 1.1.4.2 thorpej {
84 1.1.4.2 thorpej struct virtio_mainbus_softc *sc = device_private(self);
85 1.1.4.2 thorpej struct virtio_mmio_softc *msc = &sc->sc_msc;
86 1.1.4.2 thorpej struct virtio_softc *vsc = &msc->sc_sc;
87 1.1.4.2 thorpej struct mainbus_attach_args *ma = aux;
88 1.1.4.2 thorpej bus_space_handle_t ioh;
89 1.1.4.2 thorpej
90 1.1.4.2 thorpej if (bus_space_map(ma->ma_iot, ma->ma_addr, VIRTIO_STRIDE, 0, &ioh) != 0) {
91 1.1.4.2 thorpej aprint_error(": can't map i/o space\n");
92 1.1.4.2 thorpej return;
93 1.1.4.2 thorpej }
94 1.1.4.2 thorpej
95 1.1.4.2 thorpej aprint_normal("\n");
96 1.1.4.2 thorpej aprint_naive("\n");
97 1.1.4.2 thorpej
98 1.1.4.2 thorpej sc->sc_irq = ma->ma_irq;
99 1.1.4.2 thorpej
100 1.1.4.2 thorpej msc->sc_iot = ma->ma_iot;
101 1.1.4.2 thorpej msc->sc_ioh = ioh;
102 1.1.4.2 thorpej msc->sc_iosize = VIRTIO_STRIDE;
103 1.1.4.2 thorpej msc->sc_setup_interrupts = virtio_mainbus_setup_interrupts;
104 1.1.4.2 thorpej msc->sc_free_interrupts = virtio_mainbus_free_interrupts;
105 1.1.4.2 thorpej
106 1.1.4.2 thorpej vsc->sc_dev = self;
107 1.1.4.2 thorpej vsc->sc_dmat = ma->ma_dmat;
108 1.1.4.2 thorpej if (virtio_mmio_common_probe_present(msc))
109 1.1.4.2 thorpej virtio_mmio_common_attach(msc);
110 1.1.4.2 thorpej
111 1.1.4.2 thorpej virtio_mainbus_rescan(self, "virtio", NULL);
112 1.1.4.2 thorpej }
113 1.1.4.2 thorpej
114 1.1.4.2 thorpej
115 1.1.4.2 thorpej /* ARGSUSED */
116 1.1.4.2 thorpej static int
117 1.1.4.2 thorpej virtio_mainbus_rescan(device_t self, const char *attr, const int *scan_flags)
118 1.1.4.2 thorpej {
119 1.1.4.2 thorpej struct virtio_mainbus_softc *sc = device_private(self);
120 1.1.4.2 thorpej struct virtio_mmio_softc *msc = &sc->sc_msc;
121 1.1.4.2 thorpej struct virtio_softc *vsc = &msc->sc_sc;
122 1.1.4.2 thorpej struct virtio_attach_args va;
123 1.1.4.2 thorpej
124 1.1.4.2 thorpej if (vsc->sc_child) /* child already attached? */
125 1.1.4.2 thorpej return 0;
126 1.1.4.2 thorpej
127 1.1.4.2 thorpej memset(&va, 0, sizeof(va));
128 1.1.4.2 thorpej va.sc_childdevid = vsc->sc_childdevid;
129 1.1.4.2 thorpej
130 1.1.4.2 thorpej config_found_ia(self, attr, &va, NULL);
131 1.1.4.2 thorpej
132 1.1.4.2 thorpej if (virtio_attach_failed(vsc))
133 1.1.4.2 thorpej return 0;
134 1.1.4.2 thorpej return 0;
135 1.1.4.2 thorpej }
136 1.1.4.2 thorpej
137 1.1.4.2 thorpej
138 1.1.4.2 thorpej static int
139 1.1.4.2 thorpej virtio_mainbus_detach(device_t self, int flags)
140 1.1.4.2 thorpej {
141 1.1.4.2 thorpej struct virtio_mainbus_softc *sc = device_private(self);
142 1.1.4.2 thorpej struct virtio_mmio_softc * const msc = &sc->sc_msc;
143 1.1.4.2 thorpej
144 1.1.4.2 thorpej return virtio_mmio_common_detach(msc, flags);
145 1.1.4.2 thorpej }
146 1.1.4.2 thorpej
147 1.1.4.2 thorpej
148 1.1.4.2 thorpej static int
149 1.1.4.2 thorpej virtio_mainbus_setup_interrupts(struct virtio_mmio_softc *msc)
150 1.1.4.2 thorpej {
151 1.1.4.2 thorpej struct virtio_mainbus_softc *sc = (struct virtio_mainbus_softc *) msc;
152 1.1.4.2 thorpej struct virtio_softc * const vsc = &msc->sc_sc;
153 1.1.4.2 thorpej
154 1.1.4.2 thorpej msc->sc_ih = evbmips_intr_establish(sc->sc_irq, virtio_mmio_intr,
155 1.1.4.2 thorpej msc);
156 1.1.4.2 thorpej if (msc->sc_ih == NULL) {
157 1.1.4.2 thorpej aprint_error_dev(vsc->sc_dev,
158 1.1.4.2 thorpej "couldn't install interrupt handler\n");
159 1.1.4.2 thorpej return -1;
160 1.1.4.2 thorpej }
161 1.1.4.2 thorpej
162 1.1.4.2 thorpej aprint_normal_dev(vsc->sc_dev, "interrupting at irq %d\n", sc->sc_irq);
163 1.1.4.2 thorpej
164 1.1.4.2 thorpej return 0;
165 1.1.4.2 thorpej }
166 1.1.4.2 thorpej
167 1.1.4.2 thorpej
168 1.1.4.2 thorpej static void
169 1.1.4.2 thorpej virtio_mainbus_free_interrupts(struct virtio_mmio_softc *msc)
170 1.1.4.2 thorpej {
171 1.1.4.2 thorpej panic("%s not implemented\n", __func__);
172 1.1.4.2 thorpej }
173