~kris/9p

9hist

ref: 9eb51cf5371a6fdfa6fcbae8fdb23ee2da672f32 9hist/power/devcyc.c -rw-r--r-- 6.3 KiB
9eb51cf5 — David du Colombier Plan 9 from Bell Labs 1994-04-05 32 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"devtab.h"

#include	"io.h"
#include	"/sys/src/fs/cyc/comm.h"


typedef struct Commcyc	Commcyc;

enum
{
	Vmevec=		0xd2,		/* vme vector for interrupts */
	Intlevel=	5,		/* level to interrupt on */
	Qdir=		0,		/* Qid's */
	Qcyc=		1,
	Ncyc=		1,
};

Dirtab cycdir[]={
	"cyc",	{Qcyc},	0,	0600,
};

#define	NCYCDIR	(sizeof cycdir/sizeof(Dirtab))

struct Commcyc
{
	Lock;
	QLock		buflock;
	Lock		busy;
	Comm		*addr;		/* address of the device */
	int		vec;		/* vme interrupt vector */
	int		wi;		/* where to write next cmd */
	int		ri;		/* where to read next reply */
	uchar		buf[MAXFDATA+MAXMSG];
};

Commcyc cyclone[Ncyc];

void	cycintr(int);

#define	CYCLONE	VMEA24SUP(Comm, 0x10000);

/*
 * Commands
 */

Cycmsg**
cycsend(Commcyc *h, Cycmsg *m)
{
	Cycmsg **mp;

	lock(h);
	mp = (Cycmsg**)&h->addr->reqstq[h->wi];
	*mp = (Cycmsg*)MP2VME(m);
	h->wi++;
	if(h->wi >= NRQ)
		h->wi = 0;
	unlock(h);
	return mp;
}

void
cycwait(Cycmsg **mp)
{
	ulong l;

	l = 0;
	while(*mp){
		delay(0);	/* just a subroutine call; stay off VME */
		if(++l > 10*1000*1000){
			l = 0;
			panic("cycsend blocked");
		}
	}
	return;
}

/*
 *  reset all cyclone boards
 */
void
cycreset(void)
{
	int i;
	Commcyc *cp;

	for(cp=cyclone,i=0; i<Ncyc; i++,cp++){
		cp->addr = CYCLONE+i;
		/*
		 * Write queue is at end of cyclone memory
		 */
		cp->vec = Vmevec+i;
		setvmevec(cp->vec, cycintr);
	}	
}

void
cycinit(void)
{
}

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

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

	c = devattach('C', spec);
	c->dev = i;
	c->qid.path = CHDIR;
	c->qid.vers = 0;
	return c;
}

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

int	 
cycwalk(Chan *c, char *name)
{
	return devwalk(c, name, cycdir, NCYCDIR, devgen);
}

void	 
cycstat(Chan *c, char *dp)
{
	devstat(c, dp, cycdir, NCYCDIR, devgen);
}

