]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/pmsCPU.c
f241ba7d17ca60b96d6b7b853f83c90532f8cde5
[apple/xnu.git] / osfmk / ppc / pmsCPU.c
1 /*
2 * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 #include <ppc/machine_routines.h>
31 #include <ppc/machine_cpu.h>
32 #include <ppc/exception.h>
33 #include <ppc/misc_protos.h>
34 #include <ppc/Firmware.h>
35 #include <ppc/pmap.h>
36 #include <ppc/asm.h>
37 #include <ppc/proc_reg.h>
38 #include <kern/pms.h>
39 #include <ppc/savearea.h>
40 #include <ppc/Diagnostics.h>
41 #include <kern/processor.h>
42
43
44 static void pmsCPURemote(uint32_t nstep);
45
46
47 pmsDef pmsDefault[] = {
48 {
49 .pmsLimit = century, /* We can normally stay here for 100 years */
50 .pmsStepID = pmsIdle, /* Unique identifier to this step */
51 .pmsSetCmd = 0, /* Dummy platform power level */
52 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
53 .pmsDown = pmsIdle, /* We stay here */
54 .pmsNext = pmsNorm /* Next step */
55 },
56 {
57 .pmsLimit = century, /* We can normally stay here for 100 years */
58 .pmsStepID = pmsNorm, /* Unique identifier to this step */
59 .pmsSetCmd = 0, /* Dummy platform power level */
60 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
61 .pmsDown = pmsIdle, /* Down to idle */
62 .pmsNext = pmsNorm /* Next step */
63 },
64 {
65 .pmsLimit = century, /* We can normally stay here for 100 years */
66 .pmsStepID = pmsNormHigh, /* Unique identifier to this step */
67 .pmsSetCmd = 0, /* Dummy platform power level */
68 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
69 .pmsDown = pmsIdle, /* Down to idle */
70 .pmsNext = pmsNormHigh /* Next step */
71 },
72 {
73 .pmsLimit = century, /* We can normally stay here for 100 years */
74 .pmsStepID = pmsBoost, /* Unique identifier to this step */
75 .pmsSetCmd = 0, /* Dummy platform power level */
76 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
77 .pmsDown = pmsIdle, /* Step down */
78 .pmsNext = pmsBoost /* Next step */
79 },
80 {
81 .pmsLimit = century, /* We can normally stay here for 100 years */
82 .pmsStepID = pmsLow, /* Unique identifier to this step */
83 .pmsSetCmd = 0, /* Dummy platform power level */
84 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
85 .pmsDown = pmsLow, /* We always stay here */
86 .pmsNext = pmsLow /* We always stay here */
87 },
88 {
89 .pmsLimit = century, /* We can normally stay here for 100 years */
90 .pmsStepID = pmsHigh, /* Unique identifier to this step */
91 .pmsSetCmd = 0, /* Dummy platform power level */
92 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
93 .pmsDown = pmsHigh, /* We always stay here */
94 .pmsNext = pmsHigh /* We always stay here */
95 },
96 {
97 .pmsLimit = 0, /* Time doesn't matter for a prepare for change */
98 .pmsStepID = pmsPrepCng, /* Unique identifier to this step */
99 .pmsSetCmd = pmsParkIt, /* Force us to be parked */
100 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
101 .pmsDown = pmsPrepCng, /* We always stay here */
102 .pmsNext = pmsPrepCng /* We always stay here */
103 },
104 {
105 .pmsLimit = 0, /* Time doesn't matter for a prepare for sleep */
106 .pmsStepID = pmsPrepSleep, /* Unique identifier to this step */
107 .pmsSetCmd = pmsParkIt, /* Force us to be parked */
108 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
109 .pmsDown = pmsPrepSleep, /* We always stay here */
110 .pmsNext = pmsPrepSleep /* We always stay here */
111 },
112 {
113 .pmsLimit = 0, /* Time doesn't matter for a prepare for sleep */
114 .pmsStepID = pmsOverTemp, /* Unique identifier to this step */
115 .pmsSetCmd = 0, /* Dummy platform power level */
116 .sf.pmsSetFuncInd = 0, /* Dummy platform set function */
117 .pmsDown = pmsOverTemp, /* We always stay here */
118 .pmsNext = pmsOverTemp /* We always stay here */
119 }
120 };
121
122
123
124 /*
125 * This is where the CPU part of the stepper code lives.
126 *
127 * It also contains the "hacked kext" experimental code. This is/was used for
128 * experimentation and bringup. It should neither live long nor prosper.
129 *
130 */
131
132 /*
133 * Set the processor frequency and stuff
134 */
135
136 void pmsCPUSet(uint32_t sel) {
137 int nvoltage, nfreq;
138 uint32_t oldaack;
139 struct per_proc_info *pp;
140
141 pp = getPerProc(); /* Get our per_proc */
142
143 if(!((sel ^ pp->pms.pmsCSetCmd) & pmsCPU)) return; /* If there aren't any changes, bail now... */
144
145 nfreq = (sel & pmsCPU) >> 16; /* Isolate the new frequency */
146
147 switch(pp->pf.pfPowerModes & pmType) { /* Figure out what type to do */
148
149 case pmDFS: /* This is a DFS machine */
150 ml_set_processor_speed_dfs(nfreq); /* Yes, set it */
151 break;
152
153 case pmDualPLL:
154 ml_set_processor_speed_dpll(nfreq); /* THIS IS COMPLETELY UNTESTED!!! */
155 break;
156
157 case pmPowerTune: /* This is a PowerTune machine */
158 ml_set_processor_speed_powertune(nfreq); /* Diddle the deal */
159 break;
160
161 default: /* Not this time dolt!!! */
162 panic("pmsCPUSet: unsupported power manager type: %08X\n", pp->pf.pfPowerModes);
163 break;
164
165 }
166
167 }
168
169 /*
170 * This code configures the initial step tables. It should be called after the timebase frequency is initialized.
171 */
172
173 void pmsCPUConf(void) {
174
175 int i;
176 kern_return_t ret;
177 pmsSetFunc_t pmsDfltFunc[pmsSetFuncMax]; /* List of functions for the external power control to use */
178
179 for(i = 0; i < pmsSetFuncMax; i++) pmsDfltFunc[i] = 0; /* Clear this */
180
181
182 ret = pmsBuild((pmsDef *)&pmsDefault, sizeof(pmsDefault), pmsDfltFunc, 0, (pmsQueryFunc_t)0); /* Configure the default stepper */
183
184 pCCfinish:
185 if(ret != KERN_SUCCESS) { /* Some screw up? */
186 panic("pmsCPUConf: initial stepper table build failed, ret = %08X\n", ret); /* Squeal */
187 }
188
189 pmsSetStep(pmsHigh, 1); /* Slew to high speed */
190 pmsPark(); /* Then park */
191 return;
192 }
193
194 /*
195 * Machine-dependent initialization
196 */
197 void
198 pmsCPUMachineInit(void)
199 {
200 return;
201 }
202
203 /*
204 * This function should be called once for each processor to force the
205 * processor to the correct voltage and frequency.
206 */
207
208 void pmsCPUInit(void) {
209
210 int cpu;
211
212 cpu = cpu_number(); /* Who are we? */
213
214 kprintf("************ Initializing stepper hardware, cpu %d ******************\n", cpu); /* (BRINGUP) */
215
216 pmsSetStep(pmsHigh, 1); /* Slew to high speed */
217 pmsPark(); /* Then park */
218
219 kprintf("************ Stepper hardware initialized, cpu %d ******************\n", cpu); /* (BRINGUP) */
220
221 return;
222 }
223
224 uint32_t pmsCPUQuery(void) {
225
226 uint32_t result;
227 struct per_proc_info *pp;
228 uint64_t scdata;
229
230 pp = getPerProc(); /* Get our per_proc */
231
232 switch(pp->pf.pfPowerModes & pmType) { /* Figure out what type to do */
233
234 case pmDFS: /* This is a DFS machine */
235 result = hid1get(); /* Get HID1 */
236 result = (result >> 6) & 0x00030000; /* Isolate the DFS bits */
237 break;
238
239 case pmPowerTune: /* This is a PowerTune machine */
240 (void)ml_scom_read(PowerTuneStatusReg, &scdata); /* Get the current power level */
241 result = (scdata >> (32 + 8)) & 0x00030000; /* Shift the data to align with the set command */
242 break;
243
244 default: /* Query not supported for this kind */
245 result = 0; /* Return highest if not supported */
246 break;
247
248 }
249
250 return result;
251 }
252
253 /*
254 * These are not implemented for PPC.
255 */
256 void pmsCPUYellowFlag(void) {
257 }
258
259 void pmsCPUGreenFlag(void) {
260 }
261
262 uint32_t pmsCPUPackageQuery(void)
263 {
264 /* multi-core CPUs are not supported. */
265 return(~(uint32_t)0);
266 }
267
268 /*
269 * Broadcast a change to all processors including ourselves.
270 * This must transition before broadcasting because we may block and end up on a different processor.
271 *
272 * This will block until all processors have transitioned, so
273 * obviously, this can block.
274 *
275 * Called with interruptions disabled.
276 *
277 */
278
279 void pmsCPURun(uint32_t nstep) {
280
281 pmsRunLocal(nstep); /* If we aren't parking (we are already parked), transition ourselves */
282 (void)cpu_broadcast(&pmsBroadcastWait, pmsCPURemote, nstep); /* Tell everyone else to do it too */
283
284 return;
285
286 }
287
288 /*
289 * Receive a broadcast and react.
290 * This is called from the interprocessor signal handler.
291 * We wake up the initiator after we are finished.
292 *
293 */
294
295 static void pmsCPURemote(uint32_t nstep) {
296
297 pmsRunLocal(nstep); /* Go set the step */
298 if(!hw_atomic_sub(&pmsBroadcastWait, 1)) { /* Drop the wait count */
299 thread_wakeup((event_t)&pmsBroadcastWait); /* If we were the last, wake up the signaller */
300 }
301 return;
302 }
303
304 /*
305 * Control the Power Management Stepper.
306 * Called from user state by the superuser via a ppc system call.
307 * Interruptions disabled.
308 *
309 */
310 int pmsCntrl(struct savearea *save) {
311 save->save_r3 = pmsControl(save->save_r3, (user_addr_t)(uintptr_t)save->save_r4, save->save_r5);
312 return 1;
313 }
314
315
316