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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "pci.h"
#include "vga.h"
Biobuf stdout;
static int iflag, lflag, pflag, rflag;
static char *dbname = "/lib/vgadb";
static char monitordb[128];
static void
dump(Vga* vga)
{
Ctlr *ctlr;
Attr *attr;
if(vga->mode)
dbdumpmode(vga->mode);
for(attr = vga->attr; attr; attr = attr->next)
Bprint(&stdout, "vga->attr: %s=%s\n", attr->attr, attr->val);
for(ctlr = vga->link; ctlr; ctlr = ctlr->link){
if(ctlr->dump == 0)
continue;
trace("%s->dump\n", ctlr->name);
if(ctlr->flag && ctlr->flag != Fsnarf){
printitem(ctlr->name, "flag");
printflag(ctlr->flag);
Bprint(&stdout, "\n");
}
(*ctlr->dump)(vga, ctlr);
ctlr->flag |= Fdump;
}
Bprint(&stdout, "\n");
}
void
resyncinit(Vga* vga, Ctlr* ctlr, ulong on, ulong off)
{
Ctlr *link;
trace("%s->resyncinit on 0x%8.8luX off 0x%8.8luX\n",
ctlr->name, on, off);
for(link = vga->link; link; link = link->link){
link->flag |= on;
link->flag &= ~off;
if(link == ctlr)
continue;
if(link->init == 0 || (link->flag & Finit) == 0)
continue;
link->flag &= ~Finit;
trace("%s->init 0x%8.8luX\n", link->name, link->flag);
(*link->init)(vga, link);
}
}
void
sequencer(Vga* vga, int on)
{
static uchar seq01;
static int state = 1;
char *s;
if(on)
s = "on";
else
s = "off";
trace("sequencer->enter %s\n", s);
if(on){
if(vga)
seq01 = vga->sequencer[0x01];
if(state == 0){
seq01 |= 0x01;
vgaxo(Seqx, 0x01, seq01);
vgaxo(Seqx, 0x00, 0x03);
}
}
else{
vgaxo(Seqx, 0x00, 0x01);
seq01 = vgaxi(Seqx, 0x01);
vgaxo(Seqx, 0x01, seq01|0x20);
}
state = on;
trace("sequencer->leave %s\n", s);
}
static void
linear(Vga* vga)
{
char buf[256];
char *p;
/*
* Set up for linear addressing: try to allocate the
* kernel memory map then read the base-address back.
* vga->linear is a compatibility hack.
*/
if(vga->linear == 0){
vga->ctlr->flag &= ~Ulinear;
return;
}
if(vga->ctlr->flag & Ulinear){
/*
* If there's already an aperture don't bother trying
* to set up a new one.
*/
vgactlr("addr", buf);
if(atoi(buf)==0 && (buf[0]!='p' || buf[1]!=' ' || atoi(buf+2)==0)){
sprint(buf, "0x%lux 0x%lux", vga->apz ? vga->apz : vga->vmz, vga->vma);
vgactlw("linear", buf);
vgactlr("addr", buf);
}
trace("linear->addr %s\n", buf);
/*
* old: addr 0x12345678
* new: addr p 0x12345678 v 0x82345678 size 0x123
*/
if(buf[0]=='p' && buf[1]==' '){
vga->vmb = strtoul(buf+2, 0, 0);
p = strstr(buf, "size");
if(p)
vga->apz = strtoul(p+4, 0, 0);
}else
vga->vmb = strtoul(buf, 0, 0);
}
else
vgactlw("linear", "0");
}
char*
chanstr[32+1] = {
[1] "k1",
[2] "k2",
[4] "k4",
[8] "m8",
[16] "r5g6b5",
[24] "r8g8b8",
[32] "x8r8g8b8",
};
static void
usage(void)
{
fprint(2, "usage: aux/vga [ -BcdilpvV ] [ -b bios-id ] [ -m monitor ] [ -x db ] [ mode [ virtualsize ] ]\n");
exits("usage");
}
void
main(int argc, char** argv)
{
char *bios, buf[256], sizeb[256], *p, *vsize, *psize;
char *type, *vtype;
int fd, virtual, len;
Ctlr *ctlr;
Vga *vga;
fmtinstall('H', encodefmt);
Binit(&stdout, 1, OWRITE);
bios = getenv("vgactlr");
if((type = getenv("monitor")) == 0)
type = "vga";
psize = vsize = "640x480x8";
ARGBEGIN{
default:
usage();
break;
case 'b':
bios = EARGF(usage());
break;
case 'B':
dumpbios(0x10000);
exits(0);
case 'c':
cflag = 1;
break;
case 'd':
dflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'm':
type = EARGF(usage());
break;
case 'p':
pflag = 1;
break;
case 'r':
/*
* rflag > 1 means "leave me alone, I know what I'm doing."
*/
rflag++;
break;
case 'v':
vflag = 1;
break;
case 'V':
vflag = 1;
Vflag = 1;
break;
case 'x':
dbname = EARGF(usage());
break;
}ARGEND
virtual = 0;
switch(argc){
default:
usage();
break;
case 1:
vsize = psize = argv[0];
break;
case 2:
psize = argv[0];
vsize = argv[1];
virtual = 1;
break;
case 0:
break;
}
if(lflag && strcmp(vsize, "text") == 0){
vesatextmode();
vgactlw("textmode", "");
exits(0);
}
vga = alloc(sizeof(Vga));
if(bios){
if((vga->offset = strtol(bios, &p, 0)) == 0 || *p++ != '=')
error("main: bad BIOS string format - %s\n", bios);
len = strlen(p);
vga->bios = alloc(len+1);
strncpy(vga->bios, p, len);
trace("main->BIOS %s\n", bios);
}
/*
* Try to identify the VGA card and grab
* registers. Print them out if requested.
* If monitor=vesa or our vga controller can't be found
* in vgadb, try vesa modes; failing that, try vga.
*/
if(strcmp(type, "vesa") == 0 || dbctlr(dbname, vga) == 0 ||
vga->ctlr == 0)
if(dbvesa(vga) == 0 || vga->ctlr == 0){
Bprint(&stdout, "%s: controller not in %s, not vesa\n",
argv0, dbname);
dumpbios(256);
type = "vga";
vsize = psize = "640x480x1";
virtual = 0;
vga->ctlr = &generic;
vga->link = &generic;
}
trace("main->snarf\n");
for(ctlr = vga->link; ctlr; ctlr = ctlr->link){
if(ctlr->snarf == 0)
continue;
trace("%s->snarf\n", ctlr->name);
(*ctlr->snarf)(vga, ctlr);
}
if(pflag)
dump(vga);
for(ctlr = vga->link; ctlr; ctlr = ctlr->link)
if(ctlr->flag & Ferror)
error("%r\n");
if(iflag || lflag){
if(getenv(type))
snprint(monitordb, sizeof monitordb, "/env/%s", type);
else
strecpy(monitordb, monitordb+sizeof monitordb, dbname);
if(vga->vesa){
strcpy(monitordb, "vesa bios");
vga->mode = dbvesamode(psize);
}else
vga->mode = dbmode(monitordb, type, psize);
if(vga->mode == 0)
error("main: %s@%s not in %s\n", type, psize, monitordb);
if(virtual){
if((p = strchr(vsize, 'x')) == nil)
error("bad virtual size %s\n", vsize);
vga->virtx = atoi(vsize);
vga->virty = atoi(p+1);
if(vga->virtx < vga->mode->x || vga->virty < vga->mode->y)
error("virtual size smaller than physical size\n");
vga->panning = 1;
}
else{
vga->virtx = vga->mode->x;
vga->virty = vga->mode->y;
vga->panning = 0;
}
trace("vmf %d vmdf %d vf1 %lud vbw %lud\n",
vga->mode->frequency, vga->mode->deffrequency,
vga->f[1], vga->mode->videobw);
if(vga->mode->frequency == 0 && vga->mode->videobw != 0 && vga->f[1] != 0){
/*
* boost clock as much as possible subject
* to video and memory bandwidth constraints
*/
ulong bytes, freq, membw;
double rr;
freq = vga->mode->videobw;
if(freq > vga->f[1])
freq = vga->f[1];
rr = (double)freq/(vga->mode->ht*vga->mode->vt);
if(rr > 85.0) /* >85Hz is ridiculous */
rr = 85.0;
bytes = (vga->mode->x*vga->mode->y*vga->mode->z)/8;
membw = rr*bytes;
if(vga->membw != 0 && membw > vga->membw){
membw = vga->membw;
rr = (double)membw/bytes;
}
freq = rr*(vga->mode->ht*vga->mode->vt);
vga->mode->frequency = freq;
trace("using frequency %lud rr %.2f membw %lud\n",
freq, rr, membw);
}
else if(vga->mode->frequency == 0)
vga->mode->frequency = vga->mode->deffrequency;
for(ctlr = vga->link; ctlr; ctlr = ctlr->link){
if(ctlr->options == 0)
continue;
trace("%s->options\n", ctlr->name);
(*ctlr->options)(vga, ctlr);
}
/*
* skip init for vesa - vesa will do the registers for us
*/
if(!vga->vesa)
for(ctlr = vga->link; ctlr; ctlr = ctlr->link){
if(ctlr->init == 0)
continue;
trace("%s->init\n", ctlr->name);
(*ctlr->init)(vga, ctlr);
}
if(strcmp(vga->mode->chan, "") == 0){
if(vga->mode->z < nelem(chanstr) && chanstr[vga->mode->z])
strcpy(vga->mode->chan, chanstr[vga->mode->z]);
else
error("%s: unknown channel type to use for depth %d\n", vga->ctlr->name, vga->mode->z);
}
if(iflag || pflag)
dump(vga);
if(lflag){
trace("main->load\n");
if(vga->vmz && (vga->virtx*vga->virty*vga->mode->z)/8 > vga->vmz)
error("%s: not enough video memory - %lud\n",
vga->ctlr->name, vga->vmz);
if(vga->ctlr->type)
vtype = vga->ctlr->type;
else if(p = strchr(vga->ctlr->name, '-')){
strncpy(buf, vga->ctlr->name, p - vga->ctlr->name);
buf[p - vga->ctlr->name] = 0;
vtype = buf;
}
else
vtype = vga->ctlr->name;
vgactlw("type", vtype);
/*
* VESA must be set up before linear.
* Set type to vesa for linear.
*/
if(vga->vesa){
vesa.load(vga, vga->vesa);
if(vga->vesa->flag&Ferror)
error("vesa load error\n");
vgactlw("type", vesa.name);
}
/*
* The new draw device needs linear mode set
* before size.
*/
linear(vga);
/*
* Linear is over so switch to other driver for
* acceleration.
*/
if(vga->vesa)
vgactlw("type", vtype);
sprint(buf, "%ludx%ludx%d %s",
vga->virtx, vga->virty,
vga->mode->z, vga->mode->chan);
if(rflag){
vgactlr("size", sizeb);
if(rflag < 2 && strcmp(buf, sizeb) != 0)
error("bad refresh: %s != %s\n",
buf, sizeb);
}
else
vgactlw("size", buf);
/*
* No fiddling with registers if VESA set
* things up already. Sorry.
*/
if(!vga->vesa){
/*
* Turn off the display during the load.
*/
sequencer(vga, 0);
for(ctlr = vga->link; ctlr; ctlr = ctlr->link){
if(ctlr->load == 0 || ctlr == &vesa)
continue;
trace("%s->load\n", ctlr->name);
(*ctlr->load)(vga, ctlr);
}
sequencer(vga, 1);
}
vgactlw("drawinit", "");
if(vga->hwgc == 0 || cflag)
vgactlw("hwgc", "soft");
else
vgactlw("hwgc", vga->hwgc->name);
/* might as well initialize the cursor */
if((fd = open("/dev/cursor", OWRITE)) >= 0){
write(fd, buf, 0);
close(fd);
}
if(vga->virtx != vga->mode->x || vga->virty != vga->mode->y){
sprint(buf, "%dx%d", vga->mode->x, vga->mode->y);
vgactlw("actualsize", buf);
if(vga->panning)
vgactlw("panning", "on");
}
if(pflag)
dump(vga);
}
}
trace("main->exits\n");
exits(0);
}
|