Chan*
cycopen(Chan *c, int omode)
{
	Commcyc *cp;
	Cycmsg *mp, **cmp;

	if(c->qid.path == CHDIR){
		if(omode != OREAD)
			error(Eperm);
	}else if(c->qid.path == Qcyc){
		cp = &cyclone[c->dev];
		if(!canlock(&cp->busy))
			error(Einuse);
		/*
		 * Clear communications region
		 */
		memset(cp->addr->reqstq, 0, NRQ*sizeof(ulong));
		cp->addr->reqstp = 0;
		memset(cp->addr->replyq, 0, NRQ*sizeof(ulong));
		cp->addr->replyp = 0;

		/* Set the interrupt vector for the cyclone to pick up */
		cp->addr->vmevec = Vmevec;

		/*
		 * Issue reset
		 */
		cp->wi = 0;
		cp->ri = 0;
		mp = up->kcyc;
		mp->cmd = Ureset;
		cmp = cycsend(cp, up->kcyc);
		cycwait(cmp);
		delay(100);
		print("reset\n");
	}
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

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

void	 
cycclose(Chan *c)
{
	Commcyc *cp;
	Cycmsg **cmp;

	cp = &cyclone[c->dev];
	if(c->qid.path != CHDIR){
		up->kcyc->cmd = Ureboot;
		cmp = cycsend(cp, up->kcyc);
		cycwait(cmp);
		unlock(&cp->busy);
	}
}

int
cycmsgintr(Cycmsg *hm)
{
	return hm->intr;
}

/*
 * Read and write use physical addresses if they can, which they usually can.
 * Most I/O is from devmnt, which has local buffers.  Therefore just check
 * that buf is in KSEG0 and is at an even address.
 */

long	 
cycread(Chan *c, void *buf, long n, ulong offset)
{
	ulong l, m;
	Commcyc *cp;
	Cycmsg *mp, **cmp;

	USED(offset);
	cp = &cyclone[c->dev];
	switch(c->qid.path & ~CHDIR){
	case Qdir:
		return devdirread(c, buf, n, cycdir, NCYCDIR, devgen);

	case Qcyc:
		if(n > sizeof cp->buf){
			print("cyclone bufsize %d %d\n", n, sizeof cp->buf);
			error(Egreg);
		}
		if((((ulong)buf)&(KSEGM|3)) == KSEG0){
			/*
			 *  use supplied buffer, no need to lock for reply
			 */

			mp = up->kcyc;
			mp->cmd = Uread;
			mp->param[0] = MP2VME(buf);
			mp->param[1] = n;
			mp->param[2] = 0;		/* reply checksum */
			mp->param[3] = 0xdeadbeef;	/* reply count */
			mp->intr = 0;
			cycsend(cp, mp);

			while(waserror())
				;
			sleep(&mp->r, cycmsgintr, mp);
			poperror();

			m = mp->param[3];
			if(m==0 || m>n){
				print("devcyc: count 0x%lux 0x%lux\n", m, n);
				error(Egreg);
			}
		}
		else{
			/*
			 * use cyclone buffer. lock the buffer until the reply
			 */
			mp = up->ucyc;
			qlock(&cp->buflock);
			mp->cmd = Uread;
			mp->param[0] = MP2VME(cp->buf);
			mp->param[1] = n;
			mp->param[2] = 0;	/* reply checksum */
			mp->param[3] = 0;	/* reply count */
			mp->intr = 0;
			cmp = cycsend(cp, mp);
			cycwait(cmp);
			l = 100*1000*1000;
			do
				m = mp->param[3];
			while(m==0 && --l>0);

			if(m==0 || m>n){
				print("devcyc: count %ld %ld\n", m, n);
				qunlock(&cp->buflock);
				error(Egreg);
			}
			memmove(buf, cp->buf, m);
			qunlock(&cp->buflock);
		}
		return m;
	}
	print("devcyc read unk\n");
	error(Egreg);
	return 0;
}

long	 
cycwrite(Chan *c, void *buf, long n, ulong offset)
{
	Commcyc *cp;
	Cycmsg *mp, **cmp;

	USED(offset);
	cp = &cyclone[c->dev];
	switch(c->qid.path & ~CHDIR){
	case Qdir:
		return devdirread(c, buf, n, cycdir, NCYCDIR, devgen);

	case Qcyc:
		if(n > sizeof cp->buf){
			print("cyclone write bufsize\n");
			error(Egreg);
		}
		if((((ulong)buf)&(KSEGM|3)) == KSEG0){
			/*
			 * use supplied buffer, no need to lock for reply
			 */
			mp = up->kcyc;

			mp->cmd = Uwrite;
			mp->param[0] = MP2VME(buf);
			mp->param[1] = n;
			mp->param[2] = 0;

			cmp = cycsend(cp, mp);
			cycwait(cmp);
		}else{
			/*
			 * use cyclone buffer.  lock the buffer until the reply
			 */
			mp = up->ucyc;
			qlock(&cp->buflock);
			memmove(cp->buf, buf, n);

			mp->cmd = Uwrite;
			mp->param[0] = MP2VME(cp->buf);
			mp->param[1] = n;
			mp->param[2] = 0;

			cmp = cycsend(cp, mp);
			cycwait(cmp);
			qunlock(&cp->buflock);
		}
		return n;
	}
	print("cyclone write unk\n");
	error(Egreg);
	return 0;
}

void	 
cycremove(Chan *c)
{
	USED(c);
	error(Eperm);
}

void	 
cycwstat(Chan *c, char *dp)
{
	USED(c, dp);
	error(Eperm);
}

void
cycintr(int vec)
{
	Cycmsg *cm;
	Commcyc *h;
	ulong l, n, *r;

	h = &cyclone[vec - Vmevec];
	if(h<cyclone || h>&cyclone[Ncyc]){
		print("bad cyclone vec\n");
		return;
	}

	r = h->addr->replyq;

	for(;;) {
		l = r[h->ri];
		if(l == 0)
			break;

		r[h->ri++] = 0;
		if(h->ri >= NRQ)
			h->ri = 0;

		cm = (Cycmsg*)VME2MP(l);
		cm->intr = 1;
		n = cm->param[3];
		if(n==0 || n > 10000)
			print("cycintr count 0x%lux\n", n);

		wakeup(&cm->r);
	}
}