]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/mtx/main.c
bcm64: fix usb xhci controller on pi4 8GB variant (thanks richard miller)
[plan9front.git] / sys / src / 9 / mtx / main.c
1 #include        "u.h"
2 #include        "tos.h"
3 #include        "../port/lib.h"
4 #include        "mem.h"
5 #include        "dat.h"
6 #include        "fns.h"
7 #include        "io.h"
8 #include        "pool.h"
9
10 Conf    conf;
11 FPsave  initfp;
12
13 void
14 main(void)
15 {
16         memset(edata, 0, (ulong)end-(ulong)edata);
17         conf.nmach = 1;
18         machinit();
19         ioinit();
20         i8250console();
21         quotefmtinstall();
22         print("\nPlan 9\n");
23         confinit();
24         xinit();
25         raveninit();
26         trapinit();
27         printinit();
28         cpuidprint();
29         mmuinit();
30         hwintrinit();
31         clockinit();
32         procinit0();
33         initseg();
34         timersinit();
35         links();
36         chandevreset();
37         pageinit();
38         fpsave(&initfp);
39         initfp.fpscr = 0;
40         userinit();
41         schedinit();
42 }
43
44 void
45 machinit(void)
46 {
47         memset(m, 0, sizeof(Mach));
48         m->cputype = getpvr()>>16;
49
50         /*
51          * For polled uart output at boot, need
52          * a default delay constant. 100000 should
53          * be enough for a while. Cpuidentify will
54          * calculate the real value later.
55          */
56         m->loopconst = 100000;
57
58         /* turn on caches */
59         puthid0(gethid0() | BIT(16) | BIT(17));
60
61         active.machs[0] = 1;
62         active.exiting = 0;
63 }
64
65 void
66 cpuidprint(void)
67 {
68         char *id;
69
70         id = "unknown PowerPC";
71         switch(m->cputype) {
72         case 9:
73                 id = "PowerPC 604e";
74                 break;
75         }
76         print("cpu0: %s\n", id);
77 }
78
79 static struct
80 {
81         char    *name;
82         char *val;
83 }
84 plan9ini[] =
85 {
86         { "console", "0" },
87         { "ether0", "type=2114x" },
88 };
89
90 char*
91 getconf(char *name)
92 {
93         int i;
94
95         for(i = 0; i < nelem(plan9ini); i++)
96                 if(cistrcmp(name, plan9ini[i].name) == 0)
97                         return plan9ini[i].val;
98         return nil;
99 }
100
101 void
102 init0(void)
103 {
104         char buf[2*KNAMELEN];
105
106         chandevinit();
107
108         if(!waserror()){
109                 snprint(buf, sizeof(buf), "power %s mtx", conffile);
110                 ksetenv("terminal", buf, 0);
111                 ksetenv("cputype", "power", 0);
112                 if(cpuserver)
113                         ksetenv("service", "cpu", 0);
114                 else
115                         ksetenv("service", "terminal", 0);
116                 poperror();
117         }
118         kproc("alarm", alarmkproc, 0);
119         kproc("mmusweep", mmusweep, 0);
120         touser((void*)(USTKTOP - sizeof(Tos)));
121 }
122
123 /* still to do */
124 void
125 reboot(void*, void*, ulong)
126 {
127 }
128
129 void
130 exit(int)
131 {
132         cpushutdown();
133         watchreset();
134 }
135
136 /*
137  *  set up floating point for a new process
138  */
139 void
140 procsetup(Proc *p)
141 {
142         p->fpstate = FPinit;
143 }
144
145 void
146 procfork(Proc *)
147 {
148 }
149
150 /*
151  *  Save the mach dependent part of the process state.
152  */
153 void
154 procsave(Proc *p)
155 {
156         if(p->fpstate == FPactive){
157                 if(p->state != Moribund)
158                         fpsave(up->fpsave);
159                 p->fpstate = FPinactive;
160         }
161 }
162
163 void
164 confinit(void)
165 {
166         char *p;
167         int userpcnt;
168         ulong pa, kpages;
169         extern ulong memsize;   /* passed in from ROM monitor */
170
171         if(p = getconf("*kernelpercent"))
172                 userpcnt = 100 - strtol(p, 0, 0);
173         else
174                 userpcnt = 0;
175
176         pa = PGROUND(PADDR(end));
177
178         conf.mem[0].npage = memsize/BY2PG;
179         conf.mem[0].base = pa;
180         conf.npage = conf.mem[0].npage;
181
182         conf.nmach = 1;
183         conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
184         if(cpuserver)
185                 conf.nproc *= 3;
186         if(conf.nproc > 2000)
187                 conf.nproc = 2000;
188         conf.nimage = 200;
189         conf.nswap = conf.nproc*80;
190         conf.nswppo = 4096;
191         conf.copymode = 0;                      /* copy on write */
192
193         if(cpuserver) {
194                 if(userpcnt < 10)
195                         userpcnt = 70;
196                 kpages = conf.npage - (conf.npage*userpcnt)/100;
197
198                 /*
199                  * Hack for the big boys. Only good while physmem < 4GB.
200                  * Give the kernel a max. of 16MB + enough to allocate the
201                  * page pool.
202                  * This is an overestimate as conf.upages < conf.npages.
203                  * The patch of nimage is a band-aid, scanning the whole
204                  * page list in imagereclaim just takes too long.
205                  */
206                 if(kpages > (16*MB + conf.npage*sizeof(Page))/BY2PG){
207                         kpages = (16*MB + conf.npage*sizeof(Page))/BY2PG;
208                         conf.nimage = 2000;
209                         kpages += (conf.nproc*KSTACK)/BY2PG;
210                 }
211         } else {
212                 if(userpcnt < 10) {
213                         if(conf.npage*BY2PG < 16*MB)
214                                 userpcnt = 40;
215                         else
216                                 userpcnt = 60;
217                 }
218                 kpages = conf.npage - (conf.npage*userpcnt)/100;
219
220                 /*
221                  * Make sure terminals with low memory get at least
222                  * 4MB on the first Image chunk allocation.
223                  */
224                 if(conf.npage*BY2PG < 16*MB)
225                         imagmem->minarena = 4*1024*1024;
226         }
227         conf.upages = conf.npage - kpages;
228         conf.ialloc = (kpages/2)*BY2PG;
229
230         /*
231          * Guess how much is taken by the large permanent
232          * datastructures. Mntcache and Mntrpc are not accounted for.
233          */
234         kpages *= BY2PG;
235         kpages -= conf.upages*sizeof(Page)
236                 + conf.nproc*sizeof(Proc)
237                 + conf.nimage*sizeof(Image)
238                 + conf.nswap
239                 + conf.nswppo*sizeof(Page*);
240         mainmem->maxsize = kpages;
241         if(!cpuserver){
242                 /*
243                  * give terminals lots of image memory, too; the dynamic
244                  * allocation will balance the load properly, hopefully.
245                  * be careful with 32-bit overflow.
246                  */
247                 imagmem->maxsize = kpages;
248         }
249
250 //      conf.monitor = 1;       /* BUG */
251 }
252
253 static int
254 getcfields(char* lp, char** fields, int n, char* sep)
255 {
256         int i;
257
258         for(i = 0; lp && *lp && i < n; i++){
259                 while(*lp && strchr(sep, *lp) != 0)
260                         *lp++ = 0;
261                 if(*lp == 0)
262                         break;
263                 fields[i] = lp;
264                 while(*lp && strchr(sep, *lp) == 0){
265                         if(*lp == '\\' && *(lp+1) == '\n')
266                                 *lp++ = ' ';
267                         lp++;
268                 }
269         }
270
271         return i;
272 }
273
274 int
275 isaconfig(char *class, int ctlrno, ISAConf *isa)
276 {
277         char cc[32], *p, *x;
278         int i;
279
280         snprint(cc, sizeof cc, "%s%d", class, ctlrno);
281         p = getconf(cc);
282         if(p == nil)
283                 return 0;
284
285         x = nil;
286         kstrdup(&x, p);
287         p = x;
288
289         isa->type = "";
290         isa->nopt = tokenize(p, isa->opt, NISAOPT);
291         for(i = 0; i < isa->nopt; i++){
292                 p = isa->opt[i];
293                 if(cistrncmp(p, "type=", 5) == 0)
294                         isa->type = p + 5;
295                 else if(cistrncmp(p, "port=", 5) == 0)
296                         isa->port = strtoul(p+5, &p, 0);
297                 else if(cistrncmp(p, "irq=", 4) == 0)
298                         isa->irq = strtoul(p+4, &p, 0);
299                 else if(cistrncmp(p, "dma=", 4) == 0)
300                         isa->dma = strtoul(p+4, &p, 0);
301                 else if(cistrncmp(p, "mem=", 4) == 0)
302                         isa->mem = strtoul(p+4, &p, 0);
303                 else if(cistrncmp(p, "size=", 5) == 0)
304                         isa->size = strtoul(p+5, &p, 0);
305                 else if(cistrncmp(p, "freq=", 5) == 0)
306                         isa->freq = strtoul(p+5, &p, 0);
307         }
308         return 1;
309 }
310
311 void
312 setupwatchpts(Proc *, Watchpt *, int n)
313 {
314         if(n > 0)
315                 error("no watchpoints");
316 }