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