Fix calculation in move_freepages_block for counting pages
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / acpi / processor_throttling.c
blob0b8204e7082a366232b3d29121926ff0892f57cd
1 /*
2 * processor_throttling.c - Throttling submodule of the ACPI processor driver
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
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/cpufreq.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
36 #include <asm/io.h>
37 #include <asm/uaccess.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/processor.h>
42 #define ACPI_PROCESSOR_COMPONENT 0x01000000
43 #define ACPI_PROCESSOR_CLASS "processor"
44 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
45 ACPI_MODULE_NAME("processor_throttling");
47 static int acpi_processor_get_throttling(struct acpi_processor *pr);
48 int acpi_processor_set_throttling(struct acpi_processor *pr, int state);
51 * _TPC - Throttling Present Capabilities
53 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
55 acpi_status status = 0;
56 unsigned long tpc = 0;
58 if (!pr)
59 return -EINVAL;
60 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
61 if (ACPI_FAILURE(status)) {
62 if (status != AE_NOT_FOUND) {
63 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
65 return -ENODEV;
67 pr->throttling_platform_limit = (int)tpc;
68 return 0;
71 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
73 return acpi_processor_get_platform_limit(pr);
77 * _PTC - Processor Throttling Control (and status) register location
79 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
81 int result = 0;
82 acpi_status status = 0;
83 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
84 union acpi_object *ptc = NULL;
85 union acpi_object obj = { 0 };
87 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
88 if (ACPI_FAILURE(status)) {
89 if (status != AE_NOT_FOUND) {
90 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
92 return -ENODEV;
95 ptc = (union acpi_object *)buffer.pointer;
96 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
97 || (ptc->package.count != 2)) {
98 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
99 result = -EFAULT;
100 goto end;
104 * control_register
107 obj = ptc->package.elements[0];
109 if ((obj.type != ACPI_TYPE_BUFFER)
110 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
111 || (obj.buffer.pointer == NULL)) {
112 printk(KERN_ERR PREFIX
113 "Invalid _PTC data (control_register)\n");
114 result = -EFAULT;
115 goto end;
117 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
118 sizeof(struct acpi_ptc_register));
121 * status_register
124 obj = ptc->package.elements[1];
126 if ((obj.type != ACPI_TYPE_BUFFER)
127 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
128 || (obj.buffer.pointer == NULL)) {
129 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
130 result = -EFAULT;
131 goto end;
134 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
135 sizeof(struct acpi_ptc_register));
137 end:
138 kfree(buffer.pointer);
140 return result;
144 * _TSS - Throttling Supported States
146 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
148 int result = 0;
149 acpi_status status = AE_OK;
150 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
151 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
152 struct acpi_buffer state = { 0, NULL };
153 union acpi_object *tss = NULL;
154 int i;
156 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
157 if (ACPI_FAILURE(status)) {
158 if (status != AE_NOT_FOUND) {
159 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
161 return -ENODEV;
164 tss = buffer.pointer;
165 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
166 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
167 result = -EFAULT;
168 goto end;
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
172 tss->package.count));
174 pr->throttling.state_count = tss->package.count;
175 pr->throttling.states_tss =
176 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
177 GFP_KERNEL);
178 if (!pr->throttling.states_tss) {
179 result = -ENOMEM;
180 goto end;
183 for (i = 0; i < pr->throttling.state_count; i++) {
185 struct acpi_processor_tx_tss *tx =
186 (struct acpi_processor_tx_tss *)&(pr->throttling.
187 states_tss[i]);
189 state.length = sizeof(struct acpi_processor_tx_tss);
190 state.pointer = tx;
192 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
194 status = acpi_extract_package(&(tss->package.elements[i]),
195 &format, &state);
196 if (ACPI_FAILURE(status)) {
197 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
198 result = -EFAULT;
199 kfree(pr->throttling.states_tss);
200 goto end;
203 if (!tx->freqpercentage) {
204 printk(KERN_ERR PREFIX
205 "Invalid _TSS data: freq is zero\n");
206 result = -EFAULT;
207 kfree(pr->throttling.states_tss);
208 goto end;
212 end:
213 kfree(buffer.pointer);
215 return result;
219 * _TSD - T-State Dependencies
221 static int acpi_processor_get_tsd(struct acpi_processor *pr)
223 int result = 0;
224 acpi_status status = AE_OK;
225 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
226 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
227 struct acpi_buffer state = { 0, NULL };
228 union acpi_object *tsd = NULL;
229 struct acpi_tsd_package *pdomain;
231 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
232 if (ACPI_FAILURE(status)) {
233 if (status != AE_NOT_FOUND) {
234 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
236 return -ENODEV;
239 tsd = buffer.pointer;
240 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
241 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
242 result = -EFAULT;
243 goto end;
246 if (tsd->package.count != 1) {
247 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
248 result = -EFAULT;
249 goto end;
252 pdomain = &(pr->throttling.domain_info);
254 state.length = sizeof(struct acpi_tsd_package);
255 state.pointer = pdomain;
257 status = acpi_extract_package(&(tsd->package.elements[0]),
258 &format, &state);
259 if (ACPI_FAILURE(status)) {
260 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
261 result = -EFAULT;
262 goto end;
265 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
266 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
267 result = -EFAULT;
268 goto end;
271 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
272 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
273 result = -EFAULT;
274 goto end;
277 end:
278 kfree(buffer.pointer);
279 return result;
282 /* --------------------------------------------------------------------------
283 Throttling Control
284 -------------------------------------------------------------------------- */
285 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
287 int state = 0;
288 u32 value = 0;
289 u32 duty_mask = 0;
290 u32 duty_value = 0;
292 if (!pr)
293 return -EINVAL;
295 if (!pr->flags.throttling)
296 return -ENODEV;
298 pr->throttling.state = 0;
300 duty_mask = pr->throttling.state_count - 1;
302 duty_mask <<= pr->throttling.duty_offset;
304 local_irq_disable();
306 value = inl(pr->throttling.address);
309 * Compute the current throttling state when throttling is enabled
310 * (bit 4 is on).
312 if (value & 0x10) {
313 duty_value = value & duty_mask;
314 duty_value >>= pr->throttling.duty_offset;
316 if (duty_value)
317 state = pr->throttling.state_count - duty_value;
320 pr->throttling.state = state;
322 local_irq_enable();
324 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
325 "Throttling state is T%d (%d%% throttling applied)\n",
326 state, pr->throttling.states[state].performance));
328 return 0;
331 static int acpi_read_throttling_status(struct acpi_processor_throttling
332 *throttling)
334 int value = -1;
335 switch (throttling->status_register.space_id) {
336 case ACPI_ADR_SPACE_SYSTEM_IO:
337 acpi_os_read_port((acpi_io_address) throttling->status_register.
338 address, &value,
339 (u32) throttling->status_register.bit_width *
341 break;
342 case ACPI_ADR_SPACE_FIXED_HARDWARE:
343 printk(KERN_ERR PREFIX
344 "HARDWARE addr space,NOT supported yet\n");
345 break;
346 default:
347 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
348 (u32) (throttling->status_register.space_id));
350 return value;
353 static int acpi_write_throttling_state(struct acpi_processor_throttling
354 *throttling, int value)
356 int ret = -1;
358 switch (throttling->control_register.space_id) {
359 case ACPI_ADR_SPACE_SYSTEM_IO:
360 acpi_os_write_port((acpi_io_address) throttling->
361 control_register.address, value,
362 (u32) throttling->control_register.
363 bit_width * 8);
364 ret = 0;
365 break;
366 case ACPI_ADR_SPACE_FIXED_HARDWARE:
367 printk(KERN_ERR PREFIX
368 "HARDWARE addr space,NOT supported yet\n");
369 break;
370 default:
371 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
372 (u32) (throttling->control_register.space_id));
374 return ret;
377 static int acpi_get_throttling_state(struct acpi_processor *pr, int value)
379 int i;
381 for (i = 0; i < pr->throttling.state_count; i++) {
382 struct acpi_processor_tx_tss *tx =
383 (struct acpi_processor_tx_tss *)&(pr->throttling.
384 states_tss[i]);
385 if (tx->control == value)
386 break;
388 if (i > pr->throttling.state_count)
389 i = -1;
390 return i;
393 static int acpi_get_throttling_value(struct acpi_processor *pr, int state)
395 int value = -1;
396 if (state >= 0 && state <= pr->throttling.state_count) {
397 struct acpi_processor_tx_tss *tx =
398 (struct acpi_processor_tx_tss *)&(pr->throttling.
399 states_tss[state]);
400 value = tx->control;
402 return value;
405 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
407 int state = 0;
408 u32 value = 0;
410 if (!pr)
411 return -EINVAL;
413 if (!pr->flags.throttling)
414 return -ENODEV;
416 pr->throttling.state = 0;
417 local_irq_disable();
418 value = acpi_read_throttling_status(&pr->throttling);
419 if (value >= 0) {
420 state = acpi_get_throttling_state(pr, value);
421 pr->throttling.state = state;
423 local_irq_enable();
425 return 0;
428 static int acpi_processor_get_throttling(struct acpi_processor *pr)
430 return pr->throttling.acpi_processor_get_throttling(pr);
433 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
434 int state)
436 u32 value = 0;
437 u32 duty_mask = 0;
438 u32 duty_value = 0;
440 if (!pr)
441 return -EINVAL;
443 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
444 return -EINVAL;
446 if (!pr->flags.throttling)
447 return -ENODEV;
449 if (state == pr->throttling.state)
450 return 0;
452 if (state < pr->throttling_platform_limit)
453 return -EPERM;
455 * Calculate the duty_value and duty_mask.
457 if (state) {
458 duty_value = pr->throttling.state_count - state;
460 duty_value <<= pr->throttling.duty_offset;
462 /* Used to clear all duty_value bits */
463 duty_mask = pr->throttling.state_count - 1;
465 duty_mask <<= acpi_gbl_FADT.duty_offset;
466 duty_mask = ~duty_mask;
469 local_irq_disable();
472 * Disable throttling by writing a 0 to bit 4. Note that we must
473 * turn it off before you can change the duty_value.
475 value = inl(pr->throttling.address);
476 if (value & 0x10) {
477 value &= 0xFFFFFFEF;
478 outl(value, pr->throttling.address);
482 * Write the new duty_value and then enable throttling. Note
483 * that a state value of 0 leaves throttling disabled.
485 if (state) {
486 value &= duty_mask;
487 value |= duty_value;
488 outl(value, pr->throttling.address);
490 value |= 0x00000010;
491 outl(value, pr->throttling.address);
494 pr->throttling.state = state;
496 local_irq_enable();
498 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
499 "Throttling state set to T%d (%d%%)\n", state,
500 (pr->throttling.states[state].performance ? pr->
501 throttling.states[state].performance / 10 : 0)));
503 return 0;
506 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
507 int state)
509 u32 value = 0;
511 if (!pr)
512 return -EINVAL;
514 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
515 return -EINVAL;
517 if (!pr->flags.throttling)
518 return -ENODEV;
520 if (state == pr->throttling.state)
521 return 0;
523 if (state < pr->throttling_platform_limit)
524 return -EPERM;
526 local_irq_disable();
528 value = acpi_get_throttling_value(pr, state);
529 if (value >= 0) {
530 acpi_write_throttling_state(&pr->throttling, value);
531 pr->throttling.state = state;
533 local_irq_enable();
535 return 0;
538 int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
540 return pr->throttling.acpi_processor_set_throttling(pr, state);
543 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
545 int result = 0;
546 int step = 0;
547 int i = 0;
549 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
550 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
551 pr->throttling.address,
552 pr->throttling.duty_offset,
553 pr->throttling.duty_width));
555 if (!pr)
556 return -EINVAL;
559 * Evaluate _PTC, _TSS and _TPC
560 * They must all be present or none of them can be used.
562 if (acpi_processor_get_throttling_control(pr) ||
563 acpi_processor_get_throttling_states(pr) ||
564 acpi_processor_get_platform_limit(pr))
566 pr->throttling.acpi_processor_get_throttling =
567 &acpi_processor_get_throttling_fadt;
568 pr->throttling.acpi_processor_set_throttling =
569 &acpi_processor_set_throttling_fadt;
570 } else {
571 pr->throttling.acpi_processor_get_throttling =
572 &acpi_processor_get_throttling_ptc;
573 pr->throttling.acpi_processor_set_throttling =
574 &acpi_processor_set_throttling_ptc;
577 acpi_processor_get_tsd(pr);
579 if (!pr->throttling.address) {
580 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
581 return 0;
582 } else if (!pr->throttling.duty_width) {
583 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
584 return 0;
586 /* TBD: Support duty_cycle values that span bit 4. */
587 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
588 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
589 return 0;
593 * PIIX4 Errata: We don't support throttling on the original PIIX4.
594 * This shouldn't be an issue as few (if any) mobile systems ever
595 * used this part.
597 if (errata.piix4.throttle) {
598 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
599 "Throttling not supported on PIIX4 A- or B-step\n"));
600 return 0;
603 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
606 * Compute state values. Note that throttling displays a linear power/
607 * performance relationship (at 50% performance the CPU will consume
608 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
611 step = (1000 / pr->throttling.state_count);
613 for (i = 0; i < pr->throttling.state_count; i++) {
614 pr->throttling.states[i].performance = step * i;
615 pr->throttling.states[i].power = step * i;
618 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
619 pr->throttling.state_count));
621 pr->flags.throttling = 1;
624 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
625 * thermal) decide to lower performance if it so chooses, but for now
626 * we'll crank up the speed.
629 result = acpi_processor_get_throttling(pr);
630 if (result)
631 goto end;
633 if (pr->throttling.state) {
634 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
635 "Disabling throttling (was T%d)\n",
636 pr->throttling.state));
637 result = acpi_processor_set_throttling(pr, 0);
638 if (result)
639 goto end;
642 end:
643 if (result)
644 pr->flags.throttling = 0;
646 return result;
649 /* proc interface */
651 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
652 void *offset)
654 struct acpi_processor *pr = seq->private;
655 int i = 0;
656 int result = 0;
658 if (!pr)
659 goto end;
661 if (!(pr->throttling.state_count > 0)) {
662 seq_puts(seq, "<not supported>\n");
663 goto end;
666 result = acpi_processor_get_throttling(pr);
668 if (result) {
669 seq_puts(seq,
670 "Could not determine current throttling state.\n");
671 goto end;
674 seq_printf(seq, "state count: %d\n"
675 "active state: T%d\n"
676 "state available: T%d to T%d\n",
677 pr->throttling.state_count, pr->throttling.state,
678 pr->throttling_platform_limit,
679 pr->throttling.state_count - 1);
681 seq_puts(seq, "states:\n");
682 if (pr->throttling.acpi_processor_get_throttling ==
683 acpi_processor_get_throttling_fadt) {
684 for (i = 0; i < pr->throttling.state_count; i++)
685 seq_printf(seq, " %cT%d: %02d%%\n",
686 (i == pr->throttling.state ? '*' : ' '), i,
687 (pr->throttling.states[i].performance ? pr->
688 throttling.states[i].performance / 10 : 0));
689 } else {
690 for (i = 0; i < pr->throttling.state_count; i++)
691 seq_printf(seq, " %cT%d: %02d%%\n",
692 (i == pr->throttling.state ? '*' : ' '), i,
693 (int)pr->throttling.states_tss[i].
694 freqpercentage);
697 end:
698 return 0;
701 static int acpi_processor_throttling_open_fs(struct inode *inode,
702 struct file *file)
704 return single_open(file, acpi_processor_throttling_seq_show,
705 PDE(inode)->data);
708 static ssize_t acpi_processor_write_throttling(struct file *file,
709 const char __user * buffer,
710 size_t count, loff_t * data)
712 int result = 0;
713 struct seq_file *m = file->private_data;
714 struct acpi_processor *pr = m->private;
715 char state_string[12] = { '\0' };
717 if (!pr || (count > sizeof(state_string) - 1))
718 return -EINVAL;
720 if (copy_from_user(state_string, buffer, count))
721 return -EFAULT;
723 state_string[count] = '\0';
725 result = acpi_processor_set_throttling(pr,
726 simple_strtoul(state_string,
727 NULL, 0));
728 if (result)
729 return result;
731 return count;
734 struct file_operations acpi_processor_throttling_fops = {
735 .open = acpi_processor_throttling_open_fs,
736 .read = seq_read,
737 .write = acpi_processor_write_throttling,
738 .llseek = seq_lseek,
739 .release = single_release,