1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* $OpenBSD: cbus.c,v 1.11 2013/07/16 19:45:37 kettenis Exp $ */
/*
* Copyright (c) 2008 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/autoconf.h>
#include <machine/hypervisor.h>
#include <machine/mdesc.h>
#include <machine/openfirm.h>
#include <sparc64/dev/cbusvar.h>
#include <sparc64/dev/vbusvar.h>
#define CBUS_HANDLE(x) ((x) & ~0xff)
#define CBUS_INO(x) ((x) & 0xff)
struct cbus_softc {
struct device sc_dv;
bus_space_tag_t sc_bustag;
bus_dma_tag_t sc_dmatag;
/* Machine description. */
int sc_idx;
};
int cbus_match(struct device *, void *, void *);
void cbus_attach(struct device *, struct device *, void *);
int cbus_print(void *, const char *);
struct cfattach cbus_ca = {
sizeof(struct cbus_softc), cbus_match, cbus_attach
};
struct cfdriver cbus_cd = {
NULL, "cbus", DV_DULL
};
void *cbus_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
int (*)(void *), void *, const char *);
void cbus_intr_ack(struct intrhand *);
bus_space_tag_t cbus_alloc_bus_tag(struct cbus_softc *, bus_space_tag_t);
int cbus_get_channel_endpoint(struct cbus_softc *,
struct cbus_attach_args *);
int
cbus_match(struct device *parent, void *match, void *aux)
{
struct vbus_attach_args *va = aux;
if (strcmp(va->va_name, "channel-devices") == 0)
return (1);
return (0);
}
void
cbus_attach(struct device *parent, struct device *self, void *aux)
{
struct cbus_softc *sc = (struct cbus_softc *)self;
struct vbus_attach_args *va = aux;
int node;
sc->sc_bustag = cbus_alloc_bus_tag(sc, va->va_bustag);
sc->sc_dmatag = va->va_dmatag;
printf("\n");
sc->sc_idx = mdesc_find(va->va_name, va->va_reg[0]);
if (sc->sc_idx == -1)
return;
for (node = OF_child(va->va_node); node; node = OF_peer(node)) {
struct cbus_attach_args ca;
char buf[32];
bzero(&ca, sizeof(ca));
ca.ca_node = node;
if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
continue;
ca.ca_name = buf;
ca.ca_bustag = sc->sc_bustag;
ca.ca_dmatag = sc->sc_dmatag;
getprop(node, "reg", sizeof(*ca.ca_reg),
&ca.ca_nreg, (void **)&ca.ca_reg);
if (cbus_get_channel_endpoint(sc, &ca) != 0)
continue;
config_found(self, &ca, cbus_print);
}
}
int
cbus_print(void *aux, const char *name)
{
struct cbus_attach_args *ca = aux;
if (name)
printf("\"%s\" at %s", ca->ca_name, name);
if (ca->ca_id != -1)
printf(" chan 0x%llx", ca->ca_id);
return (UNCONF);
}
int
cbus_intr_map(int node, int ino, uint64_t *sysino)
{
int parent;
int reg;
int err;
parent = OF_parent(node);
if (OF_getprop(parent, "reg", ®, sizeof(reg)) != sizeof(reg))
return (-1);
*sysino = CBUS_HANDLE(reg) | CBUS_INO(ino);
err = hv_vintr_setcookie(reg, ino, *sysino);
if (err != H_EOK)
return (-1);
return (0);
}
int
cbus_intr_setstate(uint64_t sysino, uint64_t state)
{
uint64_t devhandle = CBUS_HANDLE(sysino);
uint64_t devino = CBUS_INO(sysino);
int err;
err = hv_vintr_setstate(devhandle, devino, state);
if (err != H_EOK)
return (-1);
return (0);
}
int
cbus_intr_setenabled(uint64_t sysino, uint64_t enabled)
{
uint64_t devhandle = CBUS_HANDLE(sysino);
uint64_t devino = CBUS_INO(sysino);
int err;
err = hv_vintr_setenabled(devhandle, devino, enabled);
if (err != H_EOK)
return (-1);
return (0);
}
void *
cbus_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
int level, int flags, int (*handler)(void *), void *arg, const char *what)
{
uint64_t devhandle = CBUS_HANDLE(ihandle);
uint64_t devino = CBUS_INO(ihandle);
struct intrhand *ih;
int err;
ih = bus_intr_allocate(t0, handler, arg, ihandle, level,
NULL, NULL, what);
if (ih == NULL)
return (NULL);
err = hv_vintr_setenabled(devhandle, devino, INTR_DISABLED);
if (err != H_EOK) {
printf("hv_vintr_setenabled: %d\n", err);
return (NULL);
}
if (flags & BUS_INTR_ESTABLISH_MPSAFE)
ih->ih_mpsafe = 1;
intr_establish(ih->ih_pil, ih);
ih->ih_ack = cbus_intr_ack;
err = hv_vintr_settarget(devhandle, devino, cpus->ci_upaid);
if (err != H_EOK) {
printf("hv_vintr_settarget: %d\n", err);
return (NULL);
}
/* Clear pending interrupts. */
err = hv_vintr_setstate(devhandle, devino, INTR_IDLE);
if (err != H_EOK) {
printf("hv_vintr_setstate: %d\n", err);
return (NULL);
}
return (ih);
}
void
cbus_intr_ack(struct intrhand *ih)
{
uint64_t devhandle = CBUS_HANDLE(ih->ih_number);
uint64_t devino = CBUS_INO(ih->ih_number);
hv_vintr_setstate(devhandle, devino, INTR_IDLE);
}
bus_space_tag_t
cbus_alloc_bus_tag(struct cbus_softc *sc, bus_space_tag_t parent)
{
struct sparc_bus_space_tag *bt;
bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
if (bt == NULL)
panic("could not allocate cbus bus tag");
strlcpy(bt->name, sc->sc_dv.dv_xname, sizeof(bt->name));
bt->cookie = sc;
bt->parent = parent;
bt->asi = parent->asi;
bt->sasi = parent->sasi;
bt->sparc_bus_map = parent->sparc_bus_map;
bt->sparc_intr_establish = cbus_intr_establish;
return (bt);
}
int
cbus_get_channel_endpoint(struct cbus_softc *sc, struct cbus_attach_args *ca)
{
struct md_header *hdr;
struct md_element *elem;
const char *name_blk;
const char *str;
int idx;
int arc;
idx = mdesc_find_child(sc->sc_idx, ca->ca_name, ca->ca_reg[0]);
if (idx == -1)
return (ENOENT);
hdr = (struct md_header *)mdesc;
elem = (struct md_element *)(mdesc + sizeof(struct md_header));
name_blk = mdesc + sizeof(struct md_header) + hdr->node_blk_sz;
ca->ca_idx = idx;
ca->ca_id = -1;
ca->ca_tx_ino = -1;
ca->ca_rx_ino = -1;
if (strcmp(ca->ca_name, "disk") != 0 &&
strcmp(ca->ca_name, "network") != 0)
return (0);
for (; elem[idx].tag != 'E'; idx++) {
str = name_blk + elem[idx].name_offset;
if (elem[idx].tag != 'a' || strcmp(str, "fwd") != 0)
continue;
arc = elem[idx].d.val;
str = name_blk + elem[arc].name_offset;
if (strcmp(str, "virtual-device-port") == 0) {
idx = arc;
continue;
}
if (strcmp(str, "channel-endpoint") == 0) {
ca->ca_id = mdesc_get_prop_val(arc, "id");
ca->ca_tx_ino = mdesc_get_prop_val(arc, "tx-ino");
ca->ca_rx_ino = mdesc_get_prop_val(arc, "rx-ino");
return (0);
}
}
return (0);
}
|