cbus.c revision 1.7
1/*	$OpenBSD: cbus.c,v 1.7 2009/12/14 16:06:35 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
31struct 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
40int	cbus_match(struct device *, void *, void *);
41void	cbus_attach(struct device *, struct device *, void *);
42int	cbus_print(void *, const char *);
43
44struct cfattach cbus_ca = {
45	sizeof(struct cbus_softc), cbus_match, cbus_attach
46};
47
48struct cfdriver cbus_cd = {
49	NULL, "cbus", DV_DULL
50};
51
52void	*cbus_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
53    int (*)(void *), void *, const char *);
54void	cbus_intr_ack(struct intrhand *);
55bus_space_tag_t cbus_alloc_bus_tag(struct cbus_softc *, bus_space_tag_t);
56
57int	cbus_get_channel_endpoint(struct cbus_softc *,
58	    struct cbus_attach_args *);
59
60int
61cbus_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
71void
72cbus_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
107int
108cbus_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
119int
120cbus_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
138int
139cbus_intr_setstate(uint64_t sysino, uint64_t state)
140{
141	uint64_t devhandle = INTIGN(sysino);
142	uint64_t devino = INTINO(sysino);
143	int err;
144
145	err = hv_vintr_setstate(devhandle, devino, state);
146	if (err != H_EOK)
147		return (-1);
148
149	return (0);
150}
151
152int
153cbus_intr_setenabled(uint64_t sysino, uint64_t enabled)
154{
155	uint64_t devhandle = INTIGN(sysino);
156	uint64_t devino = INTINO(sysino);
157	int err;
158
159	err = hv_vintr_setenabled(devhandle, devino, enabled);
160	if (err != H_EOK)
161		return (-1);
162
163	return (0);
164}
165
166void *
167cbus_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
168    int level, int flags, int (*handler)(void *), void *arg, const char *what)
169{
170	uint64_t devhandle = INTIGN(ihandle);
171	uint64_t devino = INTINO(ihandle);
172	struct intrhand *ih;
173	int err;
174
175	ih = bus_intr_allocate(t0, handler, arg, ihandle, level,
176	    NULL, NULL, what);
177	if (ih == NULL)
178		return (NULL);
179
180	intr_establish(ih->ih_pil, ih);
181	ih->ih_ack = cbus_intr_ack;
182
183	err = hv_vintr_settarget(devhandle, devino, cpus->ci_upaid);
184	if (err != H_EOK) {
185		printf("hv_vintr_settarget: %d\n", err);
186		return (NULL);
187	}
188
189	/* Clear pending interrupts. */
190	err = hv_vintr_setstate(devhandle, devino, INTR_IDLE);
191	if (err != H_EOK) {
192		printf("hv_vintr_setstate: %d\n", err);
193		return (NULL);
194	}
195
196	err = hv_vintr_setenabled(devhandle, devino, INTR_ENABLED);
197	if (err != H_EOK) {
198		printf("hv_vintr_setenabled: %d\n", err);
199		return (NULL);
200	}
201
202	return (ih);
203}
204
205void
206cbus_intr_ack(struct intrhand *ih)
207{
208	uint64_t devhandle = INTIGN(ih->ih_number);
209	uint64_t devino = INTINO(ih->ih_number);
210
211	hv_vintr_setstate(devhandle, devino, INTR_IDLE);
212}
213
214bus_space_tag_t
215cbus_alloc_bus_tag(struct cbus_softc *sc, bus_space_tag_t parent)
216{
217	struct sparc_bus_space_tag *bt;
218
219	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
220	if (bt == NULL)
221		panic("could not allocate cbus bus tag");
222
223	snprintf(bt->name, sizeof(bt->name), "%s", sc->sc_dv.dv_xname);
224	bt->cookie = sc;
225	bt->parent = parent;
226	bt->asi = parent->asi;
227	bt->sasi = parent->sasi;
228	bt->sparc_bus_map = parent->sparc_bus_map;
229	bt->sparc_intr_establish = cbus_intr_establish;
230
231	return (bt);
232}
233
234int
235cbus_get_channel_endpoint(struct cbus_softc *sc, struct cbus_attach_args *ca)
236{
237	struct md_header *hdr;
238	struct md_element *elem;
239	const char *name_blk;
240	const char *str;
241	int idx;
242	int arc;
243
244	idx = mdesc_find_child(sc->sc_idx, ca->ca_name, ca->ca_reg[0]);
245	if (idx == -1)
246		return (ENOENT);
247
248	hdr = (struct md_header *)mdesc;
249	elem = (struct md_element *)(mdesc + sizeof(struct md_header));
250	name_blk = mdesc + sizeof(struct md_header) + hdr->node_blk_sz;
251
252	ca->ca_idx = idx;
253
254	ca->ca_id = -1;
255	ca->ca_tx_ino = -1;
256	ca->ca_rx_ino = -1;
257
258	if (strcmp(ca->ca_name, "disk") != 0 &&
259	    strcmp(ca->ca_name, "network") != 0)
260		return (0);
261
262	for (; elem[idx].tag != 'E'; idx++) {
263		str = name_blk + elem[idx].name_offset;
264		if (elem[idx].tag != 'a' || strcmp(str, "fwd") != 0)
265			continue;
266
267		arc = elem[idx].d.val;
268		str = name_blk + elem[arc].name_offset;
269		if (strcmp(str, "virtual-device-port") == 0) {
270			idx = arc;
271			continue;
272		}
273
274		if (strcmp(str, "channel-endpoint") == 0) {
275			ca->ca_id = mdesc_get_prop_val(arc, "id");
276			ca->ca_tx_ino = mdesc_get_prop_val(arc, "tx-ino");
277			ca->ca_rx_ino = mdesc_get_prop_val(arc, "rx-ino");
278			return (0);
279		}
280	}
281
282	return (0);
283}
284