xref: /netbsd-src/sys/arch/sparc64/dev/vbus.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: vbus.c,v 1.7 2021/05/10 23:53:44 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 		    CFARG_DEVHANDLE, prom_node_to_devhandle(va.va_node),
101 		    CFARG_EOL);
102 	}
103 
104 	struct vbus_attach_args va;
105 	bzero(&va, sizeof(va));
106 	va.va_name = "rtc";
107 	config_found(self, &va, vbus_print, CFARG_EOL);
108 
109 }
110 
111 int
112 vbus_print(void *aux, const char *name)
113 {
114 	struct vbus_attach_args *va = aux;
115 
116 	if (name)
117 		printf("\"%s\" at %s", va->va_name, name);
118 	return (UNCONF);
119 }
120 
121 /*
122  * Compare a sequence of cells with a mask, return 1 if they match and
123  * 0 if they don't.
124  */
125 int
126 vbus_cmp_cells(int *cell1, int *cell2, int *mask, int ncells)
127 {
128 	int i;
129 
130 	for (i = 0; i < ncells; i++) {
131 		if (((cell1[i] ^ cell2[i]) & mask[i]) != 0)
132 			return (0);
133 	}
134 	return (1);
135 }
136 
137 int
138 vbus_intr_map(int node, int ino, uint64_t *sysino)
139 {
140 	int *imap = NULL, nimap;
141 	int *reg = NULL, nreg;
142 	int *imap_mask;
143 	int parent;
144 	int address_cells, interrupt_cells;
145 	uint64_t devhandle;
146 	uint64_t devino;
147 	int len;
148 	int err;
149 
150 	DPRINTF(VBUS_INTR, ("vbus_intr_map(): ino 0x%x\n", ino));
151 
152 	parent = OF_parent(node);
153 
154 	address_cells = prom_getpropint(parent, "#address-cells", 2);
155 	interrupt_cells = prom_getpropint(parent, "#interrupt-cells", 1);
156 	KASSERT(interrupt_cells == 1);
157 
158 	len = OF_getproplen(parent, "interrupt-map-mask");
159 	if (len < (address_cells + interrupt_cells) * sizeof(int))
160 		return (-1);
161 	imap_mask = malloc(len, M_DEVBUF, M_WAITOK);
162 	if (OF_getprop(parent, "interrupt-map-mask", imap_mask, len) != len)
163 		goto out;
164 
165 	if (prom_getprop(parent, "interrupt-map", sizeof(int), &nimap, (void **)&imap))
166 		panic("vbus: can't get interrupt-map");
167 
168 	if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)&reg))
169 		panic("vbus: can't get reg");
170 	if (nreg < address_cells)
171 		goto out;
172 
173 	while (nimap >= address_cells + interrupt_cells + 2) {
174 		if (vbus_cmp_cells(imap, reg, imap_mask, address_cells) &&
175 		    vbus_cmp_cells(&imap[address_cells], &ino,
176 		    &imap_mask[address_cells], interrupt_cells)) {
177 			node = imap[address_cells + interrupt_cells];
178 			devino = imap[address_cells + interrupt_cells + 1];
179 
180 			free(reg, M_DEVBUF);
181 			reg = NULL;
182 
183 			if (prom_getprop(node, "reg", sizeof(*reg), &nreg, (void **)&reg))
184 				panic("vbus: can't get reg");
185 
186 			devhandle = reg[0] & 0x0fffffff;
187 
188 			err = hv_intr_devino_to_sysino(devhandle, devino, sysino);
189 			if (err != H_EOK)
190 				goto out;
191 
192 			KASSERT(*sysino == INTVEC(*sysino));
193 			free(imap_mask, M_DEVBUF);
194 			return (0);
195 		}
196 		imap += address_cells + interrupt_cells + 2;
197 		nimap -= address_cells + interrupt_cells + 2;
198 	}
199 
200 out:
201 	free(imap_mask, M_DEVBUF);
202 	return (-1);
203 }
204 
205 void *
206 vbus_intr_establish(bus_space_tag_t t, int ihandle, int level,
207 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
208 {
209 	uint64_t sysino = INTVEC(ihandle);
210 	struct intrhand *ih;
211 	int ino;
212 	int err;
213 
214 	DPRINTF(VBUS_INTR, ("vbus_intr_establish()\n"));
215 
216 	ino = INTINO(ihandle);
217 
218 	ih = intrhand_alloc();
219 
220 	ih->ih_ivec = ihandle;
221 	ih->ih_fun = handler;
222 	ih->ih_arg = arg;
223 	ih->ih_pil = level;
224 	ih->ih_number = ino;
225 	ih->ih_pending = 0;
226 
227 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
228 	ih->ih_ack = vbus_intr_ack;
229 
230 	err = hv_intr_settarget(sysino, cpus->ci_cpuid);
231 	if (err != H_EOK) {
232 		printf("hv_intr_settarget(%lu, %u) failed - err = %d\n",
233 		       (long unsigned int)sysino, cpus->ci_cpuid, err);
234 		return (NULL);
235 	}
236 
237 	/* Clear pending interrupts. */
238 	err = hv_intr_setstate(sysino, INTR_IDLE);
239 	if (err != H_EOK) {
240 	  printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n",
241 		 (long unsigned int)sysino, err);
242 	  return (NULL);
243 	}
244 
245 	err = hv_intr_setenabled(sysino, INTR_ENABLED);
246 	if (err != H_EOK) {
247 	  printf("hv_intr_setenabled(%lu) failed - err = %d\n",
248 		 (long unsigned int)sysino, err);
249 	  return (NULL);
250 	}
251 
252 	return (ih);
253 }
254 
255 void
256 vbus_intr_ack(struct intrhand *ih)
257 {
258 	DPRINTF(VBUS_INTR, ("vbus_intr_ack()\n"));
259 	hv_intr_setstate(ih->ih_number, INTR_IDLE);
260 }
261 
262 bus_space_tag_t
263 vbus_alloc_bus_tag(struct vbus_softc *sc, bus_space_tag_t parent)
264 {
265 	struct sparc_bus_space_tag *bt;
266 
267 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
268 	bt->cookie = sc;
269 	bt->parent = parent;
270 	bt->sparc_bus_map = parent->sparc_bus_map;
271 	bt->sparc_intr_establish = vbus_intr_establish;
272 
273 	return (bt);
274 }
275