]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/aim/ofw_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / powerpc / aim / ofw_machdep.c
1 /*-
2  * Copyright (C) 1996 Wolfgang Solfrank.
3  * Copyright (C) 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by TooLs GmbH.
17  * 4. The name of TooLs GmbH may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/systm.h>
40 #include <sys/conf.h>
41 #include <sys/disk.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/smp.h>
45 #include <sys/stat.h>
46
47 #include <net/ethernet.h>
48
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_pci.h>
51 #include <dev/ofw/ofw_bus.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_page.h>
56
57 #include <machine/bus.h>
58 #include <machine/cpu.h>
59 #include <machine/md_var.h>
60 #include <machine/platform.h>
61 #include <machine/ofw_machdep.h>
62
63 #define OFMEM_REGIONS   32
64 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
65 static struct mem_region OFfree[OFMEM_REGIONS + 3];
66
67 struct mem_region64 {
68         vm_offset_t     mr_start_hi;
69         vm_offset_t     mr_start_lo;
70         vm_size_t       mr_size;
71 };      
72
73 extern register_t ofmsr[5];
74 extern struct   pmap ofw_pmap;
75 static int      (*ofwcall)(void *);
76 static void     *fdt;
77 int             ofw_real_mode;
78
79 static void     ofw_quiesce(void);
80 static int      openfirmware(void *args);
81
82 /*
83  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
84  */
85 register_t      ofw_sprg0_save;
86
87 static __inline void
88 ofw_sprg_prepare(void)
89 {
90         /*
91          * Assume that interrupt are disabled at this point, or
92          * SPRG1-3 could be trashed
93          */
94         __asm __volatile("mfsprg0 %0\n\t"
95                          "mtsprg0 %1\n\t"
96                          "mtsprg1 %2\n\t"
97                          "mtsprg2 %3\n\t"
98                          "mtsprg3 %4\n\t"
99                          : "=&r"(ofw_sprg0_save)
100                          : "r"(ofmsr[1]),
101                          "r"(ofmsr[2]),
102                          "r"(ofmsr[3]),
103                          "r"(ofmsr[4]));
104 }
105
106 static __inline void
107 ofw_sprg_restore(void)
108 {
109         /*
110          * Note that SPRG1-3 contents are irrelevant. They are scratch
111          * registers used in the early portion of trap handling when
112          * interrupts are disabled.
113          *
114          * PCPU data cannot be used until this routine is called !
115          */
116         __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
117 }
118
119 /*
120  * Memory region utilities: determine if two regions overlap,
121  * and merge two overlapping regions into one
122  */
123 static int
124 memr_overlap(struct mem_region *r1, struct mem_region *r2)
125 {
126         if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
127             (r2->mr_start + r2->mr_size) < r1->mr_start)
128                 return (FALSE);
129         
130         return (TRUE);  
131 }
132
133 static void
134 memr_merge(struct mem_region *from, struct mem_region *to)
135 {
136         int end;
137         end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
138         to->mr_start = imin(from->mr_start, to->mr_start);
139         to->mr_size = end - to->mr_start;
140 }
141
142 /*
143  * This is called during powerpc_init, before the system is really initialized.
144  * It shall provide the total and the available regions of RAM.
145  * Both lists must have a zero-size entry as terminator.
146  * The available regions need not take the kernel into account, but needs
147  * to provide space for two additional entry beyond the terminating one.
148  */
149 void
150 ofw_mem_regions(struct mem_region **memp, int *memsz,
151                 struct mem_region **availp, int *availsz)
152 {
153         phandle_t phandle;
154         int asz, msz, fsz;
155         int i, j;
156         int still_merging;
157         cell_t address_cells;
158
159         asz = msz = 0;
160
161         /*
162          * Get #address-cells from root node, defaulting to 1 if it cannot
163          * be found.
164          */
165         phandle = OF_finddevice("/");
166         if (OF_getprop(phandle, "#address-cells", &address_cells, 
167             sizeof(address_cells)) < sizeof(address_cells))
168                 address_cells = 1;
169         
170         /*
171          * Get memory.
172          */
173         if ((phandle = OF_finddevice("/memory")) == -1
174             || (asz = OF_getprop(phandle, "available",
175                   OFavail, sizeof OFavail[0] * OFMEM_REGIONS)) <= 0)
176         {
177                 if (ofw_real_mode) {
178                         /* XXX MAMBO */
179                         printf("Physical memory unknown -- guessing 128 MB\n");
180
181                         /* Leave the first 0xA000000 bytes for the kernel */
182                         OFavail[0].mr_start = 0xA00000;
183                         OFavail[0].mr_size = 0x75FFFFF;
184                         asz = sizeof(OFavail[0]);
185                 } else {
186                         panic("no memory?");
187                 }
188         }
189
190         if (address_cells == 2) {
191             struct mem_region64 OFmem64[OFMEM_REGIONS + 1];
192             if ((phandle == -1) || (msz = OF_getprop(phandle, "reg",
193                           OFmem64, sizeof OFmem64[0] * OFMEM_REGIONS)) <= 0) {
194                 if (ofw_real_mode) {
195                         /* XXX MAMBO */
196                         OFmem64[0].mr_start_hi = 0;
197                         OFmem64[0].mr_start_lo = 0x0;
198                         OFmem64[0].mr_size = 0x7FFFFFF;
199                         msz = sizeof(OFmem64[0]);
200                 } else {
201                         panic("Physical memory map not found");
202                 }
203             }
204
205             for (i = 0, j = 0; i < msz/sizeof(OFmem64[0]); i++) {
206                 if (OFmem64[i].mr_start_hi == 0) {
207                         OFmem[i].mr_start = OFmem64[i].mr_start_lo;
208                         OFmem[i].mr_size = OFmem64[i].mr_size;
209
210                         /*
211                          * Check for memory regions extending above 32-bit
212                          * memory space, and restrict them to stay there.
213                          */
214                         if (((uint64_t)OFmem[i].mr_start +
215                             (uint64_t)OFmem[i].mr_size) >
216                             BUS_SPACE_MAXADDR_32BIT) {
217                                 OFmem[i].mr_size = BUS_SPACE_MAXADDR_32BIT -
218                                     OFmem[i].mr_start;
219                         }
220                         j++;
221                 }
222             }
223             msz = j*sizeof(OFmem[0]);
224         } else {
225             if ((msz = OF_getprop(phandle, "reg",
226                           OFmem, sizeof OFmem[0] * OFMEM_REGIONS)) <= 0)
227                 panic("Physical memory map not found");
228         }
229
230         *memp = OFmem;
231         *memsz = msz / sizeof(struct mem_region);
232         
233
234         /*
235          * OFavail may have overlapping regions - collapse these
236          * and copy out remaining regions to OFfree
237          */
238         asz /= sizeof(struct mem_region);
239         do {
240                 still_merging = FALSE;
241                 for (i = 0; i < asz; i++) {
242                         if (OFavail[i].mr_size == 0)
243                                 continue;
244                         for (j = i+1; j < asz; j++) {
245                                 if (OFavail[j].mr_size == 0)
246                                         continue;
247                                 if (memr_overlap(&OFavail[j], &OFavail[i])) {
248                                         memr_merge(&OFavail[j], &OFavail[i]);
249                                         /* mark inactive */
250                                         OFavail[j].mr_size = 0;
251                                         still_merging = TRUE;
252                                 }
253                         }
254                 }
255         } while (still_merging == TRUE);
256
257         /* evict inactive ranges */
258         for (i = 0, fsz = 0; i < asz; i++) {
259                 if (OFavail[i].mr_size != 0) {
260                         OFfree[fsz] = OFavail[i];
261                         fsz++;
262                 }
263         }
264
265         *availp = OFfree;
266         *availsz = fsz;
267 }
268
269 void
270 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
271 {
272         if (ofmsr[0] & PSL_DR)
273                 ofw_real_mode = 0;
274         else
275                 ofw_real_mode = 1;
276
277         ofwcall = openfirm;
278         fdt = fdt_ptr;
279 }
280
281 boolean_t
282 OF_bootstrap()
283 {
284         boolean_t status = FALSE;
285
286         if (ofwcall != NULL) {
287                 if (ofw_real_mode)
288                         status = OF_install(OFW_STD_REAL, 0);
289                 else
290                         status = OF_install(OFW_STD_DIRECT, 0);
291
292                 if (status != TRUE)
293                         return status;
294
295                 OF_init(openfirmware);
296
297                 /*
298                  * On some machines, we need to quiesce OF to turn off
299                  * background processes.
300                  */
301                 ofw_quiesce();
302         } else {
303                 status = OF_install(OFW_FDT, 0);
304
305                 if (status != TRUE)
306                         return status;
307
308                 OF_init(fdt);
309         } 
310
311         return (status);
312 }
313
314 static void
315 ofw_quiesce(void)
316 {
317         phandle_t rootnode;
318         char model[32];
319         struct {
320                 cell_t name;
321                 cell_t nargs;
322                 cell_t nreturns;
323         } args;
324
325         /*
326          * Only quiesce Open Firmware on PowerMac11,2 and 12,1. It is
327          * necessary there to shut down a background thread doing fan
328          * management, and is harmful on other machines.
329          *
330          * Note: we don't need to worry about which OF module we are
331          * using since this is called only from very early boot, within
332          * OF's boot context.
333          */
334
335         rootnode = OF_finddevice("/");
336         if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
337                 if (strcmp(model, "PowerMac11,2") == 0 ||
338                     strcmp(model, "PowerMac12,1") == 0) {
339                         args.name = (cell_t)(uintptr_t)"quiesce";
340                         args.nargs = 0;
341                         args.nreturns = 0;
342                         openfirmware(&args);
343                 }
344         }
345 }
346
347 static int
348 openfirmware_core(void *args)
349 {
350         long    oldmsr;
351         int     result;
352         u_int   srsave[16];
353         u_int   i;
354
355         __asm __volatile(       "\t"
356                 "sync\n\t"
357                 "mfmsr  %0\n\t"
358                 "mtmsr  %1\n\t"
359                 "isync\n"
360                 : "=r" (oldmsr)
361                 : "r" (ofmsr[0])
362         );
363
364         ofw_sprg_prepare();
365
366         if (pmap_bootstrapped && !ofw_real_mode) {
367                 /*
368                  * Swap the kernel's address space with Open Firmware's
369                  */
370                 for (i = 0; i < 16; i++) {
371                         srsave[i] = mfsrin(i << ADDR_SR_SHFT);
372                         mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
373                 }
374
375                 /*
376                  * Clear battable[] translations
377                  */
378                 if (!(cpu_features & PPC_FEATURE_64)) {
379                         __asm __volatile("mtdbatu 2, %0\n"
380                                          "mtdbatu 3, %0" : : "r" (0));
381                 }
382                 isync();
383         }
384
385         result = ofwcall(args);
386
387         if (pmap_bootstrapped && !ofw_real_mode) {
388                 /*
389                  * Restore the kernel's addr space. The isync() doesn;t
390                  * work outside the loop unless mtsrin() is open-coded
391                  * in an asm statement :(
392                  */
393                 for (i = 0; i < 16; i++) {
394                         mtsrin(i << ADDR_SR_SHFT, srsave[i]);
395                         isync();
396                 }
397         }
398
399         ofw_sprg_restore();
400
401         __asm(  "\t"
402                 "mtmsr  %0\n\t"
403                 "isync\n"
404                 : : "r" (oldmsr)
405         );
406
407         return (result);
408 }
409
410 #ifdef SMP
411 struct ofw_rv_args {
412         void *args;
413         int retval;
414         volatile int in_progress;
415 };
416
417 static void
418 ofw_rendezvous_dispatch(void *xargs)
419 {
420         struct ofw_rv_args *rv_args = xargs;
421
422         /* NOTE: Interrupts are disabled here */
423
424         if (PCPU_GET(cpuid) == 0) {
425                 /*
426                  * Execute all OF calls on CPU 0
427                  */
428                 rv_args->retval = openfirmware_core(rv_args->args);
429                 rv_args->in_progress = 0;
430         } else {
431                 /*
432                  * Spin with interrupts off on other CPUs while OF has
433                  * control of the machine.
434                  */
435                 while (rv_args->in_progress)
436                         cpu_spinwait();
437         }
438 }
439 #endif
440
441 static int
442 openfirmware(void *args)
443 {
444         int result;
445         #ifdef SMP
446         struct ofw_rv_args rv_args;
447         #endif
448
449         if (pmap_bootstrapped && ofw_real_mode)
450                 args = (void *)pmap_kextract((vm_offset_t)args);
451
452         #ifdef SMP
453         rv_args.args = args;
454         rv_args.in_progress = 1;
455         smp_rendezvous(smp_no_rendevous_barrier, ofw_rendezvous_dispatch,
456             smp_no_rendevous_barrier, &rv_args);
457         result = rv_args.retval;
458         #else
459         result = openfirmware_core(args);
460         #endif
461
462         return (result);
463 }
464
465 void
466 OF_halt()
467 {
468         int retval;     /* dummy, this may not be needed */
469
470         OF_interpret("shut-down", 1, &retval);
471         for (;;);       /* just in case */
472 }
473
474 void
475 OF_reboot()
476 {
477         int retval;     /* dummy, this may not be needed */
478
479         OF_interpret("reset-all", 1, &retval);
480         for (;;);       /* just in case */
481 }
482
483 void
484 OF_getetheraddr(device_t dev, u_char *addr)
485 {
486         phandle_t       node;
487
488         node = ofw_bus_get_node(dev);
489         OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
490 }
491
492 /*
493  * Return a bus handle and bus tag that corresponds to the register
494  * numbered regno for the device referenced by the package handle
495  * dev. This function is intended to be used by console drivers in
496  * early boot only. It works by mapping the address of the device's
497  * register in the address space of its parent and recursively walk
498  * the device tree upward this way.
499  */
500 static void
501 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
502 {
503         char name[16];
504         uint32_t addr, size;
505         int pci, res;
506
507         res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
508         if (res == -1)
509                 addr = 2;
510         res = OF_getprop(node, "#size-cells", &size, sizeof(size));
511         if (res == -1)
512                 size = 1;
513         pci = 0;
514         if (addr == 3 && size == 2) {
515                 res = OF_getprop(node, "name", name, sizeof(name));
516                 if (res != -1) {
517                         name[sizeof(name) - 1] = '\0';
518                         pci = (strcmp(name, "pci") == 0) ? 1 : 0;
519                 }
520         }
521         if (addrp != NULL)
522                 *addrp = addr;
523         if (sizep != NULL)
524                 *sizep = size;
525         if (pcip != NULL)
526                 *pcip = pci;
527 }
528
529 int
530 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
531     bus_space_handle_t *handle)
532 {
533         uint32_t cell[32];
534         bus_addr_t addr, raddr, baddr;
535         bus_size_t size, rsize;
536         uint32_t c, nbridge, naddr, nsize;
537         phandle_t bridge, parent;
538         u_int spc, rspc;
539         int pci, pcib, res;
540
541         /* Sanity checking. */
542         if (dev == 0)
543                 return (EINVAL);
544         bridge = OF_parent(dev);
545         if (bridge == 0)
546                 return (EINVAL);
547         if (regno < 0)
548                 return (EINVAL);
549         if (tag == NULL || handle == NULL)
550                 return (EINVAL);
551
552         /* Get the requested register. */
553         OF_get_addr_props(bridge, &naddr, &nsize, &pci);
554         res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
555             cell, sizeof(cell));
556         if (res == -1)
557                 return (ENXIO);
558         if (res % sizeof(cell[0]))
559                 return (ENXIO);
560         res /= sizeof(cell[0]);
561         regno *= naddr + nsize;
562         if (regno + naddr + nsize > res)
563                 return (EINVAL);
564         spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
565         addr = 0;
566         for (c = 0; c < naddr; c++)
567                 addr = ((uint64_t)addr << 32) | cell[regno++];
568         size = 0;
569         for (c = 0; c < nsize; c++)
570                 size = ((uint64_t)size << 32) | cell[regno++];
571
572         /*
573          * Map the address range in the bridge's decoding window as given
574          * by the "ranges" property. If a node doesn't have such property
575          * then no mapping is done.
576          */
577         parent = OF_parent(bridge);
578         while (parent != 0) {
579                 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
580                 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
581                 if (res == -1)
582                         goto next;
583                 if (res % sizeof(cell[0]))
584                         return (ENXIO);
585                 res /= sizeof(cell[0]);
586                 regno = 0;
587                 while (regno < res) {
588                         rspc = (pci)
589                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
590                             : ~0;
591                         if (rspc != spc) {
592                                 regno += naddr + nbridge + nsize;
593                                 continue;
594                         }
595                         raddr = 0;
596                         for (c = 0; c < naddr; c++)
597                                 raddr = ((uint64_t)raddr << 32) | cell[regno++];
598                         rspc = (pcib)
599                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
600                             : ~0;
601                         baddr = 0;
602                         for (c = 0; c < nbridge; c++)
603                                 baddr = ((uint64_t)baddr << 32) | cell[regno++];
604                         rsize = 0;
605                         for (c = 0; c < nsize; c++)
606                                 rsize = ((uint64_t)rsize << 32) | cell[regno++];
607                         if (addr < raddr || addr >= raddr + rsize)
608                                 continue;
609                         addr = addr - raddr + baddr;
610                         if (rspc != ~0)
611                                 spc = rspc;
612                 }
613
614         next:
615                 bridge = parent;
616                 parent = OF_parent(bridge);
617                 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
618         }
619
620         *tag = &bs_le_tag;
621         return (bus_space_map(*tag, addr, size, 0, handle));
622 }
623
624 int
625 mem_valid(vm_offset_t addr, int len)
626 {
627         int i;
628
629         for (i = 0; i < OFMEM_REGIONS; i++)
630                 if ((addr >= OFmem[i].mr_start) 
631                     && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))
632                         return (0);
633
634         return (EFAULT);
635 }
636