aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/xfree86/os-support/pmax/pmax_init.c
blob: 7da733d226f38888f44373785aeefa6f326e4aac (plain)
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
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/pmax/pmax_init.c,v 1.2.2.2 1998/07/18 17:53:54 dawes Exp $ */
/*
 * Copyright 1998 by Concurrent Computer Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Concurrent Computer
 * Corporation not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Concurrent Computer Corporation makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 * CONCURRENT COMPUTER CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD
 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CONCURRENT COMPUTER CORPORATION BE
 * LIABLE FOR ANY SPECIAL, 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.
 *
 * Copyright 1998 by Metro Link Incorporated
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Metro Link
 * Incorporated not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Metro Link Incorporated makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 * METRO LINK INCORPORATED DISCLAIMS ALL WARRANTIES WITH REGARD
 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL METRO LINK INCORPORATED BE
 * LIABLE FOR ANY SPECIAL, 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.
 *
 * This file was derived in part from the original XFree86 sysv OS
 * support which contains the following copyright notice:
 *
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany
 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of Thomas Roell and David Wexelblat 
 * not be used in advertising or publicity pertaining to distribution of 
 * the software without specific, written prior permission.  Thomas Roell and
 * David Wexelblat makes no representations about the suitability of this 
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 *
 * THOMAS ROELL AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD TO 
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR DAVID WEXELBLAT BE LIABLE FOR 
 * ANY SPECIAL, 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.
 *
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include <sys/types.h>
#include <time.h>
#include <errno.h>

#include <sys/prosrfs.h>
#include <sys/cpu.h>
#include <sys/ipl.h>

#include <X11/X.h>
#include <X11/Xmd.h>

#include "compiler.h"

#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"

static Bool KeepTty = FALSE;
static Bool Protect0 = FALSE;
static Bool pmaxInitialized = FALSE;

#define VT_DEFAULT -2
#define VT_NONE    -1

static int VTnum = VT_DEFAULT;

extern void pmax_init_splmap(void);

int  pmax_sys_type;  /* Also used by pmax_pci.c */

/*
 *      PowerMAXOS_sys_type()
 *
 *      Determine type of PowerHawk, PowerStack, PowerMaxion, or NightHawk
 */
int
PowerMAXOS_sys_type(void)
{ 
  int             fd;
  procfile_t      procfile;
  
  fd = open("/system/processor/0",O_RDONLY);
  if (fd<0) {
    FatalError("Cannot open '%s'\n", "/system/processor/0");
  }
  
  if (read(fd, &procfile, sizeof(procfile)) < 0) {
    FatalError("Cannot read '%s'\n", "/system/processor/0");
  }
  close(fd);
  
  return(procfile.cpu_model);
}

void
pmaxInit(void)
{
	 char *mach;
	 
     if (pmaxInitialized)
	  return;

     pmaxInitialized = TRUE;
     
     /*
      * Determine type of machine
      */
     pmax_sys_type = PowerMAXOS_sys_type();
     switch(pmax_sys_type) {
	  
     case MODEL_NH6400:
	  mach ="PowerMAXION (NH6400)";
	  break;
	  
     case MODEL_NH6408:
	  mach = "PowerMAXION (NH6408)";
	  break;      

     case MODEL_NH6800T:
	  mach = "TurboHawk";
	  break;      
	  
     case MODEL_MPWR:
	  mach = "PowerStack";
	  break;
	  
     case MODEL_PH610:
	  mach = "PowerHawk 610";
	  break;
	  
     case MODEL_MPWR2:
	  mach = "PowerStack II (utah)";
	  break;
	  
     case MODEL_PH620:
	  mach = "PowerHawk 620";
	  break;
	  
     case MODEL_PH640:
	  mach = "PowerHawk 640";
	  break;

     case MODEL_MMTX:
	  mach = "PowerStack II (MTX)";
	  break;
	  
     default:
	  FatalError("pmaxInit: Unknown/unsupported machine type 0x%x\n",
		     pmax_sys_type);
	  /*NOTREACHED*/
     }
	 
     xf86Msg(X_INFO, "pmaxInit: Machine type: %s\n", mach);

     /*
      * Map IPL hardware so that interrupts can be (temporarily) disabled
      * (see pmax_video.c) 
      */
     pmax_init_splmap();

     /*
      * Now that we know the system type, initialize the
      * pci access routines
      */
     pciInit();
}

