Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * processor_throttling.c - Throttling submodule of the ACPI processor driver | |
3 | * | |
4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
6 | * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de> | |
7 | * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> | |
8 | * - Added processor hotplug support | |
9 | * | |
10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
25 | * | |
26 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
27 | */ | |
28 | ||
29 | #include <linux/kernel.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/init.h> | |
357dc4c3 | 32 | #include <linux/sched.h> |
1da177e4 LT |
33 | #include <linux/cpufreq.h> |
34 | #include <linux/proc_fs.h> | |
35 | #include <linux/seq_file.h> | |
36 | ||
37 | #include <asm/io.h> | |
38 | #include <asm/uaccess.h> | |
39 | ||
40 | #include <acpi/acpi_bus.h> | |
89595b8f | 41 | #include <acpi/acpi_drivers.h> |
1da177e4 LT |
42 | #include <acpi/processor.h> |
43 | ||
1da177e4 | 44 | #define ACPI_PROCESSOR_CLASS "processor" |
1da177e4 | 45 | #define _COMPONENT ACPI_PROCESSOR_COMPONENT |
f52fd66d | 46 | ACPI_MODULE_NAME("processor_throttling"); |
1da177e4 | 47 | |
e4aa5cb2 ZY |
48 | struct throttling_tstate { |
49 | unsigned int cpu; /* cpu nr */ | |
50 | int target_state; /* target T-state */ | |
51 | }; | |
52 | ||
53 | #define THROTTLING_PRECHANGE (1) | |
54 | #define THROTTLING_POSTCHANGE (2) | |
55 | ||
ff55a9ce LB |
56 | static int acpi_processor_get_throttling(struct acpi_processor *pr); |
57 | int acpi_processor_set_throttling(struct acpi_processor *pr, int state); | |
01854e69 | 58 | |
1180509f ZY |
59 | static int acpi_processor_update_tsd_coord(void) |
60 | { | |
61 | int count, count_target; | |
62 | int retval = 0; | |
63 | unsigned int i, j; | |
2fdf66b4 | 64 | cpumask_var_t covered_cpus; |
1180509f ZY |
65 | struct acpi_processor *pr, *match_pr; |
66 | struct acpi_tsd_package *pdomain, *match_pdomain; | |
67 | struct acpi_processor_throttling *pthrottling, *match_pthrottling; | |
68 | ||
2fdf66b4 RR |
69 | if (!alloc_cpumask_var(&covered_cpus, GFP_KERNEL)) |
70 | return -ENOMEM; | |
71 | ||
1180509f ZY |
72 | /* |
73 | * Now that we have _TSD data from all CPUs, lets setup T-state | |
33a2a529 | 74 | * coordination between all CPUs. |
1180509f ZY |
75 | */ |
76 | for_each_possible_cpu(i) { | |
706546d0 | 77 | pr = per_cpu(processors, i); |
1180509f ZY |
78 | if (!pr) |
79 | continue; | |
80 | ||
81 | /* Basic validity check for domain info */ | |
82 | pthrottling = &(pr->throttling); | |
83 | ||
84 | /* | |
85 | * If tsd package for one cpu is invalid, the coordination | |
86 | * among all CPUs is thought as invalid. | |
87 | * Maybe it is ugly. | |
88 | */ | |
89 | if (!pthrottling->tsd_valid_flag) { | |
90 | retval = -EINVAL; | |
91 | break; | |
92 | } | |
93 | } | |
94 | if (retval) | |
95 | goto err_ret; | |
96 | ||
2fdf66b4 | 97 | cpumask_clear(covered_cpus); |
1180509f | 98 | for_each_possible_cpu(i) { |
706546d0 | 99 | pr = per_cpu(processors, i); |
1180509f ZY |
100 | if (!pr) |
101 | continue; | |
102 | ||
2fdf66b4 | 103 | if (cpumask_test_cpu(i, covered_cpus)) |
1180509f ZY |
104 | continue; |
105 | pthrottling = &pr->throttling; | |
106 | ||
107 | pdomain = &(pthrottling->domain_info); | |
2fdf66b4 RR |
108 | cpumask_set_cpu(i, pthrottling->shared_cpu_map); |
109 | cpumask_set_cpu(i, covered_cpus); | |
1180509f ZY |
110 | /* |
111 | * If the number of processor in the TSD domain is 1, it is | |
112 | * unnecessary to parse the coordination for this CPU. | |
113 | */ | |
114 | if (pdomain->num_processors <= 1) | |
115 | continue; | |
116 | ||
117 | /* Validate the Domain info */ | |
118 | count_target = pdomain->num_processors; | |
119 | count = 1; | |
120 | ||
121 | for_each_possible_cpu(j) { | |
122 | if (i == j) | |
123 | continue; | |
124 | ||
706546d0 | 125 | match_pr = per_cpu(processors, j); |
1180509f ZY |
126 | if (!match_pr) |
127 | continue; | |
128 | ||
129 | match_pthrottling = &(match_pr->throttling); | |
130 | match_pdomain = &(match_pthrottling->domain_info); | |
131 | if (match_pdomain->domain != pdomain->domain) | |
132 | continue; | |
133 | ||
134 | /* Here i and j are in the same domain. | |
135 | * If two TSD packages have the same domain, they | |
136 | * should have the same num_porcessors and | |
137 | * coordination type. Otherwise it will be regarded | |
138 | * as illegal. | |
139 | */ | |
140 | if (match_pdomain->num_processors != count_target) { | |
141 | retval = -EINVAL; | |
142 | goto err_ret; | |
143 | } | |
144 | ||
145 | if (pdomain->coord_type != match_pdomain->coord_type) { | |
146 | retval = -EINVAL; | |
147 | goto err_ret; | |
148 | } | |
149 | ||
2fdf66b4 RR |
150 | cpumask_set_cpu(j, covered_cpus); |
151 | cpumask_set_cpu(j, pthrottling->shared_cpu_map); | |
1180509f ZY |
152 | count++; |
153 | } | |
154 | for_each_possible_cpu(j) { | |
155 | if (i == j) | |
156 | continue; | |
157 | ||
706546d0 | 158 | match_pr = per_cpu(processors, j); |
1180509f ZY |
159 | if (!match_pr) |
160 | continue; | |
161 | ||
162 | match_pthrottling = &(match_pr->throttling); | |
163 | match_pdomain = &(match_pthrottling->domain_info); | |
164 | if (match_pdomain->domain != pdomain->domain) | |
165 | continue; | |
166 | ||
167 | /* | |
168 | * If some CPUS have the same domain, they | |
169 | * will have the same shared_cpu_map. | |
170 | */ | |
2fdf66b4 RR |
171 | cpumask_copy(match_pthrottling->shared_cpu_map, |
172 | pthrottling->shared_cpu_map); | |
1180509f ZY |
173 | } |
174 | } | |
175 | ||
176 | err_ret: | |
2fdf66b4 RR |
177 | free_cpumask_var(covered_cpus); |
178 | ||
1180509f | 179 | for_each_possible_cpu(i) { |
706546d0 | 180 | pr = per_cpu(processors, i); |
1180509f ZY |
181 | if (!pr) |
182 | continue; | |
183 | ||
184 | /* | |
185 | * Assume no coordination on any error parsing domain info. | |
186 | * The coordination type will be forced as SW_ALL. | |
187 | */ | |
188 | if (retval) { | |
189 | pthrottling = &(pr->throttling); | |
2fdf66b4 RR |
190 | cpumask_clear(pthrottling->shared_cpu_map); |
191 | cpumask_set_cpu(i, pthrottling->shared_cpu_map); | |
1180509f ZY |
192 | pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL; |
193 | } | |
194 | } | |
195 | ||
196 | return retval; | |
197 | } | |
198 | ||
199 | /* | |
200 | * Update the T-state coordination after the _TSD | |
201 | * data for all cpus is obtained. | |
202 | */ | |
203 | void acpi_processor_throttling_init(void) | |
204 | { | |
205 | if (acpi_processor_update_tsd_coord()) | |
206 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
207 | "Assume no T-state coordination\n")); | |
208 | ||
209 | return; | |
210 | } | |
211 | ||
e4aa5cb2 ZY |
212 | static int acpi_processor_throttling_notifier(unsigned long event, void *data) |
213 | { | |
214 | struct throttling_tstate *p_tstate = data; | |
215 | struct acpi_processor *pr; | |
216 | unsigned int cpu ; | |
217 | int target_state; | |
218 | struct acpi_processor_limit *p_limit; | |
219 | struct acpi_processor_throttling *p_throttling; | |
220 | ||
221 | cpu = p_tstate->cpu; | |
706546d0 | 222 | pr = per_cpu(processors, cpu); |
e4aa5cb2 ZY |
223 | if (!pr) { |
224 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n")); | |
225 | return 0; | |
226 | } | |
227 | if (!pr->flags.throttling) { | |
228 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is " | |
229 | "unsupported on CPU %d\n", cpu)); | |
230 | return 0; | |
231 | } | |
232 | target_state = p_tstate->target_state; | |
233 | p_throttling = &(pr->throttling); | |
234 | switch (event) { | |
235 | case THROTTLING_PRECHANGE: | |
236 | /* | |
237 | * Prechange event is used to choose one proper t-state, | |
238 | * which meets the limits of thermal, user and _TPC. | |
239 | */ | |
240 | p_limit = &pr->limit; | |
241 | if (p_limit->thermal.tx > target_state) | |
242 | target_state = p_limit->thermal.tx; | |
243 | if (p_limit->user.tx > target_state) | |
244 | target_state = p_limit->user.tx; | |
245 | if (pr->throttling_platform_limit > target_state) | |
246 | target_state = pr->throttling_platform_limit; | |
247 | if (target_state >= p_throttling->state_count) { | |
248 | printk(KERN_WARNING | |
249 | "Exceed the limit of T-state \n"); | |
250 | target_state = p_throttling->state_count - 1; | |
251 | } | |
252 | p_tstate->target_state = target_state; | |
253 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:" | |
254 | "target T-state of CPU %d is T%d\n", | |
255 | cpu, target_state)); | |
256 | break; | |
257 | case THROTTLING_POSTCHANGE: | |
258 | /* | |
259 | * Postchange event is only used to update the | |
260 | * T-state flag of acpi_processor_throttling. | |
261 | */ | |
262 | p_throttling->state = target_state; | |
263 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:" | |
264 | "CPU %d is switched to T%d\n", | |
265 | cpu, target_state)); | |
266 | break; | |
267 | default: | |
268 | printk(KERN_WARNING | |
269 | "Unsupported Throttling notifier event\n"); | |
270 | break; | |
271 | } | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
c30c620e LB |
276 | /* |
277 | * _TPC - Throttling Present Capabilities | |
278 | */ | |
01854e69 LY |
279 | static int acpi_processor_get_platform_limit(struct acpi_processor *pr) |
280 | { | |
281 | acpi_status status = 0; | |
27663c58 | 282 | unsigned long long tpc = 0; |
01854e69 | 283 | |
ff55a9ce | 284 | if (!pr) |
01854e69 LY |
285 | return -EINVAL; |
286 | status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc); | |
c30c620e LB |
287 | if (ACPI_FAILURE(status)) { |
288 | if (status != AE_NOT_FOUND) { | |
289 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC")); | |
290 | } | |
01854e69 LY |
291 | return -ENODEV; |
292 | } | |
293 | pr->throttling_platform_limit = (int)tpc; | |
294 | return 0; | |
295 | } | |
296 | ||
297 | int acpi_processor_tstate_has_changed(struct acpi_processor *pr) | |
298 | { | |
ef54d5ad ZY |
299 | int result = 0; |
300 | int throttling_limit; | |
301 | int current_state; | |
302 | struct acpi_processor_limit *limit; | |
303 | int target_state; | |
304 | ||
305 | result = acpi_processor_get_platform_limit(pr); | |
306 | if (result) { | |
307 | /* Throttling Limit is unsupported */ | |
308 | return result; | |
309 | } | |
310 | ||
311 | throttling_limit = pr->throttling_platform_limit; | |
312 | if (throttling_limit >= pr->throttling.state_count) { | |
313 | /* Uncorrect Throttling Limit */ | |
314 | return -EINVAL; | |
315 | } | |
316 | ||
317 | current_state = pr->throttling.state; | |
318 | if (current_state > throttling_limit) { | |
319 | /* | |
320 | * The current state can meet the requirement of | |
321 | * _TPC limit. But it is reasonable that OSPM changes | |
322 | * t-states from high to low for better performance. | |
323 | * Of course the limit condition of thermal | |
324 | * and user should be considered. | |
325 | */ | |
326 | limit = &pr->limit; | |
327 | target_state = throttling_limit; | |
328 | if (limit->thermal.tx > target_state) | |
329 | target_state = limit->thermal.tx; | |
330 | if (limit->user.tx > target_state) | |
331 | target_state = limit->user.tx; | |
332 | } else if (current_state == throttling_limit) { | |
333 | /* | |
334 | * Unnecessary to change the throttling state | |
335 | */ | |
336 | return 0; | |
337 | } else { | |
338 | /* | |
339 | * If the current state is lower than the limit of _TPC, it | |
340 | * will be forced to switch to the throttling state defined | |
341 | * by throttling_platfor_limit. | |
342 | * Because the previous state meets with the limit condition | |
343 | * of thermal and user, it is unnecessary to check it again. | |
344 | */ | |
345 | target_state = throttling_limit; | |
346 | } | |
347 | return acpi_processor_set_throttling(pr, target_state); | |
01854e69 LY |
348 | } |
349 | ||
c30c620e LB |
350 | /* |
351 | * _PTC - Processor Throttling Control (and status) register location | |
352 | */ | |
01854e69 LY |
353 | static int acpi_processor_get_throttling_control(struct acpi_processor *pr) |
354 | { | |
355 | int result = 0; | |
356 | acpi_status status = 0; | |
357 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
358 | union acpi_object *ptc = NULL; | |
359 | union acpi_object obj = { 0 }; | |
9bcb2721 | 360 | struct acpi_processor_throttling *throttling; |
01854e69 LY |
361 | |
362 | status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer); | |
363 | if (ACPI_FAILURE(status)) { | |
c30c620e LB |
364 | if (status != AE_NOT_FOUND) { |
365 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC")); | |
366 | } | |
01854e69 LY |
367 | return -ENODEV; |
368 | } | |
369 | ||
370 | ptc = (union acpi_object *)buffer.pointer; | |
371 | if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE) | |
372 | || (ptc->package.count != 2)) { | |
373 | printk(KERN_ERR PREFIX "Invalid _PTC data\n"); | |
374 | result = -EFAULT; | |
375 | goto end; | |
376 | } | |
377 | ||
378 | /* | |
379 | * control_register | |
380 | */ | |
381 | ||
382 | obj = ptc->package.elements[0]; | |
383 | ||
384 | if ((obj.type != ACPI_TYPE_BUFFER) | |
385 | || (obj.buffer.length < sizeof(struct acpi_ptc_register)) | |
386 | || (obj.buffer.pointer == NULL)) { | |
ff55a9ce LB |
387 | printk(KERN_ERR PREFIX |
388 | "Invalid _PTC data (control_register)\n"); | |
01854e69 LY |
389 | result = -EFAULT; |
390 | goto end; | |
391 | } | |
392 | memcpy(&pr->throttling.control_register, obj.buffer.pointer, | |
393 | sizeof(struct acpi_ptc_register)); | |
394 | ||
395 | /* | |
396 | * status_register | |
397 | */ | |
398 | ||
399 | obj = ptc->package.elements[1]; | |
400 | ||
401 | if ((obj.type != ACPI_TYPE_BUFFER) | |
402 | || (obj.buffer.length < sizeof(struct acpi_ptc_register)) | |
403 | || (obj.buffer.pointer == NULL)) { | |
404 | printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n"); | |
405 | result = -EFAULT; | |
406 | goto end; | |
407 | } | |
408 | ||
409 | memcpy(&pr->throttling.status_register, obj.buffer.pointer, | |
ff55a9ce | 410 | sizeof(struct acpi_ptc_register)); |
01854e69 | 411 | |
9bcb2721 ZY |
412 | throttling = &pr->throttling; |
413 | ||
414 | if ((throttling->control_register.bit_width + | |
415 | throttling->control_register.bit_offset) > 32) { | |
416 | printk(KERN_ERR PREFIX "Invalid _PTC control register\n"); | |
417 | result = -EFAULT; | |
418 | goto end; | |
419 | } | |
420 | ||
421 | if ((throttling->status_register.bit_width + | |
422 | throttling->status_register.bit_offset) > 32) { | |
423 | printk(KERN_ERR PREFIX "Invalid _PTC status register\n"); | |
424 | result = -EFAULT; | |
425 | goto end; | |
426 | } | |
427 | ||
ff55a9ce | 428 | end: |
01854e69 LY |
429 | kfree(buffer.pointer); |
430 | ||
431 | return result; | |
432 | } | |
c30c620e LB |
433 | |
434 | /* | |
435 | * _TSS - Throttling Supported States | |
436 | */ | |
01854e69 LY |
437 | static int acpi_processor_get_throttling_states(struct acpi_processor *pr) |
438 | { | |
439 | int result = 0; | |
440 | acpi_status status = AE_OK; | |
441 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
442 | struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" }; | |
443 | struct acpi_buffer state = { 0, NULL }; | |
444 | union acpi_object *tss = NULL; | |
445 | int i; | |
446 | ||
447 | status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer); | |
448 | if (ACPI_FAILURE(status)) { | |
c30c620e LB |
449 | if (status != AE_NOT_FOUND) { |
450 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS")); | |
451 | } | |
01854e69 LY |
452 | return -ENODEV; |
453 | } | |
454 | ||
455 | tss = buffer.pointer; | |
456 | if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) { | |
457 | printk(KERN_ERR PREFIX "Invalid _TSS data\n"); | |
458 | result = -EFAULT; | |
459 | goto end; | |
460 | } | |
461 | ||
462 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n", | |
463 | tss->package.count)); | |
464 | ||
465 | pr->throttling.state_count = tss->package.count; | |
466 | pr->throttling.states_tss = | |
467 | kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count, | |
468 | GFP_KERNEL); | |
469 | if (!pr->throttling.states_tss) { | |
470 | result = -ENOMEM; | |
471 | goto end; | |
472 | } | |
473 | ||
474 | for (i = 0; i < pr->throttling.state_count; i++) { | |
475 | ||
ff55a9ce LB |
476 | struct acpi_processor_tx_tss *tx = |
477 | (struct acpi_processor_tx_tss *)&(pr->throttling. | |
478 | states_tss[i]); | |
01854e69 LY |
479 | |
480 | state.length = sizeof(struct acpi_processor_tx_tss); | |
481 | state.pointer = tx; | |
482 | ||
483 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i)); | |
484 | ||
485 | status = acpi_extract_package(&(tss->package.elements[i]), | |
486 | &format, &state); | |
487 | if (ACPI_FAILURE(status)) { | |
488 | ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data")); | |
489 | result = -EFAULT; | |
490 | kfree(pr->throttling.states_tss); | |
491 | goto end; | |
492 | } | |
493 | ||
494 | if (!tx->freqpercentage) { | |
495 | printk(KERN_ERR PREFIX | |
ff55a9ce | 496 | "Invalid _TSS data: freq is zero\n"); |
01854e69 LY |
497 | result = -EFAULT; |
498 | kfree(pr->throttling.states_tss); | |
499 | goto end; | |
500 | } | |
501 | } | |
502 | ||
503 | end: | |
504 | kfree(buffer.pointer); | |
505 | ||
506 | return result; | |
507 | } | |
c30c620e LB |
508 | |
509 | /* | |
510 | * _TSD - T-State Dependencies | |
511 | */ | |
ff55a9ce | 512 | static int acpi_processor_get_tsd(struct acpi_processor *pr) |
01854e69 LY |
513 | { |
514 | int result = 0; | |
515 | acpi_status status = AE_OK; | |
ff55a9ce LB |
516 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
517 | struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" }; | |
518 | struct acpi_buffer state = { 0, NULL }; | |
519 | union acpi_object *tsd = NULL; | |
01854e69 | 520 | struct acpi_tsd_package *pdomain; |
1180509f ZY |
521 | struct acpi_processor_throttling *pthrottling; |
522 | ||
523 | pthrottling = &pr->throttling; | |
524 | pthrottling->tsd_valid_flag = 0; | |
01854e69 LY |
525 | |
526 | status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer); | |
527 | if (ACPI_FAILURE(status)) { | |
c30c620e LB |
528 | if (status != AE_NOT_FOUND) { |
529 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD")); | |
530 | } | |
01854e69 LY |
531 | return -ENODEV; |
532 | } | |
533 | ||
534 | tsd = buffer.pointer; | |
535 | if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) { | |
55ac9a01 | 536 | printk(KERN_ERR PREFIX "Invalid _TSD data\n"); |
01854e69 LY |
537 | result = -EFAULT; |
538 | goto end; | |
539 | } | |
540 | ||
541 | if (tsd->package.count != 1) { | |
55ac9a01 | 542 | printk(KERN_ERR PREFIX "Invalid _TSD data\n"); |
01854e69 LY |
543 | result = -EFAULT; |
544 | goto end; | |
545 | } | |
546 | ||
547 | pdomain = &(pr->throttling.domain_info); | |
548 | ||
549 | state.length = sizeof(struct acpi_tsd_package); | |
550 | state.pointer = pdomain; | |
551 | ||
552 | status = acpi_extract_package(&(tsd->package.elements[0]), | |
ff55a9ce | 553 | &format, &state); |
01854e69 | 554 | if (ACPI_FAILURE(status)) { |
55ac9a01 | 555 | printk(KERN_ERR PREFIX "Invalid _TSD data\n"); |
01854e69 LY |
556 | result = -EFAULT; |
557 | goto end; | |
558 | } | |
559 | ||
560 | if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) { | |
55ac9a01 | 561 | printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n"); |
01854e69 LY |
562 | result = -EFAULT; |
563 | goto end; | |
564 | } | |
565 | ||
566 | if (pdomain->revision != ACPI_TSD_REV0_REVISION) { | |
55ac9a01 | 567 | printk(KERN_ERR PREFIX "Unknown _TSD:revision\n"); |
01854e69 LY |
568 | result = -EFAULT; |
569 | goto end; | |
570 | } | |
571 | ||
1180509f ZY |
572 | pthrottling = &pr->throttling; |
573 | pthrottling->tsd_valid_flag = 1; | |
574 | pthrottling->shared_type = pdomain->coord_type; | |
2fdf66b4 | 575 | cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map); |
1180509f ZY |
576 | /* |
577 | * If the coordination type is not defined in ACPI spec, | |
578 | * the tsd_valid_flag will be clear and coordination type | |
579 | * will be forecd as DOMAIN_COORD_TYPE_SW_ALL. | |
580 | */ | |
581 | if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL && | |
582 | pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY && | |
583 | pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) { | |
584 | pthrottling->tsd_valid_flag = 0; | |
585 | pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL; | |
586 | } | |
587 | ||
ff55a9ce | 588 | end: |
01854e69 LY |
589 | kfree(buffer.pointer); |
590 | return result; | |
591 | } | |
592 | ||
1da177e4 LT |
593 | /* -------------------------------------------------------------------------- |
594 | Throttling Control | |
595 | -------------------------------------------------------------------------- */ | |
01854e69 | 596 | static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr) |
1da177e4 | 597 | { |
4be44fcd LB |
598 | int state = 0; |
599 | u32 value = 0; | |
600 | u32 duty_mask = 0; | |
601 | u32 duty_value = 0; | |
1da177e4 | 602 | |
1da177e4 | 603 | if (!pr) |
d550d98d | 604 | return -EINVAL; |
1da177e4 LT |
605 | |
606 | if (!pr->flags.throttling) | |
d550d98d | 607 | return -ENODEV; |
1da177e4 LT |
608 | |
609 | pr->throttling.state = 0; | |
610 | ||
611 | duty_mask = pr->throttling.state_count - 1; | |
612 | ||
613 | duty_mask <<= pr->throttling.duty_offset; | |
614 | ||
615 | local_irq_disable(); | |
616 | ||
617 | value = inl(pr->throttling.address); | |
618 | ||
619 | /* | |
620 | * Compute the current throttling state when throttling is enabled | |
621 | * (bit 4 is on). | |
622 | */ | |
623 | if (value & 0x10) { | |
624 | duty_value = value & duty_mask; | |
625 | duty_value >>= pr->throttling.duty_offset; | |
626 | ||
627 | if (duty_value) | |
4be44fcd | 628 | state = pr->throttling.state_count - duty_value; |
1da177e4 LT |
629 | } |
630 | ||
631 | pr->throttling.state = state; | |
632 | ||
633 | local_irq_enable(); | |
634 | ||
635 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd LB |
636 | "Throttling state is T%d (%d%% throttling applied)\n", |
637 | state, pr->throttling.states[state].performance)); | |
1da177e4 | 638 | |
d550d98d | 639 | return 0; |
1da177e4 LT |
640 | } |
641 | ||
f79f06ab ZY |
642 | #ifdef CONFIG_X86 |
643 | static int acpi_throttling_rdmsr(struct acpi_processor *pr, | |
644 | acpi_integer * value) | |
645 | { | |
646 | struct cpuinfo_x86 *c; | |
647 | u64 msr_high, msr_low; | |
648 | unsigned int cpu; | |
649 | u64 msr = 0; | |
650 | int ret = -1; | |
651 | ||
652 | cpu = pr->id; | |
653 | c = &cpu_data(cpu); | |
654 | ||
655 | if ((c->x86_vendor != X86_VENDOR_INTEL) || | |
656 | !cpu_has(c, X86_FEATURE_ACPI)) { | |
657 | printk(KERN_ERR PREFIX | |
658 | "HARDWARE addr space,NOT supported yet\n"); | |
659 | } else { | |
660 | msr_low = 0; | |
661 | msr_high = 0; | |
357dc4c3 | 662 | rdmsr_safe(MSR_IA32_THERM_CONTROL, |
f79f06ab ZY |
663 | (u32 *)&msr_low , (u32 *) &msr_high); |
664 | msr = (msr_high << 32) | msr_low; | |
665 | *value = (acpi_integer) msr; | |
666 | ret = 0; | |
667 | } | |
668 | return ret; | |
669 | } | |
670 | ||
671 | static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value) | |
672 | { | |
673 | struct cpuinfo_x86 *c; | |
674 | unsigned int cpu; | |
675 | int ret = -1; | |
676 | u64 msr; | |
677 | ||
678 | cpu = pr->id; | |
679 | c = &cpu_data(cpu); | |
680 | ||
681 | if ((c->x86_vendor != X86_VENDOR_INTEL) || | |
682 | !cpu_has(c, X86_FEATURE_ACPI)) { | |
683 | printk(KERN_ERR PREFIX | |
684 | "HARDWARE addr space,NOT supported yet\n"); | |
685 | } else { | |
686 | msr = value; | |
357dc4c3 | 687 | wrmsr_safe(MSR_IA32_THERM_CONTROL, |
f79f06ab ZY |
688 | msr & 0xffffffff, msr >> 32); |
689 | ret = 0; | |
690 | } | |
691 | return ret; | |
692 | } | |
693 | #else | |
694 | static int acpi_throttling_rdmsr(struct acpi_processor *pr, | |
695 | acpi_integer * value) | |
696 | { | |
697 | printk(KERN_ERR PREFIX | |
698 | "HARDWARE addr space,NOT supported yet\n"); | |
699 | return -1; | |
700 | } | |
701 | ||
702 | static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value) | |
703 | { | |
704 | printk(KERN_ERR PREFIX | |
705 | "HARDWARE addr space,NOT supported yet\n"); | |
706 | return -1; | |
707 | } | |
708 | #endif | |
709 | ||
0753f6e0 ZY |
710 | static int acpi_read_throttling_status(struct acpi_processor *pr, |
711 | acpi_integer *value) | |
01854e69 | 712 | { |
9bcb2721 | 713 | u32 bit_width, bit_offset; |
0753f6e0 | 714 | u64 ptc_value; |
9bcb2721 | 715 | u64 ptc_mask; |
0753f6e0 ZY |
716 | struct acpi_processor_throttling *throttling; |
717 | int ret = -1; | |
718 | ||
719 | throttling = &pr->throttling; | |
01854e69 LY |
720 | switch (throttling->status_register.space_id) { |
721 | case ACPI_ADR_SPACE_SYSTEM_IO: | |
0753f6e0 | 722 | ptc_value = 0; |
9bcb2721 ZY |
723 | bit_width = throttling->status_register.bit_width; |
724 | bit_offset = throttling->status_register.bit_offset; | |
725 | ||
ff55a9ce | 726 | acpi_os_read_port((acpi_io_address) throttling->status_register. |
0753f6e0 | 727 | address, (u32 *) &ptc_value, |
9bcb2721 ZY |
728 | (u32) (bit_width + bit_offset)); |
729 | ptc_mask = (1 << bit_width) - 1; | |
730 | *value = (acpi_integer) ((ptc_value >> bit_offset) & ptc_mask); | |
0753f6e0 | 731 | ret = 0; |
01854e69 LY |
732 | break; |
733 | case ACPI_ADR_SPACE_FIXED_HARDWARE: | |
f79f06ab | 734 | ret = acpi_throttling_rdmsr(pr, value); |
01854e69 LY |
735 | break; |
736 | default: | |
737 | printk(KERN_ERR PREFIX "Unknown addr space %d\n", | |
ff55a9ce | 738 | (u32) (throttling->status_register.space_id)); |
01854e69 | 739 | } |
0753f6e0 | 740 | return ret; |
01854e69 LY |
741 | } |
742 | ||
0753f6e0 ZY |
743 | static int acpi_write_throttling_state(struct acpi_processor *pr, |
744 | acpi_integer value) | |
01854e69 | 745 | { |
9bcb2721 | 746 | u32 bit_width, bit_offset; |
0753f6e0 | 747 | u64 ptc_value; |
9bcb2721 | 748 | u64 ptc_mask; |
0753f6e0 | 749 | struct acpi_processor_throttling *throttling; |
01854e69 LY |
750 | int ret = -1; |
751 | ||
0753f6e0 | 752 | throttling = &pr->throttling; |
01854e69 LY |
753 | switch (throttling->control_register.space_id) { |
754 | case ACPI_ADR_SPACE_SYSTEM_IO: | |
9bcb2721 ZY |
755 | bit_width = throttling->control_register.bit_width; |
756 | bit_offset = throttling->control_register.bit_offset; | |
757 | ptc_mask = (1 << bit_width) - 1; | |
758 | ptc_value = value & ptc_mask; | |
759 | ||
ff55a9ce | 760 | acpi_os_write_port((acpi_io_address) throttling-> |
9bcb2721 ZY |
761 | control_register.address, |
762 | (u32) (ptc_value << bit_offset), | |
763 | (u32) (bit_width + bit_offset)); | |
01854e69 LY |
764 | ret = 0; |
765 | break; | |
766 | case ACPI_ADR_SPACE_FIXED_HARDWARE: | |
f79f06ab | 767 | ret = acpi_throttling_wrmsr(pr, value); |
01854e69 LY |
768 | break; |
769 | default: | |
770 | printk(KERN_ERR PREFIX "Unknown addr space %d\n", | |
ff55a9ce | 771 | (u32) (throttling->control_register.space_id)); |
01854e69 LY |
772 | } |
773 | return ret; | |
774 | } | |
775 | ||
0753f6e0 ZY |
776 | static int acpi_get_throttling_state(struct acpi_processor *pr, |
777 | acpi_integer value) | |
01854e69 LY |
778 | { |
779 | int i; | |
780 | ||
781 | for (i = 0; i < pr->throttling.state_count; i++) { | |
ff55a9ce LB |
782 | struct acpi_processor_tx_tss *tx = |
783 | (struct acpi_processor_tx_tss *)&(pr->throttling. | |
784 | states_tss[i]); | |
785 | if (tx->control == value) | |
53af9cfb | 786 | return i; |
01854e69 | 787 | } |
53af9cfb | 788 | return -1; |
01854e69 LY |
789 | } |
790 | ||
0753f6e0 ZY |
791 | static int acpi_get_throttling_value(struct acpi_processor *pr, |
792 | int state, acpi_integer *value) | |
01854e69 | 793 | { |
0753f6e0 ZY |
794 | int ret = -1; |
795 | ||
ff55a9ce LB |
796 | if (state >= 0 && state <= pr->throttling.state_count) { |
797 | struct acpi_processor_tx_tss *tx = | |
798 | (struct acpi_processor_tx_tss *)&(pr->throttling. | |
799 | states_tss[state]); | |
0753f6e0 ZY |
800 | *value = tx->control; |
801 | ret = 0; | |
01854e69 | 802 | } |
0753f6e0 | 803 | return ret; |
01854e69 LY |
804 | } |
805 | ||
806 | static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr) | |
807 | { | |
808 | int state = 0; | |
0753f6e0 ZY |
809 | int ret; |
810 | acpi_integer value; | |
01854e69 | 811 | |
01854e69 LY |
812 | if (!pr) |
813 | return -EINVAL; | |
814 | ||
815 | if (!pr->flags.throttling) | |
816 | return -ENODEV; | |
817 | ||
818 | pr->throttling.state = 0; | |
357dc4c3 | 819 | |
0753f6e0 ZY |
820 | value = 0; |
821 | ret = acpi_read_throttling_status(pr, &value); | |
822 | if (ret >= 0) { | |
ff55a9ce | 823 | state = acpi_get_throttling_state(pr, value); |
01854e69 LY |
824 | pr->throttling.state = state; |
825 | } | |
01854e69 LY |
826 | |
827 | return 0; | |
828 | } | |
829 | ||
01854e69 LY |
830 | static int acpi_processor_get_throttling(struct acpi_processor *pr) |
831 | { | |
2fdf66b4 | 832 | cpumask_var_t saved_mask; |
357dc4c3 ZY |
833 | int ret; |
834 | ||
87654273 ZY |
835 | if (!pr) |
836 | return -EINVAL; | |
837 | ||
838 | if (!pr->flags.throttling) | |
839 | return -ENODEV; | |
2fdf66b4 RR |
840 | |
841 | if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL)) | |
842 | return -ENOMEM; | |
843 | ||
357dc4c3 ZY |
844 | /* |
845 | * Migrate task to the cpu pointed by pr. | |
846 | */ | |
2fdf66b4 RR |
847 | cpumask_copy(saved_mask, ¤t->cpus_allowed); |
848 | /* FIXME: use work_on_cpu() */ | |
849 | set_cpus_allowed_ptr(current, cpumask_of(pr->id)); | |
357dc4c3 ZY |
850 | ret = pr->throttling.acpi_processor_get_throttling(pr); |
851 | /* restore the previous state */ | |
2fdf66b4 RR |
852 | set_cpus_allowed_ptr(current, saved_mask); |
853 | free_cpumask_var(saved_mask); | |
357dc4c3 ZY |
854 | |
855 | return ret; | |
01854e69 LY |
856 | } |
857 | ||
22cc5019 ZY |
858 | static int acpi_processor_get_fadt_info(struct acpi_processor *pr) |
859 | { | |
860 | int i, step; | |
861 | ||
862 | if (!pr->throttling.address) { | |
863 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n")); | |
864 | return -EINVAL; | |
865 | } else if (!pr->throttling.duty_width) { | |
866 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n")); | |
867 | return -EINVAL; | |
868 | } | |
869 | /* TBD: Support duty_cycle values that span bit 4. */ | |
870 | else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) { | |
871 | printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n"); | |
872 | return -EINVAL; | |
873 | } | |
874 | ||
875 | pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width; | |
876 | ||
877 | /* | |
878 | * Compute state values. Note that throttling displays a linear power | |
879 | * performance relationship (at 50% performance the CPU will consume | |
880 | * 50% power). Values are in 1/10th of a percent to preserve accuracy. | |
881 | */ | |
882 | ||
883 | step = (1000 / pr->throttling.state_count); | |
884 | ||
885 | for (i = 0; i < pr->throttling.state_count; i++) { | |
886 | pr->throttling.states[i].performance = 1000 - step * i; | |
887 | pr->throttling.states[i].power = 1000 - step * i; | |
888 | } | |
889 | return 0; | |
890 | } | |
891 | ||
6c5cf8aa AB |
892 | static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr, |
893 | int state) | |
1da177e4 | 894 | { |
4be44fcd LB |
895 | u32 value = 0; |
896 | u32 duty_mask = 0; | |
897 | u32 duty_value = 0; | |
1da177e4 | 898 | |
1da177e4 | 899 | if (!pr) |
d550d98d | 900 | return -EINVAL; |
1da177e4 LT |
901 | |
902 | if ((state < 0) || (state > (pr->throttling.state_count - 1))) | |
d550d98d | 903 | return -EINVAL; |
1da177e4 LT |
904 | |
905 | if (!pr->flags.throttling) | |
d550d98d | 906 | return -ENODEV; |
1da177e4 LT |
907 | |
908 | if (state == pr->throttling.state) | |
d550d98d | 909 | return 0; |
1da177e4 | 910 | |
01854e69 LY |
911 | if (state < pr->throttling_platform_limit) |
912 | return -EPERM; | |
1da177e4 LT |
913 | /* |
914 | * Calculate the duty_value and duty_mask. | |
915 | */ | |
916 | if (state) { | |
917 | duty_value = pr->throttling.state_count - state; | |
918 | ||
919 | duty_value <<= pr->throttling.duty_offset; | |
920 | ||
921 | /* Used to clear all duty_value bits */ | |
922 | duty_mask = pr->throttling.state_count - 1; | |
923 | ||
cee324b1 | 924 | duty_mask <<= acpi_gbl_FADT.duty_offset; |
1da177e4 LT |
925 | duty_mask = ~duty_mask; |
926 | } | |
927 | ||
928 | local_irq_disable(); | |
929 | ||
930 | /* | |
931 | * Disable throttling by writing a 0 to bit 4. Note that we must | |
932 | * turn it off before you can change the duty_value. | |
933 | */ | |
934 | value = inl(pr->throttling.address); | |
935 | if (value & 0x10) { | |
936 | value &= 0xFFFFFFEF; | |
937 | outl(value, pr->throttling.address); | |
938 | } | |
939 | ||
940 | /* | |
941 | * Write the new duty_value and then enable throttling. Note | |
942 | * that a state value of 0 leaves throttling disabled. | |
943 | */ | |
944 | if (state) { | |
945 | value &= duty_mask; | |
946 | value |= duty_value; | |
947 | outl(value, pr->throttling.address); | |
948 | ||
949 | value |= 0x00000010; | |
950 | outl(value, pr->throttling.address); | |
951 | } | |
952 | ||
953 | pr->throttling.state = state; | |
954 | ||
955 | local_irq_enable(); | |
956 | ||
957 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd LB |
958 | "Throttling state set to T%d (%d%%)\n", state, |
959 | (pr->throttling.states[state].performance ? pr-> | |
960 | throttling.states[state].performance / 10 : 0))); | |
1da177e4 | 961 | |
d550d98d | 962 | return 0; |
1da177e4 LT |
963 | } |
964 | ||
6c5cf8aa AB |
965 | static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr, |
966 | int state) | |
01854e69 | 967 | { |
0753f6e0 ZY |
968 | int ret; |
969 | acpi_integer value; | |
01854e69 LY |
970 | |
971 | if (!pr) | |
972 | return -EINVAL; | |
973 | ||
974 | if ((state < 0) || (state > (pr->throttling.state_count - 1))) | |
975 | return -EINVAL; | |
976 | ||
977 | if (!pr->flags.throttling) | |
978 | return -ENODEV; | |
979 | ||
980 | if (state == pr->throttling.state) | |
981 | return 0; | |
982 | ||
983 | if (state < pr->throttling_platform_limit) | |
984 | return -EPERM; | |
985 | ||
0753f6e0 ZY |
986 | value = 0; |
987 | ret = acpi_get_throttling_value(pr, state, &value); | |
988 | if (ret >= 0) { | |
989 | acpi_write_throttling_state(pr, value); | |
01854e69 LY |
990 | pr->throttling.state = state; |
991 | } | |
01854e69 LY |
992 | |
993 | return 0; | |
994 | } | |
995 | ||
996 | int acpi_processor_set_throttling(struct acpi_processor *pr, int state) | |
997 | { | |
2fdf66b4 | 998 | cpumask_var_t saved_mask; |
3391a76f | 999 | int ret = 0; |
33a2a529 ZY |
1000 | unsigned int i; |
1001 | struct acpi_processor *match_pr; | |
1002 | struct acpi_processor_throttling *p_throttling; | |
1003 | struct throttling_tstate t_state; | |
2fdf66b4 | 1004 | cpumask_var_t online_throttling_cpus; |
87654273 ZY |
1005 | |
1006 | if (!pr) | |
1007 | return -EINVAL; | |
1008 | ||
1009 | if (!pr->flags.throttling) | |
1010 | return -ENODEV; | |
1011 | ||
1012 | if ((state < 0) || (state > (pr->throttling.state_count - 1))) | |
1013 | return -EINVAL; | |
1014 | ||
2fdf66b4 RR |
1015 | if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL)) |
1016 | return -ENOMEM; | |
1017 | ||
1018 | if (!alloc_cpumask_var(&online_throttling_cpus, GFP_KERNEL)) { | |
1019 | free_cpumask_var(saved_mask); | |
1020 | return -ENOMEM; | |
1021 | } | |
1022 | ||
1023 | cpumask_copy(saved_mask, ¤t->cpus_allowed); | |
33a2a529 ZY |
1024 | t_state.target_state = state; |
1025 | p_throttling = &(pr->throttling); | |
2fdf66b4 RR |
1026 | cpumask_and(online_throttling_cpus, cpu_online_mask, |
1027 | p_throttling->shared_cpu_map); | |
357dc4c3 | 1028 | /* |
33a2a529 ZY |
1029 | * The throttling notifier will be called for every |
1030 | * affected cpu in order to get one proper T-state. | |
1031 | * The notifier event is THROTTLING_PRECHANGE. | |
357dc4c3 | 1032 | */ |
2fdf66b4 | 1033 | for_each_cpu(i, online_throttling_cpus) { |
33a2a529 ZY |
1034 | t_state.cpu = i; |
1035 | acpi_processor_throttling_notifier(THROTTLING_PRECHANGE, | |
1036 | &t_state); | |
1037 | } | |
1038 | /* | |
1039 | * The function of acpi_processor_set_throttling will be called | |
1040 | * to switch T-state. If the coordination type is SW_ALL or HW_ALL, | |
1041 | * it is necessary to call it for every affected cpu. Otherwise | |
1042 | * it can be called only for the cpu pointed by pr. | |
1043 | */ | |
1044 | if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) { | |
2fdf66b4 RR |
1045 | /* FIXME: use work_on_cpu() */ |
1046 | set_cpus_allowed_ptr(current, cpumask_of(pr->id)); | |
33a2a529 ZY |
1047 | ret = p_throttling->acpi_processor_set_throttling(pr, |
1048 | t_state.target_state); | |
1049 | } else { | |
1050 | /* | |
1051 | * When the T-state coordination is SW_ALL or HW_ALL, | |
1052 | * it is necessary to set T-state for every affected | |
1053 | * cpus. | |
1054 | */ | |
2fdf66b4 | 1055 | for_each_cpu(i, online_throttling_cpus) { |
706546d0 | 1056 | match_pr = per_cpu(processors, i); |
33a2a529 ZY |
1057 | /* |
1058 | * If the pointer is invalid, we will report the | |
1059 | * error message and continue. | |
1060 | */ | |
1061 | if (!match_pr) { | |
1062 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
1063 | "Invalid Pointer for CPU %d\n", i)); | |
1064 | continue; | |
1065 | } | |
1066 | /* | |
1067 | * If the throttling control is unsupported on CPU i, | |
1068 | * we will report the error message and continue. | |
1069 | */ | |
1070 | if (!match_pr->flags.throttling) { | |
1071 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
1072 | "Throttling Controll is unsupported " | |
1073 | "on CPU %d\n", i)); | |
1074 | continue; | |
1075 | } | |
1076 | t_state.cpu = i; | |
2fdf66b4 RR |
1077 | /* FIXME: use work_on_cpu() */ |
1078 | set_cpus_allowed_ptr(current, cpumask_of(i)); | |
33a2a529 ZY |
1079 | ret = match_pr->throttling. |
1080 | acpi_processor_set_throttling( | |
1081 | match_pr, t_state.target_state); | |
1082 | } | |
1083 | } | |
1084 | /* | |
1085 | * After the set_throttling is called, the | |
1086 | * throttling notifier is called for every | |
1087 | * affected cpu to update the T-states. | |
1088 | * The notifier event is THROTTLING_POSTCHANGE | |
1089 | */ | |
2fdf66b4 | 1090 | for_each_cpu(i, online_throttling_cpus) { |
33a2a529 ZY |
1091 | t_state.cpu = i; |
1092 | acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE, | |
1093 | &t_state); | |
1094 | } | |
357dc4c3 | 1095 | /* restore the previous state */ |
2fdf66b4 RR |
1096 | /* FIXME: use work_on_cpu() */ |
1097 | set_cpus_allowed_ptr(current, saved_mask); | |
1098 | free_cpumask_var(online_throttling_cpus); | |
1099 | free_cpumask_var(saved_mask); | |
357dc4c3 | 1100 | return ret; |
01854e69 LY |
1101 | } |
1102 | ||
4be44fcd | 1103 | int acpi_processor_get_throttling_info(struct acpi_processor *pr) |
1da177e4 | 1104 | { |
4be44fcd | 1105 | int result = 0; |
1180509f | 1106 | struct acpi_processor_throttling *pthrottling; |
1da177e4 | 1107 | |
1da177e4 | 1108 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
4be44fcd LB |
1109 | "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n", |
1110 | pr->throttling.address, | |
1111 | pr->throttling.duty_offset, | |
1112 | pr->throttling.duty_width)); | |
1da177e4 LT |
1113 | |
1114 | if (!pr) | |
d550d98d | 1115 | return -EINVAL; |
1da177e4 | 1116 | |
c30c620e LB |
1117 | /* |
1118 | * Evaluate _PTC, _TSS and _TPC | |
1119 | * They must all be present or none of them can be used. | |
1120 | */ | |
1121 | if (acpi_processor_get_throttling_control(pr) || | |
1122 | acpi_processor_get_throttling_states(pr) || | |
1123 | acpi_processor_get_platform_limit(pr)) | |
1124 | { | |
ff55a9ce LB |
1125 | pr->throttling.acpi_processor_get_throttling = |
1126 | &acpi_processor_get_throttling_fadt; | |
1127 | pr->throttling.acpi_processor_set_throttling = | |
1128 | &acpi_processor_set_throttling_fadt; | |
d1154be3 AS |
1129 | if (acpi_processor_get_fadt_info(pr)) |
1130 | return 0; | |
01854e69 | 1131 | } else { |
ff55a9ce LB |
1132 | pr->throttling.acpi_processor_get_throttling = |
1133 | &acpi_processor_get_throttling_ptc; | |
1134 | pr->throttling.acpi_processor_set_throttling = | |
1135 | &acpi_processor_set_throttling_ptc; | |
01854e69 | 1136 | } |
1da177e4 | 1137 | |
1180509f ZY |
1138 | /* |
1139 | * If TSD package for one CPU can't be parsed successfully, it means | |
1140 | * that this CPU will have no coordination with other CPUs. | |
1141 | */ | |
1142 | if (acpi_processor_get_tsd(pr)) { | |
1143 | pthrottling = &pr->throttling; | |
1144 | pthrottling->tsd_valid_flag = 0; | |
2fdf66b4 | 1145 | cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map); |
1180509f ZY |
1146 | pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL; |
1147 | } | |
c30c620e | 1148 | |
1da177e4 LT |
1149 | /* |
1150 | * PIIX4 Errata: We don't support throttling on the original PIIX4. | |
1151 | * This shouldn't be an issue as few (if any) mobile systems ever | |
1152 | * used this part. | |
1153 | */ | |
1154 | if (errata.piix4.throttle) { | |
1155 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 1156 | "Throttling not supported on PIIX4 A- or B-step\n")); |
d550d98d | 1157 | return 0; |
1da177e4 LT |
1158 | } |
1159 | ||
1da177e4 | 1160 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n", |
4be44fcd | 1161 | pr->throttling.state_count)); |
1da177e4 LT |
1162 | |
1163 | pr->flags.throttling = 1; | |
1164 | ||
1165 | /* | |
1166 | * Disable throttling (if enabled). We'll let subsequent policy (e.g. | |
1167 | * thermal) decide to lower performance if it so chooses, but for now | |
1168 | * we'll crank up the speed. | |
1169 | */ | |
1170 | ||
1171 | result = acpi_processor_get_throttling(pr); | |
1172 | if (result) | |
1173 | goto end; | |
1174 | ||
1175 | if (pr->throttling.state) { | |
4be44fcd LB |
1176 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
1177 | "Disabling throttling (was T%d)\n", | |
1178 | pr->throttling.state)); | |
1da177e4 LT |
1179 | result = acpi_processor_set_throttling(pr, 0); |
1180 | if (result) | |
1181 | goto end; | |
1182 | } | |
1183 | ||
4be44fcd | 1184 | end: |
1da177e4 LT |
1185 | if (result) |
1186 | pr->flags.throttling = 0; | |
1187 | ||
d550d98d | 1188 | return result; |
1da177e4 LT |
1189 | } |
1190 | ||
1da177e4 LT |
1191 | /* proc interface */ |
1192 | ||
4be44fcd LB |
1193 | static int acpi_processor_throttling_seq_show(struct seq_file *seq, |
1194 | void *offset) | |
1da177e4 | 1195 | { |
50dd0969 | 1196 | struct acpi_processor *pr = seq->private; |
4be44fcd LB |
1197 | int i = 0; |
1198 | int result = 0; | |
1da177e4 | 1199 | |
1da177e4 LT |
1200 | if (!pr) |
1201 | goto end; | |
1202 | ||
1203 | if (!(pr->throttling.state_count > 0)) { | |
1204 | seq_puts(seq, "<not supported>\n"); | |
1205 | goto end; | |
1206 | } | |
1207 | ||
1208 | result = acpi_processor_get_throttling(pr); | |
1209 | ||
1210 | if (result) { | |
4be44fcd LB |
1211 | seq_puts(seq, |
1212 | "Could not determine current throttling state.\n"); | |
1da177e4 LT |
1213 | goto end; |
1214 | } | |
1215 | ||
1216 | seq_printf(seq, "state count: %d\n" | |
01854e69 | 1217 | "active state: T%d\n" |
ff55a9ce | 1218 | "state available: T%d to T%d\n", |
01854e69 | 1219 | pr->throttling.state_count, pr->throttling.state, |
ff55a9ce LB |
1220 | pr->throttling_platform_limit, |
1221 | pr->throttling.state_count - 1); | |
1da177e4 LT |
1222 | |
1223 | seq_puts(seq, "states:\n"); | |
3cc2649b LY |
1224 | if (pr->throttling.acpi_processor_get_throttling == |
1225 | acpi_processor_get_throttling_fadt) { | |
01854e69 LY |
1226 | for (i = 0; i < pr->throttling.state_count; i++) |
1227 | seq_printf(seq, " %cT%d: %02d%%\n", | |
ff55a9ce LB |
1228 | (i == pr->throttling.state ? '*' : ' '), i, |
1229 | (pr->throttling.states[i].performance ? pr-> | |
1230 | throttling.states[i].performance / 10 : 0)); | |
3cc2649b | 1231 | } else { |
01854e69 LY |
1232 | for (i = 0; i < pr->throttling.state_count; i++) |
1233 | seq_printf(seq, " %cT%d: %02d%%\n", | |
ff55a9ce LB |
1234 | (i == pr->throttling.state ? '*' : ' '), i, |
1235 | (int)pr->throttling.states_tss[i]. | |
1236 | freqpercentage); | |
3cc2649b | 1237 | } |
1da177e4 | 1238 | |
4be44fcd | 1239 | end: |
d550d98d | 1240 | return 0; |
1da177e4 LT |
1241 | } |
1242 | ||
4be44fcd LB |
1243 | static int acpi_processor_throttling_open_fs(struct inode *inode, |
1244 | struct file *file) | |
1da177e4 LT |
1245 | { |
1246 | return single_open(file, acpi_processor_throttling_seq_show, | |
4be44fcd | 1247 | PDE(inode)->data); |
1da177e4 LT |
1248 | } |
1249 | ||
ff55a9ce | 1250 | static ssize_t acpi_processor_write_throttling(struct file *file, |
757b1866 AB |
1251 | const char __user * buffer, |
1252 | size_t count, loff_t * data) | |
1da177e4 | 1253 | { |
4be44fcd | 1254 | int result = 0; |
50dd0969 JE |
1255 | struct seq_file *m = file->private_data; |
1256 | struct acpi_processor *pr = m->private; | |
3d532d5e YY |
1257 | char state_string[5] = ""; |
1258 | char *charp = NULL; | |
1259 | size_t state_val = 0; | |
1260 | char tmpbuf[5] = ""; | |
1da177e4 | 1261 | |
1da177e4 | 1262 | if (!pr || (count > sizeof(state_string) - 1)) |
d550d98d | 1263 | return -EINVAL; |
1da177e4 LT |
1264 | |
1265 | if (copy_from_user(state_string, buffer, count)) | |
d550d98d | 1266 | return -EFAULT; |
1da177e4 LT |
1267 | |
1268 | state_string[count] = '\0'; | |
3d532d5e YY |
1269 | if ((count > 0) && (state_string[count-1] == '\n')) |
1270 | state_string[count-1] = '\0'; | |
1da177e4 | 1271 | |
3d532d5e YY |
1272 | charp = state_string; |
1273 | if ((state_string[0] == 't') || (state_string[0] == 'T')) | |
1274 | charp++; | |
1275 | ||
1276 | state_val = simple_strtoul(charp, NULL, 0); | |
1277 | if (state_val >= pr->throttling.state_count) | |
1278 | return -EINVAL; | |
1279 | ||
12b2b34e | 1280 | snprintf(tmpbuf, 5, "%zu", state_val); |
3d532d5e YY |
1281 | |
1282 | if (strcmp(tmpbuf, charp) != 0) | |
1283 | return -EINVAL; | |
1284 | ||
1285 | result = acpi_processor_set_throttling(pr, state_val); | |
1da177e4 | 1286 | if (result) |
d550d98d | 1287 | return result; |
1da177e4 | 1288 | |
d550d98d | 1289 | return count; |
1da177e4 LT |
1290 | } |
1291 | ||
070d8eb1 | 1292 | const struct file_operations acpi_processor_throttling_fops = { |
cf7acfab | 1293 | .owner = THIS_MODULE, |
4be44fcd LB |
1294 | .open = acpi_processor_throttling_open_fs, |
1295 | .read = seq_read, | |
d479e908 | 1296 | .write = acpi_processor_write_throttling, |
4be44fcd LB |
1297 | .llseek = seq_lseek, |
1298 | .release = single_release, | |
1da177e4 | 1299 | }; |