Skip to contentSkip to conversation

amd76x_pm: C2 powersaving for AMD K7

PATCH In review Joerg Sommrey 4 participants 6 messages Sep 1, 2005 – Sep 7, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 amd76x_pm: C2 powersaving for AMD K7

This is a processor idle module for AMD SMP 760MP(X) based systems. The patch was originally written by Tony Lindgren and has been around since 2002. It enables C2 mode on AMD SMP systems and thus saves about 70 - 90 W of energy in the idle mode compared to the default idle mode. The idle function has been rewritten and is now free of locking issues and is independent from the number of CPUs. The impact from this module on the system clock and on i/o transfer rates has been reduced.

This patch can also be found at
http://www.sommrey.de/amd76x_pm/amd_76x_pm-2.6.13-1.patch

Documentation/amd76x_pm.txt +326 -0
diff -Nru linux-2.6.13/Documentation/amd76x_pm.txt linux-2.6.13-jo/Documentation/amd76x_pm.txt
@@ -0,0 +1,326 @@
+	ACPI style power management for SMP AMD-760MP(X) based systems
+	==============================================================
+
+For use until the ACPI project catches up. :-)
+
+Using this module saves about 70 - 90W of energy in the idle mode compared
+to the default idle mode. Waking up from the idle mode is fast to keep the
+system response time good. Currently no CPU load calculation is done, the
+system exits the idle mode after every C2 call.
+
+NOTE: Currently there's a bug somewhere where the reading the
+      P_LVL2 for the first time causes the system to sleep instead of 
+      idling. This means that you need to hit the power button once to
+      wake the system after loading the module for the first time after
+      reboot. After that the system idles as supposed.
+      (Only observed on Tony's system.)
+
+
+Influenced by Vcool, and LVCool. Rewrote everything from scratch to
+use the PCI features in Linux, and to support SMP systems.
+
+Currently tested amongst others on a TYAN S2460 (760MP) system (Tony), an
+ASUS A7M266-D (760MPX) system (Johnathan) and a TYAN S2466 (760MPX)
+system (Jo). Adding support for other Athlon SMP or single processor
+systems should be easy if desired.  
+
+The file /sys/devices/pci0000:00/0000:00:00.0/C2_cnt shows the number of
+C2 calls since module load.
+
+There are some parameters for tuning the behaviour of amd76x_pm:
+lazy_idle, spin_idle, watch_irqs, watch_int and min_C1
+
+lazy_idle and spin_idle are closely related:
+
+- lazy_idle defines the number of idle calls into amd76x_smp_idle that are
+  needed to *enable* C2 mode.  This parameter is the maximum loop counter
+  for an outer loop with interrupts enabled that guarantees low latencies. 
+  The default for lazy_idle is 512.
+
+- spin_idle defines the maximum number of spin cycles in an inner idle loop
+  where one CPU waits for all others to get into C2-enabled mode. When all
+  CPUs are in C2-enable mode they (more ore less) simultaneously *enter* C2
+  mode. In this inner loop interrupts are disabled.  The loop is left
+  immediatly if there is something waiting to be scheduled.  The default
+  for spin_idle is 2*lazy_idle.
+
+lazy_idle and spin_idle define a "rubber measure" for the idling
+behaviour: lazy_idle defines the minimum "idling" needed to enter C2 and
+spin_idle defines when to give up.
+
+Low values for lazy_idle and high values for spin_idle give better
+cooling.  Higher values for lazy_idle simply give less cooling.  spin_idle
+is a kind of emergency break to leave C2-enable mode if CPUs don't
+synchronize.
+
+Interrupts are disabled in C2 mode.  The CPUs are woken up by timer
+interrups or by NMIs.  This causes a high interrupt latency for other
+interrupts that leads to a significant reduction in io or network
+throughput.  There has been introduced a "irq rate watcher" to reduce
+this effect.  If the irq rate watcher detects that an interrupt has a
+rate above a given limit, C2 idling is disabled and a low latency C1
+idling mode is used instead. The parameters watch_irqs, watch_int and
+min_C1 control this irq rate watcher:
+
+- watch_irqs defines which interrupts are to be watched and optionally
+  at which interrupt rate C2 mode shall be disabled.  The syntax for 
+  watch_irqs is irq1[:rate1],irq2[:rate2],...  The rate is measured in
+  interrupts per second and defaults to 128.  There is no default for
+  watch_irqs.  To enable the irq rate watcher you must specify this
+  parameter.  Enter the interrupts used by disk controllers or network
+  adapters here.
+  
+- watch_int defines the time interval (in milliseconds) at which the
+  interrupt rate is checked.  Too low values may result in an overhead
+  and too high values cause the C1 mode to "kick in" later.  The default
+  for watch_int is 1 second.
+
+- min_C1 defines the mininum number of check intervals with low
+  interrupt rates that are needed to leave the forced C1 mode.
+
+All parameters lazy_idle, spin_idle, watch_irqs and watch_int may be
+given as module parameters to amd76x_pm.  Furthermore, they may be
+queried or changed via their sysfs entries in
+/sys/module/amd76x_pm/parameters.  (The file location in sysfs has
+changed from earlier versions!)
+
+Setting watch_int to zero or removing all interrupts from watch_irqs
+causes the irq rate watcher to stop.  The module needs to be reloaded to
+start the watcher again.
+
+NB: In the past there was a major impact from amd76x_pm on the system
+clock stability.  At least on my box this effect has been reduced
+noticable.  This is caused by two changes:
+
+- The redesign of the idle function lead to a reduced time spent in
+  amd76x_smp_idle() and this propably caused lower latencies.
+  
+- The introduction of the irq rate watcher significantly reduced
+  latencies under load.
+
+Some hints on tuning lazy_idle and spin_idle:
+Watch the rate of C2 calls per second.  Anything below HZ seems to be
+completely useless.  Start with a moderate low value for lazy_idle, e.g.
+2^5 and a very high value for spin_idle, e.g. 2^20.  Then double lazy_idle
+until you see a significant increase in core temperature or C2 rate. 
+Go back to the last "good" value.  Then halve spin_idle until you see
+a decrease in C2 rate or a raising core temperature again and go back
+to the previous value.  There's a small program c2rate.c attached at the
+end of this file that you may use to watch the C2 rate.  The rtc driver
+must be enabled to use it.
+
+There is an -extra patch available that adds some optional features:
+- C3 idling: this works but is not well tested.
+- normal throttling: untested for a long time.
+- Power On Suspend: very experimental and untested.
+
+This software is licensed under GNU General Public License Version 2 
+as specified in file COPYING in the Linux kernel source tree main 
+directory.
+
+Copyright (C) 2002 - 2005	Johnathan Hicks <[email protected]>
+				Tony Lindgren <[email protected]>
+				Joerg Sommrey <[email protected]>
+
+History:
+
+  20020702 - amd-smp-idle: Tony Lindgren <[email protected]>
+Influenced by Vcool, and LVCool. Rewrote everything from scratch to
+use the PCI features in Linux, and to support SMP systems. Provides
+C2 idling on SMP AMD-760MP systems.
+
+  20020722: JH
+  	I adapted Tony's code for the AMD-765/766 southbridge and adapted it
+  	according to the AMD-768 data sheet to provide the same capability for
+  	SMP AMD-760MPX systems. Posted to acpi-devel list.
+  	
+  20020722: Alan Cox
+  	Replaces non-functional amd76x_pm code in -ac tree.
+  	
+  20020730: JH
+  	Added ability to do normal throttling (the non-thermal kind), C3 idling
+  	and Power On Suspend (S1 sleep). It would be very easy to tie swsusp
+  	into activate_amd76x_SLP(). C3 idling doesn't happen yet; see my note
+  	in amd76x_smp_idle(). I've noticed that when NTH and idling are both
+  	enabled, my hardware locks and requires a hard reset, so I have
+  	#ifndefed around the idle loop setting to prevent this. POS locks it up
+  	too, both ought to be fixable. I've also noticed that idling and NTH
+  	make some interference that is picked up by the onboard sound chip on
+  	my ASUS A7M266-D motherboard.
+
+  20030601: Pasi Savolainen
+     Simple port to 2.5
+     Added sysfs interface for making nice graphs with mrtg.
+     Look for /sys/devices/pci0/00:00.0/C2_cnt & lazy_idle (latter writable)
+
+  20050601: Joerg Sommrey (jo)
+     2.6 stuff
+     redesigned amd76x_smp_idle.  The algorithm is basically the same
+        but the implementation has changed.  This part is now independent
+        from the number of CPUs and data is locked against concurrent
+        updates from different CPUs.
+     use _smp_processor_id()
+     use cpu_idle_wait()
+     NTH and POS code not touched and not tested.
+
+  20050621: jo
+     separated C3, NTH and POS code into extra patch
+
+  20050812: jo
+     rewritten amd76x_smp_idle completely. It's much simpler now but
+     does a good job - the KISS approach.  Introduced a new tunable
+     spin_idle.
+
+  20050819: jo
+     new irq rate watcher task that forces C1-idling if irq-rate exceeds a
+     given limit.
+
+  20050820: jo
+     make all modules parameters r/w in sysfs.
+
+  20050830: jo
+     some API changes for 2.6.13
+  
+TODO: Thermal throttling (TTH).
+	 /proc interface for normal throttling level.
+	 /proc interface for POS.
+
+---- c2rate.c ---- snip here ----------------------------------------
+/* c2rate.c
+ * usage: c2rate [interval]
+ * interval is the time in seconds between two calculations and defaults
+ * to 16
+ * You may #define C3 if you're playing with C3 mode.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <linux/rtc.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define C2_CNT "/sys/devices/pci0000:00/0000:00:00.0/C2_cnt"
+#define C3_CNT "/sys/devices/pci0000:00/0000:00:00.0/C3_cnt"
+// #define C3
+int fd;
+
+void
+disable_rtc(void) {
+	int retval;
+	/* Disable alarm interrupts */
+	retval = ioctl(fd, RTC_UIE_OFF, 0);
+	if (retval == -1) {
+		perror("ioctl(RTC_UIE_OFF)");
+		_exit(errno);
+	}
+	close(fd);
+}
+
+int
+main(int argc, char **argv)
+{
+	int retval;
+	unsigned long data;
+	char idlebuffer[64];
+	int idlefd;
+	ssize_t bytes;
+	unsigned long  lastcount;
+	unsigned long  irqcount;
+	unsigned long c2_old, c2_new;
+#ifdef C3
+	unsigned long c3_old, c3_new;
+#endif
+	int rate = 0;
+	int firstloop = 1;
+
+	if(argc > 1)
+		rate = atoi(argv[1]);
+	if (rate <= 0)
+		rate = 16;
+
+	fd = open ("/dev/rtc", O_RDONLY);
+
+	if (fd ==  -1) {
+		perror("/dev/rtc");
+		exit(errno);
+	}
+
+	/* Set the alarm to every second */
+
+	retval = ioctl(fd, RTC_UIE_ON, 0);
+	if (retval == -1) {
+		perror("ioctl(RTC_UIE_ON)");
+		exit(errno);
+	}
+	atexit(disable_rtc);
+
+	irqcount = 0;
+	while (1) {
+		/* This blocks until the alarm ring causes an interrupt */
+		retval = read(fd, &data, sizeof data);
+		if (retval == -1) {
+			perror("read");
+			exit(errno);
+		}
+		if (firstloop) {
+			idlebuffer[0] = '\0';
+			idlefd = open(C2_CNT, O_RDONLY);
+			if (idlefd != -1) {
+				bytes = read(idlefd, idlebuffer, sizeof idlebuffer);
+				close(idlefd);
+				idlebuffer[bytes] = '\0';
+			}
+			c2_old = strtoul(idlebuffer, NULL, 10);
+#ifdef C3
+			idlebuffer[0] = '\0';
+			idlefd = open(C3_CNT, O_RDONLY);
+			if (idlefd != -1) {
+				bytes = read(idlefd, idlebuffer, sizeof idlebuffer);
+				close(idlefd);
+				idlebuffer[bytes] = '\0';
+			}
+			c3_old = strtoul(idlebuffer, NULL, 10);
+#endif
+			firstloop = 0;
+			continue;
+		}
+		lastcount = data >> 8;
+		irqcount += lastcount;
+
+		if (irqcount >= rate) {
+			idlebuffer[0] = '\0';
+			idlefd = open(C2_CNT, O_RDONLY);
+			if (idlefd != -1) {
+				bytes = read(idlefd, idlebuffer, sizeof idlebuffer);
+				close(idlefd);
+				idlebuffer[bytes] = '\0';
+			}
+			c2_new = strtoul(idlebuffer, NULL, 10);
+#ifdef C3
+			idlebuffer[0] = '\0';
+			idlefd = open(C3_CNT, O_RDONLY);
+			if (idlefd != -1) {
+				bytes = read(idlefd, idlebuffer, sizeof idlebuffer);
+				close(idlefd);
+				idlebuffer[bytes] = '\0';
+			}
+			c3_new = strtoul(idlebuffer, NULL, 10);
+			printf("%lu\t%lu\n", (c2_new - c2_old) / irqcount,
+					(c3_new - c3_old) / irqcount);
+			c3_old = c3_new;
+#else
+			printf("%lu\n", (c2_new - c2_old) / irqcount);
+#endif
+			c2_old = c2_new;
+			irqcount = 0;
+		}
+
+	}
+	
+	return 0;
+
+} /* end main */
+---- c2rate.c ---- snip here ----------------------------------------
drivers/acpi/Kconfig +14 -0
diff -Nru linux-2.6.13/drivers/acpi/Kconfig linux-2.6.13-jo/drivers/acpi/Kconfig
@@ -365,4 +365,18 @@
 		$>modprobe acpi_memhotplug 
 endif	# ACPI
 
