Skip to contentSkip to conversation

sleepy linux

demo patch/RFC Pavel Machek 2 participants 3 messages Feb 26, 2008 – Feb 28, 2008

Subsystems (12)
Memory management x86 SCSI MM core +8 more subsystems
lore
Share & tools
lore
#0 lore raw

Sleepy linux support, demo version, but it works on my thinkpad x60 ;-).

Documentation/power/sleepy.txt new file +55 -0
new file mode 100644
index 0000000..a9caf05
@@ -0,0 +1,55 @@
+		Sleepy Linux
+		~~~~~~~~~~~~
+
+Copyright 2007 Pavel Machek <[email protected]>
+	  GPLv2
+
+Current Linux versions can enter suspend-to-RAM just fine, but only
+can do it on explicit request. But suspend-to-RAM is important, eating
+something like 10% of power needed for idle system. Starting suspend
+manually is not too convinient; it is not an option on multiuser
+machine, and even on single user machine, some things are not easy:
+
+1) Download this big chunk in mozilla, then go to sleep
+
+2) Compile this, then go to sleep
+
+3) You can sleep now, but wake me up in 8:30 with mp3 player
+
+Todays hardware is mostly capable of doing better: with correctly set
+up wakeups, machine can sleep and successfully pretend it is not
+sleeping -- by waking up whenever something interesting happens. Of
+course, it is easier on machines not connected to the network, and on
+notebook computers.
+
+Requirements:
+
+0) Working suspend-to-RAM, with kernel being able to bring video back.
+
+1) RTC clock that can wake up system
+
+2) Lid that can wake up a system,
+   or keyboard that can wake up system and does not loose keypress
+   or special screensaver setup
+
+3) Network card that is either down
+   or can wake up system on any packet (and not loose too many packets)
+
+How to use it
+~~~~~~~~~~~~~
+
+First, make sure your config is tiny enough that cpu sleeps at least
+five or so seconds between wakeups. You'll probably need to disable
+USB, make some kernel timers way longer than default and boot with
+init=/bin/bash.
+
+Then, enable SCSI powersave by something like:
+
+mount /sys
+echo auto > /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/power/level
+echo 3 > /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/power/autosuspend
+echo adisk > /sys/power/state
+mount / -oremount,commit=900
+
+Then, echo auto > /sys/power/state should enable sleepy support. Do it
+twice, and it will ignore open lid and sleep anyway.
arch/x86/kernel/process_32.c +1 -0
index a7d50a5..4c25613 100644
@@ -187,6 +187,7 @@ void cpu_idle(void)
 	/* endless idle loop with no priority at all */
 	while (1) {
 		tick_nohz_stop_sched_tick();
+		detect_idle();
 		while (!need_resched()) {
 			void (*idle)(void);
 
drivers/ata/ahci.c +68 -8

155 lines not on this page · raw message

drivers/ata/libata-core.c +2 -0
index beaa3a9..ee77dc9 100644
@@ -782,6 +782,7 @@ void ata_lpm_schedule(struct ata_port *a
 }
 
 #ifdef CONFIG_PM
+/* This disables link power management */
 static void ata_lpm_enable(struct ata_host *host)
 {
 	struct ata_link *link;
@@ -798,6 +799,7 @@ static void ata_lpm_enable(struct ata_ho
 	}
 }
 
+/* This enables link power management */
 static void ata_lpm_disable(struct ata_host *host)
 {
 	int i;
drivers/ata/libata-eh.c +2 -1
index 4e31071..5c40ac2 100644
@@ -32,6 +32,7 @@
  *
  */
 
+#define DEBUG
 #include <linux/kernel.h>
 #include <linux/pci.h>
 #include <scsi/scsi.h>
@@ -380,7 +381,7 @@ enum scsi_eh_timer_return ata_scsi_timed
  *	Inherited from SCSI layer (none, can sleep)
  *
  *	RETURNS:
- *	Zero.
+ *	Nothing.
  */
 void ata_scsi_error(struct Scsi_Host *host)
 {
drivers/ata/libata-scsi.c +1 -0
index 1cea18f..62b3ee0 100644
@@ -33,6 +33,7 @@
  *
  */
 
+#define DEBUG
 #include <linux/kernel.h>
 #include <linux/blkdev.h>
 #include <linux/spinlock.h>
drivers/base/power/main.c +32 -5
index bdc03f7..268855d 100644
@@ -19,6 +19,7 @@
  * ancestral dependencies that the subsystem list maintains.
  */
 
+#define DEBUG
 #include <linux/device.h>
 #include <linux/kallsyms.h>
 #include <linux/mutex.h>
@@ -319,6 +320,7 @@ static void unregister_dropped_devices(v
  *
  *	Resume all the devices, unlock them all, and allow new
  *	devices to be registered once again.
+ *	Called with interrupts disabled, may not sleep.
  */
 void device_resume(void)
 {
@@ -419,11 +421,6 @@ int suspend_device(struct device *dev, p
 {
 	int error = 0;
 
-	if (dev->power.power_state.event) {
-		dev_dbg(dev, "PM: suspend %d-->%d\n",
-			dev->power.power_state.event, state.event);
-	}
-
 	if (dev->class && dev->class->suspend) {
 		suspend_device_dbg(dev, state, "class ");
 		error = dev->class->suspend(dev, state);
@@ -444,6 +441,34 @@ int suspend_device(struct device *dev, p
 	return error;
 }
 
+int device_suspend_fake(pm_message_t state)
+{
+	int error = 0;
+
+	mutex_lock(&dpm_list_mtx);
+	while (!list_empty(&dpm_active) && error == 0) {
+		struct list_head * entry = dpm_active.prev;
+		struct device * dev = to_device(entry);
+
+		get_device(dev);
+
+		/* Check if the device got removed */
+		if (!list_empty(&dev->power.entry)) {
+			/* Move it to the dpm_off list */
+			if (!error)
+				list_move(&dev->power.entry, &dpm_off);
+		}
+		if (error)
+			printk(KERN_ERR "Could not suspend device %s: "
+				"error %d%s\n",
+				kobject_name(&dev->kobj), error,
+				error == -EAGAIN ? " (please convert to suspend_late)" : "");
+		put_device(dev);
+	}
+	mutex_unlock(&dpm_list_mtx);
+	return error;
+}
+
 /**
  *	dpm_suspend - Suspend every device.
  *	@state:	Power state to put each device in.
@@ -496,6 +521,8 @@ static int dpm_suspend(pm_message_t stat
  *
  *	Go through the dpm_active list. Carefully lock each device's
  *	semaphore and put it in on the dpm_locked list.
+ *
+ *	Must be called with disabled interrupts, may not sleep.
  */
 static void lock_all_devices(void)
 {
drivers/input/input-polldev.c +1 -1
index 490918a..ed2bdf2 100644
@@ -153,7 +153,7 @@ int input_register_polled_device(struct 
 
 	INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
 	if (!dev->poll_interval)
-		dev->poll_interval = 500;
+		dev->poll_interval = 50000;
 	input->private = dev;
 	input->open = input_open_polled_device;
 	input->close = input_close_polled_device;
drivers/input/input.c +14 -1
index f02c242..7e9f46a 100644
@@ -251,6 +251,11 @@ static void input_handle_event(struct in
 		input_pass_event(dev, type, code, value);
 }
 
+struct sleep_disabled_reason keyboard_active = {
+        "keyboard"
+};
+
+
 /**
  * input_event() - report new input event
  * @dev: device that generated the event
@@ -267,8 +272,14 @@ void input_event(struct input_dev *dev,
 {
 	unsigned long flags;
 
-	if (is_event_supported(type, dev->evbit, EV_MAX)) {
+	if ((type == EV_SW) && (code == SW_LID)) {
+		int is_closed = value;
+//		printk("LID: %d\n", value);
+		if (is_closed) enable_auto_sleep(&keyboard_active);
+		else disable_auto_sleep(&keyboard_active);
+	}
 
+	if (is_event_supported(type, dev->evbit, EV_MAX)) {
 		spin_lock_irqsave(&dev->event_lock, flags);
 		add_input_randomness(type, code, value);
 		input_handle_event(dev, type, code, value);
@@ -1646,6 +1657,8 @@ static int __init input_init(void)
 		goto fail2;
 	}
 
+	/* FIXME: should only inc it if LID is open */
+	disable_auto_sleep(&keyboard_active);
 	return 0;
 
  fail2:	input_proc_exit();
drivers/rtc/rtc-cmos.c +35 -3
index e059f94..fadf651 100644
@@ -93,7 +93,7 @@ static inline int is_intr(u8 rtc_intr)
 
 /*----------------------------------------------------------------*/
 
-static int cmos_read_time(struct device *dev, struct rtc_time *t)
+int cmos_read_time(struct device *dev, struct rtc_time *t)
 {
 	/* REVISIT:  if the clock has a "century" register, use
 	 * that instead of the heuristic in get_rtc_time().
@@ -185,7 +185,7 @@ static int cmos_read_alarm(struct device
 	return 0;
 }
 
-static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
+int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
 {
 	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
 	unsigned char	mon, mday, hrs, min, sec;
@@ -494,6 +494,35 @@ static struct bin_attribute nvram = {
 /*----------------------------------------------------------------*/
 
 static struct cmos_rtc	cmos_rtc;
+static struct device *pc_rtc_device;
+
+int set_alarm(int length)
+{
+	ssize_t retval;
+	unsigned long now, alarm;
+	struct rtc_wkalrm alm;
+
+	if (!pc_rtc_device)
+		return -EFAULT;
+	retval = cmos_read_time(pc_rtc_device, &alm.time);
+	if (retval < 0) {
+		printk("Auto sleep: can't get time?\n");
+		return retval;
+	}
+	rtc_tm_to_time(&alm.time, &now);
+	printk("Auto sleep: Now %ld\n", now);
+
+	alarm = now+length;
+	rtc_time_to_tm(alarm, &alm.time);
+
+	retval = cmos_set_alarm(pc_rtc_device, &alm);
+	if (retval < 0) {
+		printk("Auto sleep: can't set alarm.\n");
+		return retval;
+	}
+	printk("Auto sleep: Alarm set\n");
+	return 0;
+}
 
 static irqreturn_t cmos_interrupt(int irq, void *p)
 {
@@ -552,6 +581,8 @@ cmos_do_probe(struct device *dev, struct
 	if (cmos_rtc.dev)
 		return -EBUSY;
 
+	pc_rtc_device = dev;
+
 	if (!ports)
 		return -ENODEV;
 
@@ -712,7 +743,7 @@ cleanup0:
 
 static void cmos_do_shutdown(void)
 {
-	unsigned char	rtc_control;
+	unsigned char rtc_control;
 
 	spin_lock_irq(&rtc_lock);
 	rtc_control = CMOS_READ(RTC_CONTROL);
@@ -727,6 +758,7 @@ static void __exit cmos_do_remove(struct
 	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
 	struct resource *ports;
 
+	pc_rtc_device = NULL;
 	cmos_do_shutdown();
 
 	sysfs_remove_bin_file(&dev->kobj, &nvram);
drivers/rtc/rtc-sysfs.c +0 -10
index 4d27ccc..b0c609e 100644
@@ -156,16 +156,6 @@ rtc_sysfs_set_wakealarm(struct device *d
 
 	alarm = simple_strtoul(buf, NULL, 0);
 	if (alarm > now) {
-		/* Avoid accidentally clobbering active alarms; we can't
-		 * entirely prevent that here, without even the minimal
-		 * locking from the /dev/rtcN api.
-		 */
-		retval = rtc_read_alarm(rtc, &alm);
-		if (retval < 0)
-			return retval;
-		if (alm.enabled)
-			return -EBUSY;
-
 		alm.enabled = 1;
 	} else {
 		alm.enabled = 0;
drivers/scsi/scsi_sysfs.c +15 -0
index 5393e15..25bebc1 100644
@@ -340,6 +340,21 @@ static int scsi_bus_uevent(struct device
 	return 0;
 }
 
+static int scsi_bus_remove(struct device *dev)
+{
+	struct device_driver *drv = dev->driver;
+	struct scsi_device *sdev = to_scsi_device(dev);
+	int err = 0;
+
+	/* reset the prep_fn back to the default since the
+	 * driver may have altered it and it's being removed */
+	blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
+
+	if (drv && drv->remove)
+		err = drv->remove(dev);
+
+	return 0;
+}
 
 struct bus_type scsi_bus_type = {
         .name		= "scsi",
drivers/scsi/sd.c +13 -2
index 16add9a..9ca12d1 100644
@@ -1596,6 +1596,8 @@ static int sd_revalidate_disk(struct gen
 	return 0;
 }
 
+struct device *my_scsi_disk; 
+
 /**
  *	sd_probe - called during driver initialization and whenever a
  *	new scsi device is attached to the system. It is called once
@@ -1715,6 +1717,7 @@ static int sd_probe(struct device *dev)
 
 	scsi_use_ULD_pm(sdp, 1);
 	scsi_autosuspend_device(sdp);
+	my_scsi_disk = dev;
 	return 0;
 
  out_suspend:
@@ -1835,6 +1838,8 @@ static int sd_suspend(struct device *dev
 	struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
 	int ret = 0;
 
+	printk("sleepy: sd_suspend start\n");
+
 	if (!sdkp)
 		return 0;	/* this can happen */
 
@@ -1845,14 +1850,17 @@ static int sd_suspend(struct device *dev
 			goto done;
 	}
 
-	if (mesg.event == PM_EVENT_SUSPEND &&
-	    sdkp->device->manage_start_stop) {
+	if ((mesg.event == PM_EVENT_SUSPEND
+	     )
+	    && sdkp->device->manage_start_stop) {
 		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
 		ret = sd_start_stop_device(sdkp, 0);
 	}
 
 done:
 	scsi_disk_put(sdkp);
+
+	printk("sleepy: sd_suspend done\n");
 	return ret;
 }
 
@@ -1861,6 +1869,8 @@ static int sd_resume(struct device *dev)
 	struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
 	int ret = 0;
 
+	printk("sleepy: sd_resume start\n");
+
 	if (!sdkp->device->manage_start_stop)
 		goto done;
 
@@ -1869,6 +1879,7 @@ static int sd_resume(struct device *dev)
 
 done:
 	scsi_disk_put(sdkp);
+	printk("sleepy: sd_resume done\n");
 	return ret;
 }
 
include/linux/ata.h +11 -0
index 78bbaca..df2dd4f 100644
@@ -298,6 +298,13 @@ enum {
 	SCR_ACTIVE		= 3,
 	SCR_NOTIFICATION	= 4,
 
+	/* SControl subfields, each field is 4 bit wide */
+	ATA_SCTL_DET		= 0, /* lsb */
+	ATA_SCTL_SPD		= 1,
+	ATA_SCTL_IPM		= 2,
+	ATA_SCTL_SPM		= 3,
+	ATA_SCTL_PMP		= 4,
+
 	/* SError bits */
 	SERR_DATA_RECOVERED	= (1 << 0), /* recovered data error */
 	SERR_COMM_RECOVERED	= (1 << 1), /* recovered comm failure */
@@ -441,8 +448,12 @@ static inline int ata_is_data(u8 prot)
 #define ata_id_is_ata(id)	(((id)[0] & (1 << 15)) == 0)
 #define ata_id_has_lba(id)	((id)[49] & (1 << 9))
 #define ata_id_has_dma(id)	((id)[49] & (1 << 8))
+#define ata_id_has_sata(id)	((id)[76] && (id)[76] != 0xffff)
 #define ata_id_has_ncq(id)	((id)[76] & (1 << 8))
 #define ata_id_queue_depth(id)	(((id)[75] & 0x1f) + 1)
+#define ata_id_has_hips(id)	(ata_id_has_sata(id) && ((id)[76] & (1 << 9)))
+#define ata_id_has_dips(id)	(ata_id_has_sata(id) && ((id)[78] & (1 << 3)))
+#define ata_id_dips_enabled(id)	(ata_id_has_sata(id) && ((id)[79] & (1 << 3)))
 #define ata_id_removeable(id)	((id)[0] & (1 << 7))
 #define ata_id_has_atapi_AN(id)	\
 	( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
include/linux/pm.h +24 -1
index eccf59e..3a02c91 100644
@@ -143,6 +143,9 @@ typedef struct pm_message {
  * 		the upcoming system state (such as PCI_D3hot), and enable
  * 		wakeup events as appropriate.
  *
+ * HIBERNATE	Enter a low power device state appropriate for the hibernation
+ * 		state (eg. ACPI S4) and enable wakeup events as appropriate.
+ *
  * FREEZE	Quiesce operations so that a consistent image can be saved;
  * 		but do NOT otherwise enter a low power device state, and do
  * 		NOT emit system wakeup events.
@@ -167,14 +170,16 @@ #define PM_EVENT_ON 0
 #define PM_EVENT_FREEZE 1
 #define PM_EVENT_SUSPEND 2
 #define PM_EVENT_PRETHAW 3
+#define PM_EVENT_HIBERNATE 4
 
 #define PMSG_FREEZE	((struct pm_message){ .event = PM_EVENT_FREEZE, })
 #define PMSG_PRETHAW	((struct pm_message){ .event = PM_EVENT_PRETHAW, })
 #define PMSG_SUSPEND	((struct pm_message){ .event = PM_EVENT_SUSPEND, })
+#define PMSG_HIBERNATE	((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
 #define PMSG_ON		((struct pm_message){ .event = PM_EVENT_ON, })
 
 struct dev_pm_info {
-	pm_message_t		power_state;
+	pm_message_t __deprecated power_state;
 	unsigned		can_wakeup:1;
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
@@ -246,6 +251,24 @@ #define device_init_wakeup(dev,val) \
 		device_set_wakeup_enable(dev,val); \
 	} while(0)
 
+void detect_idle(void);
+void enter_auto_sleep(int length);
+extern atomic_t cpu_needed;
+
+struct sleep_disabled_reason {
+	char *text;
+};
+
+static inline void disable_auto_sleep(struct sleep_disabled_reason *reason)
+{
+	atomic_inc(&cpu_needed);
+}
+
+static inline void enable_auto_sleep(struct sleep_disabled_reason *reason)
+{
+	atomic_dec(&cpu_needed);
+}
+
 /*
  * Global Power Management flags
  * Used to keep APM and ACPI from both being active
kernel/power/Makefile +1 -1
index f7dfff2..e5693d6 100644
@@ -5,7 +5,7 @@ endif
 
 obj-y				:= main.o
 obj-$(CONFIG_PM_LEGACY)		+= pm.o
-obj-$(CONFIG_PM_SLEEP)		+= process.o console.o
+obj-$(CONFIG_PM_SLEEP)		+= process.o console.o sleepy.o
 obj-$(CONFIG_HIBERNATION)	+= swsusp.o disk.o snapshot.o swap.o user.o
 
 obj-$(CONFIG_MAGIC_SYSRQ)	+= poweroff.o
kernel/power/main.c +144 -0

205 lines not on this page · raw message

kernel/power/power.h +4 -0
index 700f44e..fa93da8 100644
@@ -1,3 +1,6 @@
+#ifndef KERNEL_POWER_POWER_H
+#define KERNEL_POWER_POWER_H
+
 #include <linux/suspend.h>
 #include <linux/suspend_ioctls.h>
 #include <linux/utsname.h>
@@ -225,3 +228,4 @@ static inline void suspend_thaw_processe
 {
 }
 #endif
+#endif
kernel/power/sleepy.c new file +27 -0
new file mode 100644
index 0000000..45a178c
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2007 Pavel Machek <[email protected]>
+ * 
+ * This file is released under the GPLv2
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/suspend.h>
+#include <linux/kobject.h>
+#include <linux/string.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/console.h>
+#include <linux/cpu.h>
+#include <linux/resume-trace.h>
+#include <linux/freezer.h>
+#include <linux/vmstat.h>
+#include <linux/syscalls.h>
+#include <linux/rtc.h>
+#include <linux/kthread.h>
+
+#include <asm/percpu.h>
+
+#include "power.h"
+
kernel/time/timer_list.c +84 -0

87 lines not on this page · raw message

mm/slab.c +1 -1
index 473e6c2..87b7f06 100644
@@ -944,7 +944,7 @@ static void __cpuinit start_cpu_timer(in
 		init_reap_node(cpu);
 		INIT_DELAYED_WORK(reap_work, cache_reap);
 		schedule_delayed_work_on(cpu, reap_work,
-					__round_jiffies_relative(HZ, cpu));
+					 __round_jiffies_relative(HZ*10000, cpu)); /* FIXME !*/
 	}
 }
 
mm/vmstat.c +1 -1
index 422d960..ce25ad1 100644
@@ -793,7 +793,7 @@ #endif /* CONFIG_PROC_FS */
 
 #ifdef CONFIG_SMP
 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
-int sysctl_stat_interval __read_mostly = HZ;
+int sysctl_stat_interval __read_mostly = 10000 * HZ;
 
 static void vmstat_update(struct work_struct *w)
 {
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#1 lore raw

On Tue, 26 Feb 2008 11:26:53 +0100 Pavel Machek wrote:

Hi Pavel,

Is this limited to UP and only one disk?

[comments below]

Quoted 10 lines

Sleepy linux support, demo version, but it works on my thinkpad x60 ;-).

Documentation/power/sleepy.txt new file +0 -0
new file mode 100644
index 0000000..a9caf05

Documentation/power/sleepy.txt line 8

Quoted 8 lines
Documentation/power/sleepy.txt new file +8 -0
@@ -0,0 +1,55 @@
+		Sleepy Linux
+		~~~~~~~~~~~~
+
+Copyright 2007 Pavel Machek <[email protected]>
+	  GPLv2
+
+Current Linux versions can enter suspend-to-RAM just fine, but only
+can do it on explicit request. But suspend-to-RAM is important, eating

Usually "can only do it" AFAIK.

Documentation/power/sleepy.txt line 10

Quoted 2 lines
Documentation/power/sleepy.txt new file +2 -0
+something like 10% of power needed for idle system. Starting suspend
+manually is not too convinient; it is not an option on multiuser
                       convenient;

Documentation/power/sleepy.txt line 19

Quoted 9 lines
Documentation/power/sleepy.txt new file +9 -0
+machine, and even on single user machine, some things are not easy:
+
+1) Download this big chunk in mozilla, then go to sleep
+
+2) Compile this, then go to sleep
+
+3) You can sleep now, but wake me up in 8:30 with mp3 player
+
+Todays hardware is mostly capable of doing better: with correctly set

Today's

Documentation/power/sleepy.txt line 32

Quoted 13 lines
Documentation/power/sleepy.txt new file +13 -0
+up wakeups, machine can sleep and successfully pretend it is not
+sleeping -- by waking up whenever something interesting happens. Of
+course, it is easier on machines not connected to the network, and on
+notebook computers.
+
+Requirements:
+
+0) Working suspend-to-RAM, with kernel being able to bring video back.
+
+1) RTC clock that can wake up system
+
+2) Lid that can wake up a system,
+   or keyboard that can wake up system and does not loose keypress

lose

Documentation/power/sleepy.txt line 36

Quoted 4 lines
Documentation/power/sleepy.txt new file +4 -0
+   or special screensaver setup
+
+3) Network card that is either down
+   or can wake up system on any packet (and not loose too many packets)

lose

Documentation/power/sleepy.txt line 41

Quoted 5 lines
Documentation/power/sleepy.txt new file +5 -0
+
+How to use it
+~~~~~~~~~~~~~
+
+First, make sure your config is tiny enough that cpu sleeps at least

CPU (please)

Documentation/power/sleepy.txt line 48

Quoted 7 lines
Documentation/power/sleepy.txt new file +7 -0
+five or so seconds between wakeups. You'll probably need to disable
+USB, make some kernel timers way longer than default and boot with
+init=/bin/bash.
+
+Then, enable SCSI powersave by something like:
+
+mount /sys

Isn't /sys auto-mounted by kernel?

Quoted 18 lines
Documentation/power/sleepy.txt new file +7 -0
+echo auto > /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/power/level
+echo 3 > /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/power/autosuspend
+echo adisk > /sys/power/state
+mount / -oremount,commit=900
+
+Then, echo auto > /sys/power/state should enable sleepy support. Do it
+twice, and it will ignore open lid and sleep anyway.
drivers/ata/ahci.c +2 -0
index 29e71bd..0197b1f 100644
@@ -268,6 +269,41 @@ static struct class_device_attribute *ah
 	NULL
 };
 
+struct pci_dev *my_pdev;
+int autosuspend_enabled;

drivers/ata/ahci.c static struct class_device_attribute *ah · line 283

Quoted 10 lines
drivers/ata/ahci.c +10 -0
+
+struct sleep_disabled_reason ahci_active = {
+        "ahci"
+};
+
+/* The host and its devices are all idle so we can autosuspend */
+static int autosuspend(struct Scsi_Host *host)
+{
+	if (my_pdev && autosuspend_enabled) {
+		printk("ahci: should autosuspend\n");

Use printk() KERN_* levels (multiple places).

Quoted 8 lines
drivers/ata/ahci.c +8 -0
+		ahci_pci_device_suspend(my_pdev, PMSG_SUSPEND);
+		enable_auto_sleep(&ahci_active);
+		return 0;
+	} 
+	printk("ahci: autosuspend disabled\n");
+	return -EINVAL;
+}
+

...

Quoted 8 lines
drivers/ata/ahci.c +4 -0
+}
+
+
+
 static struct scsi_host_template ahci_sht = {
 	.module			= THIS_MODULE,
 	.name			= DRV_NAME,

drivers/ata/ahci.c static void ahci_thaw(struct ata_port *a · line 1863

Quoted 6 lines
drivers/ata/ahci.c +3 -0
@@ -1820,6 +1858,10 @@ static void ahci_thaw(struct ata_port *a
 
 static void ahci_error_handler(struct ata_port *ap)
 {
+	struct ata_host *host = ap->host;
+	int rc;
+	extern int slept;

Eh?

drivers/ata/ahci.c static void ahci_thaw(struct ata_port *a · line 1867

Quoted 4 lines
drivers/ata/ahci.c +1 -0
+
 	if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
 		/* restart engine */
 		ahci_stop_engine(ap);

General comment: Lots of the comment fixes in libata should be part of a standalone patch, not part of this patch.

include/linux/ata.h enum { · line 301

Quoted 9 lines
include/linux/ata.h +1 -0
index 78bbaca..df2dd4f 100644
@@ -298,6 +298,13 @@ enum {
 	SCR_ACTIVE		= 3,
 	SCR_NOTIFICATION	= 4,
 
+	/* SControl subfields, each field is 4 bit wide */

bits

Quoted 15 lines
include/linux/ata.h +6 -0
+	ATA_SCTL_DET		= 0, /* lsb */
+	ATA_SCTL_SPD		= 1,
+	ATA_SCTL_IPM		= 2,
+	ATA_SCTL_SPM		= 3,
+	ATA_SCTL_PMP		= 4,
+
 	/* SError bits */
 	SERR_DATA_RECOVERED	= (1 << 0), /* recovered data error */
 	SERR_COMM_RECOVERED	= (1 << 1), /* recovered comm failure */
kernel/power/main.c +0 -0
index a29da58..2470e94 100644

kernel/power/main.c line 6

Quoted 4 lines
kernel/power/main.c +1 -0
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 2003 Patrick Mochel
  * Copyright (c) 2003 Open Source Development Lab
+ * Copyright (c) 2007 Pavel Machek <[email protected]>

It's 2008 now.

kernel/power/main.c #include <linux/freezer.h> · line 29

Quoted 11 lines
kernel/power/main.c +3 -0
  * 
  * This file is released under the GPLv2
  *
@@ -22,7 +23,10 @@ #include <linux/freezer.h>
 #include <linux/vmstat.h>
 #include <linux/syscalls.h>
 
+#include <asm/percpu.h>
+
 #include "power.h"
+#include "../../drivers/scsi/scsi_priv.h"

ugh.

Quoted 10 lines
kernel/power/main.c +4 -0
 DEFINE_MUTEX(pm_mutex);
 
@@ -426,6 +437,76 @@ static int enter_state(suspend_state_t s
 	return error;
 }
 
+/* Returns how long it waited in ms */
+//extern long (*panic_blink)(long time);
+
+int slept;

kernel/power/main.c static int enter_state(suspend_state_t s · line 453

Quoted 10 lines
kernel/power/main.c +10 -0
+
+
+struct sleep_disabled_reason wakeup_tasks = {
+	"wakeup_tasks"
+};
+
+int device_suspend_fake(pm_message_t state);
+
+int
+do_auto_sleep(void)

All on one line.

kernel/power/main.c static int enter_state(suspend_state_t s · line 465

Quoted 2 lines
kernel/power/main.c +2 -0
+{
+	int error,i;

space after comma.

Quoted 21 lines
kernel/power/main.c +21 -0
+	int state = PM_SUSPEND_MEM;
+
+	/* add baby "morning tasks" to the lists. BUG if it is not
+	   the only baby */
+
+	if (slept)
+		return;
+	slept++;
+	disable_auto_sleep(&wakeup_tasks);
+//	device_suspend_fake(PMSG_SUSPEND);
+	suspend_enter(state);
+
+	/* FIXME: run "morning tasks" in process context; drivers need to check why
+	   machine was woken, and perhaps put themselves back to crying babies list
+	   if lid was opened or something. */
+
+	enable_auto_sleep(&wakeup_tasks);
+
+#if 0
+	printk("finishing\n");
+	if (suspend_ops->finish)

kernel/power/main.c static int enter_state(suspend_state_t s · line 496

Quoted 10 lines
kernel/power/main.c +10 -0
+		suspend_ops->finish();
+#endif
+
+	return 0;
+}
+
+extern int set_alarm(int length);
+
+void
+enter_auto_sleep(int length)

All on one line.

Quoted 17 lines
kernel/power/main.c +13 -0
+{
+	int error;
+
+	if (atomic_read(&cpu_needed))
+		return;
+
+	printk("Auto sleeping\n");
+	set_alarm(length);
+
+	error = do_auto_sleep();
+	if (error)
+		printk("enter auto sleep failed: %d\n", error);
+}
 
 /**
  *	pm_suspend - Externally visible function for suspending system.

kernel/power/main.c #endif · line 570

Quoted 9 lines
kernel/power/main.c +6 -0
@@ -481,6 +562,12 @@ #endif
 	return (s - buf);
 }
 
+extern struct pci_dev *my_pdev;
+extern int autosuspend_enabled;
+extern struct device *my_scsi_disk;
+extern int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
+extern int ahci_pci_device_resume(struct pci_dev *pdev);
+

Externs in header files, please.

kernel/power/main.c #endif · line 573

Quoted 3 lines
kernel/power/main.c +0 -0
 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
 			   const char *buf, size_t n)
 {

...

---
~Randy

#2 lore raw

Hi!

Quoted 1 line

Is this limited to UP and only one disk?

Yes... for now.

Quoted 1 line

[comments below]

Thanks!

Quoted 14 lines

Documentation/power/sleepy.txt line 8

Quoted 11 lines
Documentation/power/sleepy.txt new file +8 -0
@@ -0,0 +1,55 @@
+		Sleepy Linux
+		~~~~~~~~~~~~
+
+Copyright 2007 Pavel Machek <[email protected]>
+	  GPLv2
+
+Current Linux versions can enter suspend-to-RAM just fine, but only
+can do it on explicit request. But suspend-to-RAM is important, eating

Usually "can only do it" AFAIK.

Quoted 4 lines

Documentation/power/sleepy.txt line 10

Quoted 2 lines
Documentation/power/sleepy.txt new file +2 -0
+something like 10% of power needed for idle system. Starting suspend
+manually is not too convinient; it is not an option on multiuser
                       convenient;

Spelling applied.

Quoted 5 lines

Documentation/power/sleepy.txt line 48

Quoted 3 lines
Documentation/power/sleepy.txt new file +3 -0
+Then, enable SCSI powersave by something like:
+
+mount /sys

Isn't /sys auto-mounted by kernel?

No.

Quoted 7 lines

Documentation/power/sleepy.txt line 18

Quoted 5 lines
Documentation/power/sleepy.txt new file +5 -0
+/* The host and its devices are all idle so we can autosuspend */
+static int autosuspend(struct Scsi_Host *host)
+{
+	if (my_pdev && autosuspend_enabled) {
+		printk("ahci: should autosuspend\n");

Use printk() KERN_* levels (multiple places).

Well, code obviously needs a lot of work... I said it was ugly ;-).

Quoted 9 lines

Documentation/power/sleepy.txt static void ahci_thaw(struct ata_port *a · line 1863

Quoted 7 lines
Documentation/power/sleepy.txt new file +3 -0
@@ -1820,6 +1858,10 @@ static void ahci_thaw(struct ata_port *a
 
 static void ahci_error_handler(struct ata_port *ap)
 {
+	struct ata_host *host = ap->host;
+	int rc;
+	extern int slept;

Eh?

Pavel in hack mode ;-). Yes, I'll clean it up once I fix the fundamental issues -- like UP only etc...
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html