]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/aim/ofw_machdep.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[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 #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/powerpc.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 extern register_t ofmsr[5];
66 extern struct   pmap ofw_pmap;
67 static int      (*ofwcall)(void *);
68
69 /*
70  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
71  */
72 register_t      ofw_sprg0_save;
73
74 static __inline void
75 ofw_sprg_prepare(void)
76 {
77         /*
78          * Assume that interrupt are disabled at this point, or
79          * SPRG1-3 could be trashed
80          */
81         __asm __volatile("mfsprg0 %0\n\t"
82                          "mtsprg0 %1\n\t"
83                          "mtsprg1 %2\n\t"
84                          "mtsprg2 %3\n\t"
85                          "mtsprg3 %4\n\t"
86                          : "=&r"(ofw_sprg0_save)
87                          : "r"(ofmsr[1]),
88                          "r"(ofmsr[2]),
89                          "r"(ofmsr[3]),
90                          "r"(ofmsr[4]));
91 }
92
93 static __inline void
94 ofw_sprg_restore(void)
95 {
96         /*
97          * Note that SPRG1-3 contents are irrelevant. They are scratch
98          * registers used in the early portion of trap handling when
99          * interrupts are disabled.
100          *
101          * PCPU data cannot be used until this routine is called !
102          */
103         __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
104 }
105
106 /*
107  * Memory region utilities: determine if two regions overlap,
108  * and merge two overlapping regions into one
109  */
110 static int
111 memr_overlap(struct mem_region *r1, struct mem_region *r2)
112 {
113         if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
114             (r2->mr_start + r2->mr_size) < r1->mr_start)
115                 return (FALSE);
116         
117         return (TRUE);  
118 }
119
120 static void
121 memr_merge(struct mem_region *from, struct mem_region *to)
122 {
123         int end;
124         end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
125         to->mr_start = imin(from->mr_start, to->mr_start);
126         to->mr_size = end - to->mr_start;
127 }
128
129 /*
130  * This is called during powerpc_init, before the system is really initialized.
131  * It shall provide the total and the available regions of RAM.
132  * Both lists must have a zero-size entry as terminator.
133  * The available regions need not take the kernel into account, but needs
134  * to provide space for two additional entry beyond the terminating one.
135  */
136 void
137 mem_regions(struct mem_region **memp, int *memsz,
138                 struct mem_region **availp, int *availsz)
139 {
140         int phandle;
141         int asz, msz, fsz;
142         int i, j;
143         int still_merging;
144         
145         /*
146          * Get memory.
147          */
148         if ((phandle = OF_finddevice("/memory")) == -1
149             || (msz = OF_getprop(phandle, "reg",
150                           OFmem, sizeof OFmem[0] * OFMEM_REGIONS))
151                <= 0
152             || (asz = OF_getprop(phandle, "available",
153                           OFavail, sizeof OFavail[0] * OFMEM_REGIONS))
154                <= 0)
155                 panic("no memory?");
156         *memp = OFmem;
157         *memsz = msz / sizeof(struct mem_region);
158
159         /*
160          * OFavail may have overlapping regions - collapse these
161          * and copy out remaining regions to OFfree
162          */
163         asz /= sizeof(struct mem_region);
164         do {
165                 still_merging = FALSE;
166                 for (i = 0; i < asz; i++) {
167                         if (OFavail[i].mr_size == 0)
168                                 continue;
169                         for (j = i+1; j < asz; j++) {
170                                 if (OFavail[j].mr_size == 0)
171                                         continue;
172                                 if (memr_overlap(&OFavail[j], &OFavail[i])) {
173                                         memr_merge(&OFavail[j], &OFavail[i]);
174                                         /* mark inactive */
175                                         OFavail[j].mr_size = 0;
176                                         still_merging = TRUE;
177                                 }
178                         }
179                 }
180         } while (still_merging == TRUE);
181
182         /* evict inactive ranges */
183         for (i = 0, fsz = 0; i < asz; i++) {
184                 if (OFavail[i].mr_size != 0) {
185                         OFfree[fsz] = OFavail[i];
186                         fsz++;
187                 }
188         }
189
190         *availp = OFfree;
191         *availsz = fsz;
192 }
193
194 void
195 set_openfirm_callback(int (*openfirm)(void *))
196 {
197
198         ofwcall = openfirm;
199 }
200
201 int
202 openfirmware(void *args)
203 {
204         long    oldmsr;
205         int     result;
206         u_int   srsave[16];
207         u_int   i;
208
209         __asm __volatile(       "\t"
210                 "sync\n\t"
211                 "mfmsr  %0\n\t"
212                 "mtmsr  %1\n\t"
213                 "isync\n"
214                 : "=r" (oldmsr)
215                 : "r" (ofmsr[0])
216         );
217
218         ofw_sprg_prepare();
219
220         if (pmap_bootstrapped) {
221                 /*
222                  * Swap the kernel's address space with Open Firmware's
223                  */
224                 for (i = 0; i < 16; i++) {
225                         srsave[i] = mfsrin(i << ADDR_SR_SHFT);
226                         mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
227                 }
228
229                 /*
230                  * Clear battable[] translations
231                  */
232                 __asm __volatile("mtdbatu 2, %0\n"
233                                  "mtdbatu 3, %0" : : "r" (0));
234                 isync();
235         }
236
237         result = ofwcall(args);
238
239         if (pmap_bootstrapped) {
240                 /*
241                  * Restore the kernel's addr space. The isync() doesn;t
242                  * work outside the loop unless mtsrin() is open-coded
243                  * in an asm statement :(
244                  */
245                 for (i = 0; i < 16; i++) {
246                         mtsrin(i << ADDR_SR_SHFT, srsave[i]);
247                         isync();
248                 }
249         }
250
251         ofw_sprg_restore();
252
253         __asm(  "\t"
254                 "mtmsr  %0\n\t"
255                 "isync\n"
256                 : : "r" (oldmsr)
257         );
258
259         return (result);
260 }
261
262 void
263 OF_halt()
264 {
265         int retval;     /* dummy, this may not be needed */
266
267         OF_interpret("shut-down", 1, &retval);
268         for (;;);       /* just in case */
269 }
270
271 void
272 OF_reboot()
273 {
274         int retval;     /* dummy, this may not be needed */
275
276         OF_interpret("reset-all", 1, &retval);
277         for (;;);       /* just in case */
278 }
279
280 void
281 OF_getetheraddr(device_t dev, u_char *addr)
282 {
283         phandle_t       node;
284
285         node = ofw_bus_get_node(dev);
286         OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
287 }
288
289 /*
290  * Return a bus handle and bus tag that corresponds to the register
291  * numbered regno for the device referenced by the package handle
292  * dev. This function is intended to be used by console drivers in
293  * early boot only. It works by mapping the address of the device's
294  * register in the address space of its parent and recursively walk
295  * the device tree upward this way.
296  */
297 static void
298 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
299 {
300         char name[16];
301         uint32_t addr, size;
302         int pci, res;
303
304         res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
305         if (res == -1)
306                 addr = 2;
307         res = OF_getprop(node, "#size-cells", &size, sizeof(size));
308         if (res == -1)
309                 size = 1;
310         pci = 0;
311         if (addr == 3 && size == 2) {
312                 res = OF_getprop(node, "name", name, sizeof(name));
313                 if (res != -1) {
314                         name[sizeof(name) - 1] = '\0';
315                         pci = (strcmp(name, "pci") == 0) ? 1 : 0;
316                 }
317         }
318         if (addrp != NULL)
319                 *addrp = addr;
320         if (sizep != NULL)
321                 *sizep = size;
322         if (pcip != NULL)
323                 *pcip = pci;
324 }
325
326 int
327 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
328     bus_space_handle_t *handle)
329 {
330         uint32_t cell[32];
331         bus_addr_t addr, raddr, baddr;
332         bus_size_t size, rsize;
333         uint32_t c, nbridge, naddr, nsize;
334         phandle_t bridge, parent;
335         u_int spc, rspc;
336         int pci, pcib, res;
337
338         /* Sanity checking. */
339         if (dev == 0)
340                 return (EINVAL);
341         bridge = OF_parent(dev);
342         if (bridge == 0)
343                 return (EINVAL);
344         if (regno < 0)
345                 return (EINVAL);
346         if (tag == NULL || handle == NULL)
347                 return (EINVAL);
348
349         /* Get the requested register. */
350         OF_get_addr_props(bridge, &naddr, &nsize, &pci);
351         res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
352             cell, sizeof(cell));
353         if (res == -1)
354                 return (ENXIO);
355         if (res % sizeof(cell[0]))
356                 return (ENXIO);
357         res /= sizeof(cell[0]);
358         regno *= naddr + nsize;
359         if (regno + naddr + nsize > res)
360                 return (EINVAL);
361         spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
362         addr = 0;
363         for (c = 0; c < naddr; c++)
364                 addr = ((uint64_t)addr << 32) | cell[regno++];
365         size = 0;
366         for (c = 0; c < nsize; c++)
367                 size = ((uint64_t)size << 32) | cell[regno++];
368
369         /*
370          * Map the address range in the bridge's decoding window as given
371          * by the "ranges" property. If a node doesn't have such property
372          * then no mapping is done.
373          */
374         parent = OF_parent(bridge);
375         while (parent != 0) {
376                 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
377                 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
378                 if (res == -1)
379                         goto next;
380                 if (res % sizeof(cell[0]))
381                         return (ENXIO);
382                 res /= sizeof(cell[0]);
383                 regno = 0;
384                 while (regno < res) {
385                         rspc = (pci)
386                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
387                             : ~0;
388                         if (rspc != spc) {
389                                 regno += naddr + nbridge + nsize;
390                                 continue;
391                         }
392                         raddr = 0;
393                         for (c = 0; c < naddr; c++)
394                                 raddr = ((uint64_t)raddr << 32) | cell[regno++];
395                         rspc = (pcib)
396                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
397                             : ~0;
398                         baddr = 0;
399                         for (c = 0; c < nbridge; c++)
400                                 baddr = ((uint64_t)baddr << 32) | cell[regno++];
401                         rsize = 0;
402                         for (c = 0; c < nsize; c++)
403                                 rsize = ((uint64_t)rsize << 32) | cell[regno++];
404                         if (addr < raddr || addr >= raddr + rsize)
405                                 continue;
406                         addr = addr - raddr + baddr;
407                         if (rspc != ~0)
408                                 spc = rspc;
409                 }
410
411         next:
412                 bridge = parent;
413                 parent = OF_parent(bridge);
414                 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
415         }
416
417         *tag = &bs_le_tag;
418         return (bus_space_map(*tag, addr, size, 0, handle));
419 }
420
421 int
422 mem_valid(vm_offset_t addr, int len)
423 {
424         int i;
425
426         for (i = 0; i < OFMEM_REGIONS; i++)
427                 if ((addr >= OFmem[i].mr_start) 
428                     && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))
429                         return (0);
430
431         return (EFAULT);
432 }