+config AMD76X_PM
+        tristate "AMD76x Native Power Management support"
+        default n
+        depends on X86 && PCI
+        ---help---
+          This driver enables Power Management on AMD760MP & AMD760MPX chipsets.          This is about same as ACPI C2, except that ACPI folks don't do SMP ATM.
+          See Documentation/amd76x_pm.txt for further details.
+
+          To compile this driver as a module ( = code which can be inserted in
+          and removed from the running kernel whenever you want), say M
+          here.  The module will be called amd76x_pm.
+
+          If unsure, say N.
+
 endmenu
drivers/acpi/Makefile +5 -0
diff -Nru linux-2.6.13/drivers/acpi/Makefile linux-2.6.13-jo/drivers/acpi/Makefile
@@ -57,3 +57,8 @@
 obj-$(CONFIG_ACPI_TOSHIBA)	+= toshiba_acpi.o
 obj-$(CONFIG_ACPI_BUS)		+= scan.o motherboard.o
 obj-$(CONFIG_ACPI_HOTPLUG_MEMORY)	+= acpi_memhotplug.o
+
+#
+# not really ACPI thing, until they handle SMP.
+#
+obj-$(CONFIG_AMD76X_PM)               += amd76x_pm.o
drivers/acpi/amd76x_pm.c +717 -0

