3 #include "../port/lib.h"
8 #include "../port/pci.h"
13 #include "rebootcode.i"
16 * Where configuration info is left for the loaded programme.
17 * This will turn into a structure as more is done by the boot loader
18 * (e.g. why parse the .ini file twice?).
19 * There are 3584 bytes available at CONFADDR.
21 #define BOOTARGS ((char*)CONFADDR)
22 #define BOOTARGSLEN (16*KiB) /* limit in devenv.c */
24 #define MAXCONFLINE 160
27 Minmem = 256*MB, /* conservative default */
30 #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177)
32 extern char bdata[], edata[], end[], etext[];
34 uintptr kseg0 = KZERO;
35 Mach* machaddr[MAXMACH];
38 Memcache cachel[8]; /* arm arch v7 supports 1-7 */
40 * these are used by the cache.v7.s routines.
42 Lowmemcache *cacheconf;
50 /* store plan9.ini contents here at least until we stash them in #ec */
51 static char confname[MAXCONF][KNAMELEN];
52 static char confval[MAXCONF][MAXCONFLINE];
60 for(i = 0; i < nconf; i++)
61 if(cistrcmp(confname[i], name) == 0)
78 addconf(char *name, char *val)
84 if(val == nil || nconf >= MAXCONF)
87 strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
90 strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
106 /* convert to name=value\n format */
115 error("kernel configuration too large");
116 memmove(BOOTARGS, p, n);
117 memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
123 * assumes that we have loaded our /cfg/pxe/mac file at CONFADDR
124 * (usually 0x1000) with tftp in u-boot. no longer uses malloc, so
125 * can be called early.
132 k = (char *)CONFADDR;
136 for(; k && *k != '\0'; k = next) {
137 if (!isascii(*k)) /* sanity check */
139 next = strchr(k, '\n');
143 if (*k == '\0' || *k == '\n' || *k == '#')
147 continue; /* mal-formed line */
154 /* enable scheduling of this cpu */
159 if (active.machs[cpu] == 0) { /* currently off? */
160 active.machs[cpu] = 1;
166 /* disable scheduling of this cpu */
171 if (active.machs[cpu]) { /* currently on? */
172 active.machs[cpu] = 0;
188 if(machaddr[m->machno] != m) {
194 if (canlock(&testlock)) {
197 panic("cpu%d: locks don't work", m->machno);
203 if (m->machno != 0) {
204 /* synchronise with cpu 0 */
205 m->ticks = m0->ticks;
206 m->fastclock = m0->fastclock;
207 m->cpuhz = m0->cpuhz;
208 m->delayloop = m0->delayloop;
210 if (m->machno != 0 &&
211 (m->fastclock == 0 || m->cpuhz == 0 || m->delayloop == 0))
212 panic("buggered cpu 0 Mach");
218 /* l.s has already zeroed Mach, which now contains our stack. */
231 lock(&testlock); /* hold this forever */
235 l1cache->wbse(&active, sizeof active);
240 * count CPU's, set up their mach structures and l1 ptes.
241 * we're running on cpu 0 and our data structures were
242 * statically allocated.
251 for(mach = 1; mach < MAXMACH; mach++){
252 machaddr[mach] = mm = mallocalign(MACHSIZE, MACHSIZE, 0, 0);
253 l1 = mallocalign(L1SIZE, L1SIZE, 0, 0);
254 if(mm == nil || l1 == nil)
256 memset(mm, 0, MACHSIZE);
259 memmove(l1, (void *)L1, L1SIZE); /* clone cpu0's l1 table */
260 l1cache->wbse(l1, L1SIZE);
263 l1cache->wbse(mm, MACHSIZE);
265 l1cache->wbse(machaddr, sizeof machaddr);
270 dump(void *vaddr, int words)
276 iprint("%.8lux%c", *addr++, words % 8 == 0? '\n': ' ');
282 allcacheinfo(cachel);
283 cacheconf = (Lowmemcache *)CACHECONF;
284 cacheconf->l1waysh = cachel[1].waysh;
285 cacheconf->l1setsh = cachel[1].setsh;
286 /* on the tegra 2, l2 is unarchitected */
287 cacheconf->l2waysh = cachel[2].waysh;
288 cacheconf->l2setsh = cachel[2].setsh;
298 l2pages = KADDR(PHYSDRAM + DRAMSIZE - RESRVDHIMEM);
302 * at entry, l.s has set m for cpu0 and printed "Plan 9 from Be"
303 * but has not zeroed bss.
309 static ulong vfy = 0xcafebabe;
312 if (vfy != 0xcafebabe) {
315 panic("data segment misaligned");
318 memset(edata, 0, end - edata);
321 * we can't lock until smpon has run, but we're supposed to wait
322 * until l1 & l2 are on. too bad. l1 is on, l2 will soon be.
329 * data segment is aligned, bss is zeroed, caches' characteristics
330 * are known. begin initialisation.
338 /* want plan9.ini to be able to affect memory sizing in confinit */
339 plan9iniinit(); /* before we step on plan9.ini in low memory */
341 /* l2 looks for *l2off= in plan9.ini */
342 l2cache->on(); /* l2->on requires locks to work, thus smpon */
343 l2cache->info(&cachel[2]);
348 trapinit(); /* so confinit can probe memory to size it */
349 confinit(); /* figures out amount of memory */
350 /* xinit prints (if it can), so finish up the banner here. */
352 navailcpus = getncpus();
353 iprint("(mp arm; %d cpus)\n\n", navailcpus);
356 for (cpu = 1; cpu < navailcpus; cpu++)
360 irqtooearly = 0; /* now that xinit and trapinit have run */
362 mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ;
365 * Printinit will cause the first malloc call.
366 * (printinit->qopen->malloc) unless any of the
367 * above (like clockinit) do an irqenable, which
369 * If the system dies here it's probably due
370 * to malloc(->xalloc) not being initialised
371 * correctly, or the data segment is misaligned
372 * (it's amazing how far you can get with
373 * things like that completely broken).
375 * (Should be) boilerplate from here on.
378 archreset(); /* cfg clock signals, print cache cfg */
379 clockinit(); /* start clocks */
382 delay(50); /* let uart catch up */
396 iprint("pcireset...");
397 pcireset(); /* this tends to hang after a reboot */
400 chandevreset(); /* most devices are discovered here */
401 // i8250console(); /* too early; see init0 */
403 pageinit(); /* prints "1020M memory: ⋯ */
407 * starting a cpu will eventually result in it calling schedinit,
408 * so everything necessary to run user processes should be set up
409 * before starting secondary cpus.
412 /* SMP & FW are already on when we get here; u-boot set them? */
413 for (cpu = 1; cpu < navailcpus; cpu++)
414 if (startcpu(cpu) < 0)
415 panic("cpu%d didn't start", cpu);
419 panic("cpu%d: schedinit returned", m->machno);
423 * exit kernel either on a panic or user request
441 isaconfig(char *class, int ctlrno, ISAConf *isa)
446 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
456 isa->nopt = tokenize(p, isa->opt, NISAOPT);
457 for(i = 0; i < isa->nopt; i++){
459 if(cistrncmp(p, "type=", 5) == 0)
461 else if(cistrncmp(p, "port=", 5) == 0)
462 isa->port = strtoul(p+5, &p, 0);
463 else if(cistrncmp(p, "irq=", 4) == 0)
464 isa->irq = strtoul(p+4, &p, 0);
465 else if(cistrncmp(p, "dma=", 4) == 0)
466 isa->dma = strtoul(p+4, &p, 0);
467 else if(cistrncmp(p, "mem=", 4) == 0)
468 isa->mem = strtoul(p+4, &p, 0);
469 else if(cistrncmp(p, "size=", 5) == 0)
470 isa->size = strtoul(p+5, &p, 0);
471 else if(cistrncmp(p, "freq=", 5) == 0)
472 isa->freq = strtoul(p+5, &p, 0);
478 * the new kernel is already loaded at address `code'
479 * of size `size' and entry point `entry'.
482 reboot(void *entry, void *code, ulong size)
484 void (*f)(ulong, ulong, ulong);
489 * the boot processor is cpu0. execute this function on it
490 * so that the new kernel has the same cpu0.
492 if (m->machno != 0) {
499 * should be the only processor running now
503 /* turn off buffered serial console */
508 /* shutdown devices */
511 /* call off the dog */
517 /* setup reboot trampoline function */
518 f = (void*)REBOOTADDR;
519 memmove(f, rebootcode, sizeof(rebootcode));
525 /* off we go - never to return */
526 (*f)(PADDR(entry), PADDR(code), size);
530 * starting place for first process
535 char buf[2*KNAMELEN], **sp;
539 i8250console(); /* might be redundant, but harmless */
541 panic("init0: nil serialoq");
545 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
546 ksetenv("terminal", buf, 0);
547 ksetenv("cputype", "arm", 0);
549 ksetenv("service", "cpu", 0);
551 ksetenv("service", "terminal", 0);
553 /* convert plan9.ini variables to #e and #ec */
554 for(i = 0; i < nconf; i++) {
555 ksetenv(confname[i], confval[i], 0);
556 ksetenv(confname[i], confval[i], 1);
560 kproc("alarm", alarmkproc, 0);
562 sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
563 sp[3] = sp[2] = sp[1] = nil;
564 strcpy(sp[0] = (char*)&sp[4], "boot");
568 Conf conf; /* XXX - must go - gag */
570 Confmem tsmem[nelem(conf.mem)] = {
572 * Memory available to Plan 9:
574 { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
576 ulong memsize = DRAMSIZE;
583 /* back off a little from the end */
584 addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
585 if (probeaddr(addr) >= 0) { /* didn't trap? memory present */
600 if(p = getconf("service")){
601 if(strcmp(p, "cpu") == 0)
603 else if(strcmp(p,"terminal") == 0)
608 * Copy the physical memory configuration to Conf.mem.
610 if(nelem(tsmem) > nelem(conf.mem)){
611 iprint("memory configuration botch\n");
614 if(0 && (p = getconf("*maxmem")) != nil) {
615 memsize = strtoul(p, 0, 0) - PHYSDRAM;
616 if (memsize < 16*MB) /* sanity */
621 * see if all that memory exists; if not, find out how much does.
622 * trapinit must have been called first.
624 if (gotmem(memsize - RESRVDHIMEM) < 0)
625 panic("can't find 1GB of memory");
627 tsmem[0].limit = PHYSDRAM + memsize;
628 memmove(conf.mem, tsmem, sizeof(tsmem));
631 pa = PADDR(PGROUND((uintptr)end));
634 * we assume that the kernel is at the beginning of one of the
635 * contiguous chunks of memory and fits therein.
637 for(i=0; i<nelem(conf.mem); i++){
638 /* take kernel out of allocatable space */
639 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
640 conf.mem[i].base = pa;
642 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
643 conf.npage += conf.mem[i].npage;
646 conf.upages = (conf.npage*80)/100;
647 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
649 /* set up other configuration parameters */
650 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
653 if(conf.nproc > 2000)
655 conf.nswap = conf.npage*3;
660 * it's simpler on mp systems to take page-faults early,
661 * on reference, rather than later, on write, which might
662 * require tlb shootdowns.
664 conf.copymode = 1; /* copy on reference */
667 * Guess how much is taken by the large permanent
668 * datastructures. Mntcache and Mntrpc are not accounted for.
670 kpages = conf.npage - conf.upages;
672 kpages -= conf.upages*sizeof(Page)
673 + conf.nproc*sizeof(Proc)
674 + conf.nimage*sizeof(Image)
676 + conf.nswppo*sizeof(Page*);
677 mainmem->maxsize = kpages;
680 * give terminals lots of image memory, too; the dynamic
681 * allocation will balance the load properly, hopefully.
682 * be careful with 32-bit overflow.
684 imagmem->maxsize = kpages;
688 advertwfi(void) /* advertise my wfi status */
691 active.wfi |= 1 << m->machno;
696 unadvertwfi(void) /* do not advertise my wfi status */
699 active.wfi &= ~(1 << m->machno);
709 /* don't go into wfi until my local timer is ticking */
715 /* avoid recursion via ilock, advertise iff this cpu is initialised */
716 if (m->inidlehands == 1 && m->syscall > 0) {
736 * find any cpu other than me currently in wfi.
739 cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));
746 setupwatchpts(Proc *, Watchpt *, int n)
749 error("no watchpoints");