[PATCH net-next 1/5] net: ipa: use atomic exchange for suspend reference

From: Alex Elder
Date: Tue Sep 08 2020 - 20:22:04 EST


We take a single IPA clock reference to keep the clock running
until we get a system suspend operation. When a system suspend
request arrives, we drop that reference, and if that's the last
reference (likely) we'll proceed with suspending endpoints and
disabling the IPA core clock and interconnects.

In most places we simply set the reference count to 0 or 1
atomically. Instead--primarily to catch coding errors--use an
atomic exchange to update the reference count value, and report
an error in the event the previous value was unexpected.

In a few cases it's not hard to see that the error message should
never be reported. Report them anyway, but add some excitement
to the message by ending it with an exclamation point.

Signed-off-by: Alex Elder <elder@xxxxxxxxxx>
---
drivers/net/ipa/ipa_main.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ipa/ipa_main.c b/drivers/net/ipa/ipa_main.c
index 1fdfec41e4421..6b843fc989122 100644
--- a/drivers/net/ipa/ipa_main.c
+++ b/drivers/net/ipa/ipa_main.c
@@ -83,6 +83,7 @@ static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
/* Take a a single clock reference to prevent suspend. All
* endpoints will be resumed as a result. This reference will
* be dropped when we get a power management suspend request.
+ * The first call activates the clock; ignore any others.
*/
if (!atomic_xchg(&ipa->suspend_ref, 1))
ipa_clock_get(ipa);
@@ -502,13 +503,15 @@ static void ipa_resource_deconfig(struct ipa *ipa)
*/
static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
{
+ struct device *dev = &ipa->pdev->dev;
int ret;

/* Get a clock reference to allow initialization. This reference
* is held after initialization completes, and won't get dropped
* unless/until a system suspend request arrives.
*/
- atomic_set(&ipa->suspend_ref, 1);
+ if (atomic_xchg(&ipa->suspend_ref, 1))
+ dev_err(dev, "suspend clock reference already taken!\n");
ipa_clock_get(ipa);

ipa_hardware_config(ipa);
@@ -544,7 +547,8 @@ static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
err_hardware_deconfig:
ipa_hardware_deconfig(ipa);
ipa_clock_put(ipa);
- atomic_set(&ipa->suspend_ref, 0);
+ if (!atomic_xchg(&ipa->suspend_ref, 0))
+ dev_err(dev, "suspend clock reference already dropped!\n");

return ret;
}
@@ -562,7 +566,8 @@ static void ipa_deconfig(struct ipa *ipa)
ipa_endpoint_deconfig(ipa);
ipa_hardware_deconfig(ipa);
ipa_clock_put(ipa);
- atomic_set(&ipa->suspend_ref, 0);
+ if (!atomic_xchg(&ipa->suspend_ref, 0))
+ dev_err(&ipa->pdev->dev, "no suspend clock reference\n");
}

static int ipa_firmware_load(struct device *dev)
@@ -913,7 +918,8 @@ static int ipa_suspend(struct device *dev)
struct ipa *ipa = dev_get_drvdata(dev);

ipa_clock_put(ipa);
- atomic_set(&ipa->suspend_ref, 0);
+ if (!atomic_xchg(&ipa->suspend_ref, 0))
+ dev_err(dev, "suspend: missing suspend clock reference\n");

return 0;
}
@@ -933,7 +939,8 @@ static int ipa_resume(struct device *dev)
/* This clock reference will keep the IPA out of suspend
* until we get a power management suspend request.
*/
- atomic_set(&ipa->suspend_ref, 1);
+ if (atomic_xchg(&ipa->suspend_ref, 1))
+ dev_err(dev, "resume: duplicate suspend clock reference\n");
ipa_clock_get(ipa);

return 0;
--
2.20.1