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