717 lines not on this page · raw message

include/linux/amd76x_pm.h +63 -0

63 lines not on this page · raw message

#1 lore raw

Hi!

Documentation/amd76x_pm.txt line 16

Quoted 6 lines
Documentation/amd76x_pm.txt +6 -0
+NOTE: Currently there's a bug somewhere where the reading the
+      P_LVL2 for the first time causes the system to sleep instead of 
+      idling. This means that you need to hit the power button once to
+      wake the system after loading the module for the first time after
+      reboot. After that the system idles as supposed.
+      (Only observed on Tony's system.)

Could you fix this before merge?

Also remove changelog from .c file.

Signature · 1 line
64 bytes from 195.113.31.123: icmp_seq=28 ttl=51 time=448769.1 ms         
#2 lore raw
Quoted 11 lines · Pavel Machek

Hi!

Documentation/amd76x_pm.txt line 16

Quoted 6 lines
Documentation/amd76x_pm.txt +6 -0
+NOTE: Currently there's a bug somewhere where the reading the
+      P_LVL2 for the first time causes the system to sleep instead of 
+      idling. This means that you need to hit the power button once to
+      wake the system after loading the module for the first time after
+      reboot. After that the system idles as supposed.
+      (Only observed on Tony's system.)

Could you fix this before merge?

I think this is some BIOS issue or hardware bug. It happens only on Tyan S2460. I tried dumping the registers few years ago on my Tyan s2460, but no luck.

Low chance for anybody fixing it...

Tony

#3 lore raw

Hi!

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Documentation/amd76x_pm.txt +6 -0
+NOTE: Currently there's a bug somewhere where the reading the
+      P_LVL2 for the first time causes the system to sleep instead of 
+      idling. This means that you need to hit the power button once to
+      wake the system after loading the module for the first time after
+      reboot. After that the system idles as supposed.
+      (Only observed on Tony's system.)
Quoted 8 lines
Quoted 1 line

Could you fix this before merge?

I think this is some BIOS issue or hardware bug. It happens only on Tyan S2460. I tried dumping the registers few years ago on my Tyan s2460, but no luck.

Low chance for anybody fixing it...

So at least DMI-blacklist it...
Pavel

Signature · 1 line
64 bytes from 195.113.31.123: icmp_seq=28 ttl=51 time=448769.1 ms         
#4 lore raw
Quoted 11 lines · Pavel Machek

Hi!

Quoted 9 lines
Quoted 8 lines

Documentation/amd76x_pm.txt line 16

Quoted 6 lines
Documentation/amd76x_pm.txt +6 -0
+NOTE: Currently there's a bug somewhere where the reading the
+      P_LVL2 for the first time causes the system to sleep instead of 
+      idling. This means that you need to hit the power button once to
+      wake the system after loading the module for the first time after
+      reboot. After that the system idles as supposed.
+      (Only observed on Tony's system.)

Could you fix this before merge?

Quoted 8 lines
Quoted 6 lines

I think this is some BIOS issue or hardware bug. It happens only on Tyan S2460. I tried dumping the registers few years ago on my Tyan s2460, but no luck.

Low chance for anybody fixing it...

So at least DMI-blacklist it...

I rarely have access to that hardware, so don't count on me doing this...

Tony

#5 lore raw
Quoted 16 lines · Tony Lindgren
Quoted 15 lines · Pavel Machek

Hi!

Quoted 13 lines
Quoted 8 lines

Documentation/amd76x_pm.txt line 16

Quoted 6 lines
Documentation/amd76x_pm.txt +6 -0
+NOTE: Currently there's a bug somewhere where the reading the
+      P_LVL2 for the first time causes the system to sleep instead of 
+      idling. This means that you need to hit the power button once to
+      wake the system after loading the module for the first time after
+      reboot. After that the system idles as supposed.
+      (Only observed on Tony's system.)

Could you fix this before merge?

I think this is some BIOS issue or hardware bug. It happens only on Tyan S2460. I tried dumping the registers few years ago on my Tyan s2460, but no luck.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Low chance for anybody fixing it...

So at least DMI-blacklist it...

I rarely have access to that hardware, so don't count on me doing this...

I'm unable to do this neither. Looks like this won't be fixed.

-jo

Signature · 1 line
-rw-r--r--  1 jo users 63 2005-09-06 20:21 /home/jo/.signature