]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/aim/platform_chrp.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / powerpc / aim / platform_chrp.c
1 /*-
2  * Copyright (c) 2008 Marcel Moolenaar
3  * Copyright (c) 2009 Nathan Whitehorn
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  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/pcpu.h>
36 #include <sys/proc.h>
37 #include <sys/smp.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/hid.h>
44 #include <machine/platformvar.h>
45 #include <machine/pmap.h>
46 #include <machine/smp.h>
47 #include <machine/spr.h>
48
49 #include <dev/ofw/openfirm.h>
50 #include <machine/ofw_machdep.h>
51
52 #include "platform_if.h"
53
54 #ifdef SMP
55 extern void *ap_pcpu;
56 #endif
57
58 static int chrp_probe(platform_t);
59 void chrp_mem_regions(platform_t, struct mem_region **phys, int *physsz,
60     struct mem_region **avail, int *availsz);
61 static u_long chrp_timebase_freq(platform_t, struct cpuref *cpuref);
62 static int chrp_smp_first_cpu(platform_t, struct cpuref *cpuref);
63 static int chrp_smp_next_cpu(platform_t, struct cpuref *cpuref);
64 static int chrp_smp_get_bsp(platform_t, struct cpuref *cpuref);
65 static int chrp_smp_start_cpu(platform_t, struct pcpu *cpu);
66
67 static platform_method_t chrp_methods[] = {
68         PLATFORMMETHOD(platform_probe,          chrp_probe),
69         PLATFORMMETHOD(platform_mem_regions,    chrp_mem_regions),
70         PLATFORMMETHOD(platform_timebase_freq,  chrp_timebase_freq),
71         
72         PLATFORMMETHOD(platform_smp_first_cpu,  chrp_smp_first_cpu),
73         PLATFORMMETHOD(platform_smp_next_cpu,   chrp_smp_next_cpu),
74         PLATFORMMETHOD(platform_smp_get_bsp,    chrp_smp_get_bsp),
75         PLATFORMMETHOD(platform_smp_start_cpu,  chrp_smp_start_cpu),
76
77         { 0, 0 }
78 };
79
80 static platform_def_t chrp_platform = {
81         "chrp",
82         chrp_methods,
83         0
84 };
85
86 PLATFORM_DEF(chrp_platform);
87
88 static int
89 chrp_probe(platform_t plat)
90 {
91         if (OF_finddevice("/memory") != -1)
92                 return (BUS_PROBE_GENERIC);
93
94         return (ENXIO);
95 }
96
97 void
98 chrp_mem_regions(platform_t plat, struct mem_region **phys, int *physsz,
99     struct mem_region **avail, int *availsz)
100 {
101         ofw_mem_regions(phys,physsz,avail,availsz);
102 }
103
104 static u_long
105 chrp_timebase_freq(platform_t plat, struct cpuref *cpuref)
106 {
107         phandle_t phandle;
108         long ticks = -1;
109
110         phandle = cpuref->cr_hwref;
111
112         OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks));
113
114         if (ticks <= 0)
115                 panic("Unable to determine timebase frequency!");
116
117         return (ticks);
118 }
119
120
121 static int
122 chrp_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu)
123 {
124         int cpuid, res;
125
126         cpuref->cr_hwref = cpu;
127         res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid));
128
129         /*
130          * psim doesn't have a reg property, so assume 0 as for the
131          * uniprocessor case in the CHRP spec. 
132          */
133         if (res < 0) {
134                 cpuid = 0;
135         }
136
137         cpuref->cr_cpuid = cpuid & 0xff;
138         return (0);
139 }
140
141 static int
142 chrp_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
143 {
144         char buf[8];
145         phandle_t cpu, dev, root;
146         int res;
147
148         root = OF_peer(0);
149
150         dev = OF_child(root);
151         while (dev != 0) {
152                 res = OF_getprop(dev, "name", buf, sizeof(buf));
153                 if (res > 0 && strcmp(buf, "cpus") == 0)
154                         break;
155                 dev = OF_peer(dev);
156         }
157         if (dev == 0) {
158                 /*
159                  * psim doesn't have a name property on the /cpus node,
160                  * but it can be found directly
161                  */
162                 dev = OF_finddevice("/cpus");
163                 if (dev == 0)
164                         return (ENOENT);
165         }
166
167         cpu = OF_child(dev);
168
169         while (cpu != 0) {
170                 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
171                 if (res > 0 && strcmp(buf, "cpu") == 0)
172                         break;
173                 cpu = OF_peer(cpu);
174         }
175         if (cpu == 0)
176                 return (ENOENT);
177
178         return (chrp_smp_fill_cpuref(cpuref, cpu));
179 }
180
181 static int
182 chrp_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
183 {
184         char buf[8];
185         phandle_t cpu;
186         int res;
187
188         cpu = OF_peer(cpuref->cr_hwref);
189         while (cpu != 0) {
190                 res = OF_getprop(cpu, "device_type", buf, sizeof(buf));
191                 if (res > 0 && strcmp(buf, "cpu") == 0)
192                         break;
193                 cpu = OF_peer(cpu);
194         }
195         if (cpu == 0)
196                 return (ENOENT);
197
198         return (chrp_smp_fill_cpuref(cpuref, cpu));
199 }
200
201 static int
202 chrp_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
203 {
204         ihandle_t inst;
205         phandle_t bsp, chosen;
206         int res;
207
208         chosen = OF_finddevice("/chosen");
209         if (chosen == 0)
210                 return (ENXIO);
211
212         res = OF_getprop(chosen, "cpu", &inst, sizeof(inst));
213         if (res < 0)
214                 return (ENXIO);
215
216         bsp = OF_instance_to_package(inst);
217         return (chrp_smp_fill_cpuref(cpuref, bsp));
218 }
219
220 static int
221 chrp_smp_start_cpu(platform_t plat, struct pcpu *pc)
222 {
223 #ifdef SMP
224         phandle_t cpu;
225         volatile uint8_t *rstvec;
226         static volatile uint8_t *rstvec_virtbase = NULL;
227         int res, reset, timeout;
228
229         cpu = pc->pc_hwref;
230         res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset));
231         if (res < 0)
232                 return (ENXIO);
233
234         ap_pcpu = pc;
235
236         if (rstvec_virtbase == NULL)
237                 rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE);
238
239         rstvec = rstvec_virtbase + reset;
240
241         *rstvec = 4;
242         (void)(*rstvec);
243         powerpc_sync();
244         DELAY(1);
245         *rstvec = 0;
246         (void)(*rstvec);
247         powerpc_sync();
248
249         timeout = 10000;
250         while (!pc->pc_awake && timeout--)
251                 DELAY(100);
252
253         return ((pc->pc_awake) ? 0 : EBUSY);
254 #else
255         /* No SMP support */
256         return (ENXIO);
257 #endif
258 }
259