]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/aim/ofw_machdep.c
This commit was generated by cvs2svn to compensate for changes in r159609,
[FreeBSD/FreeBSD.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
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_page.h>
53
54 #include <machine/powerpc.h>
55 #include <machine/ofw_machdep.h>
56 #include <powerpc/ofw/ofw_pci.h>
57
58 #define OFMEM_REGIONS   32
59 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
60 static struct mem_region OFfree[OFMEM_REGIONS + 3];
61
62 extern register_t ofmsr[5];
63 extern struct   pcpu __pcpu[MAXCPU];
64 extern struct   pmap ofw_pmap;
65 static int      (*ofwcall)(void *);
66
67 /*
68  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
69  */
70 register_t      ofw_sprg0_save;
71
72 static __inline void
73 ofw_sprg_prepare(void)
74 {
75         /*
76          * Assume that interrupt are disabled at this point, or
77          * SPRG1-3 could be trashed
78          */
79         __asm __volatile("mfsprg0 %0\n\t"
80                          "mtsprg0 %1\n\t"
81                          "mtsprg1 %2\n\t"
82                          "mtsprg2 %3\n\t"
83                          "mtsprg3 %4\n\t"
84                          : "=&r"(ofw_sprg0_save)
85                          : "r"(ofmsr[1]),
86                          "r"(ofmsr[2]),
87                          "r"(ofmsr[3]),
88                          "r"(ofmsr[4]));
89 }
90
91 static __inline void
92 ofw_sprg_restore(void)
93 {
94         /*
95          * Note that SPRG1-3 contents are irrelevant. They are scratch
96          * registers used in the early portion of trap handling when
97          * interrupts are disabled.
98          *
99          * PCPU data cannot be used until this routine is called !
100          */
101         __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
102 }
103
104 /*
105  * Memory region utilities: determine if two regions overlap,
106  * and merge two overlapping regions into one
107  */
108 static int
109 memr_overlap(struct mem_region *r1, struct mem_region *r2)
110 {
111         if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
112             (r2->mr_start + r2->mr_size) < r1->mr_start)
113                 return (FALSE);
114         
115         return (TRUE);  
116 }
117
118 static void
119 memr_merge(struct mem_region *from, struct mem_region *to)
120 {
121         int end;
122         end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
123         to->mr_start = imin(from->mr_start, to->mr_start);
124         to->mr_size = end - to->mr_start;
125 }
126
127 /*
128  * This is called during powerpc_init, before the system is really initialized.
129  * It shall provide the total and the available regions of RAM.
130  * Both lists must have a zero-size entry as terminator.
131  * The available regions need not take the kernel into account, but needs
132  * to provide space for two additional entry beyond the terminating one.
133  */
134 void
135 mem_regions(struct mem_region **memp, int *memsz,
136                 struct mem_region **availp, int *availsz)
137 {
138         int phandle;
139         int asz, msz, fsz;
140         int i, j;
141         int still_merging;
142         
143         /*
144          * Get memory.
145          */
146         if ((phandle = OF_finddevice("/memory")) == -1
147             || (msz = OF_getprop(phandle, "reg",
148                           OFmem, sizeof OFmem[0] * OFMEM_REGIONS))
149                <= 0
150             || (asz = OF_getprop(phandle, "available",
151                           OFavail, sizeof OFavail[0] * OFMEM_REGIONS))
152                <= 0)
153                 panic("no memory?");
154         *memp = OFmem;
155         *memsz = msz / sizeof(struct mem_region);
156
157         /*
158          * OFavail may have overlapping regions - collapse these
159          * and copy out remaining regions to OFfree
160          */
161         asz /= sizeof(struct mem_region);
162         do {
163                 still_merging = FALSE;
164                 for (i = 0; i < asz; i++) {
165                         if (OFavail[i].mr_size == 0)
166                                 continue;
167                         for (j = i+1; j < asz; j++) {
168                                 if (OFavail[j].mr_size == 0)
169                                         continue;
170                                 if (memr_overlap(&OFavail[j], &OFavail[i])) {
171                                         memr_merge(&OFavail[j], &OFavail[i]);
172                                         /* mark inactive */
173                                         OFavail[j].mr_size = 0;
174                                         still_merging = TRUE;
175                                 }
176                         }
177                 }
178         } while (still_merging == TRUE);
179
180         /* evict inactive ranges */
181         for (i = 0, fsz = 0; i < asz; i++) {
182                 if (OFavail[i].mr_size != 0) {
183                         OFfree[fsz] = OFavail[i];
184                         fsz++;
185                 }
186         }
187
188         *availp = OFfree;
189         *availsz = fsz;
190 }
191
192 void
193 set_openfirm_callback(int (*openfirm)(void *))
194 {
195
196         ofwcall = openfirm;
197 }
198
199 int
200 openfirmware(void *args)
201 {
202         long    oldmsr;
203         int     result;
204         u_int   srsave[16];
205         u_int   i;
206
207         __asm __volatile(       "\t"
208                 "sync\n\t"
209                 "mfmsr  %0\n\t"
210                 "mtmsr  %1\n\t"
211                 "isync\n"
212                 : "=r" (oldmsr)
213                 : "r" (ofmsr[0])
214         );
215
216         ofw_sprg_prepare();
217
218         if (pmap_bootstrapped) {
219                 /*
220                  * Swap the kernel's address space with Open Firmware's
221                  */
222                 for (i = 0; i < 16; i++) {
223                         srsave[i] = mfsrin(i << ADDR_SR_SHFT);
224                         mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
225                 }
226
227                 /*
228                  * Clear battable[] translations
229                  */
230                 __asm __volatile("mtdbatu 2, %0\n"
231                                  "mtdbatu 3, %0" : : "r" (0));
232                 isync();
233         }
234
235         result = ofwcall(args);
236
237         if (pmap_bootstrapped) {
238                 /*
239                  * Restore the kernel's addr space. The isync() doesn;t
240                  * work outside the loop unless mtsrin() is open-coded
241                  * in an asm statement :(
242                  */
243                 for (i = 0; i < 16; i++) {
244                         mtsrin(i << ADDR_SR_SHFT, srsave[i]);
245                         isync();
246                 }
247         }
248
249         ofw_sprg_restore();
250
251         __asm(  "\t"
252                 "mtmsr  %0\n\t"
253                 "isync\n"
254                 : : "r" (oldmsr)
255         );
256
257         return (result);
258 }
259
260 void
261 OF_halt()
262 {
263         int retval;     /* dummy, this may not be needed */
264
265         OF_interpret("shut-down", 1, &retval);
266         for (;;);       /* just in case */
267 }
268
269 void
270 OF_reboot()
271 {
272         int retval;     /* dummy, this may not be needed */
273
274         OF_interpret("reset-all", 1, &retval);
275         for (;;);       /* just in case */
276 }
277
278 void
279 OF_getetheraddr(device_t dev, u_char *addr)
280 {
281         phandle_t       node;
282
283         node = ofw_pci_find_node(dev);
284         OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
285 }
286
287 int
288 mem_valid(vm_offset_t addr, int len)
289 {
290         int i;
291
292         for (i = 0; i < OFMEM_REGIONS; i++)
293                 if ((addr >= OFmem[i].mr_start) 
294                     && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))
295                         return (0);
296
297         return (EFAULT);
298 }