virtio_mmio_fdt.c revision 1.11 1 /* $NetBSD: virtio_mmio_fdt.c,v 1.11 2025/01/14 16:46:38 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2018 Jonathan A. Kollasch
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: virtio_mmio_fdt.c,v 1.11 2025/01/14 16:46:38 riastradh Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34
35 #include <sys/device.h>
36
37 #include <dev/fdt/fdtvar.h>
38
39 #define VIRTIO_PRIVATE
40 #include <dev/virtio/virtio_mmiovar.h>
41
42 static int virtio_mmio_fdt_match(device_t, cfdata_t, void *);
43 static void virtio_mmio_fdt_attach(device_t, device_t, void *);
44 static int virtio_mmio_fdt_rescan(device_t, const char *, const int *);
45 static int virtio_mmio_fdt_detach(device_t, int);
46
47 static int virtio_mmio_fdt_alloc_interrupts(struct virtio_mmio_softc *);
48 static void virtio_mmio_fdt_free_interrupts(struct virtio_mmio_softc *);
49
50 struct virtio_mmio_fdt_softc {
51 struct virtio_mmio_softc sc_msc;
52 int sc_phandle;
53 };
54
55 CFATTACH_DECL3_NEW(virtio_mmio_fdt, sizeof(struct virtio_mmio_fdt_softc),
56 virtio_mmio_fdt_match, virtio_mmio_fdt_attach, virtio_mmio_fdt_detach, NULL,
57 virtio_mmio_fdt_rescan, NULL, 0);
58
59 static const struct device_compatible_entry compat_data[] = {
60 { .compat = "virtio,mmio" },
61 DEVICE_COMPAT_EOL
62 };
63
64 static int
65 virtio_mmio_fdt_match(device_t parent, cfdata_t match, void *aux)
66 {
67 struct fdt_attach_args * const faa = aux;
68
69 return of_compatible_match(faa->faa_phandle, compat_data);
70 }
71
72 static void
73 virtio_mmio_fdt_attach(device_t parent, device_t self, void *aux)
74 {
75 struct virtio_mmio_fdt_softc * const fsc = device_private(self);
76 struct virtio_mmio_softc * const msc = &fsc->sc_msc;
77 struct virtio_softc * const vsc = &msc->sc_sc;
78 struct fdt_attach_args * const faa = aux;
79 bus_addr_t addr;
80 bus_size_t size;
81 int error;
82
83 aprint_normal("\n");
84 aprint_naive("\n");
85
86 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
87 aprint_error_dev(self, "couldn't get registers\n");
88 return;
89 }
90
91 fsc->sc_phandle = faa->faa_phandle;
92 msc->sc_iot = faa->faa_bst;
93 vsc->sc_dev = self;
94 vsc->sc_dmat = faa->faa_dmat;
95
96 error = bus_space_map(msc->sc_iot, addr, size, 0, &msc->sc_ioh);
97 if (error) {
98 aprint_error_dev(self, "couldn't map %#" PRIx64 ": %d",
99 (uint64_t)addr, error);
100 return;
101 }
102 msc->sc_iosize = size;
103
104 msc->sc_alloc_interrupts = virtio_mmio_fdt_alloc_interrupts;
105 msc->sc_free_interrupts = virtio_mmio_fdt_free_interrupts;
106
107 virtio_mmio_common_attach(msc);
108 virtio_mmio_fdt_rescan(self, NULL, NULL);
109 }
110
111 /* ARGSUSED */
112 static int
113 virtio_mmio_fdt_rescan(device_t self, const char *attr, const int *scan_flags)
114 {
115 struct virtio_mmio_fdt_softc * const fsc = device_private(self);
116 struct virtio_mmio_softc * const msc = &fsc->sc_msc;
117 struct virtio_softc * const vsc = &msc->sc_sc;
118 struct virtio_attach_args va;
119
120 if (vsc->sc_child) /* Child already attached? */
121 return 0;
122
123 memset(&va, 0, sizeof(va));
124 va.sc_childdevid = vsc->sc_childdevid;
125
126 config_found(self, &va, NULL, CFARGS_NONE);
127
128 if (virtio_attach_failed(vsc))
129 return 0;
130
131 return 0;
132 }
133
134 static int
135 virtio_mmio_fdt_detach(device_t self, int flags)
136 {
137 struct virtio_mmio_fdt_softc * const fsc = device_private(self);
138 struct virtio_mmio_softc * const msc = &fsc->sc_msc;
139
140 return virtio_mmio_common_detach(msc, flags);
141 }
142
143 static int
144 virtio_mmio_fdt_alloc_interrupts(struct virtio_mmio_softc *msc)
145 {
146 struct virtio_mmio_fdt_softc * const fsc = (void *)msc;
147 struct virtio_softc * const vsc = &msc->sc_sc;
148 char intrstr[128];
149 int flags = 0;
150
151 if (!fdtbus_intr_str(fsc->sc_phandle, 0, intrstr, sizeof(intrstr))) {
152 aprint_error_dev(vsc->sc_dev, "failed to decode interrupt\n");
153 return -1;
154 }
155
156 if (vsc->sc_flags & VIRTIO_F_INTR_MPSAFE)
157 flags |= FDT_INTR_MPSAFE;
158
159 msc->sc_ih = fdtbus_intr_establish_xname(fsc->sc_phandle, 0,
160 vsc->sc_ipl, flags, virtio_mmio_intr, msc,
161 device_xname(vsc->sc_dev));
162 if (msc->sc_ih == NULL) {
163 aprint_error_dev(vsc->sc_dev,
164 "failed to establish interrupt on %s\n", intrstr);
165 return -1;
166 }
167 aprint_normal_dev(vsc->sc_dev, "interrupting on %s\n", intrstr);
168
169 return 0;
170 }
171
172 static void
173 virtio_mmio_fdt_free_interrupts(struct virtio_mmio_softc *msc)
174 {
175 struct virtio_mmio_fdt_softc * const fsc = (void *)msc;
176
177 if (msc->sc_ih != NULL) {
178 fdtbus_intr_disestablish(fsc->sc_phandle, msc->sc_ih);
179 msc->sc_ih = NULL;
180 }
181 }
182