Commit 9aab2449 authored by Cyril Bur's avatar Cyril Bur Committed by Michael Ellerman

powerpc/opal: Add opal_async_wait_response_interruptible() to opal-async

This patch adds an _interruptible version of opal_async_wait_response().
This is useful when a long running OPAL call is performed on behalf of
a userspace thread, for example, the opal_flash_{read,write,erase}
functions performed by the powernv-flash MTD driver.

It is foreseeable that these functions would take upwards of two
minutes causing the wait_event() to block long enough to cause hung
task warnings. Furthermore, wait_event_interruptible() is preferable
as otherwise there is no way for signals to stop the process which is
going to be confusing in userspace.
Signed-off-by: default avatarCyril Bur <cyrilbur@gmail.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
parent 95e1bc1d
...@@ -309,6 +309,8 @@ extern void opal_notifier_update_evt(uint64_t evt_mask, uint64_t evt_val); ...@@ -309,6 +309,8 @@ extern void opal_notifier_update_evt(uint64_t evt_mask, uint64_t evt_val);
extern int opal_async_get_token_interruptible(void); extern int opal_async_get_token_interruptible(void);
extern int opal_async_release_token(int token); extern int opal_async_release_token(int token);
extern int opal_async_wait_response(uint64_t token, struct opal_msg *msg); extern int opal_async_wait_response(uint64_t token, struct opal_msg *msg);
extern int opal_async_wait_response_interruptible(uint64_t token,
struct opal_msg *msg);
extern int opal_get_sensor_data(u32 sensor_hndl, u32 *sensor_data); extern int opal_get_sensor_data(u32 sensor_hndl, u32 *sensor_data);
struct rtc_time; struct rtc_time;
......
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
enum opal_async_token_state { enum opal_async_token_state {
ASYNC_TOKEN_UNALLOCATED = 0, ASYNC_TOKEN_UNALLOCATED = 0,
ASYNC_TOKEN_ALLOCATED, ASYNC_TOKEN_ALLOCATED,
ASYNC_TOKEN_DISPATCHED,
ASYNC_TOKEN_ABANDONED,
ASYNC_TOKEN_COMPLETED ASYNC_TOKEN_COMPLETED
}; };
...@@ -61,8 +63,9 @@ static int __opal_async_get_token(void) ...@@ -61,8 +63,9 @@ static int __opal_async_get_token(void)
/* /*
* Note: If the returned token is used in an opal call and opal returns * Note: If the returned token is used in an opal call and opal returns
* OPAL_ASYNC_COMPLETION you MUST call opal_async_wait_response() before * OPAL_ASYNC_COMPLETION you MUST call one of opal_async_wait_response() or
* calling another other opal_async_* function * opal_async_wait_response_interruptible() at least once before calling another
* opal_async_* function
*/ */
int opal_async_get_token_interruptible(void) int opal_async_get_token_interruptible(void)
{ {
...@@ -98,6 +101,14 @@ static int __opal_async_release_token(int token) ...@@ -98,6 +101,14 @@ static int __opal_async_release_token(int token)
opal_async_tokens[token].state = ASYNC_TOKEN_UNALLOCATED; opal_async_tokens[token].state = ASYNC_TOKEN_UNALLOCATED;
rc = 0; rc = 0;
break; break;
/*
* DISPATCHED and ABANDONED tokens must wait for OPAL to respond.
* Mark a DISPATCHED token as ABANDONED so that the response handling
* code knows no one cares and that it can free it then.
*/
case ASYNC_TOKEN_DISPATCHED:
opal_async_tokens[token].state = ASYNC_TOKEN_ABANDONED;
/* Fall through */
default: default:
rc = 1; rc = 1;
} }
...@@ -130,7 +141,11 @@ int opal_async_wait_response(uint64_t token, struct opal_msg *msg) ...@@ -130,7 +141,11 @@ int opal_async_wait_response(uint64_t token, struct opal_msg *msg)
return -EINVAL; return -EINVAL;
} }
/* Wakeup the poller before we wait for events to speed things /*
* There is no need to mark the token as dispatched, wait_event()
* will block until the token completes.
*
* Wakeup the poller before we wait for events to speed things
* up on platforms or simulators where the interrupts aren't * up on platforms or simulators where the interrupts aren't
* functional. * functional.
*/ */
...@@ -143,11 +158,66 @@ int opal_async_wait_response(uint64_t token, struct opal_msg *msg) ...@@ -143,11 +158,66 @@ int opal_async_wait_response(uint64_t token, struct opal_msg *msg)
} }
EXPORT_SYMBOL_GPL(opal_async_wait_response); EXPORT_SYMBOL_GPL(opal_async_wait_response);
int opal_async_wait_response_interruptible(uint64_t token, struct opal_msg *msg)
{
unsigned long flags;
int ret;
if (token >= opal_max_async_tokens) {
pr_err("%s: Invalid token passed\n", __func__);
return -EINVAL;
}
if (!msg) {
pr_err("%s: Invalid message pointer passed\n", __func__);
return -EINVAL;
}
/*
* The first time this gets called we mark the token as DISPATCHED
* so that if wait_event_interruptible() returns not zero and the
* caller frees the token, we know not to actually free the token
* until the response comes.
*
* Only change if the token is ALLOCATED - it may have been
* completed even before the caller gets around to calling this
* the first time.
*
* There is also a dirty great comment at the token allocation
* function that if the opal call returns OPAL_ASYNC_COMPLETION to
* the caller then the caller *must* call this or the not
* interruptible version before doing anything else with the
* token.
*/
if (opal_async_tokens[token].state == ASYNC_TOKEN_ALLOCATED) {
spin_lock_irqsave(&opal_async_comp_lock, flags);
if (opal_async_tokens[token].state == ASYNC_TOKEN_ALLOCATED)
opal_async_tokens[token].state = ASYNC_TOKEN_DISPATCHED;
spin_unlock_irqrestore(&opal_async_comp_lock, flags);
}
/*
* Wakeup the poller before we wait for events to speed things
* up on platforms or simulators where the interrupts aren't
* functional.
*/
opal_wake_poller();
ret = wait_event_interruptible(opal_async_wait,
opal_async_tokens[token].state ==
ASYNC_TOKEN_COMPLETED);
if (!ret)
memcpy(msg, &opal_async_tokens[token].response, sizeof(*msg));
return ret;
}
EXPORT_SYMBOL_GPL(opal_async_wait_response_interruptible);
/* Called from interrupt context */ /* Called from interrupt context */
static int opal_async_comp_event(struct notifier_block *nb, static int opal_async_comp_event(struct notifier_block *nb,
unsigned long msg_type, void *msg) unsigned long msg_type, void *msg)
{ {
struct opal_msg *comp_msg = msg; struct opal_msg *comp_msg = msg;
enum opal_async_token_state state;
unsigned long flags; unsigned long flags;
uint64_t token; uint64_t token;
...@@ -155,11 +225,17 @@ static int opal_async_comp_event(struct notifier_block *nb, ...@@ -155,11 +225,17 @@ static int opal_async_comp_event(struct notifier_block *nb,
return 0; return 0;
token = be64_to_cpu(comp_msg->params[0]); token = be64_to_cpu(comp_msg->params[0]);
memcpy(&opal_async_tokens[token].response, comp_msg, sizeof(*comp_msg));
spin_lock_irqsave(&opal_async_comp_lock, flags); spin_lock_irqsave(&opal_async_comp_lock, flags);
state = opal_async_tokens[token].state;
opal_async_tokens[token].state = ASYNC_TOKEN_COMPLETED; opal_async_tokens[token].state = ASYNC_TOKEN_COMPLETED;
spin_unlock_irqrestore(&opal_async_comp_lock, flags); spin_unlock_irqrestore(&opal_async_comp_lock, flags);
if (state == ASYNC_TOKEN_ABANDONED) {
/* Free the token, no one else will */
opal_async_release_token(token);
return 0;
}
memcpy(&opal_async_tokens[token].response, comp_msg, sizeof(*comp_msg));
wake_up(&opal_async_wait); wake_up(&opal_async_wait);
return 0; return 0;
......
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