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