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