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