]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/ofw/ofw_machdep.c
MFV r326007: less v529.
[FreeBSD/FreeBSD.git] / sys / powerpc / ofw / 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 "opt_platform.h"
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/disk.h>
43 #include <sys/fcntl.h>
44 #include <sys/malloc.h>
45 #include <sys/smp.h>
46 #include <sys/stat.h>
47 #include <sys/endian.h>
48
49 #include <net/ethernet.h>
50
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_pci.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_subr.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/vm_page.h>
60
61 #include <machine/bus.h>
62 #include <machine/cpu.h>
63 #include <machine/md_var.h>
64 #include <machine/platform.h>
65 #include <machine/ofw_machdep.h>
66 #include <machine/trap.h>
67
68 static void     *fdt;
69 int             ofw_real_mode;
70
71 #ifdef AIM
72 extern register_t ofmsr[5];
73 extern void     *openfirmware_entry;
74 char            save_trap_init[0x2f00];          /* EXC_LAST */
75 char            save_trap_of[0x2f00];            /* EXC_LAST */
76
77 int             ofwcall(void *);
78 static int      openfirmware(void *args);
79
80 __inline void
81 ofw_save_trap_vec(char *save_trap_vec)
82 {
83         if (!ofw_real_mode)
84                 return;
85
86         bcopy((void *)EXC_RST, save_trap_vec, EXC_LAST - EXC_RST);
87 }
88
89 static __inline void
90 ofw_restore_trap_vec(char *restore_trap_vec)
91 {
92         if (!ofw_real_mode)
93                 return;
94
95         bcopy(restore_trap_vec, (void *)EXC_RST, EXC_LAST - EXC_RST);
96         __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
97 }
98
99 /*
100  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
101  */
102 register_t      ofw_sprg0_save;
103
104 static __inline void
105 ofw_sprg_prepare(void)
106 {
107         if (ofw_real_mode)
108                 return;
109         
110         /*
111          * Assume that interrupt are disabled at this point, or
112          * SPRG1-3 could be trashed
113          */
114 #ifdef __powerpc64__
115         __asm __volatile("mtsprg1 %0\n\t"
116                          "mtsprg2 %1\n\t"
117                          "mtsprg3 %2\n\t"
118                          :
119                          : "r"(ofmsr[2]),
120                          "r"(ofmsr[3]),
121                          "r"(ofmsr[4]));
122 #else
123         __asm __volatile("mfsprg0 %0\n\t"
124                          "mtsprg0 %1\n\t"
125                          "mtsprg1 %2\n\t"
126                          "mtsprg2 %3\n\t"
127                          "mtsprg3 %4\n\t"
128                          : "=&r"(ofw_sprg0_save)
129                          : "r"(ofmsr[1]),
130                          "r"(ofmsr[2]),
131                          "r"(ofmsr[3]),
132                          "r"(ofmsr[4]));
133 #endif
134 }
135
136 static __inline void
137 ofw_sprg_restore(void)
138 {
139         if (ofw_real_mode)
140                 return;
141         
142         /*
143          * Note that SPRG1-3 contents are irrelevant. They are scratch
144          * registers used in the early portion of trap handling when
145          * interrupts are disabled.
146          *
147          * PCPU data cannot be used until this routine is called !
148          */
149 #ifndef __powerpc64__
150         __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
151 #endif
152 }
153 #endif
154
155 static int
156 parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
157 {
158         cell_t address_cells, size_cells;
159         cell_t OFmem[4 * PHYS_AVAIL_SZ];
160         int sz, i, j;
161         phandle_t phandle;
162
163         sz = 0;
164
165         /*
166          * Get #address-cells from root node, defaulting to 1 if it cannot
167          * be found.
168          */
169         phandle = OF_finddevice("/");
170         if (OF_getencprop(phandle, "#address-cells", &address_cells, 
171             sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
172                 address_cells = 1;
173         if (OF_getencprop(phandle, "#size-cells", &size_cells, 
174             sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
175                 size_cells = 1;
176
177         /*
178          * Get memory.
179          */
180         if (node == -1 || (sz = OF_getencprop(node, prop,
181             OFmem, sizeof(OFmem))) <= 0)
182                 panic("Physical memory map not found");
183
184         i = 0;
185         j = 0;
186         while (i < sz/sizeof(cell_t)) {
187                 output[j].mr_start = OFmem[i++];
188                 if (address_cells == 2) {
189                         output[j].mr_start <<= 32;
190                         output[j].mr_start += OFmem[i++];
191                 }
192                         
193                 output[j].mr_size = OFmem[i++];
194                 if (size_cells == 2) {
195                         output[j].mr_size <<= 32;
196                         output[j].mr_size += OFmem[i++];
197                 }
198
199                 if (output[j].mr_start > BUS_SPACE_MAXADDR)
200                         continue;
201
202                 /*
203                  * Constrain memory to that which we can access.
204                  * 32-bit AIM can only reference 32 bits of address currently,
205                  * but Book-E can access 36 bits.
206                  */
207                 if (((uint64_t)output[j].mr_start +
208                     (uint64_t)output[j].mr_size - 1) >
209                     BUS_SPACE_MAXADDR) {
210                         output[j].mr_size = BUS_SPACE_MAXADDR -
211                             output[j].mr_start + 1;
212                 }
213
214                 j++;
215         }
216         sz = j*sizeof(output[0]);
217
218         return (sz);
219 }
220
221 static int
222 excise_fdt_reserved(struct mem_region *avail, int asz)
223 {
224         struct {
225                 uint64_t address;
226                 uint64_t size;
227         } fdtmap[16];
228         ssize_t fdtmapsize;
229         phandle_t chosen;
230         int i, j, k;
231
232         chosen = OF_finddevice("/chosen");
233         fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
234
235         for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
236                 fdtmap[j].address = be64toh(fdtmap[j].address);
237                 fdtmap[j].size = be64toh(fdtmap[j].size);
238         }
239
240         for (i = 0; i < asz; i++) {
241                 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
242                         /*
243                          * Case 1: Exclusion region encloses complete
244                          * available entry. Drop it and move on.
245                          */
246                         if (fdtmap[j].address <= avail[i].mr_start &&
247                             fdtmap[j].address + fdtmap[j].size >=
248                             avail[i].mr_start + avail[i].mr_size) {
249                                 for (k = i+1; k < asz; k++)
250                                         avail[k-1] = avail[k];
251                                 asz--;
252                                 i--; /* Repeat some entries */
253                                 continue;
254                         }
255
256                         /*
257                          * Case 2: Exclusion region starts in available entry.
258                          * Trim it to where the entry begins and append
259                          * a new available entry with the region after
260                          * the excluded region, if any.
261                          */
262                         if (fdtmap[j].address >= avail[i].mr_start &&
263                             fdtmap[j].address < avail[i].mr_start +
264                             avail[i].mr_size) {
265                                 if (fdtmap[j].address + fdtmap[j].size < 
266                                     avail[i].mr_start + avail[i].mr_size) {
267                                         avail[asz].mr_start =
268                                             fdtmap[j].address + fdtmap[j].size;
269                                         avail[asz].mr_size = avail[i].mr_start +
270                                              avail[i].mr_size -
271                                              avail[asz].mr_start;
272                                         asz++;
273                                 }
274
275                                 avail[i].mr_size = fdtmap[j].address -
276                                     avail[i].mr_start;
277                         }
278
279                         /*
280                          * Case 3: Exclusion region ends in available entry.
281                          * Move start point to where the exclusion zone ends.
282                          * The case of a contained exclusion zone has already
283                          * been caught in case 2.
284                          */
285                         if (fdtmap[j].address + fdtmap[j].size >=
286                             avail[i].mr_start && fdtmap[j].address +
287                             fdtmap[j].size < avail[i].mr_start +
288                             avail[i].mr_size) {
289                                 avail[i].mr_size += avail[i].mr_start;
290                                 avail[i].mr_start =
291                                     fdtmap[j].address + fdtmap[j].size;
292                                 avail[i].mr_size -= avail[i].mr_start;
293                         }
294                 }
295         }
296
297         return (asz);
298 }
299
300 /*
301  * This is called during powerpc_init, before the system is really initialized.
302  * It shall provide the total and the available regions of RAM.
303  * The available regions need not take the kernel into account.
304  */
305 void
306 ofw_mem_regions(struct mem_region *memp, int *memsz,
307                 struct mem_region *availp, int *availsz)
308 {
309         phandle_t phandle;
310         int asz, msz;
311         int res;
312         char name[31];
313
314         asz = msz = 0;
315
316         /*
317          * Get memory from all the /memory nodes.
318          */
319         for (phandle = OF_child(OF_peer(0)); phandle != 0;
320             phandle = OF_peer(phandle)) {
321                 if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
322                         continue;
323                 if (strncmp(name, "memory", sizeof(name)) != 0 &&
324                     strncmp(name, "memory@", strlen("memory@")) != 0)
325                         continue;
326
327                 res = parse_ofw_memory(phandle, "reg", &memp[msz]);
328                 msz += res/sizeof(struct mem_region);
329                 if (OF_getproplen(phandle, "available") >= 0)
330                         res = parse_ofw_memory(phandle, "available",
331                             &availp[asz]);
332                 else
333                         res = parse_ofw_memory(phandle, "reg", &availp[asz]);
334                 asz += res/sizeof(struct mem_region);
335         }
336
337         phandle = OF_finddevice("/chosen");
338         if (OF_hasprop(phandle, "fdtmemreserv"))
339                 asz = excise_fdt_reserved(availp, asz);
340
341         *memsz = msz;
342         *availsz = asz;
343 }
344
345 void
346 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
347 {
348 #ifdef AIM
349         ofmsr[0] = mfmsr();
350         #ifdef __powerpc64__
351         ofmsr[0] &= ~PSL_SF;
352         #else
353         __asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
354         #endif
355         __asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
356         __asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
357         __asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
358         openfirmware_entry = openfirm;
359
360         if (ofmsr[0] & PSL_DR)
361                 ofw_real_mode = 0;
362         else
363                 ofw_real_mode = 1;
364
365         ofw_save_trap_vec(save_trap_init);
366 #else
367         ofw_real_mode = 1;
368 #endif
369
370         fdt = fdt_ptr;
371
372         #ifdef FDT_DTB_STATIC
373         /* Check for a statically included blob */
374         if (fdt == NULL)
375                 fdt = &fdt_static_dtb;
376         #endif
377 }
378
379 boolean_t
380 OF_bootstrap()
381 {
382         boolean_t status = FALSE;
383
384 #ifdef AIM
385         if (openfirmware_entry != NULL) {
386                 if (ofw_real_mode) {
387                         status = OF_install(OFW_STD_REAL, 0);
388                 } else {
389                         #ifdef __powerpc64__
390                         status = OF_install(OFW_STD_32BIT, 0);
391                         #else
392                         status = OF_install(OFW_STD_DIRECT, 0);
393                         #endif
394                 }
395
396                 if (status != TRUE)
397                         return status;
398
399                 OF_init(openfirmware);
400         } else
401 #endif
402         if (fdt != NULL) {
403                 status = OF_install(OFW_FDT, 0);
404
405                 if (status != TRUE)
406                         return status;
407
408                 OF_init(fdt);
409                 OF_interpret("perform-fixup", 0);
410         } 
411
412         return (status);
413 }
414
415 #ifdef AIM
416 void
417 ofw_quiesce(void)
418 {
419         struct {
420                 cell_t name;
421                 cell_t nargs;
422                 cell_t nreturns;
423         } args;
424
425         KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
426
427         args.name = (cell_t)(uintptr_t)"quiesce";
428         args.nargs = 0;
429         args.nreturns = 0;
430         openfirmware(&args);
431 }
432
433 static int
434 openfirmware_core(void *args)
435 {
436         int             result;
437         register_t      oldmsr;
438
439         if (openfirmware_entry == NULL)
440                 return (-1);
441
442         /*
443          * Turn off exceptions - we really don't want to end up
444          * anywhere unexpected with PCPU set to something strange
445          * or the stack pointer wrong.
446          */
447         oldmsr = intr_disable();
448
449         ofw_sprg_prepare();
450
451         /* Save trap vectors */
452         ofw_save_trap_vec(save_trap_of);
453
454         /* Restore initially saved trap vectors */
455         ofw_restore_trap_vec(save_trap_init);
456
457 #ifndef __powerpc64__
458         /*
459          * Clear battable[] translations
460          */
461         if (!(cpu_features & PPC_FEATURE_64))
462                 __asm __volatile("mtdbatu 2, %0\n"
463                                  "mtdbatu 3, %0" : : "r" (0));
464         isync();
465 #endif
466
467         result = ofwcall(args);
468
469         /* Restore trap vecotrs */
470         ofw_restore_trap_vec(save_trap_of);
471
472         ofw_sprg_restore();
473
474         intr_restore(oldmsr);
475
476         return (result);
477 }
478
479 #ifdef SMP
480 struct ofw_rv_args {
481         void *args;
482         int retval;
483         volatile int in_progress;
484 };
485
486 static void
487 ofw_rendezvous_dispatch(void *xargs)
488 {
489         struct ofw_rv_args *rv_args = xargs;
490
491         /* NOTE: Interrupts are disabled here */
492
493         if (PCPU_GET(cpuid) == 0) {
494                 /*
495                  * Execute all OF calls on CPU 0
496                  */
497                 rv_args->retval = openfirmware_core(rv_args->args);
498                 rv_args->in_progress = 0;
499         } else {
500                 /*
501                  * Spin with interrupts off on other CPUs while OF has
502                  * control of the machine.
503                  */
504                 while (rv_args->in_progress)
505                         cpu_spinwait();
506         }
507 }
508 #endif
509
510 static int
511 openfirmware(void *args)
512 {
513         int result;
514         #ifdef SMP
515         struct ofw_rv_args rv_args;
516         #endif
517
518         if (openfirmware_entry == NULL)
519                 return (-1);
520
521         #ifdef SMP
522         rv_args.args = args;
523         rv_args.in_progress = 1;
524         smp_rendezvous(smp_no_rendezvous_barrier, ofw_rendezvous_dispatch,
525             smp_no_rendezvous_barrier, &rv_args);
526         result = rv_args.retval;
527         #else
528         result = openfirmware_core(args);
529         #endif
530
531         return (result);
532 }
533
534 void
535 OF_reboot()
536 {
537         struct {
538                 cell_t name;
539                 cell_t nargs;
540                 cell_t nreturns;
541                 cell_t arg;
542         } args;
543
544         args.name = (cell_t)(uintptr_t)"interpret";
545         args.nargs = 1;
546         args.nreturns = 0;
547         args.arg = (cell_t)(uintptr_t)"reset-all";
548         openfirmware_core(&args); /* Don't do rendezvous! */
549
550         for (;;);       /* just in case */
551 }
552
553 #endif /* AIM */
554
555 void
556 OF_getetheraddr(device_t dev, u_char *addr)
557 {
558         phandle_t       node;
559
560         node = ofw_bus_get_node(dev);
561         OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
562 }
563
564 /*
565  * Return a bus handle and bus tag that corresponds to the register
566  * numbered regno for the device referenced by the package handle
567  * dev. This function is intended to be used by console drivers in
568  * early boot only. It works by mapping the address of the device's
569  * register in the address space of its parent and recursively walk
570  * the device tree upward this way.
571  */
572 int
573 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
574     bus_space_handle_t *handle, bus_size_t *sz)
575 {
576         bus_addr_t addr;
577         bus_size_t size;
578         pcell_t pci_hi;
579         int flags, res;
580
581         res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
582         if (res < 0)
583                 return (res);
584
585         if (pci_hi == OFW_PADDR_NOT_PCI) {
586                 *tag = &bs_be_tag;
587                 flags = 0;
588         } else {
589                 *tag = &bs_le_tag;
590                 flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ? 
591                     BUS_SPACE_MAP_PREFETCHABLE: 0;
592         }
593
594         if (sz != NULL)
595                 *sz = size;
596
597         return (bus_space_map(*tag, addr, size, flags, handle));
598 }
599