]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/powerpc/aim/ofw_machdep.c
MFC r208152,208172:
[FreeBSD/stable/8.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/stat.h>
45
46 #include <net/ethernet.h>
47
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_pci.h>
50 #include <dev/ofw/ofw_bus.h>
51
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_page.h>
55
56 #include <machine/bus.h>
57 #include <machine/cpu.h>
58 #include <machine/md_var.h>
59 #include <machine/platform.h>
60 #include <machine/ofw_machdep.h>
61
62 #define OFMEM_REGIONS   32
63 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
64 static struct mem_region OFfree[OFMEM_REGIONS + 3];
65
66 struct mem_region64 {
67         vm_offset_t     mr_start_hi;
68         vm_offset_t     mr_start_lo;
69         vm_size_t       mr_size;
70 };      
71
72 extern register_t ofmsr[5];
73 extern struct   pmap ofw_pmap;
74 static int      (*ofwcall)(void *);
75 static void     *fdt;
76 int             ofw_real_mode;
77
78 static void     ofw_quiesce(void);
79 static int      openfirmware(void *args);
80
81 /*
82  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
83  */
84 register_t      ofw_sprg0_save;
85
86 static __inline void
87 ofw_sprg_prepare(void)
88 {
89         /*
90          * Assume that interrupt are disabled at this point, or
91          * SPRG1-3 could be trashed
92          */
93         __asm __volatile("mfsprg0 %0\n\t"
94                          "mtsprg0 %1\n\t"
95                          "mtsprg1 %2\n\t"
96                          "mtsprg2 %3\n\t"
97                          "mtsprg3 %4\n\t"
98                          : "=&r"(ofw_sprg0_save)
99                          : "r"(ofmsr[1]),
100                          "r"(ofmsr[2]),
101                          "r"(ofmsr[3]),
102                          "r"(ofmsr[4]));
103 }
104
105 static __inline void
106 ofw_sprg_restore(void)
107 {
108         /*
109          * Note that SPRG1-3 contents are irrelevant. They are scratch
110          * registers used in the early portion of trap handling when
111          * interrupts are disabled.
112          *
113          * PCPU data cannot be used until this routine is called !
114          */
115         __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
116 }
117
118 /*
119  * Memory region utilities: determine if two regions overlap,
120  * and merge two overlapping regions into one
121  */
122 static int
123 memr_overlap(struct mem_region *r1, struct mem_region *r2)
124 {
125         if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
126             (r2->mr_start + r2->mr_size) < r1->mr_start)
127                 return (FALSE);
128         
129         return (TRUE);  
130 }
131
132 static void
133 memr_merge(struct mem_region *from, struct mem_region *to)
134 {
135         int end;
136         end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
137         to->mr_start = imin(from->mr_start, to->mr_start);
138         to->mr_size = end - to->mr_start;
139 }
140
141 /*
142  * This is called during powerpc_init, before the system is really initialized.
143  * It shall provide the total and the available regions of RAM.
144  * Both lists must have a zero-size entry as terminator.
145  * The available regions need not take the kernel into account, but needs
146  * to provide space for two additional entry beyond the terminating one.
147  */
148 void
149 ofw_mem_regions(struct mem_region **memp, int *memsz,
150                 struct mem_region **availp, int *availsz)
151 {
152         phandle_t phandle;
153         int asz, msz, fsz;
154         int i, j;
155         int still_merging;
156         cell_t address_cells;
157
158         asz = msz = 0;
159
160         /*
161          * Get #address-cells from root node, defaulting to 1 if it cannot
162          * be found.
163          */
164         phandle = OF_finddevice("/");
165         if (OF_getprop(phandle, "#address-cells", &address_cells, 
166             sizeof(address_cells)) < sizeof(address_cells))
167                 address_cells = 1;
168         
169         /*
170          * Get memory.
171          */
172         if ((phandle = OF_finddevice("/memory")) == -1
173             || (asz = OF_getprop(phandle, "available",
174                   OFavail, sizeof OFavail[0] * OFMEM_REGIONS)) <= 0)
175         {
176                 if (ofw_real_mode) {
177                         /* XXX MAMBO */
178                         printf("Physical memory unknown -- guessing 128 MB\n");
179
180                         /* Leave the first 0xA000000 bytes for the kernel */
181                         OFavail[0].mr_start = 0xA00000;
182                         OFavail[0].mr_size = 0x75FFFFF;
183                         asz = sizeof(OFavail[0]);
184                 } else {
185                         panic("no memory?");
186                 }
187         }
188
189         if (address_cells == 2) {
190             struct mem_region64 OFmem64[OFMEM_REGIONS + 1];
191             if ((phandle == -1) || (msz = OF_getprop(phandle, "reg",
192                           OFmem64, sizeof OFmem64[0] * OFMEM_REGIONS)) <= 0) {
193                 if (ofw_real_mode) {
194                         /* XXX MAMBO */
195                         OFmem64[0].mr_start_hi = 0;
196                         OFmem64[0].mr_start_lo = 0x0;
197                         OFmem64[0].mr_size = 0x7FFFFFF;
198                         msz = sizeof(OFmem64[0]);
199                 } else {
200                         panic("Physical memory map not found");
201                 }
202             }
203
204             for (i = 0, j = 0; i < msz/sizeof(OFmem64[0]); i++) {
205                 if (OFmem64[i].mr_start_hi == 0) {
206                         OFmem[i].mr_start = OFmem64[i].mr_start_lo;
207                         OFmem[i].mr_size = OFmem64[i].mr_size;
208
209                         /*
210                          * Check for memory regions extending above 32-bit
211                          * memory space, and restrict them to stay there.
212                          */
213                         if (((uint64_t)OFmem[i].mr_start +
214                             (uint64_t)OFmem[i].mr_size) >
215                             BUS_SPACE_MAXADDR_32BIT) {
216                                 OFmem[i].mr_size = BUS_SPACE_MAXADDR_32BIT -
217                                     OFmem[i].mr_start;
218                         }
219                         j++;
220                 }
221             }
222             msz = j*sizeof(OFmem[0]);
223         } else {
224             if ((msz = OF_getprop(phandle, "reg",
225                           OFmem, sizeof OFmem[0] * OFMEM_REGIONS)) <= 0)
226                 panic("Physical memory map not found");
227         }
228
229         *memp = OFmem;
230         *memsz = msz / sizeof(struct mem_region);
231         
232
233         /*
234          * OFavail may have overlapping regions - collapse these
235          * and copy out remaining regions to OFfree
236          */
237         asz /= sizeof(struct mem_region);
238         do {
239                 still_merging = FALSE;
240                 for (i = 0; i < asz; i++) {
241                         if (OFavail[i].mr_size == 0)
242                                 continue;
243                         for (j = i+1; j < asz; j++) {
244                                 if (OFavail[j].mr_size == 0)
245                                         continue;
246                                 if (memr_overlap(&OFavail[j], &OFavail[i])) {
247                                         memr_merge(&OFavail[j], &OFavail[i]);
248                                         /* mark inactive */
249                                         OFavail[j].mr_size = 0;
250                                         still_merging = TRUE;
251                                 }
252                         }
253                 }
254         } while (still_merging == TRUE);
255
256         /* evict inactive ranges */
257         for (i = 0, fsz = 0; i < asz; i++) {
258                 if (OFavail[i].mr_size != 0) {
259                         OFfree[fsz] = OFavail[i];
260                         fsz++;
261                 }
262         }
263
264         *availp = OFfree;
265         *availsz = fsz;
266 }
267
268 void
269 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
270 {
271         if (ofmsr[0] & PSL_DR)
272                 ofw_real_mode = 0;
273         else
274                 ofw_real_mode = 1;
275
276         ofwcall = openfirm;
277         fdt = fdt_ptr;
278 }
279
280 boolean_t
281 OF_bootstrap()
282 {
283         boolean_t status = FALSE;
284
285         if (ofwcall != NULL) {
286                 if (ofw_real_mode)
287                         status = OF_install(OFW_STD_REAL, 0);
288                 else
289                         status = OF_install(OFW_STD_DIRECT, 0);
290
291                 if (status != TRUE)
292                         return status;
293
294                 OF_init(openfirmware);
295
296                 /*
297                  * On some machines, we need to quiesce OF to turn off
298                  * background processes.
299                  */
300                 ofw_quiesce();
301         } else {
302                 status = OF_install(OFW_FDT, 0);
303
304                 if (status != TRUE)
305                         return status;
306
307                 OF_init(fdt);
308         } 
309
310         return (status);
311 }
312
313 static void
314 ofw_quiesce(void)
315 {
316         phandle_t rootnode;
317         char model[32];
318         struct {
319                 cell_t name;
320                 cell_t nargs;
321                 cell_t nreturns;
322         } args;
323
324         /*
325          * Only quiesce Open Firmware on PowerMac11,2 and 12,1. It is
326          * necessary there to shut down a background thread doing fan
327          * management, and is harmful on other machines.
328          *
329          * Note: we don't need to worry about which OF module we are
330          * using since this is called only from very early boot, within
331          * OF's boot context.
332          */
333
334         rootnode = OF_finddevice("/");
335         if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
336                 if (strcmp(model, "PowerMac11,2") == 0 ||
337                     strcmp(model, "PowerMac12,1") == 0) {
338                         args.name = (cell_t)(uintptr_t)"quiesce";
339                         args.nargs = 0;
340                         args.nreturns = 0;
341                         openfirmware(&args);
342                 }
343         }
344 }
345
346 static int
347 openfirmware(void *args)
348 {
349         long    oldmsr;
350         int     result;
351         u_int   srsave[16];
352         u_int   i;
353
354         if (pmap_bootstrapped && ofw_real_mode)
355                 args = (void *)pmap_kextract((vm_offset_t)args);
356
357         __asm __volatile(       "\t"
358                 "sync\n\t"
359                 "mfmsr  %0\n\t"
360                 "mtmsr  %1\n\t"
361                 "isync\n"
362                 : "=r" (oldmsr)
363                 : "r" (ofmsr[0])
364         );
365
366         ofw_sprg_prepare();
367
368         if (pmap_bootstrapped && !ofw_real_mode) {
369                 /*
370                  * Swap the kernel's address space with Open Firmware's
371                  */
372                 for (i = 0; i < 16; i++) {
373                         srsave[i] = mfsrin(i << ADDR_SR_SHFT);
374                         mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
375                 }
376
377                 /*
378                  * Clear battable[] translations
379                  */
380                 if (!(cpu_features & PPC_FEATURE_64)) {
381                         __asm __volatile("mtdbatu 2, %0\n"
382                                          "mtdbatu 3, %0" : : "r" (0));
383                 }
384                 isync();
385         }
386
387         result = ofwcall(args);
388
389         if (pmap_bootstrapped && !ofw_real_mode) {
390                 /*
391                  * Restore the kernel's addr space. The isync() doesn;t
392                  * work outside the loop unless mtsrin() is open-coded
393                  * in an asm statement :(
394                  */
395                 for (i = 0; i < 16; i++) {
396                         mtsrin(i << ADDR_SR_SHFT, srsave[i]);
397                         isync();
398                 }
399         }
400
401         ofw_sprg_restore();
402
403         __asm(  "\t"
404                 "mtmsr  %0\n\t"
405                 "isync\n"
406                 : : "r" (oldmsr)
407         );
408
409         return (result);
410 }
411
412 void
413 OF_halt()
414 {
415         int retval;     /* dummy, this may not be needed */
416
417         OF_interpret("shut-down", 1, &retval);
418         for (;;);       /* just in case */
419 }
420
421 void
422 OF_reboot()
423 {
424         int retval;     /* dummy, this may not be needed */
425
426         OF_interpret("reset-all", 1, &retval);
427         for (;;);       /* just in case */
428 }
429
430 void
431 OF_getetheraddr(device_t dev, u_char *addr)
432 {
433         phandle_t       node;
434
435         node = ofw_bus_get_node(dev);
436         OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
437 }
438
439 /*
440  * Return a bus handle and bus tag that corresponds to the register
441  * numbered regno for the device referenced by the package handle
442  * dev. This function is intended to be used by console drivers in
443  * early boot only. It works by mapping the address of the device's
444  * register in the address space of its parent and recursively walk
445  * the device tree upward this way.
446  */
447 static void
448 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
449 {
450         char name[16];
451         uint32_t addr, size;
452         int pci, res;
453
454         res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
455         if (res == -1)
456                 addr = 2;
457         res = OF_getprop(node, "#size-cells", &size, sizeof(size));
458         if (res == -1)
459                 size = 1;
460         pci = 0;
461         if (addr == 3 && size == 2) {
462                 res = OF_getprop(node, "name", name, sizeof(name));
463                 if (res != -1) {
464                         name[sizeof(name) - 1] = '\0';
465                         pci = (strcmp(name, "pci") == 0) ? 1 : 0;
466                 }
467         }
468         if (addrp != NULL)
469                 *addrp = addr;
470         if (sizep != NULL)
471                 *sizep = size;
472         if (pcip != NULL)
473                 *pcip = pci;
474 }
475
476 int
477 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
478     bus_space_handle_t *handle)
479 {
480         uint32_t cell[32];
481         bus_addr_t addr, raddr, baddr;
482         bus_size_t size, rsize;
483         uint32_t c, nbridge, naddr, nsize;
484         phandle_t bridge, parent;
485         u_int spc, rspc;
486         int pci, pcib, res;
487
488         /* Sanity checking. */
489         if (dev == 0)
490                 return (EINVAL);
491         bridge = OF_parent(dev);
492         if (bridge == 0)
493                 return (EINVAL);
494         if (regno < 0)
495                 return (EINVAL);
496         if (tag == NULL || handle == NULL)
497                 return (EINVAL);
498
499         /* Get the requested register. */
500         OF_get_addr_props(bridge, &naddr, &nsize, &pci);
501         res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
502             cell, sizeof(cell));
503         if (res == -1)
504                 return (ENXIO);
505         if (res % sizeof(cell[0]))
506                 return (ENXIO);
507         res /= sizeof(cell[0]);
508         regno *= naddr + nsize;
509         if (regno + naddr + nsize > res)
510                 return (EINVAL);
511         spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
512         addr = 0;
513         for (c = 0; c < naddr; c++)
514                 addr = ((uint64_t)addr << 32) | cell[regno++];
515         size = 0;
516         for (c = 0; c < nsize; c++)
517                 size = ((uint64_t)size << 32) | cell[regno++];
518
519         /*
520          * Map the address range in the bridge's decoding window as given
521          * by the "ranges" property. If a node doesn't have such property
522          * then no mapping is done.
523          */
524         parent = OF_parent(bridge);
525         while (parent != 0) {
526                 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
527                 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
528                 if (res == -1)
529                         goto next;
530                 if (res % sizeof(cell[0]))
531                         return (ENXIO);
532                 res /= sizeof(cell[0]);
533                 regno = 0;
534                 while (regno < res) {
535                         rspc = (pci)
536                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
537                             : ~0;
538                         if (rspc != spc) {
539                                 regno += naddr + nbridge + nsize;
540                                 continue;
541                         }
542                         raddr = 0;
543                         for (c = 0; c < naddr; c++)
544                                 raddr = ((uint64_t)raddr << 32) | cell[regno++];
545                         rspc = (pcib)
546                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
547                             : ~0;
548                         baddr = 0;
549                         for (c = 0; c < nbridge; c++)
550                                 baddr = ((uint64_t)baddr << 32) | cell[regno++];
551                         rsize = 0;
552                         for (c = 0; c < nsize; c++)
553                                 rsize = ((uint64_t)rsize << 32) | cell[regno++];
554                         if (addr < raddr || addr >= raddr + rsize)
555                                 continue;
556                         addr = addr - raddr + baddr;
557                         if (rspc != ~0)
558                                 spc = rspc;
559                 }
560
561         next:
562                 bridge = parent;
563                 parent = OF_parent(bridge);
564                 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
565         }
566
567         *tag = &bs_le_tag;
568         return (bus_space_map(*tag, addr, size, 0, handle));
569 }
570
571 int
572 mem_valid(vm_offset_t addr, int len)
573 {
574         int i;
575
576         for (i = 0; i < OFMEM_REGIONS; i++)
577                 if ((addr >= OFmem[i].mr_start) 
578                     && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))
579                         return (0);
580
581         return (EFAULT);
582 }
583