diff -Nru linux-2.6.15/Documentation/amd76x_pm.txt linux-2.6.15-jo/Documentation/amd76x_pm.txt
--- linux-2.6.15/Documentation/amd76x_pm.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.15-jo/Documentation/amd76x_pm.txt	2006-02-15 22:04:31.000000000 +0100
@@ -0,0 +1,254 @@
+	ACPI style power management for SMP AMD-760MP(X) based systems
+	==============================================================
+
+The ACPI project now supports C3 for SMP.  However, to get AMD K7
+into C2/C3 some registers need to be adjusted.  This should be done by
+the BIOS, but it isn't on known boards.  You may read this as: "This
+feature is not supported".  Because of this, there is no C2/C3 support
+from the ACPI processor module and there will be probably none in the
+future.
+
+Using the module is regarded as dangerous.
+USE THIS SOFTWARE ON YOUR OWN RISK.
+
+When this module is loaded and properly parameterised it saves about
+70 - 90 W 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. 
+
+This code runs on two-way AMD boxes at the moment, but the basic idling
+algorithm is independent of the number of CPUs.
+
+Known issues:
+-------------
+- 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 - S2460.)
+
+- There might be reduced throughput of disk or network devices.  See
+  below for how to configure the "irq rate watcher" to avoid this.
+
+- Occasional hard lockups might be caused by amd76x_pm.  I'm still working
+  on this issue.
+
+- I have reports about a buzzing sound from the mainboard when amd76x_pm
+  is loaded and some reports about unclean audio playback.
+
+Influenced by Vcool, and LVCool. Rewrote everything from scratch to
+use the PCI features in Linux, and to support SMP systems. (Tony)
+
+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), success reports from TYAN S2462, MSI K7D, ASUS A7M266-D,
+Gigabyte GA-7DPXDW+. 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/sleep_cnt shows the number of
+C2/C3 calls (per CPU) since module load and in
+/sys/devices/pci0000:00/0000:00:00.0/sleep_time you find an average
+(per CPU) over the time spent in C2/C3 mode for the last few seconds
+(measured in microseconds).
+sleep_cnt should always be equal for all CPUs - otherwise there's a bug
+somewhere.  sleep_time will only be updated when going into C2/C3, i.e.
+if sleep_cnt doesn't change, sleep_time is not valid.
+
+There are some parameters for tuning the behaviour of amd76x_pm:
+mode, lazy_idle, spin_idle, watch_irqs, watch_limits, watch_int and min_C1
+
+- mode lets you select between C2 and C3 idling.  There are two main
+  differences between C2 and C3 idling: power consumption in C3 is even
+  lower than in C2 and "cache spoofing" is disabled in C3.  This means:
+  DMA bus mastering activities are still possible in C2 and cause a
+  wakeup in C3.  As this wakeup takes a few cycles you shall expect some
+  latencies with DMA in C3.  C2 mode is the default.  The mode cannot be
+  changed at runtime.
+
+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/C3 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 sleep-enable mode. When
+  all CPUs are sleep-enabled and achieve a commitment about this state,
+  then all CPUs enter C2/C3.  Otherwise none will try to enter
+  powersaving mode.  In this inner loop interrupts are disabled, thus
+  causing higher interrupt latencies.  The default for spin_idle is
+  4*lazy_idle.
+
+lazy_idle and spin_idle define a "rubber measure" for the idling
+behaviour: lazy_idle defines the minimum idle cycles needed to enter
+C2/C3 and spin_idle defines when to give up.  The defaults are chosen
+very conservative.  To benefit from amd76x_pm you propably need to
+tune them to your needs.
+
+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 sleep-enable mode if CPUs don't
+synchronize.
+
+irq rate watcher:
+-----------------
+Interrupts are disabled in C2/C3 mode.  CPUs are woken up by timer
+interrupts or by NMIs (and DMA bus mastering in case of C3).  This causes
+high interrupt latencies for non-timer interrupts that might lead to a
+significant reduction in i/o or network throughput.  There has been
+introduced an "irq rate watcher" to get rid of this issue.  If the irq
+rate watcher detects that an interrupt has a rate above a given limit,
+C2/C3 idling will be disabled and a low latency C1 idling mode is used
+instead (HLT instruction). The parameters watch_irqs, watch_limits,
+watch_int and min_C1 control this irq rate watcher: 
+
+- watch_irqs defines which interrupts are to be watched.  This is a
+  comma separated list of interrupts.  To enable the irq rate watcher
+  you must specify this parameter.  You may provide the interrupts used
+  by disk controllers or network adapters here.
+  
+- watch_limits defines at which interrupt rate C2/C3 mode shall be
+  disabled.  This is a list of comma separated interrupt rates per
+  second.  To enable the irq rate watcher you must specify this
+  parameter.  Watch the interrupt rates for those devices you are
+  interested in and choose a limit that discrimitates idle from busy
+  state.
+
+- 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.
+
+The parameters lazy_idle, spin_idle, watch_irqs, watch_limits,
+watch_int and mode may be given as module parameters to amd76x_pm and
+queried via their sysfs entries in /sys/module/amd76x_pm/parameters.
+All parameters except mode may be altered via sysfs.
+
+To get the irq watcher started, watch_int, watch_irqs and watch_limits
+must all be provided.  If any of these parameters is cleared via sysfs,
+the irq rate watcher stopps and cannot be restarted (unless the module
+is reloaded).
+
+Some hints on tuning lazy_idle and spin_idle:
+Watch sleep_time in sysfs on an almost idle system.  Start with a low
+value for lazy_idle and spin_idle, e.g. 8.  Raise spin_idle, e.g. by
+doubling. This should cause a raise in sleep_time too.  Stop when
+there's no more change in sleep_time.  Then raise lazy_idle, e.g. by
+doubling.  If this causes a sleep_time reduction, try to raise
+spin_idle.  Stop raising lazy_idle when you cannot compensate the sleep
+time reduction with a spin_idle raise.  (You may use this as a starting
+point for furter fine tuning.) 
+
+If you want to do further experiments this field: There is another
+version of amd76x_pm.c that has experimental und untested support for
+"normal throttling" and "power on suspend".  See
+http://www.sommrey.de/amd76x_pm
+
+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		Johnathan Hicks <thetech@folkwolf.net>
+				Tony Lindgren <tony@atomide.com>
+
+Copyright (C) 2005 - 2006	Joerg Sommrey <jo@sommrey.de>
+
+History:
+
+  20020702 - amd-smp-idle: Tony Lindgren <tony@atomide.com>
+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.
+
+  20050906: jo
+     avoid some local_irq_disable/enable
+
+  20060108: jo
+     using percpu-variables
+
+  20060110: jo
+     eliminated a race condition in the idle loop.  Thanks to Arjan van
+     de Ven, who pointed to this issue.
+  
+  20060121: jo
+     Switched from C2 to C3 idling, inspired by processor_idle.c
+     C2 idling has just been replaced by C3 idling, there is no C2/C3
+     transition.
+     Restoring all touched northbridge and southbridge register bits to
+     their original values on module unload.
+
+  20060129: jo
+     Major redesign of the idle loop again.  Now there is a two-phase
+     process to make sure all or no CPUs go into C3.  In phase one each
+     CPU waits for the other to become idle.  In phase two all CPUs come
+     to an agreement about going C3 or not.  Now using spinlocks.
+     Simplicity has gone :-(
+
+  20060202: jo
+     Make the source conforming to the kernel coding style. Remove some
+     minor buglets.
+
+  20060206: jo
+     Code redesign for PCI register setup
+     C2/C3 mode selection
+
+  20060214: jo
+    - Redesigned the idle function again.  Now there shouldn't be any
+      situations left where not all CPUs try to enter C2/C3.
+    - Removed the (ugly) parsing of watch_irqs.  Using two separate arrays
+      watch_irqs and watch_limits now.
+    - sleep_time calculation and sysfs entries added.
diff -Nru linux-2.6.15/drivers/acpi/Kconfig linux-2.6.15-jo/drivers/acpi/Kconfig
--- linux-2.6.15/drivers/acpi/Kconfig	2006-01-03 21:07:59.000000000 +0100
+++ linux-2.6.15-jo/drivers/acpi/Kconfig	2006-02-15 21:03:17.000000000 +0100
@@ -340,4 +340,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 C3, but ACPI doesn't
+	  support known AMD760MP(X) boards.
+	  See Documentation/amd76x_pm.txt for details.
+
+	  Say M here to build a module called amd76x_pm.
+
+	  If unsure, say N.
+
 endmenu
diff -Nru linux-2.6.15/drivers/acpi/Makefile linux-2.6.15-jo/drivers/acpi/Makefile
--- linux-2.6.15/drivers/acpi/Makefile	2006-01-03 21:07:59.000000000 +0100
+++ linux-2.6.15-jo/drivers/acpi/Makefile	2006-01-22 12:34:17.000000000 +0100
@@ -57,3 +57,8 @@
 obj-$(CONFIG_ACPI_TOSHIBA)	+= toshiba_acpi.o
 obj-y				+= scan.o motherboard.o
 obj-$(CONFIG_ACPI_HOTPLUG_MEMORY)	+= acpi_memhotplug.o
+
+#
+# not really ACPI thing, but closely related
+#
+obj-$(CONFIG_AMD76X_PM)               += amd76x_pm.o
diff -Nru linux-2.6.15/drivers/acpi/amd76x_pm.c linux-2.6.15-jo/drivers/acpi/amd76x_pm.c
--- linux-2.6.15/drivers/acpi/amd76x_pm.c	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.15-jo/drivers/acpi/amd76x_pm.c	2006-02-15 21:21:29.000000000 +0100
@@ -0,0 +1,813 @@
+/*
+ * ACPI style PM for SMP AMD-760MP(X) based systems.
+ *
+ * Copyright (C) 2002		Johnathan Hicks <thetech@folkwolf.net>
+ * 				Tony Lindgren <tony@atomide.com>
+ *
+ * Copyright (C) 2005-2006	Joerg Sommrey <jo@sommrey.de>
+ *
+ * See Documentation/amd76x_pm.txt for details.
+ *
+ * This software is licensed under GNU General Public License Version 2
+ * as specified in file COPYING in the Linux kernel source tree main
+ * directory.
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/delay.h>
+#include <linux/pm.h>
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/version.h>
+#include <linux/kernel.h>
+#include <linux/workqueue.h>
+#include <linux/jiffies.h>
+#include <linux/kernel_stat.h>
+#include <linux/spinlock.h>
+#include <linux/percpu.h>
+#include <linux/amd76x_pm.h>
+#include <asm/atomic.h>
+
+#define VERSION	"20060214"
+
+//#define AMD76X_LOG_C1 1
+
+static void amd76x_smp_idle(void);
+static int amd76x_pm_main(void);
+
+static unsigned long lazy_idle;
+static unsigned long spin_idle;
+static unsigned long watch_int;
+static unsigned long min_C1 = AMD76X_MIN_C1;
+static unsigned int sb_id;
+static unsigned int nb_id;
+static char modeparm[] = AMD76X_SLEEP_MODE;
+static int mode;
+static int watch_irqs[AMD76X_WATCH_MAX];
+static int nr_watch_irqs;
+static int watch_limits[AMD76X_WATCH_MAX];
+static int nr_watch_limits;
+static int irq_count[AMD76X_WATCH_MAX];
+
+module_param(lazy_idle, long, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(lazy_idle,
+	" number of idle cycles before entering power saving mode");
+
+module_param(spin_idle, long, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(spin_idle,
+	" number of spin cycles to wait for other CPUs to become idle");
+
+module_param(watch_int, long, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(watch_int,
+		" watch interval (in milliseconds) for interrupts");
+
+module_param_string(mode, modeparm, sizeof modeparm, S_IRUGO);
+MODULE_PARM_DESC(mode, " idling mode (C2 or C3)");
+
+module_param_array(watch_irqs, int, &nr_watch_irqs, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(watch_irqs,
+		" list of irqs to be watched");
+
+module_param_array(watch_limits, int, &nr_watch_limits, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(watch_limits,
+		" interrupt rate that causes fallback to C1 idling if"
+		" corresponding interrupt exceeds this rate");
+
+module_param(min_C1, long, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(min_C1,
+		" minimum irq rate watch intervals spent in C1 mode");
+
+MODULE_AUTHOR("Tony Lindgren, Johnathan Hicks, Joerg Sommrey, others");
+MODULE_DESCRIPTION("ACPI style power management for SMP AMD-760MP(X) "
+		"based systems, version " VERSION);
+
+
+static struct pci_dev *pdev_nb;
+static struct pci_dev *pdev_sb;
+
+struct PM_cfg {
+	void (*orig_idle) (void);
+	void (*curr_idle) (void);
+};
+
+static struct PM_cfg amd76x_pm_cfg;
+
+struct PM_slp {
+	unsigned int sleep_reg;
+	unsigned int status_reg;
+	unsigned int tmr_reg;
+};
+
+static struct PM_slp amd76x_pm_slp __read_mostly;
+
+struct cpu_stat {
+	int idle_count;
+	int sleep_cnt;
+	unsigned long sleep_ticks;
+	unsigned long last;
+};
+
+struct idle_stat {
+	rwlock_t inclock;
+	rwlock_t declock;
+	atomic_t num_idle;
+	atomic_t num_sleep;
+};
+
+static struct idle_stat amd76x_stat __cacheline_aligned_in_smp = {
+	.inclock = RW_LOCK_UNLOCKED,
+	.declock = RW_LOCK_UNLOCKED,
+	.num_idle = ATOMIC_INIT(0),
+	.num_sleep = ATOMIC_INIT(0),
+};
+
+static void *prs_ref __read_mostly;
+
+/* need a member for every dev/reg combination used in
+ * amd76x_bits_*-macros
+ */
+struct reg_save {
+	unsigned int nb58;
+	unsigned int nb60;
+	unsigned int nb68;
+	unsigned int nb70;
+	unsigned int sb41;
+	unsigned int sb4f;
+	unsigned int sb50;
+};
+
+/* used in amd76x_bits_*-macros to save register contents */
+static struct reg_save amd76x_saved;
+
+static int force_C1 __read_mostly;
+
+static struct work_struct work;
+static void watch_irq(void *);
+
+static DECLARE_WORK(work, watch_irq, NULL);
+
+static struct pci_device_id __devinitdata amd_nb_tbl[] = {
+	{PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C, PCI_ANY_ID,
+		PCI_ANY_ID,},
+	{0,}
+};
+
+static struct pci_device_id __devinitdata amd_sb_tbl[] = {
+	{PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413, PCI_ANY_ID,
+		PCI_ANY_ID,},
+	{PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7443, PCI_ANY_ID,
+		PCI_ANY_ID,},
+	{0,}
+};
+
+/*
+ * Configures the AMD-762 northbridge to support PM calls
+ */
+static int config_amd762(int enable)
+{
+	unsigned int regdword;
+
+	/* Enable/disable STPGNT in BIU Status/Control for cpu0,
+	 * page 64 in AMD-762 doc */
+	pci_read_config_dword(pdev_nb, 0x60, &regdword);
+	if (enable)
+		amd76x_bits_set_store(nb, 60, STP_GRANT_DISCON_EN, regdword);
+	else
+		amd76x_bits_restore(nb, 60, STP_GRANT_DISCON_EN, regdword);
+	pci_write_config_dword(pdev_nb, 0x60, regdword);
+
+	/* Enable/disable STPGNT in BIU Status/Control for cpu1,
+	 * page 69 in AMD-762 doc */
+	pci_read_config_dword(pdev_nb, 0x68, &regdword);
+	if (enable)
+		amd76x_bits_set_store(nb, 68, STP_GRANT_DISCON_EN, regdword);
+	else
+		amd76x_bits_restore(nb, 68, STP_GRANT_DISCON_EN, regdword);
+	pci_write_config_dword(pdev_nb, 0x68, regdword);
+
+	/* dis/enable "DRAM refresh disable", page 60 in AMD-762 doc */
+	pci_read_config_dword(pdev_nb, 0x58, &regdword);
+	if (enable)
+		amd76x_bits_clear_store(nb, 58, REF_DIS, regdword);
+	else
+		amd76x_bits_restore(nb, 58, REF_DIS, regdword);
+	pci_write_config_dword(pdev_nb, 0x58, regdword);
+
+	/* Self refresh enable, page 75 in AMD-762 doc */
+	pci_read_config_dword(pdev_nb, 0x70, &regdword);
+	if (enable)
+		amd76x_bits_set_store(nb, 70, SELF_REF_EN, regdword);
+	else
+		amd76x_bits_restore(nb, 70, SELF_REF_EN, regdword);
+	pci_write_config_dword(pdev_nb, 0x70, regdword);
+
+	return 0;
+}
+
+
+/*
+ * Get the base PMIO address and set the pm registers in amd76x_pm_cfg.
+ */
+static void amd76x_get_PM(void)
+{
+	unsigned int regdword;
+
+	/* Get the address for pm status, P_LVL2, etc
+	 * DevB:3x58, page 102 in AMD-768 doc
+	 * C3A58, page 64 in AMD-766 doc
+	 */
+	pci_read_config_dword(pdev_sb, 0x58, &regdword);
+	regdword &= PMBASE;
+	amd76x_pm_slp.status_reg = (regdword + 0x00);
+	switch (mode) {
+	case 2:
+		amd76x_pm_slp.sleep_reg = (regdword + 0x14);
+		break;
+	case 3:
+		amd76x_pm_slp.sleep_reg = (regdword + 0x15);
+		break;
+	}
+	amd76x_pm_slp.tmr_reg = (regdword + 0x08);
+}
+
+/*
+ * En/Disable PMIO and configure W4SG & STPGNT.
+ */
+static int config_PMIO_amd76x(int is_766, int enable)
+{
+	unsigned char regbyte;
+	unsigned char bits = 0;
+
+	/* Set W4SG and PMIOEN, if using a 765/766 set STPGNT as well.
+	 * AMD-766: C3A41; page 59 in AMD-766 doc
+	 * AMD-768: DevB:3x41; page 94 in AMD-768 doc */
+	pci_read_config_byte(pdev_sb, 0x41, &regbyte);
+	if (is_766)
+		bits |= STPGNT;
+	bits |= (PMIOEN | W4SG | TMR32);
+	if (enable)
+		amd76x_bits_set_store(sb, 41, bits, regbyte);
+	else
+		amd76x_bits_restore(sb, 41, bits, regbyte);
+	pci_write_config_byte(pdev_sb, 0x41, regbyte);
+	return 0;
+}
+
+/*
+ * C2 idle support for AMD-766.
+ */
+static void config_amd766_C2(int enable)
+{
+	unsigned int regdword;
+
+	/* Set C2 options in C3A50, page 63 in AMD-766 doc */
+	pci_read_config_dword(pdev_sb, 0x50, &regdword);
+	if (enable) {
+		amd76x_bits_clear_store(sb, 50,
+			(DCSTOP_EN | PCISTP_EN | SUSPND_EN | CPURST_EN)
+			<< C2_REGS,
+			regdword);
+		amd76x_bits_set_store(sb, 50,
+			(STPCLK_EN | CPUSLP_EN) << C2_REGS,
+			regdword);
+	} else 
+		amd76x_bits_restore(sb, 50,
+			(DCSTOP_EN | PCISTP_EN | SUSPND_EN | CPURST_EN |
+			 STPCLK_EN | CPUSLP_EN)
+			<< C2_REGS,
+			regdword);
+	pci_write_config_dword(pdev_sb, 0x50, regdword);
+}
+
+/*
+ * C3 idle support for AMD-766.
+ */
+static void config_amd766_C3(int enable)
+{
+	unsigned int regdword;
+
+	/* Set C3 options in C3A50, page 63 in AMD-766 doc */
+	pci_read_config_dword(pdev_sb, 0x50, &regdword);
+	if (enable) {
+		amd76x_bits_clear_store(sb, 50,
+			(DCSTOP_EN | PCISTP_EN | SUSPND_EN | CPURST_EN)
+			<< C3_REGS,
+			regdword);
+		amd76x_bits_set_store(sb, 50,
+			(STPCLK_EN | CPUSLP_EN | CPUSTP_EN)
+			<< C3_REGS,
+			regdword);
+	} else
+		amd76x_bits_restore(sb, 50,
+			(DCSTOP_EN | PCISTP_EN | SUSPND_EN | CPURST_EN |
+			 STPCLK_EN | CPUSLP_EN | CPUSTP_EN)
+			<< C3_REGS,
+			regdword);
+	pci_write_config_dword(pdev_sb, 0x50, regdword);
+}
+
+/*
+ * Configures the 765 & 766 southbridges.
+ */
+static int config_amd766(int enable)
+{
+	if (enable)
+		amd76x_get_PM();
+	config_PMIO_amd76x(1, enable);
+	switch (mode) {
+	case 2:
+		config_amd766_C2(enable);
+		break;
+	case 3:
+		config_amd766_C3(enable);
+		break;
+	}
+	return 0;
+}
+
+/*
+ * C2 idling support for AMD-768.
+ */
+static void config_amd768_C2(int enable)
+{
+	unsigned char regbyte;
+
+	/* Set C2 options in DevB:3x4F, page 100 in AMD-768 doc */
+	pci_read_config_byte(pdev_sb, 0x4F, &regbyte);
+	if (enable)
+		amd76x_bits_set_store(sb, 4f, C2EN, regbyte);
+	else
+		amd76x_bits_restore(sb, 4f, C2EN, regbyte);
+	pci_write_config_byte(pdev_sb, 0x4F, regbyte);
+}
+
+/*
+ * C3 idling support for AMD-768.
+ */
+static void config_amd768_C3(int enable)
+{
+	unsigned char regbyte;
+
+	/* Set C3 options in DevB:3x4F, page 100 in AMD-768 doc */
+	pci_read_config_byte(pdev_sb, 0x4F, &regbyte);
+	if (enable)
+		amd76x_bits_set_store(sb, 4f,
+			C3EN | ZZ_C3EN | CSLP_C3EN | CSTP_C3EN,
+			regbyte);
+	else
+		amd76x_bits_restore(sb, 4f,
+			C3EN | ZZ_C3EN | CSLP_C3EN |CSTP_C3EN,
+			regbyte);
+	pci_write_config_byte(pdev_sb, 0x4F, regbyte);
+}
+
+/*
+ * Configures the 768 southbridge to support idle calls, and gets
+ * the processor idle call register location.
+ */
+static int config_amd768(int enable)
+{
+	if (enable)
+		amd76x_get_PM();
+	config_PMIO_amd76x(0, enable);
+	switch (mode) {
+	case 2:
+		config_amd768_C2(enable);
+		break;
+	case 3:
+		config_amd768_C3(enable);
+		break;
+	}
+	return 0;
+}
+
+/*
+ * Idle loop for single processor systems
+ */
+void amd76x_up_idle(void)
+{
+	/* ACPI knows how to do C2/C3 on SMP when cpu_count < 2
+	 * we really shouldn't end up here anyway. 
+	 */
+	amd76x_pm_cfg.orig_idle();
+}
+
+/*
+ * Idle loop for SMP systems
+ *
+ */
+static void amd76x_smp_idle(void)
+{
+	int i;
+	int num_online;
+	struct cpu_stat *prs;
+	int ready_for_sleep;
+	unsigned long oldticks;
+	unsigned long newticks;
+	unsigned long busy;
+
+	if (unlikely(force_C1)) {
+		local_irq_disable();
+		safe_halt();
+		return;
+	}
+
+	prs = per_cpu_ptr(prs_ref, raw_smp_processor_id());
+	/* Spin inside (outer) idle loop until lazy_idle cycles
+	 * are reached.
+	 */
+	if (likely(++(prs->idle_count) <= lazy_idle))
+		return;
+
+	/* Now we are ready do go C2/C3. */
+	num_online = num_online_cpus();
+
+	/* lots of locking stuff and Cx transition needs irqs disabled
+	 */
+	local_irq_disable();
+	/* get permission to increment num_idle */
+	read_lock(&amd76x_stat.inclock);
+	atomic_inc(&amd76x_stat.num_idle);
+
+	/* Spin inside inner loop until either
+	 * - spin_idle cycles are reached
+	 * - there is work
+	 * - all CPUs are idle
+	 */
+
+	ready_for_sleep = 0;
+	for (i = 0; i < spin_idle; i++) {
+		if (unlikely(need_resched()))
+			break;
+		/* barrier needed here, don't know why */
+		smp_mb();
+		if (unlikely(atomic_read(&amd76x_stat.num_idle)
+					== num_online)) {
+			ready_for_sleep = 1;
+			/* prevent num_sleep from being decremented */
+			read_lock(&amd76x_stat.declock);
+			atomic_inc(&amd76x_stat.num_sleep);
+			break;
+		}
+	}
+	read_unlock(&amd76x_stat.inclock);
+	/* if num_idle < num_online then num_sleep cannot be incrementet */
+	atomic_dec(&amd76x_stat.num_idle);
+	/* wait for inner loop to become empty */
+	write_lock(&amd76x_stat.inclock);
+	write_unlock(&amd76x_stat.inclock);
+	
+	prs->idle_count = 0;
+	/* now num_sleep cannot be incremented and
+	 * cannot be decremented if num_sleep == num_online
+	 */
+	if(atomic_read(&amd76x_stat.num_sleep) == num_online) {
+		read_unlock(&amd76x_stat.declock);
+		/* get lock to decrement num_sleep */
+		write_lock(&amd76x_stat.declock);
+		write_unlock(&amd76x_stat.declock);
+		atomic_dec(&amd76x_stat.num_sleep);
+		/* Invoke C2/C3 */
+		prs->sleep_cnt++;
+		newticks = inl(amd76x_pm_slp.tmr_reg);
+		/* some integer arithmetics here:
+		 * divide then multiply large values
+		 * multiply then divide small values
+		 * busy is the number of timer ticks we haven't
+		 * been asleep
+		 */
+		if (newticks - prs->last > ULONG_MAX / HZ)
+			busy =
+			((newticks - prs->last) / PM_TMR_FREQ) * HZ;
+		else
+			busy =
+			((newticks - prs->last) * HZ) / PM_TMR_FREQ;
+		/* zero out sleep_ticks for the busy time */
+		if (busy > 0) {
+			if (busy < AVG_WIN * HZ)
+				prs->sleep_ticks =
+				(AVG_WIN * HZ - busy) * prs->sleep_ticks /
+				(AVG_WIN * HZ);
+			else
+				prs->sleep_ticks = 0;
+		}
+				
+		oldticks = inl(amd76x_pm_slp.tmr_reg);
+		smp_mb();
+		inb(amd76x_pm_slp.sleep_reg);
+		/* quote from processor_idle.c:
+		 * Dummy op - must do something useless after P_LVL2 read
+		 */
+		inw(amd76x_pm_slp.status_reg);
+		newticks = inl(amd76x_pm_slp.tmr_reg);
+		/* weighted average: one sleep cycle has a weight of
+		 * 1 / (AVG_WIN * HZ) i.e. the average is taken over about
+		 * AVG_WIN seconds
+		 * Need to scale up the counted ticks so that
+		 * (AVG_SCALE * ticks) / (AVG_WIN * HZ) mostly isn't zero
+		 * and AVG_WIN * AVG_SCALE * PM_TMR_FREQ is below
+		 * ULONG_MAX
+		 */
+		prs->sleep_ticks =
+			((AVG_WIN * HZ - 1) * prs->sleep_ticks +
+			AVG_SCALE * (newticks - oldticks)) /
+			(AVG_WIN * HZ);
+				
+		prs->last = newticks;
+		local_irq_enable();
+		return;
+	}
+	if (ready_for_sleep) {
+		read_unlock(&amd76x_stat.declock);
+		/* num_sleep < num_online here, no lock needed to
+		 * further decrement 
+		 */
+		atomic_dec(&amd76x_stat.num_sleep);
+	}
+	local_irq_enable();
+}
+
+/*
+ *   sysfs support, RW
+ */
+static ssize_t show_sleep_cnt (struct device *dev,
+		struct device_attribute *attr,
+		char *buf)
+{
+	struct cpu_stat *prs;
+	ssize_t ret = 0;
+	int cpu;
+
+	for_each_online_cpu(cpu) {
+		prs = per_cpu_ptr(prs_ref, cpu);
+		ret += sprintf(buf + ret, "%u ", prs->sleep_cnt);
+	}
+	buf[ret - 1] = '\n';
+	return ret;
+}
+
+static DEVICE_ATTR(sleep_cnt, S_IRUGO,
+		   show_sleep_cnt, NULL);
+
+static ssize_t show_sleep_time (struct device *dev,
+		struct device_attribute *attr,
+		char *buf)
+{
+	struct cpu_stat *prs;
+	ssize_t ret = 0;
+	int cpu;
+
+	for_each_online_cpu(cpu) {
+		prs = per_cpu_ptr(prs_ref, cpu);
+		ret += sprintf(buf + ret, "%lu ",
+				TICKS_TO_US(prs->sleep_ticks / AVG_SCALE));
+	}
+	buf[ret - 1] = '\n';
+	return ret;
+}
+
+static DEVICE_ATTR(sleep_time, S_IRUGO,
+		   show_sleep_time, NULL);
+
+/* setup the irq watcher task */
+static void setup_watch(void)
+{
+	if (watch_int <= 0)
+		watch_int = AMD76X_WATCH_INT;
+	if (min(nr_watch_irqs, nr_watch_limits) > 0 && watch_int > 0) {
+		schedule_work(&work);
+		printk(KERN_INFO "amd76x_pm: irq rate watcher started. "
+				"watch_int = %lu ms, min_C1 = %lu\n",
+				watch_int, min_C1);
+	} else {
+		nr_watch_irqs = 0;
+		nr_watch_limits = 0;
+		watch_int = 0;
+		printk(KERN_INFO "amd76x_pm: irq rate watcher disabled.\n");
+	}
+}
+
+/* watch the irq rate of configured irqs and force C1 if rate is above
+ * given limit. This function is scheduled via the work queue and will
+ * schedule itself until disabled.
+ */
+static void watch_irq(void *parm)
+{
+	int i;
+	int irq_cnt;
+	int set_force_C1 = 0;
+
+	for (i = 0; i < min(nr_watch_irqs, nr_watch_limits); i++) {
+		irq_cnt = kstat_irqs(watch_irqs[i]);
+		if ((irq_cnt - irq_count[i]) * 1000 >
+				watch_limits[i] * watch_int) {
+			set_force_C1 = 1;
+		}
+		irq_count[i] = irq_cnt;
+	}
+
+#ifdef AMD76X_LOG_C1
+	if (set_force_C1 && !force_C1)
+		printk(KERN_INFO "amd76x_pm: C1 on\n");
+	if (!set_force_C1 && force_C1 == 1)
+		printk(KERN_INFO "amd76x_pm: C1 off\n");
+#endif
+
+	if (set_force_C1)
+		force_C1 = min_C1;
+	else if (force_C1)
+		force_C1--;
+
+	if (watch_int > 0 && min(nr_watch_irqs, nr_watch_limits) > 0)
+		schedule_delayed_work(&work,
+			msecs_to_jiffies(watch_int));
+	else {
+		nr_watch_irqs = 0;
+		nr_watch_limits = 0;
+		watch_int = 0;
+		printk(KERN_INFO "amd76x_pm: irq rate watcher stopped.\n");
+	}
+}
+
+
+/*
+ * Finds and initializes the bridges, and then sets the idle function
+ */
+static int amd76x_pm_main(void)
+{
+	int cpu;
+	struct cpu_stat *prs;
+
+	if (strnicmp(modeparm, "C3", 2) == 0)
+		mode = 3;
+	else if (strnicmp(modeparm, "C2", 2) == 0)
+		mode = 2;
+	else
+		return -EINVAL;
+
+	amd76x_pm_cfg.orig_idle = 0;
+	if(lazy_idle == 0)
+		lazy_idle = AMD76X_LAZY_IDLE;
+	printk(KERN_INFO "amd76x_pm: lazy_idle = %lu\n", lazy_idle);
+	if(spin_idle == 0)
+		spin_idle = 2 * lazy_idle;
+	printk(KERN_INFO "amd76x_pm: spin_idle = %lu\n", spin_idle);
+	printk(KERN_INFO "amd76x_pm: using C%d idling\n", mode);
+
+	/* Find southbridge */
+	for_each_pci_dev(pdev_sb)
+		if(pci_match_id(amd_sb_tbl, pdev_sb) != NULL)
+			goto found_sb;
+
+	printk(KERN_ERR "amd76x_pm: Could not find southbridge\n");
+	return -ENODEV;
+
+found_sb:
+	/* Find northbridge */
+	for_each_pci_dev(pdev_nb)
+		if(pci_match_id(amd_nb_tbl, pdev_nb) != NULL)
+			goto found_nb;
+	printk(KERN_ERR "amd76x_pm: Could not find northbridge\n");
+	return -ENODEV;
+
+found_nb:	
+	
+	/* Init southbridge */
+	switch (pdev_sb->device) {
+	case PCI_DEVICE_ID_AMD_VIPER_7413:	/* AMD-765 or 766 */
+		sb_id = PCI_DEVICE_ID_AMD_VIPER_7413;
+		config_amd766(1);
+		break;
+	case PCI_DEVICE_ID_AMD_VIPER_7443:	/* AMD-768 */
+		sb_id = PCI_DEVICE_ID_AMD_VIPER_7443;
+		config_amd768(1);
+		break;
+	default:
+		printk(KERN_ERR "amd76x_pm: No southbridge to initialize\n");
+		break;
+	}
+
+	/* Init northbridge and queue the new idle function */
+	if(!pdev_nb) {
+		printk("amd76x_pm: No northbridge found.\n");
+		return -ENODEV;
+	}
+
+	switch (pdev_nb->device) {
+	case PCI_DEVICE_ID_AMD_FE_GATE_700C:	/* AMD-762 */
+		nb_id = PCI_DEVICE_ID_AMD_FE_GATE_700C;
+		config_amd762(1);
+		amd76x_pm_cfg.curr_idle = amd76x_smp_idle;
+		break;
+	default:
+		printk(KERN_ERR "amd76x_pm: No northbridge to initialize\n");
+		break;
+	}
+
+	if(num_online_cpus() == 1) {
+		amd76x_pm_cfg.curr_idle = amd76x_up_idle;
+		printk(KERN_ERR "amd76x_pm: UP machine detected. "
+				"ACPI is your friend.\n");
+	}
+	if (!amd76x_pm_cfg.curr_idle) {
+		printk(KERN_ERR "amd76x_pm: Idle function not changed\n");
+		return 1;
+	}
+
+	prs_ref = alloc_percpu(struct cpu_stat);
+	
+	for_each_cpu(cpu) {
+		prs = per_cpu_ptr(prs_ref, cpu);
+		prs->idle_count = 0;
+		prs->sleep_cnt = 0;
+	}
+
+	amd76x_pm_cfg.orig_idle = pm_idle;
+	pm_idle = amd76x_pm_cfg.curr_idle;
+
+	wmb();
+	
+	/* sysfs */
+	device_create_file(&pdev_nb->dev, &dev_attr_sleep_cnt);
+	device_create_file(&pdev_nb->dev, &dev_attr_sleep_time);
+
+	setup_watch();
+
+	return 0;
+}
+
+
+static int __init amd76x_pm_init(void)
+{
+	int ret;
+	ret =  amd76x_pm_main();
+	if (ret == 0)
+		printk(KERN_INFO "amd76x_pm: Version %s loaded.\n", VERSION);
+	else
+		printk(KERN_INFO "amd76x_pm: Version %s loading failed.\n",
+				VERSION);
+
+	return ret;
+}
+
+
+static void __exit amd76x_pm_cleanup(void)
+{
+	int cpu;
+	unsigned int sleep_cnt = 0;
+	struct cpu_stat *prs;
+	
+	pm_idle = amd76x_pm_cfg.orig_idle;
+
+	cpu_idle_wait();
+
+	/* This isn't really needed. */
+	for_each_online_cpu(cpu) {
+		prs = per_cpu_ptr(prs_ref, cpu);
+		sleep_cnt += prs->sleep_cnt;
+	}
+	printk(KERN_INFO "amd76x_pm: %u C%d calls\n",
+			sleep_cnt, mode);
+	
+	/* remove sysfs */
+	device_remove_file(&pdev_nb->dev, &dev_attr_sleep_cnt);
+	device_remove_file(&pdev_nb->dev, &dev_attr_sleep_time);
+
+	/* disable the irq rate watcher */
+	watch_int = 0;
+	flush_scheduled_work();
+	cancel_delayed_work(&work);
+	flush_scheduled_work();
+	
+	/* free percpu memory */
+	free_percpu(prs_ref);
+
+	switch (sb_id) {
+	case PCI_DEVICE_ID_AMD_VIPER_7413:	/* AMD-765 or 766 */
+		config_amd766(0);
+		break;
+	case PCI_DEVICE_ID_AMD_VIPER_7443:	/* AMD-768 */
+		config_amd768(0);
+		break;
+	default:
+		printk(KERN_ERR "amd76x_pm: No southbridge to deinitialize\n");
+		break;
+	}
+
+	switch (nb_id) {
+	case PCI_DEVICE_ID_AMD_FE_GATE_700C:	/* AMD-762 */
+		config_amd762(0);
+		break;
+	default:
+		printk(KERN_ERR "amd76x_pm: No northbridge to deinitialize\n");
+		break;
+	}
+}
+
+
+MODULE_LICENSE("GPL v2");
+module_init(amd76x_pm_init);
+module_exit(amd76x_pm_cleanup);
diff -Nru linux-2.6.15/include/linux/amd76x_pm.h linux-2.6.15-jo/include/linux/amd76x_pm.h
--- linux-2.6.15/include/linux/amd76x_pm.h	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.15-jo/include/linux/amd76x_pm.h	2006-02-15 21:17:29.000000000 +0100
@@ -0,0 +1,106 @@
+#ifndef __LINUX_AMD76X_PM_H
+#define __LINUX_AMD76X_PM_H
+
+/*
+ * Begin 762
+ */
+
+/* BIU0 options in Dev0:F0:0x60, page 64 in AMD-762 doc
+ * BIU1 options in Dev0:F0:0x68, page 69 in AMD-762 doc
+ */ 
+#define STP_GRANT_DISCON_EN	(1 << 17)
+
+/* DRAM control in Dev0:F0:0x58, page 60 in AMD-762 doc */
+#define REF_DIS			(1 << 19)
+
+/* Memory control in Dev0:F0:0x70, page 75 in AMD-762 doc */
+#define SELF_REF_EN		(1 << 18)
+
+/*
+ * End 762
+ */
+
+/* 
+ * Begin 765/766
+ */
+/* C2/C3 options in C3A50, page 63 in AMD-766 doc */
+#define ZZ_CACHE_EN	1
+#define DCSTOP_EN	(1 << 1)
+#define STPCLK_EN	(1 << 2)
+#define CPUSTP_EN	(1 << 3)
+#define PCISTP_EN	(1 << 4)
+#define CPUSLP_EN	(1 << 5)
+#define SUSPND_EN	(1 << 6)
+#define CPURST_EN	(1 << 7)
+
+#define C2_REGS		0
+#define C3_REGS		8
+/*
+ * End 765/766
+ */
+
+/*
+ * Begin 768
+ */
+/* C2/C3 options in DevB:3x4F, page 100 in AMD-768 doc */
+#define C2EN		1
+#define C3EN		(1 << 1)
+#define ZZ_C3EN		(1 << 2)
+#define CSLP_C3EN	(1 << 3)
+#define CSTP_C3EN	(1 << 4)
+/*
+ * End 768
+ */
+
+/* General options in C3A41, page 59 in AMD-766 doc
+ * devB:3x41, page 94 in AMD-768 doc
+ */
+#define PMIOEN		(1 << 7)
+#define W4SG		(1 << 0)
+#define TMR32		(1 << 3)
+#define STPGNT		(1 << 1)
+
+/* PMxx System Management IO Space Pointer
+ * page 64 in AMD-766 doc
+ * page 102 in AMD-768 doc
+ */
+#define PMBASE 0xff80
+
+/* parameter defaults */
+#define AMD76X_SLEEP_MODE "C2"
+#define AMD76X_LAZY_IDLE 1024
+#define AMD76X_WATCH_INT 500
+#define AMD76X_WATCH_LIM 128
+#define AMD76X_WATCH_MAX 8
+#define AMD76X_MIN_C1 2
+
+/* timer related stuff */
+/* pm timer frequency */
+#define PM_TMR_FREQ 3579545UL
+/* convert timer ticks to microseconds */
+#define TICKS_TO_US(t) (((t) * 1000000UL) / PM_TMR_FREQ)
+/* window for calculating a sleep tick average */
+#define AVG_WIN 8U
+/* need to scale ticks when calculating an average:
+ * make scale large enough to ensure usable results from integer
+ * division and small enough to avoid integer overflows.
+ */
+#define AVG_SCALE 4U
+
+/* some macros for register fiddling */
+/* save + set bits */
+#define amd76x_bits_set_store(dev, reg, bits, tmp) do {\
+	amd76x_saved.dev##reg |= tmp & (bits);\
+	tmp |= bits;} while(0)
+
+/* save + clear bits */
+#define amd76x_bits_clear_store(dev, reg, bits, tmp) do {\
+	amd76x_saved.dev##reg |= tmp & (bits);\
+	tmp &= ~(bits);} while(0)
+
+/* restore bits */
+#define amd76x_bits_restore(dev, reg, bits, tmp) do {\
+	tmp &= ~(bits) | amd76x_saved.dev##reg;\
+	tmp |= (bits) & amd76x_saved.dev##reg;} while (0)
+			
+#endif
