1 /*- 2 * Copyright (c) 2010 Nathan Whitehorn 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/bus.h> 34 #include <sys/pcpu.h> 35 #include <sys/proc.h> 36 #include <sys/reboot.h> 37 #include <sys/smp.h> 38 39 #include <vm/vm.h> 40 #include <vm/pmap.h> 41 42 #include <machine/bus.h> 43 #include <machine/cpu.h> 44 #include <machine/hid.h> 45 #include <machine/platform.h> 46 #include <machine/platformvar.h> 47 #include <machine/pmap.h> 48 #include <machine/smp.h> 49 #include <machine/spr.h> 50 #include <machine/vmparam.h> 51 52 #include <dev/ofw/openfirm.h> 53 54 #include "platform_if.h" 55 #include "ps3-hvcall.h" 56 57 #ifdef SMP 58 extern void *ap_pcpu; 59 #endif 60 61 static int ps3_probe(platform_t); 62 static int ps3_attach(platform_t); 63 static void ps3_mem_regions(platform_t, struct mem_region *phys, int *physsz, 64 struct mem_region *avail, int *availsz); 65 static vm_offset_t ps3_real_maxaddr(platform_t); 66 static u_long ps3_timebase_freq(platform_t, struct cpuref *cpuref); 67 #ifdef SMP 68 static int ps3_smp_first_cpu(platform_t, struct cpuref *cpuref); 69 static int ps3_smp_next_cpu(platform_t, struct cpuref *cpuref); 70 static int ps3_smp_get_bsp(platform_t, struct cpuref *cpuref); 71 static int ps3_smp_start_cpu(platform_t, struct pcpu *cpu); 72 static struct cpu_group *ps3_smp_topo(platform_t); 73 #endif 74 static void ps3_reset(platform_t); 75 static void ps3_cpu_idle(sbintime_t); 76 77 static platform_method_t ps3_methods[] = { 78 PLATFORMMETHOD(platform_probe, ps3_probe), 79 PLATFORMMETHOD(platform_attach, ps3_attach), 80 PLATFORMMETHOD(platform_mem_regions, ps3_mem_regions), 81 PLATFORMMETHOD(platform_real_maxaddr, ps3_real_maxaddr), 82 PLATFORMMETHOD(platform_timebase_freq, ps3_timebase_freq), 83 84 #ifdef SMP 85 PLATFORMMETHOD(platform_smp_first_cpu, ps3_smp_first_cpu), 86 PLATFORMMETHOD(platform_smp_next_cpu, ps3_smp_next_cpu), 87 PLATFORMMETHOD(platform_smp_get_bsp, ps3_smp_get_bsp), 88 PLATFORMMETHOD(platform_smp_start_cpu, ps3_smp_start_cpu), 89 PLATFORMMETHOD(platform_smp_topo, ps3_smp_topo), 90 #endif 91 92 PLATFORMMETHOD(platform_reset, ps3_reset), 93 94 PLATFORMMETHOD_END 95 }; 96 97 static platform_def_t ps3_platform = { 98 "ps3", 99 ps3_methods, 100 0 101 }; 102 103 PLATFORM_DEF(ps3_platform); 104 105 static int 106 ps3_probe(platform_t plat) 107 { 108 phandle_t root; 109 char compatible[64]; 110 111 root = OF_finddevice("/"); 112 if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0) 113 return (BUS_PROBE_NOWILDCARD); 114 115 if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0) 116 return (BUS_PROBE_NOWILDCARD); 117 118 return (BUS_PROBE_SPECIFIC); 119 } 120 121 static int 122 ps3_attach(platform_t plat) 123 { 124 125 pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC); 126 cpu_idle_hook = ps3_cpu_idle; 127 128 /* Set a breakpoint to make NULL an invalid address */ 129 lv1_set_dabr(0x7 /* read and write, MMU on */, 2 /* kernel accesses */); 130 131 return (0); 132 } 133 134 void 135 ps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz, 136 struct mem_region *avail_regions, int *availsz) 137 { 138 uint64_t lpar_id, junk, ppe_id; 139 140 /* Get real mode memory region */ 141 avail_regions[0].mr_start = 0; 142 lv1_get_logical_partition_id(&lpar_id); 143 lv1_get_logical_ppe_id(&ppe_id); 144 lv1_get_repository_node_value(lpar_id, 145 lv1_repository_string("bi") >> 32, lv1_repository_string("pu"), 146 ppe_id, lv1_repository_string("rm_size"), 147 &avail_regions[0].mr_size, &junk); 148 149 /* Now get extended memory region */ 150 lv1_get_repository_node_value(lpar_id, 151 lv1_repository_string("bi") >> 32, 152 lv1_repository_string("rgntotal"), 0, 0, 153 &avail_regions[1].mr_size, &junk); 154 155 /* Convert to maximum amount we can allocate in 16 MB pages */ 156 avail_regions[1].mr_size -= avail_regions[0].mr_size; 157 avail_regions[1].mr_size -= avail_regions[1].mr_size % (16*1024*1024); 158 159 /* Allocate extended memory region */ 160 lv1_allocate_memory(avail_regions[1].mr_size, 24 /* 16 MB pages */, 161 0, 0x04 /* any address */, &avail_regions[1].mr_start, &junk); 162 163 *availsz = 2; 164 165 if (phys != NULL) { 166 memcpy(phys, avail_regions, sizeof(*phys)*2); 167 *physsz = 2; 168 } 169 } 170 171 static u_long 172 ps3_timebase_freq(platform_t plat, struct cpuref *cpuref) 173 { 174 uint64_t ticks, node_id, junk; 175 176 lv1_get_repository_node_value(PS3_LPAR_ID_PME, 177 lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk); 178 lv1_get_repository_node_value(PS3_LPAR_ID_PME, 179 lv1_repository_string("be") >> 32, node_id, 180 lv1_repository_string("clock"), 0, &ticks, &junk); 181 182 return (ticks); 183 } 184 185 #ifdef SMP 186 static int 187 ps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref) 188 { 189 190 cpuref->cr_cpuid = 0; 191 cpuref->cr_hwref = cpuref->cr_cpuid; 192 193 return (0); 194 } 195 196 static int 197 ps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref) 198 { 199 200 if (cpuref->cr_cpuid >= 1) 201 return (ENOENT); 202 203 cpuref->cr_cpuid++; 204 cpuref->cr_hwref = cpuref->cr_cpuid; 205 206 return (0); 207 } 208 209 static int 210 ps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref) 211 { 212 213 cpuref->cr_cpuid = 0; 214 cpuref->cr_hwref = cpuref->cr_cpuid; 215 216 return (0); 217 } 218 219 static int 220 ps3_smp_start_cpu(platform_t plat, struct pcpu *pc) 221 { 222 /* loader(8) is spinning on 0x40 == 0 right now */ 223 uint32_t *secondary_spin_sem = (uint32_t *)(0x40); 224 int timeout; 225 226 if (pc->pc_hwref != 1) 227 return (ENXIO); 228 229 ap_pcpu = pc; 230 *secondary_spin_sem = 1; 231 powerpc_sync(); 232 DELAY(1); 233 234 timeout = 10000; 235 while (!pc->pc_awake && timeout--) 236 DELAY(100); 237 238 return ((pc->pc_awake) ? 0 : EBUSY); 239 } 240 241 static struct cpu_group * 242 ps3_smp_topo(platform_t plat) 243 { 244 return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT)); 245 } 246 #endif 247 248 static void 249 ps3_reset(platform_t plat) 250 { 251 lv1_panic(1); 252 } 253 254 static vm_offset_t 255 ps3_real_maxaddr(platform_t plat) 256 { 257 struct mem_region *phys, *avail; 258 int nphys, navail; 259 260 mem_regions(&phys, &nphys, &avail, &navail); 261 262 return (phys[0].mr_start + phys[0].mr_size); 263 } 264 265 static void 266 ps3_cpu_idle(sbintime_t sbt) 267 { 268 lv1_pause(0); 269 } 270 271