void
xf86OpenConsole()
{
    struct vt_mode VT;
    char vtname[10];
    MessageType from = X_DEFAULT;

    if (serverGeneration == 1) 
    {
    
    	/* check if we're run with euid==0 */
    	if (geteuid() != 0)
	{
      	    FatalError("xf86OpenConsole: Server must be suid root\n");
	}

	/* Protect page 0 to help find NULL dereferencing */
	/* mprotect() doesn't seem to work */
	if (Protect0)
	{
	    int fd = -1;

	    if ((fd = open("/dev/zero", O_RDONLY, 0)) < 0)
	    {
		xf86Msg(X_WARNING,
			"xf86OpenConsole: cannot open /dev/zero (%s)\n",
			strerror(errno));
	    }
	    else
	    {
		if ((int)mmap(0, 0x1000, PROT_NONE,
			      MAP_FIXED | MAP_SHARED, fd, 0) == -1)
		{
		    xf86Msg(X_WARNING,
			"xf86OpenConsole: failed to protect page 0 (%s)\n",
			strerror(errno));
		}
		close(fd);
	    }
	}

	pmaxInit();  /* Initialize OS specific functions */
	
    	/*
     	 * setup the virtual terminal manager
     	 */
	if (VTnum == VT_DEFAULT) {
	    int fd;
	    
	    /*
	     * No specific VT specified, so ask the vtl term mgr
	     * for the next available VT
	     */
	    if ((fd = open("/dev/vt00",O_WRONLY,0)) < 0) {
        	xf86Msg(X_WARNING,
			"xf86OpenConsole: Could not open /dev/vt00 (%s)\n",
			strerror(errno));
		VTnum = VT_NONE;
	    }
	    else {
		if (ioctl(fd, VT_OPENQRY, &VTnum) < 0)
		{
		    xf86Msg(X_WARNING,
			    "xf86OpenConsole: Cannot find a free VT\n");
		    VTnum = VT_NONE;
		}
		close(fd);
	    }
	} else {
	    from = X_CMDLINE;
	}

	xf86Info.vtno = VTnum;

	if (xf86Info.vtno == VT_NONE)
	    strcpy(vtname, "/dev/null");
	else
	    sprintf(vtname,"/dev/vt%02d",xf86Info.vtno);
	    
        xf86Msg(from, "using VT \"%s\"\n\n", vtname);
	
	if (!KeepTty)
    	{
    	    setpgrp();
	}

	if ((xf86Info.consoleFd = open(vtname, O_RDWR|O_NDELAY, 0)) < 0)
	{
            FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
		       vtname, strerror(errno));
	}

	if (xf86Info.vtno != VT_NONE)
	{
	     /* change ownership of the vt */
	     (void) chown(vtname, getuid(), getgid());

	     /*
	      * now get the VT
	      */
	     if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0)
	     {
		  xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
	     }
	     if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) != 0)
	     {
		  xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
	     }
	     if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0) 
	     {
		  FatalError("xf86OpenConsole: VT_GETMODE failed\n");
	     }

	     signal(SIGUSR1, xf86VTRequest);
	     
	     VT.mode = VT_PROCESS;
	     VT.relsig = SIGUSR1;
	     VT.acqsig = SIGUSR1;
	     if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0) 
	     {
		  FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
	     }
	     if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
	     {
		  FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed\n");
	     }
	}
    }
    else 
    {   
	/* serverGeneration != 1 */
	/*
	 * now get the VT
	 */
	if (xf86Info.vtno != VT_NONE)
	{
	     if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0)
	     {
		  xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
	     }
	     if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) != 0)
	     {
		  xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
	     }
	     /*
	      * If the server doesn't have the VT when the reset occurs,
	      * this is to make sure we don't continue until the activate
	      * signal is received.
	      */
	     if (!xf86Screens[0]->vtSema)
		  sleep(5);
	}
    }
    return;
}

void xf86CloseConsole()
{
    struct vt_mode   VT;

    if (xf86Info.vtno != VT_NONE)
    {
	 
#if 0
	 ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno);
	 ioctl(xf86Info.consoleFd, VT_WAITACTIVE, 0);
#endif
	 ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT);  /* Back to text mode ... */
	 if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1)
	 {
	      VT.mode = VT_AUTO;
	      ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); /* set dflt vt handling */
	 }
    }
    
    close(xf86Info.consoleFd);                 /* make the vt-manager happy */
    return;
}

int xf86ProcessArgument(argc, argv, i)
int argc;
char *argv[];
int i;
{
	/*
	 * Keep server from detaching from controlling tty.  This is useful 
	 * when debugging (so the server can receive keyboard signals.
	 */
	if (!strcmp(argv[i], "-keeptty"))
	{
		KeepTty = TRUE;
		return(1);
	}

	/*
	 * Prevent server from attemping to open a new VT in the "-novt"
	 * flag was specified.
	 */
	if (!strcmp(argv[i], "-novt"))
	{
		VTnum = VT_NONE;
		return(1);
	}

	/*
	 * Undocumented flag to protect page 0 from read/write to help
	 * catch NULL pointer dereferences.  This is purely a debugging
	 * flag.
	 */
	if (!strcmp(argv[i], "-protect0"))
	{
		Protect0 = TRUE;
		return(1);
	}

	if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
	{
		if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
		{
			UseMsg();
			VTnum = VT_DEFAULT;
			return(0);
		}
		return(1);
	}
	return(0);
}

void xf86UseMsg()
{
	ErrorF("vtXX                   use the specified VT number\n");
	ErrorF("-keeptty               ");
	ErrorF("don't detach controlling tty (for debugging only)\n");
	ErrorF("-novt                  ");
	ErrorF("don't allocate and open a new virtual terminal\n");
	return;
}


void
xf86_pmax_usleep(unsigned long n)
{
  struct timespec requested,remaining;
  int rv;

  requested.tv_sec = n/1000000;
  requested.tv_nsec = (n % 1000000) * 1000;
  
  while ((rv = nanosleep(&requested,&remaining)) < 0) {
	  if (errno != EINTR)
		  break;
	  
	  remaining = requested; /* structure assignment */
  }
    
  if (rv) {
      ErrorF("xf86_pmax_usleep: nanosleep() failed: rv=%d, errno=%d\n", rv, errno);
  }
}

#ifndef usleep

void
usleep(unsigned long n)
{
	xf86_pmax_usleep(n);
}

#endif