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