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