~kris/9p

9hist

ref: d6bbe2d86ff53ed2dcf484ca6c7d4f7f33360caf 9hist/power/devhotrod.c -rw-r--r-- 3.7 KiB
d6bbe2d8 — David du Colombier Plan 9 from Bell Labs 1990-10-22 35 years ago
                                                                                
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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"
#include	"devtab.h"

#include	"io.h"

typedef struct Hotrod	Hotrod;
typedef struct Device	Device;

enum {
	Vmevec=		0xd2,		/* vme vector for interrupts */
	Intlevel=	5,		/* level to interrupt on */
	Nhotrod=	2,
};

/*
 *  the hotrod fiber interface responds to 1MB
 *  of either user or supervisor accesses at:
 *  	0x30000000 to 0x300FFFFF  in	A32 space
 *  and	  0xB00000 to   0xBFFFFF  in	A24 space
 */
struct Device {
	ulong	mem[1024*1024/sizeof(ulong)];
};
#define HOTROD		VMEA24SUP(Device, 0xB00000)

struct Hotrod {
	QLock;

	Device	*addr;
	int	vec;
	char	name[NAMELEN];
};

Hotrod hotrod[Nhotrod];

static void hotrodintr(int);

int
hotrodgen(Chan *c, Dirtab *tab, int ntab, int i, Dir *dp)
{
	if(i || c->dev>=Nhotrod)
		return -1;
	devdir(c, 0, hotrod[c->dev].name, sizeof(Device), 0666, dp);
	return 1;
}

/*
 *  reset all hotrod boards
 */
void
hotrodreset(void)
{
	int i;
	Hotrod *hp;

	for(i=0; i<Nhotrod; i++){
		hotrod[i].addr = HOTROD+i;
		hotrod[i].vec = Vmevec+i;
		sprint(hotrod[i].name, "hotrod%d", i);
		setvmevec(hotrod[i].vec, hotrodintr);
	}	
	wbflush();
	delay(20);
}

void
hotrodinit(void)
{
}

/*
 *  enable the device for interrupts, spec is the device number
 */
Chan*
hotrodattach(char *spec)
{
	int i;
	Chan *c;

	i = strtoul(spec, 0, 0);
	if(i >= Nhotrod)
		error(0, Ebadarg);

	c = devattach('H', spec);
	c->dev = i;
	c->qid = CHDIR;
	return c;
}

Chan*
hotrodclone(Chan *c, Chan *nc)
{
	return devclone(c, nc);
}

int	 
hotrodwalk(Chan *c, char *name)
{
	return devwalk(c, name, 0, 0, hotrodgen);
}

void	 
hotrodstat(Chan *c, char *dp)
{
	devstat(c, dp, 0, 0, hotrodgen);
}

Chan*
hotrodopen(Chan *c, int omode)
{
	if(c->qid == CHDIR){
		if(omode != OREAD)
			error(0, Eperm);
	}
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

void	 
hotrodcreate(Chan *c, char *name, int omode, ulong perm)
{
	error(0, Eperm);
}

void	 
hotrodclose(Chan *c)
{
}

/*
 *  read the hotrod memory
 */
long	 
hotrodread(Chan *c, void *buf, long n)
{
	Hotrod *hp;
	Device *dp;
	ulong *from;
	ulong *to;
	ulong *end;

	if(c->qid&CHDIR)
		return devdirread(c, buf, n, 0, 0, hotrodgen);

	/*
	 *  allow full word access only
	 */
	if((c->offset&(sizeof(ulong)-1)) || (n&(sizeof(ulong)-1)))
		error(0, Ebadarg);

	hp = &hotrod[c->dev];
	dp = hp->addr;
	if(c->offset >= sizeof(dp->mem))
		return 0;
	if(c->offset+n > sizeof(dp->mem))
		n = sizeof(dp->mem) - c->offset;

	/*
	 *  avoid memcpy to ensure VME 32-bit reads
	 */
	qlock(hp);
	to = buf;
	from = &dp->mem[c->offset/sizeof(ulong)];
	end = to + (n/sizeof(ulong));
	while(to != end){
		*to++ = *from++;
	}
	qunlock(hp);
	return n;
}

/*
 *  write hotrod memory
 */
long	 
hotrodwrite(Chan *c, void *buf, long n)
{
	Hotrod *hp;
	Device *dp;
	ulong *from;
	ulong *to;
	ulong *end;

	/*
	 *  allow full word access only
	 */
	if((c->offset&(sizeof(ulong)-1)) || (n&(sizeof(ulong)-1)))
		error(0, Ebadarg);

	hp = &hotrod[c->dev];
	dp = hp->addr;
	if(c->offset >= sizeof(dp->mem))
		return 0;
	if(c->offset+n > sizeof(dp->mem))
		n = sizeof(dp->mem) - c->offset;

	/*
	 *  avoid memcpy to ensure VME 32-bit writes
	 */
	qlock(hp);
	from = buf;
	to = &dp->mem[c->offset/sizeof(ulong)];
	end = to + (n/sizeof(ulong));
	while(to != end)
		*to++ = *from++;
	qunlock(hp);
	return n;
}

void	 
hotrodremove(Chan *c)
{
	error(0, Eperm);
}

void	 
hotrodwstat(Chan *c, char *dp)
{
	error(0, Eperm);
}

void
hotroduserstr(Error *e, char *buf)
{
	consuserstr(e, buf);
}

void	 
hotroderrstr(Error *e, char *buf)
{
	rooterrstr(e, buf);
}

static void
hotrodintr(int vec)
{
	Hotrod *hp;

	print("hotrod%d interrupt\n", vec - Vmevec);
	hp = &hotrod[vec - Vmevec];
	if(hp < hotrod || hp > &hotrod[Nhotrod]){
		print("bad hotrod vec\n");
		return;
	}
}