xref: /openbsd-src/sys/arch/sparc64/dev/cbus.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: cbus.c,v 1.5 2009/04/12 14:53:15 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/mdesc.h>
26 #include <machine/openfirm.h>
27 
28 #include <sparc64/dev/cbusvar.h>
29 #include <sparc64/dev/vbusvar.h>
30 
31 struct cbus_softc {
32 	struct device		sc_dv;
33 	bus_space_tag_t		sc_bustag;
34 	bus_dma_tag_t		sc_dmatag;
35 
36 	/* Machine description. */
37 	int			sc_idx;
38 };
39 
40 int	cbus_match(struct device *, void *, void *);
41 void	cbus_attach(struct device *, struct device *, void *);
42 int	cbus_print(void *, const char *);
43 
44 struct cfattach cbus_ca = {
45 	sizeof(struct cbus_softc), cbus_match, cbus_attach
46 };
47 
48 struct cfdriver cbus_cd = {
49 	NULL, "cbus", DV_DULL
50 };
51 
52 void	*cbus_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
53     int (*)(void *), void *, const char *);
54 void	cbus_intr_ack(struct intrhand *);
55 bus_space_tag_t cbus_alloc_bus_tag(struct cbus_softc *, bus_space_tag_t);
56 
57 int	cbus_get_channel_endpoint(struct cbus_softc *,
58 	    struct cbus_attach_args *);
59 
60 int
61 cbus_match(struct device *parent, void *match, void *aux)
62 {
63 	struct vbus_attach_args *va = aux;
64 
65 	if (strcmp(va->va_name, "channel-devices") == 0)
66 		return (1);
67 
68 	return (0);
69 }
70 
71 void
72 cbus_attach(struct device *parent, struct device *self, void *aux)
73 {
74 	struct cbus_softc *sc = (struct cbus_softc *)self;
75 	struct vbus_attach_args *va = aux;
76 	int node;
77 
78 	sc->sc_bustag = cbus_alloc_bus_tag(sc, va->va_bustag);
79 	sc->sc_dmatag = va->va_dmatag;
80 
81 	printf("\n");
82 
83 	sc->sc_idx = mdesc_find(va->va_name, va->va_reg[0]);
84 	if (sc->sc_idx == -1)
85 		return;
86 
87 	for (node = OF_child(va->va_node); node; node = OF_peer(node)) {
88 		struct cbus_attach_args ca;
89 		char buf[32];
90 
91 		bzero(&ca, sizeof(ca));
92 		ca.ca_node = node;
93 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
94 			continue;
95 		ca.ca_name = buf;
96 		ca.ca_bustag = sc->sc_bustag;
97 		ca.ca_dmatag = sc->sc_dmatag;
98 		getprop(node, "reg", sizeof(*ca.ca_reg),
99 		    &ca.ca_nreg, (void **)&ca.ca_reg);
100 		if (cbus_get_channel_endpoint(sc, &ca) != 0)
101 			continue;
102 
103 		config_found(self, &ca, cbus_print);
104 	}
105 }
106 
107 int
108 cbus_print(void *aux, const char *name)
109 {
110 	struct cbus_attach_args *ca = aux;
111 
112 	if (name)
113 		printf("\"%s\" at %s", ca->ca_name, name);
114 	if (ca->ca_id != -1)
115 		printf(" chan 0x%llx", ca->ca_id);
116 	return (UNCONF);
117 }
118 
119 int
120 cbus_intr_map(int node, int ino, uint64_t *sysino)
121 {
122 	int parent;
123 	int reg;
124 	int err;
125 
126 	parent = OF_parent(node);
127 	if (OF_getprop(parent, "reg", &reg, sizeof(reg)) != sizeof(reg))
128 		return (-1);
129 
130 	*sysino = INTIGN(reg) | INTINO(ino);
131 	err = hv_vintr_setcookie(reg, ino, *sysino);
132 	if (err != H_EOK)
133 		return (-1);
134 
135 	return (0);
136 }
137 
138 void *
139 cbus_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
140     int level, int flags, int (*handler)(void *), void *arg, const char *what)
141 {
142 	uint64_t devhandle = INTIGN(ihandle);
143 	uint64_t devino = INTINO(ihandle);
144 	struct intrhand *ih;
145 	int err;
146 
147 	ih = bus_intr_allocate(t0, handler, arg, ihandle, level,
148 	    NULL, NULL, what);
149 	if (ih == NULL)
150 		return (NULL);
151 
152 	intr_establish(ih->ih_pil, ih);
153 	ih->ih_ack = cbus_intr_ack;
154 
155 	err = hv_vintr_settarget(devhandle, devino, cpus->ci_upaid);
156 	if (err != H_EOK) {
157 		printf("hv_vintr_settarget: %d\n", err);
158 		return (NULL);
159 	}
160 
161 	/* Clear pending interrupts. */
162 	err = hv_vintr_setstate(devhandle, devino, INTR_IDLE);
163 	if (err != H_EOK) {
164 		printf("hv_vintr_setstate: %d\n", err);
165 		return (NULL);
166 	}
167 
168 	err = hv_vintr_setenabled(devhandle, devino, INTR_ENABLED);
169 	if (err != H_EOK) {
170 		printf("hv_vintr_setenabled: %d\n", err);
171 		return (NULL);
172 	}
173 
174 	return (ih);
175 }
176 
177 void
178 cbus_intr_ack(struct intrhand *ih)
179 {
180 	uint64_t devhandle = INTIGN(ih->ih_number);
181 	uint64_t devino = INTINO(ih->ih_number);
182 
183 	hv_vintr_setstate(devhandle, devino,  INTR_IDLE);
184 }
185 
186 bus_space_tag_t
187 cbus_alloc_bus_tag(struct cbus_softc *sc, bus_space_tag_t parent)
188 {
189 	struct sparc_bus_space_tag *bt;
190 
191 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
192 	if (bt == NULL)
193 		panic("could not allocate cbus bus tag");
194 
195 	snprintf(bt->name, sizeof(bt->name), "%s", sc->sc_dv.dv_xname);
196 	bt->cookie = sc;
197 	bt->parent = parent;
198 	bt->asi = parent->asi;
199 	bt->sasi = parent->sasi;
200 	bt->sparc_bus_map = parent->sparc_bus_map;
201 	bt->sparc_intr_establish = cbus_intr_establish;
202 
203 	return (bt);
204 }
205 
206 int
207 cbus_get_channel_endpoint(struct cbus_softc *sc, struct cbus_attach_args *ca)
208 {
209 	struct md_header *hdr;
210 	struct md_element *elem;
211 	const char *name_blk;
212 	const char *str;
213 	int idx;
214 	int arc;
215 
216 	idx = mdesc_find_child(sc->sc_idx, ca->ca_name, ca->ca_reg[0]);
217 	if (idx == -1)
218 		return (ENOENT);
219 
220 	hdr = (struct md_header *)mdesc;
221 	elem = (struct md_element *)(mdesc + sizeof(struct md_header));
222 	name_blk = mdesc + sizeof(struct md_header) + hdr->node_blk_sz;
223 
224 	for (; elem[idx].tag != 'E'; idx++) {
225 		str = name_blk + elem[idx].name_offset;
226 		if (elem[idx].tag != 'a' || strcmp(str, "fwd") != 0)
227 			continue;
228 
229 		arc = elem[idx].d.val;
230 		str = name_blk + elem[arc].name_offset;
231 		if (strcmp(str, "virtual-device-port") == 0) {
232 			idx = arc;
233 			continue;
234 		}
235 
236 		if (strcmp(str, "channel-endpoint") == 0) {
237 			ca->ca_id = mdesc_get_prop_val(arc, "id");
238 			ca->ca_tx_ino = mdesc_get_prop_val(arc, "tx-ino");
239 			ca->ca_rx_ino = mdesc_get_prop_val(arc, "rx-ino");
240 			return (0);
241 		}
242 	}
243 
244 	ca->ca_id = -1;
245 	ca->ca_tx_ino = -1;
246 	ca->ca_rx_ino = -1;
247 	return (0);
248 }
249