xref: /netbsd-src/sys/arch/sparc64/dev/vbus.c (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /*	$NetBSD: vbus.c,v 1.5 2021/01/04 14:48:52 thorpej Exp $	*/
2 /*	$OpenBSD: vbus.c,v 1.8 2015/09/27 11:29:20 kettenis Exp $	*/
3 /*
4  * Copyright (c) 2008 Mark Kettenis
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/device.h>
21 #include <sys/malloc.h>
22 #include <sys/kmem.h>
23 #include <sys/systm.h>
24 
25 #include <machine/autoconf.h>
26 #include <machine/hypervisor.h>
27 #include <machine/openfirm.h>
28 
29 #include <sparc64/dev/vbusvar.h>
30 
31 #include <sparc64/dev/iommureg.h>
32 
33 #include <dev/clock_subr.h>
34 extern todr_chip_handle_t todr_handle;
35 
36 #ifdef DEBUG
37 #define VBUS_INTR             0x01
38 int vbus_debug = 0x00|VBUS_INTR;
39 #define DPRINTF(l, s)   do { if (vbus_debug & l) printf s; } while (0)
40 #else
41 #define DPRINTF(l, s)
42 #endif
43 
44 struct vbus_softc {
45 	device_t		sc_dv;
46 	bus_space_tag_t		sc_bustag;
47 	bus_dma_tag_t		sc_dmatag;
48 };
49 int	vbus_cmp_cells(int *, int *, int *, int);
50 int	vbus_match(device_t, cfdata_t, void *);
51 void	vbus_attach(device_t, device_t, void *);
52 int	vbus_print(void *, const char *);
53 
54 CFATTACH_DECL_NEW(vbus, sizeof(struct vbus_softc),
55     vbus_match, vbus_attach, NULL, NULL);
56 
57 void *vbus_intr_establish(bus_space_tag_t, int, int,
58     int (*)(void *), void *, void (*)(void));
59 void	vbus_intr_ack(struct intrhand *);
60 bus_space_tag_t vbus_alloc_bus_tag(struct vbus_softc *, bus_space_tag_t);
61 
62 int
63 vbus_match(device_t parent, cfdata_t match, void *aux)
64 {
65 	struct mainbus_attach_args *ma = aux;
66 
67 	if (strcmp(ma->ma_name, "virtual-devices") == 0)
68 		return (1);
69 
70 	return (0);
71 }
72 
73 void
74 vbus_attach(device_t parent, device_t self, void *aux)
75 {
76         struct vbus_softc *sc = device_private(self);
77 	struct mainbus_attach_args *ma = aux;
78 	int node;
79 
80 	sc->sc_bustag = vbus_alloc_bus_tag(sc, ma->ma_bustag);
81 	sc->sc_dmatag = ma->ma_dmatag;
82 	printf("\n");
83 
84 	for (node = OF_child(ma->ma_node); node; node = OF_peer(node)) {
85 		struct vbus_attach_args va;
86 		char buf[32];
87 
88 		bzero(&va, sizeof(va));
89 		va.va_node = node;
90 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
91 			continue;
92 		va.va_name = buf;
93 		va.va_bustag = sc->sc_bustag;
94 		va.va_dmatag = sc->sc_dmatag;
95 		prom_getprop(node, "reg", sizeof(*va.va_reg),
96 			     &va.va_nreg, (void **)&va.va_reg);
97 		prom_getprop(node, "interrupts", sizeof(*va.va_intr),
98 			     &va.va_nintr, (void **)&va.va_intr);
99 		config_found(self, &va, vbus_print);
100 	}
101 
102 	struct vbus_attach_args va;
103 	bzero(&va, sizeof(va));
104 	va.va_name = "rtc";
105 	config_found(self, &va, vbus_print);
106 
107 }
108 
109 int
110 vbus_print(void *aux, const char *name)
111 {
112 	struct vbus_attach_args *va = aux;
113 
114 	if (name)
115 		printf("\"%s\" at %s", va->va_name, name);
116 	return (UNCONF);
117 }
118 
119 /*
120  * Compare a sequence of cells with a mask, return 1 if they match and
121  * 0 if they don't.
122  */
123 int
124 vbus_cmp_cells(int *cell1, int *cell2, int *mask, int ncells)
125 {
126 	int i;
127 
128 	for (i = 0; i < ncells; i++) {
129 		if (((cell1[i] ^ cell2[i]) & mask[i]) != 0)
130 			return (0);
131 	}
132 	return (1);
133 }
134 
135 int
136 vbus_intr_map(int node, int ino, uint64_t *sysino)
137 {
138 	int *imap = NULL, nimap;
139 	int *reg = NULL, nreg;
140 	int *imap_mask;
141 	int parent;
142 	int address_cells, interrupt_cells;
143 	uint64_t devhandle;
144 	uint64_t devino;
145 	int len;
146 	int err;
147 
148 	DPRINTF(VBUS_INTR, ("vbus_intr_map(): ino 0x%x\n", ino));
149 
150 	parent = OF_parent(node);
151 
152 	address_cells = prom_getpropint(parent, "#address-cells", 2);
153 	interrupt_cells = prom_getpropint(parent, "#interrupt-cells", 1);
154 	KASSERT(interrupt_cells == 1);
155 
156 	len = OF_getproplen(parent, "interrupt-map-mask");
157 	if (len < (address_cells + interrupt_cells) * sizeof(int))
158 		return (-1);
159 	imap_mask = malloc(len, M_DEVBUF, M_WAITOK);
160 	if (OF_getprop(parent, "interrupt-map-mask", imap_mask, len) != len)
161 		goto out;
162 
163 	if (prom_getprop(parent, "interrupt-map", sizeof(int), &nimap, (void **)&imap))
164 		panic("vbus: can't get interrupt-map");
165 
166 	if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)&reg))
167 		panic("vbus: can't get reg");
168 	if (nreg < address_cells)
169 		goto out;
170 
171 	while (nimap >= address_cells + interrupt_cells + 2) {
172 		if (vbus_cmp_cells(imap, reg, imap_mask, address_cells) &&
173 		    vbus_cmp_cells(&imap[address_cells], &ino,
174 		    &imap_mask[address_cells], interrupt_cells)) {
175 			node = imap[address_cells + interrupt_cells];
176 			devino = imap[address_cells + interrupt_cells + 1];
177 
178 			free(reg, M_DEVBUF);
179 			reg = NULL;
180 
181 			if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)&reg))
182 				panic("vbus: can't get reg");
183 
184 			devhandle = reg[0] & 0x0fffffff;
185 
186 			err = hv_intr_devino_to_sysino(devhandle, devino, sysino);
187 			if (err != H_EOK)
188 				goto out;
189 
190 			KASSERT(*sysino == INTVEC(*sysino));
191 			free(imap_mask, M_DEVBUF);
192 			return (0);
193 		}
194 		imap += address_cells + interrupt_cells + 2;
195 		nimap -= address_cells + interrupt_cells + 2;
196 	}
197 
198 out:
199 	free(imap_mask, M_DEVBUF);
200 	return (-1);
201 }
202 
203 void *
204 vbus_intr_establish(bus_space_tag_t t, int ihandle, int level,
205 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
206 {
207 	uint64_t sysino = INTVEC(ihandle);
208 	struct intrhand *ih;
209 	int ino;
210 	int err;
211 
212 	DPRINTF(VBUS_INTR, ("vbus_intr_establish()\n"));
213 
214 	ino = INTINO(ihandle);
215 
216 	ih = intrhand_alloc();
217 
218 	ih->ih_ivec = ihandle;
219 	ih->ih_fun = handler;
220 	ih->ih_arg = arg;
221 	ih->ih_pil = level;
222 	ih->ih_number = ino;
223 	ih->ih_pending = 0;
224 
225 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
226 	ih->ih_ack = vbus_intr_ack;
227 
228 	err = hv_intr_settarget(sysino, cpus->ci_cpuid);
229 	if (err != H_EOK) {
230 		printf("hv_intr_settarget(%lu, %u) failed - err = %d\n",
231 		       (long unsigned int)sysino, cpus->ci_cpuid, err);
232 		return (NULL);
233 	}
234 
235 	/* Clear pending interrupts. */
236 	err = hv_intr_setstate(sysino, INTR_IDLE);
237 	if (err != H_EOK) {
238 	  printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n",
239 		 (long unsigned int)sysino, err);
240 	  return (NULL);
241 	}
242 
243 	err = hv_intr_setenabled(sysino, INTR_ENABLED);
244 	if (err != H_EOK) {
245 	  printf("hv_intr_setenabled(%lu) failed - err = %d\n",
246 		 (long unsigned int)sysino, err);
247 	  return (NULL);
248 	}
249 
250 	return (ih);
251 }
252 
253 void
254 vbus_intr_ack(struct intrhand *ih)
255 {
256 	DPRINTF(VBUS_INTR, ("vbus_intr_ack()\n"));
257 	hv_intr_setstate(ih->ih_number, INTR_IDLE);
258 }
259 
260 bus_space_tag_t
261 vbus_alloc_bus_tag(struct vbus_softc *sc, bus_space_tag_t parent)
262 {
263 	struct sparc_bus_space_tag *bt;
264 
265 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
266 	bt->cookie = sc;
267 	bt->parent = parent;
268 	bt->sparc_bus_map = parent->sparc_bus_map;
269 	bt->sparc_intr_establish = vbus_intr_establish;
270 
271 	return (bt);
272 }
273