1 /*
2  * acpi_processor.c - ACPI Processor Driver ($Revision: 69 $)
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  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  *  TBD:
25  *	1. Make # power/performance states dynamic.
26  *	2. Support duty_cycle values that span bit 4.
27  *	3. Optimize by having scheduler determine business instead of
28  *	   having us try to calculate it here.
29  *	4. Need C1 timing -- must modify kernel (IRQ handler) to get this.
30  */
31 
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/pm.h>
38 #include <asm/io.h>
39 #include <asm/system.h>
40 #include <asm/delay.h>
41 #include <linux/compatmac.h>
42 #include <linux/proc_fs.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 
46 
47 #define _COMPONENT		ACPI_PROCESSOR_COMPONENT
48 ACPI_MODULE_NAME		("acpi_processor")
49 
50 MODULE_AUTHOR("Paul Diefenbaugh");
51 MODULE_DESCRIPTION(ACPI_PROCESSOR_DRIVER_NAME);
52 MODULE_LICENSE("GPL");
53 
54 #define PREFIX				"ACPI: "
55 
56 #define US_TO_PM_TIMER_TICKS(t)		((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
57 #define C2_OVERHEAD			4	/* 1us (3.579 ticks per us) */
58 #define C3_OVERHEAD			4	/* 1us (3.579 ticks per us) */
59 
60 #define ACPI_PROCESSOR_BUSY_METRIC	10
61 
62 #define ACPI_PROCESSOR_MAX_POWER	ACPI_C_STATE_COUNT
63 #define ACPI_PROCESSOR_MAX_C2_LATENCY	100
64 #define ACPI_PROCESSOR_MAX_C3_LATENCY	1000
65 
66 #define ACPI_PROCESSOR_MAX_PERFORMANCE	8
67 
68 #define ACPI_PROCESSOR_MAX_THROTTLING	16
69 #define ACPI_PROCESSOR_MAX_THROTTLE	250	/* 25% */
70 #define ACPI_PROCESSOR_MAX_DUTY_WIDTH	4
71 
72 #define ACPI_PROCESSOR_LIMIT_USER	0
73 #define ACPI_PROCESSOR_LIMIT_THERMAL	1
74 
75 static int acpi_processor_add (struct acpi_device *device);
76 static int acpi_processor_remove (struct acpi_device *device, int type);
77 
78 static struct acpi_driver acpi_processor_driver = {
79 	.name =		ACPI_PROCESSOR_DRIVER_NAME,
80 	.class =	ACPI_PROCESSOR_CLASS,
81 	.ids =		ACPI_PROCESSOR_HID,
82 	.ops =		{
83 				.add =		acpi_processor_add,
84 				.remove =	acpi_processor_remove,
85 			},
86 };
87 
88 /* Power Management */
89 
90 struct acpi_processor_cx_policy {
91 	u32			count;
92 	int			state;
93 	struct {
94 		u32			time;
95 		u32			ticks;
96 		u32			count;
97 		u32			bm;
98 	}			threshold;
99 };
100 
101 struct acpi_processor_cx {
102 	u8			valid;
103 	u32			address;
104 	u32			latency;
105 	u32			latency_ticks;
106 	u32			power;
107 	u32			usage;
108 	struct acpi_processor_cx_policy promotion;
109 	struct acpi_processor_cx_policy demotion;
110 };
111 
112 struct acpi_processor_power {
113 	int			state;
114 	int			default_state;
115 	u32			bm_activity;
116 	struct acpi_processor_cx states[ACPI_PROCESSOR_MAX_POWER];
117 };
118 
119 /* Performance Management */
120 
121 struct acpi_pct_register {
122 	u8			descriptor;
123 	u16			length;
124 	u8			space_id;
125 	u8			bit_width;
126 	u8			bit_offset;
127 	u8			reserved;
128 	u64			address;
129 } __attribute__ ((packed));
130 
131 struct acpi_processor_px {
132 	acpi_integer		core_frequency;		/* megahertz */
133 	acpi_integer		power;			/* milliWatts */
134 	acpi_integer		transition_latency;	/* microseconds */
135 	acpi_integer		bus_master_latency;	/* microseconds */
136 	acpi_integer		control;		/* control value */
137 	acpi_integer		status;			/* success indicator */
138 };
139 
140 struct acpi_processor_performance {
141 	int			state;
142 	int			platform_limit;
143 	u16			control_register;
144 	u16			status_register;
145 	u8			control_register_bit_width;
146 	u8			status_register_bit_width;
147 	int			state_count;
148 	struct acpi_processor_px states[ACPI_PROCESSOR_MAX_PERFORMANCE];
149 };
150 
151 
152 /* Throttling Control */
153 
154 struct acpi_processor_tx {
155 	u16			power;
156 	u16			performance;
157 };
158 
159 struct acpi_processor_throttling {
160 	int			state;
161 	u32			address;
162 	u8			duty_offset;
163 	u8			duty_width;
164 	int			state_count;
165 	struct acpi_processor_tx states[ACPI_PROCESSOR_MAX_THROTTLING];
166 };
167 
168 /* Limit Interface */
169 
170 struct acpi_processor_lx {
171 	int			px;		/* performace state */
172 	int			tx;		/* throttle level */
173 };
174 
175 struct acpi_processor_limit {
176 	struct acpi_processor_lx state;		/* current limit */
177 	struct acpi_processor_lx thermal;	/* thermal limit */
178 	struct acpi_processor_lx user;		/* user limit */
179 };
180 
181 
182 struct acpi_processor_flags {
183 	u8			power:1;
184 	u8			performance:1;
185 	u8			throttling:1;
186 	u8			limit:1;
187 	u8			bm_control:1;
188 	u8			bm_check:1;
189 	u8			reserved:2;
190 };
191 
192 struct acpi_processor {
193 	acpi_handle		handle;
194 	u32			acpi_id;
195 	u32			id;
196 	struct acpi_processor_flags flags;
197 	struct acpi_processor_power power;
198 	struct acpi_processor_performance performance;
199 	struct acpi_processor_throttling throttling;
200 	struct acpi_processor_limit limit;
201 };
202 
203 struct acpi_processor_errata {
204 	u8			smp;
205 	struct {
206 		u8			throttle:1;
207 		u8			fdma:1;
208 		u8			reserved:6;
209 		u32			bmisx;
210 	}			piix4;
211 };
212 
213 static struct acpi_processor	*processors[NR_CPUS];
214 static struct acpi_processor_errata errata;
215 static void (*pm_idle_save)(void);
216 
217 
218 /* --------------------------------------------------------------------------
219                                 Errata Handling
220    -------------------------------------------------------------------------- */
221 
222 int
acpi_processor_errata_piix4(struct pci_dev * dev)223 acpi_processor_errata_piix4 (
224 	struct pci_dev		*dev)
225 {
226 	u8			rev = 0;
227 	u8			value1 = 0;
228 	u8			value2 = 0;
229 
230 	ACPI_FUNCTION_TRACE("acpi_processor_errata_piix4");
231 
232 	if (!dev)
233 		return_VALUE(-EINVAL);
234 
235 	/*
236 	 * Note that 'dev' references the PIIX4 ACPI Controller.
237 	 */
238 
239 	pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
240 
241 	switch (rev) {
242 	case 0:
243 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
244 		break;
245 	case 1:
246 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
247 		break;
248 	case 2:
249 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
250 		break;
251 	case 3:
252 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
253 		break;
254 	default:
255 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
256 		break;
257 	}
258 
259 	switch (rev) {
260 
261 	case 0:		/* PIIX4 A-step */
262 	case 1:		/* PIIX4 B-step */
263 		/*
264 		 * See specification changes #13 ("Manual Throttle Duty Cycle")
265 		 * and #14 ("Enabling and Disabling Manual Throttle"), plus
266 		 * erratum #5 ("STPCLK# Deassertion Time") from the January
267 		 * 2002 PIIX4 specification update.  Applies to only older
268 		 * PIIX4 models.
269 		 */
270 		errata.piix4.throttle = 1;
271 
272 	case 2:		/* PIIX4E */
273 	case 3:		/* PIIX4M */
274 		/*
275 		 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
276 		 * Livelock") from the January 2002 PIIX4 specification update.
277 		 * Applies to all PIIX4 models.
278 		 */
279 
280 		/*
281 		 * BM-IDE
282 		 * ------
283 		 * Find the PIIX4 IDE Controller and get the Bus Master IDE
284 		 * Status register address.  We'll use this later to read
285 		 * each IDE controller's DMA status to make sure we catch all
286 		 * DMA activity.
287 		 */
288 		dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
289 		           PCI_DEVICE_ID_INTEL_82371AB,
290                            PCI_ANY_ID, PCI_ANY_ID, NULL);
291 		if (dev)
292 			errata.piix4.bmisx = pci_resource_start(dev, 4);
293 
294 		/*
295 		 * Type-F DMA
296 		 * ----------
297 		 * Find the PIIX4 ISA Controller and read the Motherboard
298 		 * DMA controller's status to see if Type-F (Fast) DMA mode
299 		 * is enabled (bit 7) on either channel.  Note that we'll
300 		 * disable C3 support if this is enabled, as some legacy
301 		 * devices won't operate well if fast DMA is disabled.
302 		 */
303 		dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
304 			PCI_DEVICE_ID_INTEL_82371AB_0,
305 			PCI_ANY_ID, PCI_ANY_ID, NULL);
306 		if (dev) {
307 			pci_read_config_byte(dev, 0x76, &value1);
308 			pci_read_config_byte(dev, 0x77, &value2);
309 			if ((value1 & 0x80) || (value2 & 0x80))
310 				errata.piix4.fdma = 1;
311 		}
312 
313 		break;
314 	}
315 
316 	if (errata.piix4.bmisx)
317 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
318 			"Bus master activity detection (BM-IDE) erratum enabled\n"));
319 	if (errata.piix4.fdma)
320 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 			"Type-F DMA livelock erratum (C3 disabled)\n"));
322 
323 	return_VALUE(0);
324 }
325 
326 
327 int
acpi_processor_errata(struct acpi_processor * pr)328 acpi_processor_errata (
329 	struct acpi_processor	*pr)
330 {
331 	int			result = 0;
332 	struct pci_dev		*dev = NULL;
333 
334 	ACPI_FUNCTION_TRACE("acpi_processor_errata");
335 
336 	if (!pr)
337 		return_VALUE(-EINVAL);
338 
339 	/*
340 	 * PIIX4
341 	 */
342 	dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
343 		PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID, PCI_ANY_ID, NULL);
344 	if (dev)
345 		result = acpi_processor_errata_piix4(dev);
346 
347 	return_VALUE(result);
348 }
349 
350 
351 /* --------------------------------------------------------------------------
352                                 Power Management
353    -------------------------------------------------------------------------- */
354 
355 static inline u32
ticks_elapsed(u32 t1,u32 t2)356 ticks_elapsed (
357 	u32			t1,
358 	u32			t2)
359 {
360 	if (t2 >= t1)
361 		return (t2 - t1);
362 	else if (!acpi_fadt.tmr_val_ext)
363 		return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
364 	else
365 		return ((0xFFFFFFFF - t1) + t2);
366 }
367 
368 
369 static void
acpi_processor_power_activate(struct acpi_processor * pr,int state)370 acpi_processor_power_activate (
371 	struct acpi_processor	*pr,
372 	int			state)
373 {
374 	if (!pr)
375 		return;
376 
377 	pr->power.states[pr->power.state].promotion.count = 0;
378 	pr->power.states[pr->power.state].demotion.count = 0;
379 
380 	/* Cleanup from old state. */
381 	switch (pr->power.state) {
382 	case ACPI_STATE_C3:
383 		/* Disable bus master reload */
384 		acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0, ACPI_MTX_DO_NOT_LOCK);
385 		break;
386 	}
387 
388 	/* Prepare to use new state. */
389 	switch (state) {
390 	case ACPI_STATE_C3:
391 		/* Enable bus master reload */
392 		acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1, ACPI_MTX_DO_NOT_LOCK);
393 		break;
394 	}
395 
396 	pr->power.state = state;
397 
398 	return;
399 }
400 
401 
402 static void
acpi_processor_idle(void)403 acpi_processor_idle (void)
404 {
405 	struct acpi_processor	*pr = NULL;
406 	struct acpi_processor_cx *cx = NULL;
407 	int			next_state = 0;
408 	int			sleep_ticks = 0;
409 	u32			t1, t2 = 0;
410 
411 	pr = processors[smp_processor_id()];
412 	if (!pr)
413 		return;
414 
415 	/*
416 	 * Interrupts must be disabled during bus mastering calculations and
417 	 * for C2/C3 transitions.
418 	 */
419 	__cli();
420 
421 	cx = &(pr->power.states[pr->power.state]);
422 
423 	/*
424 	 * Check BM Activity
425 	 * -----------------
426 	 * Check for bus mastering activity (if required), record, and check
427 	 * for demotion.
428 	 */
429 	if (pr->flags.bm_check) {
430 		u32		bm_status = 0;
431 
432 		pr->power.bm_activity <<= 1;
433 
434 		acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS,
435 			&bm_status, ACPI_MTX_DO_NOT_LOCK);
436 		if (bm_status) {
437 			pr->power.bm_activity++;
438 			acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS,
439 				1, ACPI_MTX_DO_NOT_LOCK);
440 		}
441 		/*
442 		 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
443 		 * the true state of bus mastering activity; forcing us to
444 		 * manually check the BMIDEA bit of each IDE channel.
445 		 */
446 		else if (errata.piix4.bmisx) {
447 			if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
448 				|| (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
449 				pr->power.bm_activity++;
450 		}
451 		/*
452 		 * Apply bus mastering demotion policy.  Automatically demote
453 		 * to avoid a faulty transition.  Note that the processor
454 		 * won't enter a low-power state during this call (to this
455 		 * funciton) but should upon the next.
456 		 *
457 		 * TBD: A better policy might be to fallback to the demotion
458 		 *      state (use it for this quantum only) istead of
459 		 *      demoting -- and rely on duration as our sole demotion
460 		 *      qualification.  This may, however, introduce DMA
461 		 *      issues (e.g. floppy DMA transfer overrun/underrun).
462 		 */
463 		if (pr->power.bm_activity & cx->demotion.threshold.bm) {
464 			__sti();
465 			next_state = cx->demotion.state;
466 			goto end;
467 		}
468 	}
469 
470 	cx->usage++;
471 
472 	/*
473 	 * Sleep:
474 	 * ------
475 	 * Invoke the current Cx state to put the processor to sleep.
476 	 */
477 	switch (pr->power.state) {
478 
479 	case ACPI_STATE_C1:
480 		/* Invoke C1. */
481 		safe_halt();
482 		/*
483                  * TBD: Can't get time duration while in C1, as resumes
484 		 *      go to an ISR rather than here.  Need to instrument
485 		 *      base interrupt handler.
486 		 */
487 		sleep_ticks = 0xFFFFFFFF;
488 		break;
489 
490 	case ACPI_STATE_C2:
491 		/* Get start time (ticks) */
492 		t1 = inl(acpi_fadt.xpm_tmr_blk.address);
493 		/* Invoke C2 */
494 		inb(pr->power.states[ACPI_STATE_C2].address);
495 		/* Dummy op - must do something useless after P_LVL2 read */
496 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
497 		/* Get end time (ticks) */
498 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
499 		/* Re-enable interrupts */
500 		__sti();
501 		/* Compute time (ticks) that we were actually asleep */
502 		sleep_ticks = ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
503 		break;
504 
505 	case ACPI_STATE_C3:
506 		/* Disable bus master arbitration */
507 		acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1, ACPI_MTX_DO_NOT_LOCK);
508 		/* Get start time (ticks) */
509 		t1 = inl(acpi_fadt.xpm_tmr_blk.address);
510 		/* Invoke C3 */
511 		inb(pr->power.states[ACPI_STATE_C3].address);
512 		/* Dummy op - must do something useless after P_LVL3 read */
513 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
514 		/* Get end time (ticks) */
515 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
516 		/* Enable bus master arbitration */
517 		acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0, ACPI_MTX_DO_NOT_LOCK);
518 		/* Re-enable interrupts */
519 		__sti();
520 		/* Compute time (ticks) that we were actually asleep */
521 		sleep_ticks = ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
522 		break;
523 
524 	default:
525 		__sti();
526 		return;
527 	}
528 
529 	next_state = pr->power.state;
530 
531 	/*
532 	 * Promotion?
533 	 * ----------
534 	 * Track the number of longs (time asleep is greater than threshold)
535 	 * and promote when the count threshold is reached.  Note that bus
536 	 * mastering activity may prevent promotions.
537 	 */
538 	if (cx->promotion.state) {
539 		if (sleep_ticks > cx->promotion.threshold.ticks) {
540 			cx->promotion.count++;
541  			cx->demotion.count = 0;
542 			if (cx->promotion.count >= cx->promotion.threshold.count) {
543 				if (pr->flags.bm_check) {
544 					if (!(pr->power.bm_activity & cx->promotion.threshold.bm)) {
545 						next_state = cx->promotion.state;
546 						goto end;
547 					}
548 				}
549 				else {
550 					next_state = cx->promotion.state;
551 					goto end;
552 				}
553 			}
554 		}
555 	}
556 
557 	/*
558 	 * Demotion?
559 	 * ---------
560 	 * Track the number of shorts (time asleep is less than time threshold)
561 	 * and demote when the usage threshold is reached.
562 	 */
563 	if (cx->demotion.state) {
564 		if (sleep_ticks < cx->demotion.threshold.ticks) {
565 			cx->demotion.count++;
566 			cx->promotion.count = 0;
567 			if (cx->demotion.count >= cx->demotion.threshold.count) {
568 				next_state = cx->demotion.state;
569 				goto end;
570 			}
571 		}
572 	}
573 
574 end:
575 	/*
576 	 * New Cx State?
577 	 * -------------
578 	 * If we're going to start using a new Cx state we must clean up
579 	 * from the previous and prepare to use the new.
580 	 */
581 	if (next_state != pr->power.state)
582 		acpi_processor_power_activate(pr, next_state);
583 
584 	return;
585 }
586 
587 
588 static int
acpi_processor_set_power_policy(struct acpi_processor * pr)589 acpi_processor_set_power_policy (
590 	struct acpi_processor	*pr)
591 {
592 	ACPI_FUNCTION_TRACE("acpi_processor_set_power_policy");
593 
594 	/*
595 	 * This function sets the default Cx state policy (OS idle handler).
596 	 * Our scheme is to promote quickly to C2 but more conservatively
597 	 * to C3.  We're favoring C2  for its characteristics of low latency
598 	 * (quick response), good power savings, and ability to allow bus
599 	 * mastering activity.  Note that the Cx state policy is completely
600 	 * customizable and can be altered dynamically.
601 	 */
602 
603 	if (!pr)
604 		return_VALUE(-EINVAL);
605 
606 	/*
607 	 * C0/C1
608 	 * -----
609 	 */
610 	pr->power.state = ACPI_STATE_C1;
611 	pr->power.default_state = ACPI_STATE_C1;
612 
613 	/*
614 	 * C1/C2
615 	 * -----
616 	 * Set the default C1 promotion and C2 demotion policies, where we
617 	 * promote from C1 to C2 after several (10) successive C1 transitions,
618 	 * as we cannot (currently) measure the time spent in C1. Demote from
619 	 * C2 to C1 anytime we experience a 'short' (time spent in C2 is less
620 	 * than the C2 transtion latency).  Note the simplifying assumption
621 	 * that the 'cost' of a transition is amortized when we sleep for at
622 	 * least as long as the transition's latency (thus the total transition
623 	 * time is two times the latency).
624 	 *
625 	 * TBD: Measure C1 sleep times by instrumenting the core IRQ handler.
626 	 * TBD: Demote to default C-State after long periods of activity.
627 	 * TBD: Investigate policy's use of CPU utilization -vs- sleep duration.
628 	 */
629 	if (pr->power.states[ACPI_STATE_C2].valid) {
630 		pr->power.states[ACPI_STATE_C1].promotion.threshold.count = 10;
631 		pr->power.states[ACPI_STATE_C1].promotion.threshold.ticks =
632 			pr->power.states[ACPI_STATE_C2].latency_ticks;
633 		pr->power.states[ACPI_STATE_C1].promotion.state = ACPI_STATE_C2;
634 
635 		pr->power.states[ACPI_STATE_C2].demotion.threshold.count = 1;
636 		pr->power.states[ACPI_STATE_C2].demotion.threshold.ticks =
637 			pr->power.states[ACPI_STATE_C2].latency_ticks;
638 		pr->power.states[ACPI_STATE_C2].demotion.state = ACPI_STATE_C1;
639 	}
640 
641 	/*
642 	 * C2/C3
643 	 * -----
644 	 * Set default C2 promotion and C3 demotion policies, where we promote
645 	 * from C2 to C3 after several (4) cycles of no bus mastering activity
646 	 * while maintaining sleep time criteria.  Demote immediately on a
647 	 * short or whenever bus mastering activity occurs.
648 	 */
649 	if ((pr->power.states[ACPI_STATE_C2].valid) &&
650 		(pr->power.states[ACPI_STATE_C3].valid)) {
651 		pr->power.states[ACPI_STATE_C2].promotion.threshold.count = 4;
652 		pr->power.states[ACPI_STATE_C2].promotion.threshold.ticks =
653 			pr->power.states[ACPI_STATE_C3].latency_ticks;
654 		pr->power.states[ACPI_STATE_C2].promotion.threshold.bm = 0x0F;
655 		pr->power.states[ACPI_STATE_C2].promotion.state = ACPI_STATE_C3;
656 
657 		pr->power.states[ACPI_STATE_C3].demotion.threshold.count = 1;
658 		pr->power.states[ACPI_STATE_C3].demotion.threshold.ticks =
659 			pr->power.states[ACPI_STATE_C3].latency_ticks;
660 		pr->power.states[ACPI_STATE_C3].demotion.threshold.bm = 0x0F;
661 		pr->power.states[ACPI_STATE_C3].demotion.state = ACPI_STATE_C2;
662 	}
663 
664 	return_VALUE(0);
665 }
666 
667 
668 int
acpi_processor_get_power_info(struct acpi_processor * pr)669 acpi_processor_get_power_info (
670 	struct acpi_processor	*pr)
671 {
672 	int			result = 0;
673 
674 	ACPI_FUNCTION_TRACE("acpi_processor_get_power_info");
675 
676 	if (!pr)
677 		return_VALUE(-EINVAL);
678 
679 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
680 		"lvl2[0x%08x] lvl3[0x%08x]\n",
681 		pr->power.states[ACPI_STATE_C2].address,
682 		pr->power.states[ACPI_STATE_C3].address));
683 
684 	/* TBD: Support ACPI 2.0 objects */
685 
686 	/*
687 	 * C0
688 	 * --
689 	 * This state exists only as filler in our array.
690 	 */
691 	pr->power.states[ACPI_STATE_C0].valid = 1;
692 
693 	/*
694 	 * C1
695 	 * --
696 	 * ACPI requires C1 support for all processors.
697 	 *
698 	 * TBD: What about PROC_C1?
699 	 */
700 	pr->power.states[ACPI_STATE_C1].valid = 1;
701 
702 	/*
703 	 * C2
704 	 * --
705 	 * We're (currently) only supporting C2 on UP systems.
706 	 *
707 	 * TBD: Support for C2 on MP (P_LVL2_UP).
708 	 */
709 	if (pr->power.states[ACPI_STATE_C2].address) {
710 
711 		pr->power.states[ACPI_STATE_C2].latency = acpi_fadt.plvl2_lat;
712 
713 		/*
714 		 * C2 latency must be less than or equal to 100 microseconds.
715 		 */
716 		if (acpi_fadt.plvl2_lat > ACPI_PROCESSOR_MAX_C2_LATENCY)
717 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
718 				"C2 latency too large [%d]\n",
719 				acpi_fadt.plvl2_lat));
720 		/*
721 		 * Only support C2 on UP systems (see TBD above).
722 		 */
723 		else if (errata.smp)
724 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
725 				"C2 not supported in SMP mode\n"));
726 		/*
727 		 * Otherwise we've met all of our C2 requirements.
728 		 * Normalize the C2 latency to expidite policy.
729 		 */
730 		else {
731 			pr->power.states[ACPI_STATE_C2].valid = 1;
732 			pr->power.states[ACPI_STATE_C2].latency_ticks =
733 				US_TO_PM_TIMER_TICKS(acpi_fadt.plvl2_lat);
734 		}
735 	}
736 
737 	/*
738 	 * C3
739 	 * --
740 	 * TBD: Investigate use of WBINVD on UP/SMP system in absence of
741 	 *	bm_control.
742 	 */
743 	if (pr->power.states[ACPI_STATE_C3].address) {
744 
745 		pr->power.states[ACPI_STATE_C3].latency = acpi_fadt.plvl3_lat;
746 
747 		/*
748 		 * C3 latency must be less than or equal to 1000 microseconds.
749 		 */
750 		if (acpi_fadt.plvl3_lat > ACPI_PROCESSOR_MAX_C3_LATENCY)
751 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
752 				"C3 latency too large [%d]\n",
753 				acpi_fadt.plvl3_lat));
754 		/*
755 		 * Only support C3 when bus mastering arbitration control
756 		 * is present (able to disable bus mastering to maintain
757 		 * cache coherency while in C3).
758 		 */
759 		else if (!pr->flags.bm_control)
760 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
761 				"C3 support requires bus mastering control\n"));
762 		/*
763 		 * Only support C3 on UP systems, as bm_control is only viable
764 		 * on a UP system and flushing caches (e.g. WBINVD) is simply
765 		 * too costly (at this time).
766 		 */
767 		else if (errata.smp)
768 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
769 				"C3 not supported in SMP mode\n"));
770 		/*
771 		 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
772 		 * DMA transfers are used by any ISA device to avoid livelock.
773 		 * Note that we could disable Type-F DMA (as recommended by
774 		 * the erratum), but this is known to disrupt certain ISA
775 		 * devices thus we take the conservative approach.
776 		 */
777 		else if (errata.piix4.fdma) {
778 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
779 				"C3 not supported on PIIX4 with Type-F DMA\n"));
780 		}
781 		/*
782 		 * Otherwise we've met all of our C3 requirements.
783 		 * Normalize the C2 latency to expidite policy.  Enable
784 		 * checking of bus mastering status (bm_check) so we can
785 		 * use this in our C3 policy.
786 		 */
787 		else {
788 			pr->power.states[ACPI_STATE_C3].valid = 1;
789 			pr->power.states[ACPI_STATE_C3].latency_ticks =
790 				US_TO_PM_TIMER_TICKS(acpi_fadt.plvl3_lat);
791 			pr->flags.bm_check = 1;
792 		}
793 	}
794 
795 	/*
796 	 * Set Default Policy
797 	 * ------------------
798 	 * Now that we know which state are supported, set the default
799 	 * policy.  Note that this policy can be changed dynamically
800 	 * (e.g. encourage deeper sleeps to conserve battery life when
801 	 * not on AC).
802 	 */
803 	result = acpi_processor_set_power_policy(pr);
804 	if (result)
805 		return_VALUE(result);
806 
807 	/*
808 	 * If this processor supports C2 or C3 we denote it as being 'power
809 	 * manageable'.  Note that there's really no policy involved for
810 	 * when only C1 is supported.
811 	 */
812 	if (pr->power.states[ACPI_STATE_C2].valid
813 		|| pr->power.states[ACPI_STATE_C3].valid)
814 		pr->flags.power = 1;
815 
816 	return_VALUE(0);
817 }
818 
819 
820 /* --------------------------------------------------------------------------
821                               Performance Management
822    -------------------------------------------------------------------------- */
823 
824 static int
acpi_processor_get_platform_limit(struct acpi_processor * pr)825 acpi_processor_get_platform_limit (
826 	struct acpi_processor*	pr)
827 {
828 	acpi_status		status = 0;
829 	unsigned long		ppc = 0;
830 
831 	ACPI_FUNCTION_TRACE("acpi_processor_get_platform_limit");
832 
833 	if (!pr)
834 		return_VALUE(-EINVAL);
835 
836 	/*
837 	 * _PPC indicates the maximum state currently supported by the platform
838 	 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
839 	 */
840 	status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
841 	if(ACPI_FAILURE(status)) {
842 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PPC\n"));
843 		return_VALUE(-ENODEV);
844 	}
845 
846 	pr->performance.platform_limit = (int) ppc;
847 
848 	return_VALUE(0);
849 }
850 
851 
852 static int
acpi_processor_get_performance_control(struct acpi_processor * pr)853 acpi_processor_get_performance_control (
854 	struct acpi_processor	*pr)
855 {
856 	int			result = 0;
857 	acpi_status		status = 0;
858 	struct acpi_buffer	buffer = {ACPI_ALLOCATE_BUFFER, NULL};
859 	union acpi_object	*pct = NULL;
860 	union acpi_object	obj = {0};
861 	struct acpi_pct_register *reg = NULL;
862 
863 	ACPI_FUNCTION_TRACE("acpi_processor_get_performance_control");
864 
865 	status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
866 	if(ACPI_FAILURE(status)) {
867 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PCT\n"));
868 		return_VALUE(-ENODEV);
869 	}
870 
871 	pct = (union acpi_object *) buffer.pointer;
872 	if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
873 		|| (pct->package.count != 2)) {
874 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PCT data\n"));
875 		result = -EFAULT;
876 		goto end;
877 	}
878 
879 	/*
880 	 * control_register
881 	 */
882 
883 	obj = pct->package.elements[0];
884 
885 	if ((obj.type != ACPI_TYPE_BUFFER)
886 		|| (obj.buffer.length < sizeof(struct acpi_pct_register))
887 		|| (obj.buffer.pointer == NULL)) {
888 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
889 			"Invalid _PCT data (control_register)\n"));
890 		result = -EFAULT;
891 		goto end;
892 	}
893 
894 	reg = (struct acpi_pct_register *) (obj.buffer.pointer);
895 
896 	if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
897 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
898 			"Unsupported address space [%d] (control_register)\n",
899 			(u32) reg->space_id));
900 		result = -EFAULT;
901 		goto end;
902 	}
903 
904 	pr->performance.control_register = (u16) reg->address;
905 	pr->performance.control_register_bit_width = reg->bit_width;
906 	/*
907 	 * status_register
908 	 */
909 
910 	obj = pct->package.elements[1];
911 
912 	if ((obj.type != ACPI_TYPE_BUFFER)
913 		|| (obj.buffer.length < sizeof(struct acpi_pct_register))
914 		|| (obj.buffer.pointer == NULL)) {
915 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
916 			"Invalid _PCT data (status_register)\n"));
917 		result = -EFAULT;
918 		goto end;
919 	}
920 
921 	reg = (struct acpi_pct_register *) (obj.buffer.pointer);
922 
923 	if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
924 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
925 			"Unsupported address space [%d] (status_register)\n",
926 			(u32) reg->space_id));
927 		result = -EFAULT;
928 		goto end;
929 	}
930 
931 	pr->performance.status_register = (u16) reg->address;
932 	pr->performance.status_register_bit_width = reg->bit_width;
933 
934 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
935 		"control_register[0x%04x] status_register[0x%04x]\n",
936 		pr->performance.control_register,
937 		pr->performance.status_register));
938 
939 end:
940 	acpi_os_free(buffer.pointer);
941 
942 	return_VALUE(result);
943 }
944 
945 
946 static int
acpi_processor_get_performance_states(struct acpi_processor * pr)947 acpi_processor_get_performance_states (
948 	struct acpi_processor*	pr)
949 {
950 	int			result = 0;
951 	acpi_status		status = AE_OK;
952 	struct acpi_buffer	buffer = {ACPI_ALLOCATE_BUFFER, NULL};
953 	struct acpi_buffer	format = {sizeof("NNNNNN"), "NNNNNN"};
954 	struct acpi_buffer	state = {0, NULL};
955 	union acpi_object 	*pss = NULL;
956 	int			i = 0;
957 
958 	ACPI_FUNCTION_TRACE("acpi_processor_get_performance_states");
959 
960 	status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
961 	if(ACPI_FAILURE(status)) {
962 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PSS\n"));
963 		return_VALUE(-ENODEV);
964 	}
965 
966 	pss = (union acpi_object *) buffer.pointer;
967 	if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
968 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSS data\n"));
969 		result = -EFAULT;
970 		goto end;
971 	}
972 
973 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
974 		pss->package.count));
975 
976 	if (pss->package.count > ACPI_PROCESSOR_MAX_PERFORMANCE) {
977 		pr->performance.state_count = ACPI_PROCESSOR_MAX_PERFORMANCE;
978 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
979 			"Limiting number of states to max (%d)\n",
980 			ACPI_PROCESSOR_MAX_PERFORMANCE));
981 	}
982 	else
983 		pr->performance.state_count = pss->package.count;
984 
985 	if (pr->performance.state_count > 1)
986 		pr->flags.performance = 1;
987 
988 	for (i = 0; i < pr->performance.state_count; i++) {
989 
990 		struct acpi_processor_px *px = &(pr->performance.states[i]);
991 
992 		state.length = sizeof(struct acpi_processor_px);
993 		state.pointer = px;
994 
995 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
996 
997 		status = acpi_extract_package(&(pss->package.elements[i]),
998 			&format, &state);
999 		if (ACPI_FAILURE(status)) {
1000 			ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSS data\n"));
1001 			result = -EFAULT;
1002 			goto end;
1003 		}
1004 
1005 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1006 			"State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
1007 			i,
1008 			(u32) px->core_frequency,
1009 			(u32) px->power,
1010 			(u32) px->transition_latency,
1011 			(u32) px->bus_master_latency,
1012 			(u32) px->control,
1013 			(u32) px->status));
1014 	}
1015 
1016 end:
1017 	acpi_os_free(buffer.pointer);
1018 
1019 	return_VALUE(result);
1020 }
1021 
1022 static int
acpi_processor_write_port(u16 port,u8 bit_width,u32 value)1023 acpi_processor_write_port(
1024 	u16	port,
1025 	u8	bit_width,
1026 	u32	value)
1027 {
1028 	if (bit_width <= 8) {
1029 		outb(value, port);
1030 	} else if (bit_width <= 16) {
1031 		outw(value, port);
1032 	} else if (bit_width <= 32) {
1033 		outl(value, port);
1034 	} else {
1035 		return -ENODEV;
1036 	}
1037 	return 0;
1038 }
1039 
1040 static int
acpi_processor_read_port(u16 port,u8 bit_width,u32 * ret)1041 acpi_processor_read_port(
1042 	u16	port,
1043 	u8	bit_width,
1044 	u32	*ret)
1045 {
1046 	*ret = 0;
1047 	if (bit_width <= 8) {
1048 		*ret = inb(port);
1049 	} else if (bit_width <= 16) {
1050 		*ret = inw(port);
1051 	} else if (bit_width <= 32) {
1052 		*ret = inl(port);
1053 	} else {
1054 		return -ENODEV;
1055 	}
1056 	return 0;
1057 }
1058 
1059 static int
acpi_processor_set_performance(struct acpi_processor * pr,int state)1060 acpi_processor_set_performance (
1061 	struct acpi_processor	*pr,
1062 	int			state)
1063 {
1064 	u16			port = 0;
1065 	u8			bit_width = 0;
1066 	int			ret = 0;
1067 	u32			value = 0;
1068 	int			i = 0;
1069 
1070 	ACPI_FUNCTION_TRACE("acpi_processor_set_performance");
1071 
1072 	if (!pr)
1073 		return_VALUE(-EINVAL);
1074 
1075 	if (!pr->flags.performance)
1076 		return_VALUE(-ENODEV);
1077 
1078 	if (state >= pr->performance.state_count) {
1079 		ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1080 			"Invalid target state (P%d)\n", state));
1081 		return_VALUE(-ENODEV);
1082 	}
1083 
1084 	if (state < pr->performance.platform_limit) {
1085 		ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1086 			"Platform limit (P%d) overrides target state (P%d)\n",
1087 			pr->performance.platform_limit, state));
1088 		return_VALUE(-ENODEV);
1089 	}
1090 
1091 	if (state == pr->performance.state) {
1092 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1093 			"Already at target state (P%d)\n", state));
1094 		return_VALUE(0);
1095 	}
1096 
1097 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Transitioning from P%d to P%d\n",
1098 		pr->performance.state, state));
1099 
1100 	/*
1101 	 * First we write the target state's 'control' value to the
1102 	 * control_register.
1103 	 */
1104 
1105 	port = pr->performance.control_register;
1106 	value = (u32) pr->performance.states[state].control;
1107 	bit_width = pr->performance.control_register_bit_width;
1108 
1109 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1110 		"Writing 0x%08x to port 0x%04x\n", value, port));
1111 
1112 	ret = acpi_processor_write_port(port, bit_width, value);
1113 	if (ret) {
1114 		ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1115 			"Invalid port width 0x%04x\n", bit_width));
1116 		return_VALUE(ret);
1117 	}
1118 
1119 	/*
1120 	 * Then we read the 'status_register' and compare the value with the
1121 	 * target state's 'status' to make sure the transition was successful.
1122 	 * Note that we'll poll for up to 1ms (100 cycles of 10us) before
1123 	 * giving up.
1124 	 */
1125 
1126 	port = pr->performance.status_register;
1127 	bit_width = pr->performance.status_register_bit_width;
1128 
1129 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1130 		"Looking for 0x%08x from port 0x%04x\n",
1131 		(u32) pr->performance.states[state].status, port));
1132 
1133 	for (i=0; i<100; i++) {
1134 		ret = acpi_processor_read_port(port, bit_width, &value);
1135 		if (ret) {
1136 			ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1137 				"Invalid port width 0x%04x\n", bit_width));
1138 			return_VALUE(ret);
1139 		}
1140 		if (value == (u32) pr->performance.states[state].status)
1141 			break;
1142 		udelay(10);
1143 	}
1144 
1145 	if (value != (u32) pr->performance.states[state].status) {
1146 		ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Transition failed\n"));
1147 		return_VALUE(-ENODEV);
1148 	}
1149 
1150 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1151 		"Transition successful after %d microseconds\n",
1152 		i * 10));
1153 
1154 	pr->performance.state = state;
1155 
1156 	return_VALUE(0);
1157 }
1158 
1159 
1160 static int
acpi_processor_get_performance_info(struct acpi_processor * pr)1161 acpi_processor_get_performance_info (
1162 	struct acpi_processor	*pr)
1163 {
1164 	int			result = 0;
1165 	acpi_status		status = AE_OK;
1166 	acpi_handle		handle = NULL;
1167 
1168 	ACPI_FUNCTION_TRACE("acpi_processor_get_performance_info");
1169 
1170 	if (!pr)
1171 		return_VALUE(-EINVAL);
1172 
1173 	status = acpi_get_handle(pr->handle, "_PCT", &handle);
1174 	if (ACPI_FAILURE(status)) {
1175 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1176 			"ACPI-based processor performance control unavailable\n"));
1177 		return_VALUE(0);
1178 	}
1179 
1180 	result = acpi_processor_get_performance_control(pr);
1181 	if (result)
1182 		return_VALUE(result);
1183 
1184 	result = acpi_processor_get_performance_states(pr);
1185 	if (result)
1186 		return_VALUE(result);
1187 
1188 	result = acpi_processor_get_platform_limit(pr);
1189 	if (result)
1190 		return_VALUE(result);
1191 
1192 	/*
1193 	 * TBD: Don't trust the latency values we get from BIOS, but rather
1194 	 *      measure the latencies during run-time (e.g. get_latencies).
1195 	 */
1196 
1197 	return_VALUE(0);
1198 }
1199 
1200 
1201 /* --------------------------------------------------------------------------
1202                               Throttling Control
1203    -------------------------------------------------------------------------- */
1204 
1205 static int
acpi_processor_get_throttling(struct acpi_processor * pr)1206 acpi_processor_get_throttling (
1207 	struct acpi_processor	*pr)
1208 {
1209 	int			state = 0;
1210 	u32			value = 0;
1211 	u32			duty_mask = 0;
1212 	u32			duty_value = 0;
1213 
1214 	ACPI_FUNCTION_TRACE("acpi_processor_get_throttling");
1215 
1216 	if (!pr)
1217 		return_VALUE(-EINVAL);
1218 
1219 	if (!pr->flags.throttling)
1220 		return_VALUE(-ENODEV);
1221 
1222 	pr->throttling.state = 0;
1223 
1224 	__cli();
1225 
1226 	duty_mask = pr->throttling.state_count - 1;
1227 
1228 	duty_mask <<= pr->throttling.duty_offset;
1229 
1230 	value = inl(pr->throttling.address);
1231 
1232 	/*
1233 	 * Compute the current throttling state when throttling is enabled
1234 	 * (bit 4 is on).
1235 	 */
1236 	if (value & 0x10) {
1237 		duty_value = value & duty_mask;
1238 		duty_value >>= pr->throttling.duty_offset;
1239 
1240 		if (duty_value)
1241 			state = pr->throttling.state_count-duty_value;
1242 	}
1243 
1244 	pr->throttling.state = state;
1245 
1246 	__sti();
1247 
1248 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1249 		"Throttling state is T%d (%d%% throttling applied)\n",
1250 		state, pr->throttling.states[state].performance));
1251 
1252 	return_VALUE(0);
1253 }
1254 
1255 
1256 static int
acpi_processor_set_throttling(struct acpi_processor * pr,int state)1257 acpi_processor_set_throttling (
1258 	struct acpi_processor	*pr,
1259 	int			state)
1260 {
1261 	u32                     value = 0;
1262 	u32                     duty_mask = 0;
1263 	u32                     duty_value = 0;
1264 
1265 	ACPI_FUNCTION_TRACE("acpi_processor_set_throttling");
1266 
1267 	if (!pr)
1268 		return_VALUE(-EINVAL);
1269 
1270 	if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1271 		return_VALUE(-EINVAL);
1272 
1273 	if (!pr->flags.throttling)
1274 		return_VALUE(-ENODEV);
1275 
1276 	if (state == pr->throttling.state)
1277 		return_VALUE(0);
1278 
1279 	__cli();
1280 
1281 	/*
1282 	 * Calculate the duty_value and duty_mask.
1283 	 */
1284 	if (state) {
1285 		duty_value = pr->throttling.state_count - state;
1286 
1287 		duty_value <<= pr->throttling.duty_offset;
1288 
1289 		/* Used to clear all duty_value bits */
1290 		duty_mask = pr->throttling.state_count - 1;
1291 
1292 		duty_mask <<= acpi_fadt.duty_offset;
1293 		duty_mask = ~duty_mask;
1294 	}
1295 
1296 	/*
1297 	 * Disable throttling by writing a 0 to bit 4.  Note that we must
1298 	 * turn it off before you can change the duty_value.
1299 	 */
1300 	value = inl(pr->throttling.address);
1301 	if (value & 0x10) {
1302 		value &= 0xFFFFFFEF;
1303 		outl(value, pr->throttling.address);
1304 	}
1305 
1306 	/*
1307 	 * Write the new duty_value and then enable throttling.  Note
1308 	 * that a state value of 0 leaves throttling disabled.
1309 	 */
1310 	if (state) {
1311 		value &= duty_mask;
1312 		value |= duty_value;
1313 		outl(value, pr->throttling.address);
1314 
1315 		value |= 0x00000010;
1316 		outl(value, pr->throttling.address);
1317 	}
1318 
1319 	pr->throttling.state = state;
1320 
1321 	__sti();
1322 
1323 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1324 		"Throttling state set to T%d (%d%%)\n", state,
1325 		(pr->throttling.states[state].performance?pr->throttling.states[state].performance/10:0)));
1326 
1327 	return_VALUE(0);
1328 }
1329 
1330 
1331 static int
acpi_processor_get_throttling_info(struct acpi_processor * pr)1332 acpi_processor_get_throttling_info (
1333 	struct acpi_processor	*pr)
1334 {
1335 	int			result = 0;
1336 	int			step = 0;
1337 	int			i = 0;
1338 
1339 	ACPI_FUNCTION_TRACE("acpi_processor_get_throttling_info");
1340 
1341 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1342 		"pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1343 		pr->throttling.address,
1344 		pr->throttling.duty_offset,
1345 		pr->throttling.duty_width));
1346 
1347 	if (!pr)
1348 		return_VALUE(-EINVAL);
1349 
1350 	/* TBD: Support ACPI 2.0 objects */
1351 
1352 	if (!pr->throttling.address) {
1353 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
1354 		return_VALUE(0);
1355 	}
1356 	else if (!pr->throttling.duty_width) {
1357 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
1358 		return_VALUE(0);
1359 	}
1360 	/* TBD: Support duty_cycle values that span bit 4. */
1361 	else if ((pr->throttling.duty_offset
1362 		+ pr->throttling.duty_width) > 4) {
1363 		ACPI_DEBUG_PRINT((ACPI_DB_WARN, "duty_cycle spans bit 4\n"));
1364 		return_VALUE(0);
1365 	}
1366 
1367 	/*
1368 	 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1369 	 * This shouldn't be an issue as few (if any) mobile systems ever
1370 	 * used this part.
1371 	 */
1372 	if (errata.piix4.throttle) {
1373 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1374 			"Throttling not supported on PIIX4 A- or B-step\n"));
1375 		return_VALUE(0);
1376 	}
1377 
1378 	pr->throttling.state_count = 1 << acpi_fadt.duty_width;
1379 
1380 	/*
1381 	 * Compute state values. Note that throttling displays a linear power/
1382 	 * performance relationship (at 50% performance the CPU will consume
1383 	 * 50% power).  Values are in 1/10th of a percent to preserve accuracy.
1384 	 */
1385 
1386 	step = (1000 / pr->throttling.state_count);
1387 
1388 	for (i=0; i<pr->throttling.state_count; i++) {
1389 		pr->throttling.states[i].performance = step * i;
1390 		pr->throttling.states[i].power = step * i;
1391 	}
1392 
1393 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
1394 		pr->throttling.state_count));
1395 
1396 	pr->flags.throttling = 1;
1397 
1398 	/*
1399 	 * Disable throttling (if enabled).  We'll let subsequent policy (e.g.
1400 	 * thermal) decide to lower performance if it so chooses, but for now
1401 	 * we'll crank up the speed.
1402 	 */
1403 
1404 	result = acpi_processor_get_throttling(pr);
1405 	if (result)
1406 		goto end;
1407 
1408 	if (pr->throttling.state) {
1409 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabling throttling (was T%d)\n",
1410 			pr->throttling.state));
1411 		result = acpi_processor_set_throttling(pr, 0);
1412 		if (result)
1413 			goto end;
1414 	}
1415 
1416 end:
1417 	if (result)
1418 		pr->flags.throttling = 0;
1419 
1420 	return_VALUE(result);
1421 }
1422 
1423 
1424 /* --------------------------------------------------------------------------
1425                                  Limit Interface
1426    -------------------------------------------------------------------------- */
1427 
1428 static int
acpi_processor_apply_limit(struct acpi_processor * pr)1429 acpi_processor_apply_limit (
1430 	struct acpi_processor* 	pr)
1431 {
1432 	int			result = 0;
1433 	u16			px = 0;
1434 	u16			tx = 0;
1435 
1436 	ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
1437 
1438 	if (!pr)
1439 		return_VALUE(-EINVAL);
1440 
1441 	if (!pr->flags.limit)
1442 		return_VALUE(-ENODEV);
1443 
1444 	if (pr->flags.performance) {
1445 		px = pr->performance.platform_limit;
1446 		if (pr->limit.user.px > px)
1447 			px = pr->limit.user.px;
1448 		if (pr->limit.thermal.px > px)
1449 			px = pr->limit.thermal.px;
1450 
1451 		result = acpi_processor_set_performance(pr, px);
1452 		if (result)
1453 			goto end;
1454 	}
1455 
1456 	if (pr->flags.throttling) {
1457 		if (pr->limit.user.tx > tx)
1458 			tx = pr->limit.user.tx;
1459 		if (pr->limit.thermal.tx > tx)
1460 			tx = pr->limit.thermal.tx;
1461 
1462 		result = acpi_processor_set_throttling(pr, tx);
1463 		if (result)
1464 			goto end;
1465 	}
1466 
1467 	pr->limit.state.px = px;
1468 	pr->limit.state.tx = tx;
1469 
1470 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d] limit set to (P%d:T%d)\n",
1471 		pr->id,
1472 		pr->limit.state.px,
1473 		pr->limit.state.tx));
1474 
1475 end:
1476 	if (result)
1477 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to set limit\n"));
1478 
1479 	return_VALUE(result);
1480 }
1481 
1482 
1483 int
acpi_processor_set_thermal_limit(acpi_handle handle,int type)1484 acpi_processor_set_thermal_limit (
1485 	acpi_handle		handle,
1486 	int			type)
1487 {
1488 	int			result = 0;
1489 	struct acpi_processor	*pr = NULL;
1490 	struct acpi_device	*device = NULL;
1491 	int			px = 0;
1492 	int			tx = 0;
1493 
1494 	ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
1495 
1496 	if ((type < ACPI_PROCESSOR_LIMIT_NONE)
1497 		|| (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
1498 		return_VALUE(-EINVAL);
1499 
1500 	result = acpi_bus_get_device(handle, &device);
1501 	if (result)
1502 		return_VALUE(result);
1503 
1504 	pr = (struct acpi_processor *) acpi_driver_data(device);
1505 	if (!pr)
1506 		return_VALUE(-ENODEV);
1507 
1508 	if (!pr->flags.limit)
1509 		return_VALUE(-ENODEV);
1510 
1511 	/* Thermal limits are always relative to the current Px/Tx state. */
1512 	if (pr->flags.performance)
1513 		pr->limit.thermal.px = pr->performance.state;
1514 	if (pr->flags.throttling)
1515 		pr->limit.thermal.tx = pr->throttling.state;
1516 
1517 	/*
1518 	 * Our default policy is to only use throttling at the lowest
1519 	 * performance state.
1520 	 */
1521 
1522 	px = pr->limit.thermal.px;
1523 	tx = pr->limit.thermal.tx;
1524 
1525 	switch (type) {
1526 
1527 	case ACPI_PROCESSOR_LIMIT_NONE:
1528 		px = 0;
1529 		tx = 0;
1530 		break;
1531 
1532 	case ACPI_PROCESSOR_LIMIT_INCREMENT:
1533 		if (pr->flags.performance) {
1534 			if (px == (pr->performance.state_count - 1))
1535 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1536 					"At maximum performance state\n"));
1537 			else {
1538 				px++;
1539 				goto end;
1540 			}
1541 		}
1542 		if (pr->flags.throttling) {
1543 			if (tx == (pr->throttling.state_count - 1))
1544 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1545 					"At maximum throttling state\n"));
1546 			else
1547 				tx++;
1548 		}
1549 		break;
1550 
1551 	case ACPI_PROCESSOR_LIMIT_DECREMENT:
1552 		if (pr->flags.performance) {
1553 			if (px == pr->performance.platform_limit)
1554 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1555 					"At minimum performance state\n"));
1556 			else  {
1557 				px--;
1558 				goto end;
1559 			}
1560 		}
1561 		if (pr->flags.throttling) {
1562 			if (tx == 0)
1563 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1564 					"At minimum throttling state\n"));
1565 			else
1566 				tx--;
1567 		}
1568 		break;
1569 	}
1570 
1571 end:
1572 	pr->limit.thermal.px = px;
1573 	pr->limit.thermal.tx = tx;
1574 
1575 	result = acpi_processor_apply_limit(pr);
1576 	if (result)
1577 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1578 			"Unable to set thermal limit\n"));
1579 
1580 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
1581 		pr->limit.thermal.px,
1582 		pr->limit.thermal.tx));
1583 
1584 	return_VALUE(result);
1585 }
1586 
1587 
1588 static int
acpi_processor_get_limit_info(struct acpi_processor * pr)1589 acpi_processor_get_limit_info (
1590 	struct acpi_processor	*pr)
1591 {
1592 	ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
1593 
1594 	if (!pr)
1595 		return_VALUE(-EINVAL);
1596 
1597 	if (pr->flags.performance || pr->flags.throttling)
1598 		pr->flags.limit = 1;
1599 
1600 	return_VALUE(0);
1601 }
1602 
1603 
1604 /* --------------------------------------------------------------------------
1605                               FS Interface (/proc)
1606    -------------------------------------------------------------------------- */
1607 
1608 struct proc_dir_entry		*acpi_processor_dir = NULL;
1609 
1610 static int
acpi_processor_read_info(char * page,char ** start,off_t off,int count,int * eof,void * data)1611 acpi_processor_read_info (
1612 	char			*page,
1613 	char			**start,
1614 	off_t			off,
1615 	int 			count,
1616 	int 			*eof,
1617 	void			*data)
1618 {
1619 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1620 	char			*p = page;
1621 	int			len = 0;
1622 
1623 	ACPI_FUNCTION_TRACE("acpi_processor_read_info");
1624 
1625 	if (!pr || (off != 0))
1626 		goto end;
1627 
1628 	p += sprintf(p, "processor id:            %d\n",
1629 		pr->id);
1630 
1631 	p += sprintf(p, "acpi id:                 %d\n",
1632 		pr->acpi_id);
1633 
1634 	p += sprintf(p, "bus mastering control:   %s\n",
1635 		pr->flags.bm_control ? "yes" : "no");
1636 
1637 	p += sprintf(p, "power management:        %s\n",
1638 		pr->flags.power ? "yes" : "no");
1639 
1640 	p += sprintf(p, "throttling control:      %s\n",
1641 		pr->flags.throttling ? "yes" : "no");
1642 
1643 	p += sprintf(p, "performance management:  %s\n",
1644 		pr->flags.performance ? "yes" : "no");
1645 
1646 	p += sprintf(p, "limit interface:         %s\n",
1647 		pr->flags.limit ? "yes" : "no");
1648 
1649 end:
1650 	len = (p - page);
1651 	if (len <= off+count) *eof = 1;
1652 	*start = page + off;
1653 	len -= off;
1654 	if (len>count) len = count;
1655 	if (len<0) len = 0;
1656 
1657 	return_VALUE(len);
1658 }
1659 
1660 
1661 static int
acpi_processor_read_power(char * page,char ** start,off_t off,int count,int * eof,void * data)1662 acpi_processor_read_power (
1663 	char			*page,
1664 	char			**start,
1665 	off_t			off,
1666 	int 			count,
1667 	int 			*eof,
1668 	void			*data)
1669 {
1670 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1671 	char			*p = page;
1672 	int			len = 0;
1673 	int			i = 0;
1674 
1675 	ACPI_FUNCTION_TRACE("acpi_processor_read_power");
1676 
1677 	if (!pr || (off != 0))
1678 		goto end;
1679 
1680 	p += sprintf(p, "active state:            C%d\n",
1681 		pr->power.state);
1682 
1683 	p += sprintf(p, "default state:           C%d\n",
1684 		pr->power.default_state);
1685 
1686 	p += sprintf(p, "bus master activity:     %08x\n",
1687 		pr->power.bm_activity);
1688 
1689 	p += sprintf(p, "states:\n");
1690 
1691 	for (i=1; i<ACPI_C_STATE_COUNT; i++) {
1692 
1693 		p += sprintf(p, "   %cC%d:                  ",
1694 			(i == pr->power.state?'*':' '), i);
1695 
1696 		if (!pr->power.states[i].valid) {
1697 			p += sprintf(p, "<not supported>\n");
1698 			continue;
1699 		}
1700 
1701 		if (pr->power.states[i].promotion.state)
1702 			p += sprintf(p, "promotion[C%d] ",
1703 				pr->power.states[i].promotion.state);
1704 		else
1705 			p += sprintf(p, "promotion[--] ");
1706 
1707 		if (pr->power.states[i].demotion.state)
1708 			p += sprintf(p, "demotion[C%d] ",
1709 				pr->power.states[i].demotion.state);
1710 		else
1711 			p += sprintf(p, "demotion[--] ");
1712 
1713 		p += sprintf(p, "latency[%03d] usage[%08d]\n",
1714 			pr->power.states[i].latency,
1715 			pr->power.states[i].usage);
1716 	}
1717 
1718 end:
1719 	len = (p - page);
1720 	if (len <= off+count) *eof = 1;
1721 	*start = page + off;
1722 	len -= off;
1723 	if (len>count) len = count;
1724 	if (len<0) len = 0;
1725 
1726 	return_VALUE(len);
1727 }
1728 
1729 
1730 static int
acpi_processor_read_performance(char * page,char ** start,off_t off,int count,int * eof,void * data)1731 acpi_processor_read_performance (
1732 	char			*page,
1733 	char			**start,
1734 	off_t			off,
1735 	int 			count,
1736 	int 			*eof,
1737 	void			*data)
1738 {
1739 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1740 	char			*p = page;
1741 	int			len = 0;
1742 	int			i = 0;
1743 
1744 	ACPI_FUNCTION_TRACE("acpi_processor_read_performance");
1745 
1746 	if (!pr || (off != 0))
1747 		goto end;
1748 
1749 	if (!pr->flags.performance) {
1750 		p += sprintf(p, "<not supported>\n");
1751 		goto end;
1752 	}
1753 
1754 	p += sprintf(p, "state count:             %d\n",
1755 		pr->performance.state_count);
1756 
1757 	p += sprintf(p, "active state:            P%d\n",
1758 		pr->performance.state);
1759 
1760 	p += sprintf(p, "states:\n");
1761 
1762 	for (i=0; i<pr->performance.state_count; i++)
1763 		p += sprintf(p, "   %cP%d:                  %d MHz, %d mW, %d uS\n",
1764 			(i == pr->performance.state?'*':' '), i,
1765 			(u32) pr->performance.states[i].core_frequency,
1766 			(u32) pr->performance.states[i].power,
1767 			(u32) pr->performance.states[i].transition_latency);
1768 
1769 end:
1770 	len = (p - page);
1771 	if (len <= off+count) *eof = 1;
1772 	*start = page + off;
1773 	len -= off;
1774 	if (len>count) len = count;
1775 	if (len<0) len = 0;
1776 
1777 	return_VALUE(len);
1778 }
1779 
1780 
1781 static int
acpi_processor_write_performance(struct file * file,const char * buffer,unsigned long count,void * data)1782 acpi_processor_write_performance (
1783         struct file		*file,
1784         const char		*buffer,
1785         unsigned long		count,
1786         void			*data)
1787 {
1788 	int			result = 0;
1789 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1790 	char			state_string[12] = {'\0'};
1791 
1792 	ACPI_FUNCTION_TRACE("acpi_processor_write_performance");
1793 
1794 	if (!pr || (count > sizeof(state_string) - 1))
1795 		return_VALUE(-EINVAL);
1796 
1797 	if (copy_from_user(state_string, buffer, count))
1798 		return_VALUE(-EFAULT);
1799 
1800 	state_string[count] = '\0';
1801 
1802 	result = acpi_processor_set_performance(pr,
1803 		simple_strtoul(state_string, NULL, 0));
1804 	if (result)
1805 		return_VALUE(result);
1806 
1807 	return_VALUE(count);
1808 }
1809 
1810 
1811 static int
acpi_processor_read_throttling(char * page,char ** start,off_t off,int count,int * eof,void * data)1812 acpi_processor_read_throttling (
1813 	char			*page,
1814 	char			**start,
1815 	off_t			off,
1816 	int 			count,
1817 	int 			*eof,
1818 	void			*data)
1819 {
1820 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1821 	char			*p = page;
1822 	int			len = 0;
1823 	int			i = 0;
1824 	int                     result = 0;
1825 
1826 	ACPI_FUNCTION_TRACE("acpi_processor_read_throttling");
1827 
1828 	if (!pr || (off != 0))
1829 		goto end;
1830 
1831 	if (!(pr->throttling.state_count > 0)) {
1832 		p += sprintf(p, "<not supported>\n");
1833 		goto end;
1834 	}
1835 
1836 	result = acpi_processor_get_throttling(pr);
1837 
1838 	if (result) {
1839 		p += sprintf(p, "Could not determine current throttling state.\n");
1840 		goto end;
1841 	}
1842 
1843 	p += sprintf(p, "state count:             %d\n",
1844 		pr->throttling.state_count);
1845 
1846 	p += sprintf(p, "active state:            T%d\n",
1847 		pr->throttling.state);
1848 
1849 	p += sprintf(p, "states:\n");
1850 
1851 	for (i=0; i<pr->throttling.state_count; i++)
1852 		p += sprintf(p, "   %cT%d:                  %02d%%\n",
1853 			(i == pr->throttling.state?'*':' '), i,
1854 			(pr->throttling.states[i].performance?pr->throttling.states[i].performance/10:0));
1855 
1856 end:
1857 	len = (p - page);
1858 	if (len <= off+count) *eof = 1;
1859 	*start = page + off;
1860 	len -= off;
1861 	if (len>count) len = count;
1862 	if (len<0) len = 0;
1863 
1864 	return_VALUE(len);
1865 }
1866 
1867 
1868 static int
acpi_processor_write_throttling(struct file * file,const char * buffer,unsigned long count,void * data)1869 acpi_processor_write_throttling (
1870         struct file		*file,
1871         const char		*buffer,
1872         unsigned long		count,
1873         void			*data)
1874 {
1875 	int			result = 0;
1876 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1877 	char			state_string[12] = {'\0'};
1878 
1879 	ACPI_FUNCTION_TRACE("acpi_processor_write_throttling");
1880 
1881 	if (!pr || (count > sizeof(state_string) - 1))
1882 		return_VALUE(-EINVAL);
1883 
1884 	if (copy_from_user(state_string, buffer, count))
1885 		return_VALUE(-EFAULT);
1886 
1887 	state_string[count] = '\0';
1888 
1889 	result = acpi_processor_set_throttling(pr,
1890 		simple_strtoul(state_string, NULL, 0));
1891 	if (result)
1892 		return_VALUE(result);
1893 
1894 	return_VALUE(count);
1895 }
1896 
1897 
1898 static int
acpi_processor_read_limit(char * page,char ** start,off_t off,int count,int * eof,void * data)1899 acpi_processor_read_limit (
1900 	char			*page,
1901 	char			**start,
1902 	off_t			off,
1903 	int 			count,
1904 	int 			*eof,
1905 	void			*data)
1906 {
1907 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1908 	char			*p = page;
1909 	int			len = 0;
1910 
1911 	ACPI_FUNCTION_TRACE("acpi_processor_read_limit");
1912 
1913 	if (!pr || (off != 0))
1914 		goto end;
1915 
1916 	if (!pr->flags.limit) {
1917 		p += sprintf(p, "<not supported>\n");
1918 		goto end;
1919 	}
1920 
1921 	p += sprintf(p, "active limit:            P%d:T%d\n",
1922 		pr->limit.state.px, pr->limit.state.tx);
1923 
1924 	p += sprintf(p, "platform limit:          P%d:T0\n",
1925 		pr->flags.performance?pr->performance.platform_limit:0);
1926 
1927 	p += sprintf(p, "user limit:              P%d:T%d\n",
1928 		pr->limit.user.px, pr->limit.user.tx);
1929 
1930 	p += sprintf(p, "thermal limit:           P%d:T%d\n",
1931 		pr->limit.thermal.px, pr->limit.thermal.tx);
1932 
1933 end:
1934 	len = (p - page);
1935 	if (len <= off+count) *eof = 1;
1936 	*start = page + off;
1937 	len -= off;
1938 	if (len>count) len = count;
1939 	if (len<0) len = 0;
1940 
1941 	return_VALUE(len);
1942 }
1943 
1944 
1945 static int
acpi_processor_write_limit(struct file * file,const char * buffer,unsigned long count,void * data)1946 acpi_processor_write_limit (
1947         struct file		*file,
1948         const char		*buffer,
1949         unsigned long		count,
1950         void			*data)
1951 {
1952 	int			result = 0;
1953 	struct acpi_processor	*pr = (struct acpi_processor *) data;
1954 	char			limit_string[25] = {'\0'};
1955 	int			px = 0;
1956 	int			tx = 0;
1957 
1958 	ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
1959 
1960 	if (!pr || (count > sizeof(limit_string) - 1)) {
1961 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
1962 		return_VALUE(-EINVAL);
1963 	}
1964 
1965 	if (copy_from_user(limit_string, buffer, count)) {
1966 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
1967 		return_VALUE(-EFAULT);
1968 	}
1969 
1970 	limit_string[count] = '\0';
1971 
1972 	if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
1973 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
1974 		return_VALUE(-EINVAL);
1975 	}
1976 
1977 	if (pr->flags.performance) {
1978 		if ((px < pr->performance.platform_limit)
1979 			|| (px > (pr->performance.state_count - 1))) {
1980 			ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid px\n"));
1981 			return_VALUE(-EINVAL);
1982 		}
1983 		pr->limit.user.px = px;
1984 	}
1985 
1986 	if (pr->flags.throttling) {
1987 		if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
1988 			ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid tx\n"));
1989 			return_VALUE(-EINVAL);
1990 		}
1991 		pr->limit.user.tx = tx;
1992 	}
1993 
1994 	result = acpi_processor_apply_limit(pr);
1995 
1996 	return_VALUE(count);
1997 }
1998 
1999 
2000 static int
acpi_processor_add_fs(struct acpi_device * device)2001 acpi_processor_add_fs (
2002 	struct acpi_device	*device)
2003 {
2004 	struct proc_dir_entry	*entry = NULL;
2005 
2006 	ACPI_FUNCTION_TRACE("acpi_processor_add_fs");
2007 
2008 	if (!acpi_device_dir(device)) {
2009 		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
2010 			acpi_processor_dir);
2011 		if (!acpi_device_dir(device))
2012 			return_VALUE(-ENODEV);
2013 	}
2014 	acpi_device_dir(device)->owner = THIS_MODULE;
2015 
2016 	/* 'info' [R] */
2017 	entry = create_proc_entry(ACPI_PROCESSOR_FILE_INFO,
2018 		S_IRUGO, acpi_device_dir(device));
2019 	if (!entry)
2020 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2021 			"Unable to create '%s' fs entry\n",
2022 			ACPI_PROCESSOR_FILE_INFO));
2023 	else {
2024 		entry->read_proc = acpi_processor_read_info;
2025 		entry->data = acpi_driver_data(device);
2026 		entry->owner = THIS_MODULE;
2027 	}
2028 
2029 	/* 'power' [R] */
2030 	entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
2031 		S_IRUGO, acpi_device_dir(device));
2032 	if (!entry)
2033 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2034 			"Unable to create '%s' fs entry\n",
2035 			ACPI_PROCESSOR_FILE_POWER));
2036 	else {
2037 		entry->read_proc = acpi_processor_read_power;
2038 		entry->data = acpi_driver_data(device);
2039 		entry->owner = THIS_MODULE;
2040 	}
2041 
2042 	/* 'performance' [R/W] */
2043 	entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
2044 		S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
2045 	if (!entry)
2046 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2047 			"Unable to create '%s' fs entry\n",
2048 			ACPI_PROCESSOR_FILE_PERFORMANCE));
2049 	else {
2050 		entry->read_proc = acpi_processor_read_performance;
2051 		entry->write_proc = acpi_processor_write_performance;
2052 		entry->data = acpi_driver_data(device);
2053 		entry->owner = THIS_MODULE;
2054 	}
2055 
2056 	/* 'throttling' [R/W] */
2057 	entry = create_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
2058 		S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
2059 	if (!entry)
2060 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2061 			"Unable to create '%s' fs entry\n",
2062 			ACPI_PROCESSOR_FILE_THROTTLING));
2063 	else {
2064 		entry->read_proc = acpi_processor_read_throttling;
2065 		entry->write_proc = acpi_processor_write_throttling;
2066 		entry->data = acpi_driver_data(device);
2067 		entry->owner = THIS_MODULE;
2068 	}
2069 
2070 	/* 'limit' [R/W] */
2071 	entry = create_proc_entry(ACPI_PROCESSOR_FILE_LIMIT,
2072 		S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
2073 	if (!entry)
2074 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2075 			"Unable to create '%s' fs entry\n",
2076 			ACPI_PROCESSOR_FILE_LIMIT));
2077 	else {
2078 		entry->read_proc = acpi_processor_read_limit;
2079 		entry->write_proc = acpi_processor_write_limit;
2080 		entry->data = acpi_driver_data(device);
2081 		entry->owner = THIS_MODULE;
2082 	}
2083 
2084 	return_VALUE(0);
2085 }
2086 
2087 
2088 static int
acpi_processor_remove_fs(struct acpi_device * device)2089 acpi_processor_remove_fs (
2090 	struct acpi_device	*device)
2091 {
2092 	ACPI_FUNCTION_TRACE("acpi_processor_remove_fs");
2093 
2094 	if (acpi_device_dir(device)) {
2095 		remove_proc_entry(acpi_device_bid(device), acpi_processor_dir);
2096 		acpi_device_dir(device) = NULL;
2097 	}
2098 
2099 	return_VALUE(0);
2100 }
2101 
2102 
2103 /* --------------------------------------------------------------------------
2104                                  Driver Interface
2105    -------------------------------------------------------------------------- */
2106 
2107 static int
acpi_processor_get_info(struct acpi_processor * pr)2108 acpi_processor_get_info (
2109 	struct acpi_processor	*pr)
2110 {
2111 	acpi_status		status = 0;
2112 	union acpi_object	object = {0};
2113 	struct acpi_buffer	buffer = {sizeof(union acpi_object), &object};
2114 	static int		cpu_index = 0;
2115 
2116 	ACPI_FUNCTION_TRACE("acpi_processor_get_info");
2117 
2118 	if (!pr)
2119 		return_VALUE(-EINVAL);
2120 
2121 #ifdef CONFIG_SMP
2122 	if (smp_num_cpus > 1)
2123 		errata.smp = smp_num_cpus;
2124 
2125 	/*
2126 	 *  Extra Processor objects may be enumerated on MP systems with
2127 	 *  less than the max # of CPUs. They should be ignored.
2128 	 */
2129 	if ((cpu_index + 1) > smp_num_cpus)
2130 		return_VALUE(-ENODEV);
2131 #endif
2132 
2133 	acpi_processor_errata(pr);
2134 
2135 	/*
2136 	 * Check to see if we have bus mastering arbitration control.  This
2137 	 * is required for proper C3 usage (to maintain cache coherency).
2138 	 */
2139 	if (acpi_fadt.V1_pm2_cnt_blk && acpi_fadt.pm2_cnt_len) {
2140 		pr->flags.bm_control = 1;
2141 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2142 			"Bus mastering arbitration control present\n"));
2143 	}
2144 	else
2145 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2146 			"No bus mastering arbitration control\n"));
2147 
2148 	/*
2149 	 * Evalute the processor object.  Note that it is common on SMP to
2150 	 * have the first (boot) processor with a valid PBLK address while
2151 	 * all others have a NULL address.
2152 	 */
2153 	status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
2154 	if (ACPI_FAILURE(status)) {
2155 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2156 			"Error evaluating processor object\n"));
2157 		return_VALUE(-ENODEV);
2158 	}
2159 
2160 	/*
2161 	 * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
2162 	 *	>>> 'acpi_get_processor_id(acpi_id, &id)' in arch/xxx/acpi.c
2163 	 */
2164 	pr->id = cpu_index++;
2165 	pr->acpi_id = object.processor.proc_id;
2166 
2167 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
2168 		pr->acpi_id));
2169 
2170 	if (!object.processor.pblk_address)
2171 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
2172 	else if (object.processor.pblk_length != 6)
2173 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid PBLK length [%d]\n",
2174 			object.processor.pblk_length));
2175 	else {
2176 		pr->throttling.address = object.processor.pblk_address;
2177 		pr->throttling.duty_offset = acpi_fadt.duty_offset;
2178 		pr->throttling.duty_width = acpi_fadt.duty_width;
2179 		pr->power.states[ACPI_STATE_C2].address =
2180 			object.processor.pblk_address + 4;
2181 		pr->power.states[ACPI_STATE_C3].address =
2182 			object.processor.pblk_address + 5;
2183 	}
2184 
2185 	acpi_processor_get_power_info(pr);
2186 	acpi_processor_get_performance_info(pr);
2187 	acpi_processor_get_throttling_info(pr);
2188 	acpi_processor_get_limit_info(pr);
2189 
2190 	return_VALUE(0);
2191 }
2192 
2193 
2194 static void
acpi_processor_notify(acpi_handle handle,u32 event,void * data)2195 acpi_processor_notify (
2196 	acpi_handle		handle,
2197 	u32			event,
2198 	void			*data)
2199 {
2200 	int			result = 0;
2201 	struct acpi_processor	*pr = (struct acpi_processor *) data;
2202 	struct acpi_device	*device = NULL;
2203 
2204 	ACPI_FUNCTION_TRACE("acpi_processor_notify");
2205 
2206 	if (!pr)
2207 		return_VOID;
2208 
2209 	if (acpi_bus_get_device(pr->handle, &device))
2210 		return_VOID;
2211 
2212 	switch (event) {
2213 	case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
2214 		result = acpi_processor_get_platform_limit(pr);
2215 		if (!result)
2216 			acpi_processor_apply_limit(pr);
2217 
2218 		acpi_bus_generate_event(device, event,
2219 			pr->performance.platform_limit);
2220 		break;
2221 	case ACPI_PROCESSOR_NOTIFY_POWER:
2222 		/* TBD */
2223 		acpi_bus_generate_event(device, event, 0);
2224 		break;
2225 	default:
2226 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2227 			"Unsupported event [0x%x]\n", event));
2228 		break;
2229 	}
2230 
2231 	return_VOID;
2232 }
2233 
2234 
2235 static int
acpi_processor_add(struct acpi_device * device)2236 acpi_processor_add (
2237 	struct acpi_device	*device)
2238 {
2239 	int			result = 0;
2240 	acpi_status		status = AE_OK;
2241 	struct acpi_processor	*pr = NULL;
2242 	u32			i = 0;
2243 
2244 	ACPI_FUNCTION_TRACE("acpi_processor_add");
2245 
2246 	if (!device)
2247 		return_VALUE(-EINVAL);
2248 
2249 	pr = kmalloc(sizeof(struct acpi_processor), GFP_KERNEL);
2250 	if (!pr)
2251 		return_VALUE(-ENOMEM);
2252 	memset(pr, 0, sizeof(struct acpi_processor));
2253 
2254 	pr->handle = device->handle;
2255 	sprintf(acpi_device_name(device), "%s", ACPI_PROCESSOR_DEVICE_NAME);
2256 	sprintf(acpi_device_class(device), "%s", ACPI_PROCESSOR_CLASS);
2257 	acpi_driver_data(device) = pr;
2258 
2259 	result = acpi_processor_get_info(pr);
2260 	if (result)
2261 		goto end;
2262 
2263 	result = acpi_processor_add_fs(device);
2264 	if (result)
2265 		goto end;
2266 
2267 	status = acpi_install_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY,
2268 		acpi_processor_notify, pr);
2269 	if (ACPI_FAILURE(status)) {
2270 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2271 			"Error installing notify handler\n"));
2272 		result = -ENODEV;
2273 		goto end;
2274 	}
2275 
2276 	processors[pr->id] = pr;
2277 
2278 	/*
2279 	 * Install the idle handler if processor power management is supported.
2280 	 * Note that the default idle handler (default_idle) will be used on
2281 	 * platforms that only support C1.
2282 	 */
2283 	if ((pr->id == 0) && (pr->flags.power)) {
2284 		pm_idle_save = pm_idle;
2285 		pm_idle = acpi_processor_idle;
2286 	}
2287 
2288 	printk(KERN_INFO PREFIX "%s [%s] (supports",
2289 		acpi_device_name(device), acpi_device_bid(device));
2290 	for (i=1; i<ACPI_C_STATE_COUNT; i++)
2291 		if (pr->power.states[i].valid)
2292 			printk(" C%d", i);
2293 	if (pr->flags.performance)
2294 		printk(", %d performance states", pr->performance.state_count);
2295 	if (pr->flags.throttling)
2296 		printk(", %d throttling states", pr->throttling.state_count);
2297 	printk(")\n");
2298 
2299 end:
2300 	if (result) {
2301 		acpi_processor_remove_fs(device);
2302 		kfree(pr);
2303 	}
2304 
2305 	return_VALUE(result);
2306 }
2307 
2308 
2309 static int
acpi_processor_remove(struct acpi_device * device,int type)2310 acpi_processor_remove (
2311 	struct acpi_device	*device,
2312 	int			type)
2313 {
2314 	acpi_status		status = AE_OK;
2315 	struct acpi_processor	*pr = NULL;
2316 
2317 	ACPI_FUNCTION_TRACE("acpi_processor_remove");
2318 
2319 	if (!device || !acpi_driver_data(device))
2320 		return_VALUE(-EINVAL);
2321 
2322 	pr = (struct acpi_processor *) acpi_driver_data(device);
2323 
2324 	/* Unregister the idle handler when processor #0 is removed. */
2325 	if (pr->id == 0)
2326 		pm_idle = pm_idle_save;
2327 
2328 	status = acpi_remove_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY,
2329 		acpi_processor_notify);
2330 	if (ACPI_FAILURE(status)) {
2331 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2332 			"Error removing notify handler\n"));
2333 	}
2334 
2335 	acpi_processor_remove_fs(device);
2336 
2337 	processors[pr->id] = NULL;
2338 
2339 	kfree(pr);
2340 
2341 	return_VALUE(0);
2342 }
2343 
2344 
2345 static int __init
acpi_processor_init(void)2346 acpi_processor_init (void)
2347 {
2348 	int			result = 0;
2349 
2350 	ACPI_FUNCTION_TRACE("acpi_processor_init");
2351 
2352 	memset(&processors, 0, sizeof(processors));
2353 	memset(&errata, 0, sizeof(errata));
2354 
2355 	acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
2356 	if (!acpi_processor_dir)
2357 		return_VALUE(-ENODEV);
2358 	acpi_processor_dir->owner = THIS_MODULE;
2359 
2360 	result = acpi_bus_register_driver(&acpi_processor_driver);
2361 	if (result < 0) {
2362 		remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
2363 		return_VALUE(-ENODEV);
2364 	}
2365 
2366 	return_VALUE(0);
2367 }
2368 
2369 
2370 static void __exit
acpi_processor_exit(void)2371 acpi_processor_exit (void)
2372 {
2373 	ACPI_FUNCTION_TRACE("acpi_processor_exit");
2374 
2375 	acpi_bus_unregister_driver(&acpi_processor_driver);
2376 
2377 	remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
2378 
2379 	return_VOID;
2380 }
2381 
2382 
2383 module_init(acpi_processor_init);
2384 module_exit(acpi_processor_exit);
2385 
2386 EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
2387