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 powermac_probe(platform_t); 59 static int powermac_attach(platform_t); 60 void powermac_mem_regions(platform_t, struct mem_region **phys, int *physsz, 61 struct mem_region **avail, int *availsz); 62 static u_long powermac_timebase_freq(platform_t, struct cpuref *cpuref); 63 static int powermac_smp_first_cpu(platform_t, struct cpuref *cpuref); 64 static int powermac_smp_next_cpu(platform_t, struct cpuref *cpuref); 65 static int powermac_smp_get_bsp(platform_t, struct cpuref *cpuref); 66 static int powermac_smp_start_cpu(platform_t, struct pcpu *cpu); 67 static void powermac_reset(platform_t); 68 69 static platform_method_t powermac_methods[] = { 70 PLATFORMMETHOD(platform_probe, powermac_probe), 71 PLATFORMMETHOD(platform_attach, powermac_attach), 72 PLATFORMMETHOD(platform_mem_regions, powermac_mem_regions), 73 PLATFORMMETHOD(platform_timebase_freq, powermac_timebase_freq), 74 75 PLATFORMMETHOD(platform_smp_first_cpu, powermac_smp_first_cpu), 76 PLATFORMMETHOD(platform_smp_next_cpu, powermac_smp_next_cpu), 77 PLATFORMMETHOD(platform_smp_get_bsp, powermac_smp_get_bsp), 78 PLATFORMMETHOD(platform_smp_start_cpu, powermac_smp_start_cpu), 79 80 PLATFORMMETHOD(platform_reset, powermac_reset), 81 82 PLATFORMMETHOD_END 83 }; 84 85 static platform_def_t powermac_platform = { 86 "powermac", 87 powermac_methods, 88 0 89 }; 90 91 PLATFORM_DEF(powermac_platform); 92 93 static int 94 powermac_probe(platform_t plat) 95 { 96 char compat[255]; 97 ssize_t compatlen; 98 char *curstr; 99 phandle_t root; 100 101 root = OF_peer(0); 102 if (root == 0) 103 return (ENXIO); 104 105 compatlen = OF_getprop(root, "compatible", compat, sizeof(compat)); 106 107 for (curstr = compat; curstr < compat + compatlen; 108 curstr += strlen(curstr) + 1) { 109 if (strncmp(curstr, "MacRISC", 7) == 0) 110 return (BUS_PROBE_SPECIFIC); 111 } 112 113 return (ENXIO); 114 } 115 116 void 117 powermac_mem_regions(platform_t plat, struct mem_region **phys, int *physsz, 118 struct mem_region **avail, int *availsz) 119 { 120 ofw_mem_regions(phys,physsz,avail,availsz); 121 } 122 123 static int 124 powermac_attach(platform_t plat) 125 { 126 phandle_t rootnode; 127 char model[32]; 128 129 130 /* 131 * Quiesce Open Firmware on PowerMac11,2 and 12,1. It is 132 * necessary there to shut down a background thread doing fan 133 * management, and is harmful on other machines (it will make OF 134 * shut off power to various system components it had turned on). 135 * 136 * Note: we don't need to worry about which OF module we are 137 * using since this is called only from very early boot, within 138 * OF's boot context. 139 */ 140 141 rootnode = OF_finddevice("/"); 142 if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) { 143 if (strcmp(model, "PowerMac11,2") == 0 || 144 strcmp(model, "PowerMac12,1") == 0) { 145 ofw_quiesce(); 146 } 147 } 148 149 return (0); 150 } 151 152 static u_long 153 powermac_timebase_freq(platform_t plat, struct cpuref *cpuref) 154 { 155 phandle_t phandle; 156 int32_t ticks = -1; 157 158 phandle = cpuref->cr_hwref; 159 160 OF_getprop(phandle, "timebase-frequency", &ticks, sizeof(ticks)); 161 162 if (ticks <= 0) 163 panic("Unable to determine timebase frequency!"); 164 165 return (ticks); 166 } 167 168 169 static int 170 powermac_smp_fill_cpuref(struct cpuref *cpuref, phandle_t cpu) 171 { 172 cell_t cpuid, res; 173 174 cpuref->cr_hwref = cpu; 175 res = OF_getprop(cpu, "reg", &cpuid, sizeof(cpuid)); 176 177 /* 178 * psim doesn't have a reg property, so assume 0 as for the 179 * uniprocessor case in the CHRP spec. 180 */ 181 if (res < 0) { 182 cpuid = 0; 183 } 184 185 cpuref->cr_cpuid = cpuid & 0xff; 186 return (0); 187 } 188 189 static int 190 powermac_smp_first_cpu(platform_t plat, struct cpuref *cpuref) 191 { 192 char buf[8]; 193 phandle_t cpu, dev, root; 194 int res; 195 196 root = OF_peer(0); 197 198 dev = OF_child(root); 199 while (dev != 0) { 200 res = OF_getprop(dev, "name", buf, sizeof(buf)); 201 if (res > 0 && strcmp(buf, "cpus") == 0) 202 break; 203 dev = OF_peer(dev); 204 } 205 if (dev == 0) { 206 /* 207 * psim doesn't have a name property on the /cpus node, 208 * but it can be found directly 209 */ 210 dev = OF_finddevice("/cpus"); 211 if (dev == -1) 212 return (ENOENT); 213 } 214 215 cpu = OF_child(dev); 216 217 while (cpu != 0) { 218 res = OF_getprop(cpu, "device_type", buf, sizeof(buf)); 219 if (res > 0 && strcmp(buf, "cpu") == 0) 220 break; 221 cpu = OF_peer(cpu); 222 } 223 if (cpu == 0) 224 return (ENOENT); 225 226 return (powermac_smp_fill_cpuref(cpuref, cpu)); 227 } 228 229 static int 230 powermac_smp_next_cpu(platform_t plat, struct cpuref *cpuref) 231 { 232 char buf[8]; 233 phandle_t cpu; 234 int res; 235 236 cpu = OF_peer(cpuref->cr_hwref); 237 while (cpu != 0) { 238 res = OF_getprop(cpu, "device_type", buf, sizeof(buf)); 239 if (res > 0 && strcmp(buf, "cpu") == 0) 240 break; 241 cpu = OF_peer(cpu); 242 } 243 if (cpu == 0) 244 return (ENOENT); 245 246 return (powermac_smp_fill_cpuref(cpuref, cpu)); 247 } 248 249 static int 250 powermac_smp_get_bsp(platform_t plat, struct cpuref *cpuref) 251 { 252 ihandle_t inst; 253 phandle_t bsp, chosen; 254 int res; 255 256 chosen = OF_finddevice("/chosen"); 257 if (chosen == -1) 258 return (ENXIO); 259 260 res = OF_getprop(chosen, "cpu", &inst, sizeof(inst)); 261 if (res < 0) 262 return (ENXIO); 263 264 bsp = OF_instance_to_package(inst); 265 return (powermac_smp_fill_cpuref(cpuref, bsp)); 266 } 267 268 static int 269 powermac_smp_start_cpu(platform_t plat, struct pcpu *pc) 270 { 271 #ifdef SMP 272 phandle_t cpu; 273 volatile uint8_t *rstvec; 274 static volatile uint8_t *rstvec_virtbase = NULL; 275 int res, reset, timeout; 276 277 cpu = pc->pc_hwref; 278 res = OF_getprop(cpu, "soft-reset", &reset, sizeof(reset)); 279 if (res < 0) { 280 reset = 0x58; 281 282 switch (pc->pc_cpuid) { 283 case 0: 284 reset += 0x03; 285 break; 286 case 1: 287 reset += 0x04; 288 break; 289 case 2: 290 reset += 0x0f; 291 break; 292 case 3: 293 reset += 0x10; 294 break; 295 default: 296 return (ENXIO); 297 } 298 } 299 300 ap_pcpu = pc; 301 302 if (rstvec_virtbase == NULL) 303 rstvec_virtbase = pmap_mapdev(0x80000000, PAGE_SIZE); 304 305 rstvec = rstvec_virtbase + reset; 306 307 *rstvec = 4; 308 powerpc_sync(); 309 (void)(*rstvec); 310 powerpc_sync(); 311 DELAY(1); 312 *rstvec = 0; 313 powerpc_sync(); 314 (void)(*rstvec); 315 powerpc_sync(); 316 317 timeout = 10000; 318 while (!pc->pc_awake && timeout--) 319 DELAY(100); 320 321 return ((pc->pc_awake) ? 0 : EBUSY); 322 #else 323 /* No SMP support */ 324 return (ENXIO); 325 #endif 326 } 327 328 static void 329 powermac_reset(platform_t platform) 330 { 331 OF_reboot(); 332 } 333 334