Commit b729c09a authored by Russell King's avatar Russell King Committed by Russell King

[ARM] Improve reliability of APM-emulation suspend

The APM emulation can sometimes cause suspend to fail to work due
to apparantly waiting for some process to acknowledge an event when
it actually has already done so.  We re-jig the event handling to
work around this behaviour.
Signed-off-by: default avatarRussell King <rmk+kernel@arm.linux.org.uk>
parent 0215ffb0
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
*/ */
#include <linux/module.h> #include <linux/module.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/timer.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
...@@ -26,6 +25,7 @@ ...@@ -26,6 +25,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/completion.h> #include <linux/completion.h>
#include <linux/kthread.h> #include <linux/kthread.h>
#include <linux/delay.h>
#include <asm/apm.h> /* apm_power_info */ #include <asm/apm.h> /* apm_power_info */
#include <asm/system.h> #include <asm/system.h>
...@@ -71,7 +71,8 @@ struct apm_user { ...@@ -71,7 +71,8 @@ struct apm_user {
#define SUSPEND_PENDING 1 /* suspend pending read */ #define SUSPEND_PENDING 1 /* suspend pending read */
#define SUSPEND_READ 2 /* suspend read, pending ack */ #define SUSPEND_READ 2 /* suspend read, pending ack */
#define SUSPEND_ACKED 3 /* suspend acked */ #define SUSPEND_ACKED 3 /* suspend acked */
#define SUSPEND_DONE 4 /* suspend completed */ #define SUSPEND_WAIT 4 /* waiting for suspend */
#define SUSPEND_DONE 5 /* suspend completed */
struct apm_queue queue; struct apm_queue queue;
}; };
...@@ -101,6 +102,7 @@ static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait); ...@@ -101,6 +102,7 @@ static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
static DEFINE_SPINLOCK(kapmd_queue_lock); static DEFINE_SPINLOCK(kapmd_queue_lock);
static struct apm_queue kapmd_queue; static struct apm_queue kapmd_queue;
static DECLARE_MUTEX(state_lock);
static const char driver_version[] = "1.13"; /* no spaces */ static const char driver_version[] = "1.13"; /* no spaces */
...@@ -148,38 +150,60 @@ static void queue_add_event(struct apm_queue *q, apm_event_t event) ...@@ -148,38 +150,60 @@ static void queue_add_event(struct apm_queue *q, apm_event_t event)
q->events[q->event_head] = event; q->events[q->event_head] = event;
} }
static void queue_event_one_user(struct apm_user *as, apm_event_t event) static void queue_event(apm_event_t event)
{ {
if (as->suser && as->writer) { struct apm_user *as;
switch (event) {
case APM_SYS_SUSPEND:
case APM_USER_SUSPEND:
/*
* If this user already has a suspend pending,
* don't queue another one.
*/
if (as->suspend_state != SUSPEND_NONE)
return;
as->suspend_state = SUSPEND_PENDING; down_read(&user_list_lock);
suspends_pending++; list_for_each_entry(as, &apm_user_list, list) {
break; if (as->reader)
}
}
queue_add_event(&as->queue, event); queue_add_event(&as->queue, event);
}
up_read(&user_list_lock);
wake_up_interruptible(&apm_waitqueue);
} }
static void queue_event(apm_event_t event, struct apm_user *sender) /*
* queue_suspend_event - queue an APM suspend event.
*
* Check that we're in a state where we can suspend. If not,
* return -EBUSY. Otherwise, queue an event to all "writer"
* users. If there are no "writer" users, return '1' to
* indicate that we can immediately suspend.
*/
static int queue_suspend_event(apm_event_t event, struct apm_user *sender)
{ {
struct apm_user *as; struct apm_user *as;
int ret = 1;
down(&state_lock);
down_read(&user_list_lock); down_read(&user_list_lock);
/*
* If a thread is still processing, we can't suspend, so reject
* the request.
*/
list_for_each_entry(as, &apm_user_list, list) {
if (as != sender && as->reader && as->writer && as->suser &&
as->suspend_state != SUSPEND_NONE) {
ret = -EBUSY;
goto out;
}
}
list_for_each_entry(as, &apm_user_list, list) { list_for_each_entry(as, &apm_user_list, list) {
if (as != sender && as->reader) if (as != sender && as->reader && as->writer && as->suser) {
queue_event_one_user(as, event); as->suspend_state = SUSPEND_PENDING;
suspends_pending++;
queue_add_event(&as->queue, event);
ret = 0;
} }
}
out:
up_read(&user_list_lock); up_read(&user_list_lock);
up(&state_lock);
wake_up_interruptible(&apm_waitqueue); wake_up_interruptible(&apm_waitqueue);
return ret;
} }
static void apm_suspend(void) static void apm_suspend(void)
...@@ -191,17 +215,22 @@ static void apm_suspend(void) ...@@ -191,17 +215,22 @@ static void apm_suspend(void)
* Anyone on the APM queues will think we're still suspended. * Anyone on the APM queues will think we're still suspended.
* Send a message so everyone knows we're now awake again. * Send a message so everyone knows we're now awake again.
*/ */
queue_event(APM_NORMAL_RESUME, NULL); queue_event(APM_NORMAL_RESUME);
/* /*
* Finally, wake up anyone who is sleeping on the suspend. * Finally, wake up anyone who is sleeping on the suspend.
*/ */
down(&state_lock);
down_read(&user_list_lock); down_read(&user_list_lock);
list_for_each_entry(as, &apm_user_list, list) { list_for_each_entry(as, &apm_user_list, list) {
if (as->suspend_state == SUSPEND_WAIT ||
as->suspend_state == SUSPEND_ACKED) {
as->suspend_result = err; as->suspend_result = err;
as->suspend_state = SUSPEND_DONE; as->suspend_state = SUSPEND_DONE;
} }
}
up_read(&user_list_lock); up_read(&user_list_lock);
up(&state_lock);
wake_up(&apm_suspend_waitqueue); wake_up(&apm_suspend_waitqueue);
} }
...@@ -227,8 +256,11 @@ static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t ...@@ -227,8 +256,11 @@ static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t
if (copy_to_user(buf, &event, sizeof(event))) if (copy_to_user(buf, &event, sizeof(event)))
break; break;
if (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND) down(&state_lock);
if (as->suspend_state == SUSPEND_PENDING &&
(event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
as->suspend_state = SUSPEND_READ; as->suspend_state = SUSPEND_READ;
up(&state_lock);
buf += sizeof(event); buf += sizeof(event);
i -= sizeof(event); i -= sizeof(event);
...@@ -270,9 +302,13 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg) ...@@ -270,9 +302,13 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
switch (cmd) { switch (cmd) {
case APM_IOC_SUSPEND: case APM_IOC_SUSPEND:
down(&state_lock);
as->suspend_result = -EINTR; as->suspend_result = -EINTR;
if (as->suspend_state == SUSPEND_READ) { if (as->suspend_state == SUSPEND_READ) {
int pending;
/* /*
* If we read a suspend command from /dev/apm_bios, * If we read a suspend command from /dev/apm_bios,
* then the corresponding APM_IOC_SUSPEND ioctl is * then the corresponding APM_IOC_SUSPEND ioctl is
...@@ -280,47 +316,66 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg) ...@@ -280,47 +316,66 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
*/ */
as->suspend_state = SUSPEND_ACKED; as->suspend_state = SUSPEND_ACKED;
suspends_pending--; suspends_pending--;
pending = suspends_pending == 0;
up(&state_lock);
/*
* If there are no further acknowledges required,
* suspend the system.
*/
if (pending)
apm_suspend();
/*
* Wait for the suspend/resume to complete. If there
* are pending acknowledges, we wait here for them.
*
* Note: we need to ensure that the PM subsystem does
* not kick us out of the wait when it suspends the
* threads.
*/
flags = current->flags;
current->flags |= PF_NOFREEZE;
wait_event(apm_suspend_waitqueue,
as->suspend_state == SUSPEND_DONE);
} else { } else {
up(&state_lock);
/* /*
* Otherwise it is a request to suspend the system. * Otherwise it is a request to suspend the system.
* Queue an event for all readers, and expect an * Queue an event for all readers, and expect an
* acknowledge from all writers who haven't already * acknowledge from all writers who haven't already
* acknowledged. * acknowledged.
*/ */
queue_event(APM_USER_SUSPEND, as); err = queue_suspend_event(APM_USER_SUSPEND, as);
} if (err < 0)
break;
/* if (err > 0)
* If there are no further acknowledges required, suspend
* the system.
*/
if (suspends_pending == 0)
apm_suspend(); apm_suspend();
/* /*
* Wait for the suspend/resume to complete. If there are * Wait for the suspend/resume to complete. If there
* pending acknowledges, we wait here for them. * are pending acknowledges, we wait here for them.
* *
* Note that we need to ensure that the PM subsystem does * Note: we need to ensure that the PM subsystem does
* not kick us out of the wait when it suspends the threads. * not kick us out of the wait when it suspends the
* threads.
*/ */
flags = current->flags; flags = current->flags;
current->flags |= PF_NOFREEZE; current->flags |= PF_NOFREEZE;
/*
* Note: do not allow a thread which is acking the suspend
* to escape until the resume is complete.
*/
if (as->suspend_state == SUSPEND_ACKED)
wait_event(apm_suspend_waitqueue,
as->suspend_state == SUSPEND_DONE);
else
wait_event_interruptible(apm_suspend_waitqueue, wait_event_interruptible(apm_suspend_waitqueue,
as->suspend_state == SUSPEND_DONE); as->suspend_state == SUSPEND_DONE);
}
current->flags = flags; current->flags = flags;
down(&state_lock);
err = as->suspend_result; err = as->suspend_result;
as->suspend_state = SUSPEND_NONE; as->suspend_state = SUSPEND_NONE;
up(&state_lock);
break; break;
} }
...@@ -330,6 +385,8 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg) ...@@ -330,6 +385,8 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
static int apm_release(struct inode * inode, struct file * filp) static int apm_release(struct inode * inode, struct file * filp)
{ {
struct apm_user *as = filp->private_data; struct apm_user *as = filp->private_data;
int pending = 0;
filp->private_data = NULL; filp->private_data = NULL;
down_write(&user_list_lock); down_write(&user_list_lock);
...@@ -342,11 +399,14 @@ static int apm_release(struct inode * inode, struct file * filp) ...@@ -342,11 +399,14 @@ static int apm_release(struct inode * inode, struct file * filp)
* need to balance suspends_pending, which means the * need to balance suspends_pending, which means the
* possibility of sleeping. * possibility of sleeping.
*/ */
down(&state_lock);
if (as->suspend_state != SUSPEND_NONE) { if (as->suspend_state != SUSPEND_NONE) {
suspends_pending -= 1; suspends_pending -= 1;
if (suspends_pending == 0) pending = suspends_pending == 0;
apm_suspend();
} }
up(&state_lock);
if (pending)
apm_suspend();
kfree(as); kfree(as);
return 0; return 0;
...@@ -470,6 +530,7 @@ static int kapmd(void *arg) ...@@ -470,6 +530,7 @@ static int kapmd(void *arg)
{ {
do { do {
apm_event_t event; apm_event_t event;
int ret;
wait_event_interruptible(kapmd_wait, wait_event_interruptible(kapmd_wait,
!queue_empty(&kapmd_queue) || kthread_should_stop()); !queue_empty(&kapmd_queue) || kthread_should_stop());
...@@ -489,13 +550,20 @@ static int kapmd(void *arg) ...@@ -489,13 +550,20 @@ static int kapmd(void *arg)
case APM_LOW_BATTERY: case APM_LOW_BATTERY:
case APM_POWER_STATUS_CHANGE: case APM_POWER_STATUS_CHANGE:
queue_event(event, NULL); queue_event(event);
break; break;
case APM_USER_SUSPEND: case APM_USER_SUSPEND:
case APM_SYS_SUSPEND: case APM_SYS_SUSPEND:
queue_event(event, NULL); ret = queue_suspend_event(event, NULL);
if (suspends_pending == 0) if (ret < 0) {
/*
* We were busy. Try again in 50ms.
*/
queue_add_event(&kapmd_queue, event);
msleep(50);
}
if (ret > 0)
apm_suspend(); apm_suspend();
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment