Commit d2a8cfc6 authored by Douglas Anderson's avatar Douglas Anderson Committed by Bjorn Andersson

soc: qcom: rpmh-rsc: Remove the pm_lock

It has been postulated that the pm_lock is bad for performance because
a CPU currently running rpmh_flush() could block other CPUs from
coming out of idle.  Similarly CPUs coming out of / going into idle
all need to contend with each other for the spinlock just to update
the variable tracking who's in PM.

Let's optimize this a bit.  Specifically:

- Use a count rather than a bitmask.  This is faster to access and
  also means we can use the atomic_inc_return() function to really
  detect who the last one to enter PM was.
- Accept that it's OK if we race and are doing the flush (because we
  think we're last) while another CPU is coming out of idle.  As long
  as we block that CPU if/when it tries to do an active-only transfer
  we're OK.
Signed-off-by: default avatarDouglas Anderson <dianders@chromium.org>
Reviewed-by: default avatarStephen Boyd <swboyd@chromium.org>
Link: https://lore.kernel.org/r/20200504104917.v6.5.I295cb72bc5334a2af80313cbe97cb5c9dcb1442c@changeidSigned-off-by: default avatarBjorn Andersson <bjorn.andersson@linaro.org>
parent 555701a4
...@@ -95,7 +95,7 @@ struct rpmh_ctrlr { ...@@ -95,7 +95,7 @@ struct rpmh_ctrlr {
* @num_tcs: Number of TCSes in this DRV. * @num_tcs: Number of TCSes in this DRV.
* @rsc_pm: CPU PM notifier for controller. * @rsc_pm: CPU PM notifier for controller.
* Used when solver mode is not present. * Used when solver mode is not present.
* @cpus_entered_pm: CPU mask for cpus in idle power collapse. * @cpus_in_pm: Number of CPUs not in idle power collapse.
* Used when solver mode is not present. * Used when solver mode is not present.
* @tcs: TCS groups. * @tcs: TCS groups.
* @tcs_in_use: S/W state of the TCS; only set for ACTIVE_ONLY * @tcs_in_use: S/W state of the TCS; only set for ACTIVE_ONLY
...@@ -103,9 +103,9 @@ struct rpmh_ctrlr { ...@@ -103,9 +103,9 @@ struct rpmh_ctrlr {
* it was borrowed for an active_only transfer. You * it was borrowed for an active_only transfer. You
* must hold the lock in this struct (AKA drv->lock) in * must hold the lock in this struct (AKA drv->lock) in
* order to update this. * order to update this.
* @lock: Synchronize state of the controller. * @lock: Synchronize state of the controller. If RPMH's cache
* @pm_lock: Synchronize during PM notifications. * lock will also be held, the order is: drv->lock then
* Used when solver mode is not present. * cache_lock.
* @client: Handle to the DRV's client. * @client: Handle to the DRV's client.
*/ */
struct rsc_drv { struct rsc_drv {
...@@ -114,11 +114,10 @@ struct rsc_drv { ...@@ -114,11 +114,10 @@ struct rsc_drv {
int id; int id;
int num_tcs; int num_tcs;
struct notifier_block rsc_pm; struct notifier_block rsc_pm;
struct cpumask cpus_entered_pm; atomic_t cpus_in_pm;
struct tcs_group tcs[TCS_TYPE_NR]; struct tcs_group tcs[TCS_TYPE_NR];
DECLARE_BITMAP(tcs_in_use, MAX_TCS_NR); DECLARE_BITMAP(tcs_in_use, MAX_TCS_NR);
spinlock_t lock; spinlock_t lock;
spinlock_t pm_lock;
struct rpmh_ctrlr client; struct rpmh_ctrlr client;
}; };
......
...@@ -750,6 +750,8 @@ int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv, const struct tcs_request *msg) ...@@ -750,6 +750,8 @@ int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv, const struct tcs_request *msg)
* SLEEP and WAKE sets. If AMCs are busy, controller can not enter * SLEEP and WAKE sets. If AMCs are busy, controller can not enter
* power collapse, so deny from the last cpu's pm notification. * power collapse, so deny from the last cpu's pm notification.
* *
* Context: Must be called with the drv->lock held.
*
* Return: * Return:
* * False - AMCs are idle * * False - AMCs are idle
* * True - AMCs are busy * * True - AMCs are busy
...@@ -764,9 +766,6 @@ static bool rpmh_rsc_ctrlr_is_busy(struct rsc_drv *drv) ...@@ -764,9 +766,6 @@ static bool rpmh_rsc_ctrlr_is_busy(struct rsc_drv *drv)
* dedicated TCS for active state use, then re-purposed wake TCSes * dedicated TCS for active state use, then re-purposed wake TCSes
* should be checked for not busy, because we used wake TCSes for * should be checked for not busy, because we used wake TCSes for
* active requests in this case. * active requests in this case.
*
* Since this is called from the last cpu, need not take drv->lock
* before checking tcs_is_free().
*/ */
if (!tcs->num_tcs) if (!tcs->num_tcs)
tcs = &drv->tcs[WAKE_TCS]; tcs = &drv->tcs[WAKE_TCS];
...@@ -801,43 +800,62 @@ static int rpmh_rsc_cpu_pm_callback(struct notifier_block *nfb, ...@@ -801,43 +800,62 @@ static int rpmh_rsc_cpu_pm_callback(struct notifier_block *nfb,
{ {
struct rsc_drv *drv = container_of(nfb, struct rsc_drv, rsc_pm); struct rsc_drv *drv = container_of(nfb, struct rsc_drv, rsc_pm);
int ret = NOTIFY_OK; int ret = NOTIFY_OK;
int cpus_in_pm;
spin_lock(&drv->pm_lock);
switch (action) { switch (action) {
case CPU_PM_ENTER: case CPU_PM_ENTER:
cpumask_set_cpu(smp_processor_id(), &drv->cpus_entered_pm); cpus_in_pm = atomic_inc_return(&drv->cpus_in_pm);
/*
if (!cpumask_equal(&drv->cpus_entered_pm, cpu_online_mask)) * NOTE: comments for num_online_cpus() point out that it's
goto exit; * only a snapshot so we need to be careful. It should be OK
* for us to use, though. It's important for us not to miss
* if we're the last CPU going down so it would only be a
* problem if a CPU went offline right after we did the check
* AND that CPU was not idle AND that CPU was the last non-idle
* CPU. That can't happen. CPUs would have to come out of idle
* before the CPU could go offline.
*/
if (cpus_in_pm < num_online_cpus())
return NOTIFY_OK;
break; break;
case CPU_PM_ENTER_FAILED: case CPU_PM_ENTER_FAILED:
case CPU_PM_EXIT: case CPU_PM_EXIT:
cpumask_clear_cpu(smp_processor_id(), &drv->cpus_entered_pm); atomic_dec(&drv->cpus_in_pm);
goto exit; return NOTIFY_OK;
default: default:
ret = NOTIFY_DONE; return NOTIFY_DONE;
goto exit;
} }
ret = rpmh_rsc_ctrlr_is_busy(drv); /*
if (ret) { * It's likely we're on the last CPU. Grab the drv->lock and write
ret = NOTIFY_BAD; * out the sleep/wake commands to RPMH hardware. Grabbing the lock
goto exit; * means that if we race with another CPU coming up we are still
* guaranteed to be safe. If another CPU came up just after we checked
* and has grabbed the lock or started an active transfer then we'll
* notice we're busy and abort. If another CPU comes up after we start
* flushing it will be blocked from starting an active transfer until
* we're done flushing. If another CPU starts an active transfer after
* we release the lock we're still OK because we're no longer the last
* CPU.
*/
if (spin_trylock(&drv->lock)) {
if (rpmh_rsc_ctrlr_is_busy(drv) || rpmh_flush(&drv->client))
ret = NOTIFY_BAD;
spin_unlock(&drv->lock);
} else {
/* Another CPU must be up */
return NOTIFY_OK;
} }
ret = rpmh_flush(&drv->client); if (ret == NOTIFY_BAD) {
if (ret) /* Double-check if we're here because someone else is up */
ret = NOTIFY_BAD; if (cpus_in_pm < num_online_cpus())
else ret = NOTIFY_OK;
ret = NOTIFY_OK; else
/* We won't be called w/ CPU_PM_ENTER_FAILED */
exit: atomic_dec(&drv->cpus_in_pm);
if (ret == NOTIFY_BAD) }
/* We won't be called w/ CPU_PM_ENTER_FAILED */
cpumask_clear_cpu(smp_processor_id(), &drv->cpus_entered_pm);
spin_unlock(&drv->pm_lock);
return ret; return ret;
} }
...@@ -980,7 +998,6 @@ static int rpmh_rsc_probe(struct platform_device *pdev) ...@@ -980,7 +998,6 @@ static int rpmh_rsc_probe(struct platform_device *pdev)
solver_config = solver_config >> DRV_HW_SOLVER_SHIFT; solver_config = solver_config >> DRV_HW_SOLVER_SHIFT;
if (!solver_config) { if (!solver_config) {
drv->rsc_pm.notifier_call = rpmh_rsc_cpu_pm_callback; drv->rsc_pm.notifier_call = rpmh_rsc_cpu_pm_callback;
spin_lock_init(&drv->pm_lock);
cpu_pm_register_notifier(&drv->rsc_pm); cpu_pm_register_notifier(&drv->rsc_pm);
} }
......
...@@ -435,9 +435,6 @@ static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state, ...@@ -435,9 +435,6 @@ static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state,
* *
* @ctrlr: Controller making request to flush cached data * @ctrlr: Controller making request to flush cached data
* *
* This function is called from sleep code on the last CPU
* (thus no spinlock needed).
*
* Return: * Return:
* * 0 - Success * * 0 - Success
* * Error code - Otherwise * * Error code - Otherwise
...@@ -445,13 +442,21 @@ static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state, ...@@ -445,13 +442,21 @@ static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state,
int rpmh_flush(struct rpmh_ctrlr *ctrlr) int rpmh_flush(struct rpmh_ctrlr *ctrlr)
{ {
struct cache_req *p; struct cache_req *p;
int ret; int ret = 0;
lockdep_assert_irqs_disabled(); lockdep_assert_irqs_disabled();
/*
* Currently rpmh_flush() is only called when we think we're running
* on the last processor. If the lock is busy it means another
* processor is up and it's better to abort than spin.
*/
if (!spin_trylock(&ctrlr->cache_lock))
return -EBUSY;
if (!ctrlr->dirty) { if (!ctrlr->dirty) {
pr_debug("Skipping flush, TCS has latest data.\n"); pr_debug("Skipping flush, TCS has latest data.\n");
return 0; goto exit;
} }
/* Invalidate the TCSes first to avoid stale data */ /* Invalidate the TCSes first to avoid stale data */
...@@ -460,7 +465,7 @@ int rpmh_flush(struct rpmh_ctrlr *ctrlr) ...@@ -460,7 +465,7 @@ int rpmh_flush(struct rpmh_ctrlr *ctrlr)
/* First flush the cached batch requests */ /* First flush the cached batch requests */
ret = flush_batch(ctrlr); ret = flush_batch(ctrlr);
if (ret) if (ret)
return ret; goto exit;
list_for_each_entry(p, &ctrlr->cache, list) { list_for_each_entry(p, &ctrlr->cache, list) {
if (!is_req_valid(p)) { if (!is_req_valid(p)) {
...@@ -471,16 +476,18 @@ int rpmh_flush(struct rpmh_ctrlr *ctrlr) ...@@ -471,16 +476,18 @@ int rpmh_flush(struct rpmh_ctrlr *ctrlr)
ret = send_single(ctrlr, RPMH_SLEEP_STATE, p->addr, ret = send_single(ctrlr, RPMH_SLEEP_STATE, p->addr,
p->sleep_val); p->sleep_val);
if (ret) if (ret)
return ret; goto exit;
ret = send_single(ctrlr, RPMH_WAKE_ONLY_STATE, p->addr, ret = send_single(ctrlr, RPMH_WAKE_ONLY_STATE, p->addr,
p->wake_val); p->wake_val);
if (ret) if (ret)
return ret; goto exit;
} }
ctrlr->dirty = false; ctrlr->dirty = false;
return 0; exit:
spin_unlock(&ctrlr->cache_lock);
return ret;
} }
/** /**
......
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