2020-03-05 21:28:19 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
|
2024-04-16 16:10:14 -07:00
|
|
|
* Copyright (C) 2018-2024 Linaro Ltd.
|
2020-03-05 21:28:19 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/interconnect.h>
|
2021-08-04 08:36:24 -07:00
|
|
|
#include <linux/pm.h>
|
2021-08-10 12:27:00 -07:00
|
|
|
#include <linux/pm_runtime.h>
|
2020-03-05 21:28:19 -07:00
|
|
|
|
2022-02-01 08:02:05 -07:00
|
|
|
#include "linux/soc/qcom/qcom_aoss.h"
|
|
|
|
|
2020-03-05 21:28:19 -07:00
|
|
|
#include "ipa.h"
|
2024-04-16 16:10:18 -07:00
|
|
|
#include "ipa_data.h"
|
2021-08-04 08:36:24 -07:00
|
|
|
#include "ipa_endpoint.h"
|
2024-04-16 16:10:18 -07:00
|
|
|
#include "ipa_interrupt.h"
|
2020-03-05 21:28:19 -07:00
|
|
|
#include "ipa_modem.h"
|
2024-04-16 16:10:18 -07:00
|
|
|
#include "ipa_power.h"
|
2020-03-05 21:28:19 -07:00
|
|
|
|
|
|
|
/**
|
2021-08-20 09:01:28 -07:00
|
|
|
* DOC: IPA Power Management
|
2020-03-05 21:28:19 -07:00
|
|
|
*
|
2021-08-20 09:01:28 -07:00
|
|
|
* The IPA hardware is enabled when the IPA core clock and all the
|
|
|
|
* interconnects (buses) it depends on are enabled. Runtime power
|
|
|
|
* management is used to determine whether the core clock and
|
|
|
|
* interconnects are enabled, and if not in use to be suspended
|
|
|
|
* automatically.
|
2020-03-05 21:28:19 -07:00
|
|
|
*
|
2021-08-20 09:01:28 -07:00
|
|
|
* The core clock currently runs at a fixed clock rate when enabled,
|
|
|
|
* an all interconnects use a fixed average and peak bandwidth.
|
2020-03-05 21:28:19 -07:00
|
|
|
*/
|
|
|
|
|
2021-08-20 09:01:27 -07:00
|
|
|
#define IPA_AUTOSUSPEND_DELAY 500 /* milliseconds */
|
|
|
|
|
2020-03-05 21:28:19 -07:00
|
|
|
/**
|
2021-08-20 09:01:28 -07:00
|
|
|
* struct ipa_power - IPA power management information
|
2021-08-10 12:27:01 -07:00
|
|
|
* @dev: IPA device pointer
|
2020-03-05 21:28:19 -07:00
|
|
|
* @core: IPA core clock
|
2022-02-01 08:02:05 -07:00
|
|
|
* @qmp: QMP handle for AOSS communication
|
2021-01-15 05:50:50 -07:00
|
|
|
* @interconnect_count: Number of elements in interconnect[]
|
2021-01-15 05:50:46 -07:00
|
|
|
* @interconnect: Interconnect array
|
2020-03-05 21:28:19 -07:00
|
|
|
*/
|
2021-08-20 09:01:28 -07:00
|
|
|
struct ipa_power {
|
2021-08-10 12:27:01 -07:00
|
|
|
struct device *dev;
|
2020-03-05 21:28:19 -07:00
|
|
|
struct clk *core;
|
2022-02-01 08:02:05 -07:00
|
|
|
struct qmp *qmp;
|
2021-01-15 05:50:50 -07:00
|
|
|
u32 interconnect_count;
|
2023-09-22 10:28:50 -07:00
|
|
|
struct icc_bulk_data interconnect[] __counted_by(interconnect_count);
|
2020-03-05 21:28:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialize interconnects required for IPA operation */
|
2022-03-09 12:20:37 -07:00
|
|
|
static int ipa_interconnect_init(struct ipa_power *power,
|
2021-01-15 05:50:47 -07:00
|
|
|
const struct ipa_interconnect_data *data)
|
2020-03-05 21:28:19 -07:00
|
|
|
{
|
2022-03-09 12:20:31 -07:00
|
|
|
struct icc_bulk_data *interconnect;
|
2021-01-15 05:50:49 -07:00
|
|
|
int ret;
|
2022-03-09 12:20:36 -07:00
|
|
|
u32 i;
|
2021-01-15 05:50:50 -07:00
|
|
|
|
2022-03-09 12:20:35 -07:00
|
|
|
/* Initialize our interconnect data array for bulk operations */
|
2022-03-09 12:20:36 -07:00
|
|
|
interconnect = &power->interconnect[0];
|
|
|
|
for (i = 0; i < power->interconnect_count; i++) {
|
2022-03-09 12:20:35 -07:00
|
|
|
/* interconnect->path is filled in by of_icc_bulk_get() */
|
|
|
|
interconnect->name = data->name;
|
|
|
|
interconnect->avg_bw = data->average_bandwidth;
|
|
|
|
interconnect->peak_bw = data->peak_bandwidth;
|
|
|
|
data++;
|
2021-01-15 05:50:50 -07:00
|
|
|
interconnect++;
|
|
|
|
}
|
2020-03-05 21:28:19 -07:00
|
|
|
|
2022-03-09 12:20:37 -07:00
|
|
|
ret = of_icc_bulk_get(power->dev, power->interconnect_count,
|
2022-03-09 12:20:35 -07:00
|
|
|
power->interconnect);
|
|
|
|
if (ret)
|
2022-03-09 12:20:36 -07:00
|
|
|
return ret;
|
2022-03-09 12:20:35 -07:00
|
|
|
|
|
|
|
/* All interconnects are initially disabled */
|
|
|
|
icc_bulk_disable(power->interconnect_count, power->interconnect);
|
|
|
|
|
|
|
|
/* Set the bandwidth values to be used when enabled */
|
|
|
|
ret = icc_bulk_set_bw(power->interconnect_count, power->interconnect);
|
|
|
|
if (ret)
|
2022-03-09 12:20:36 -07:00
|
|
|
icc_bulk_put(power->interconnect_count, power->interconnect);
|
2021-01-15 05:50:49 -07:00
|
|
|
|
|
|
|
return ret;
|
2020-03-05 21:28:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Inverse of ipa_interconnect_init() */
|
2021-08-20 09:01:28 -07:00
|
|
|
static void ipa_interconnect_exit(struct ipa_power *power)
|
2020-03-05 21:28:19 -07:00
|
|
|
{
|
2022-03-09 12:20:35 -07:00
|
|
|
icc_bulk_put(power->interconnect_count, power->interconnect);
|
2020-03-05 21:28:19 -07:00
|
|
|
}
|
|
|
|
|
2021-08-20 09:01:28 -07:00
|
|
|
/* Enable IPA power, enabling interconnects and the core clock */
|
|
|
|
static int ipa_power_enable(struct ipa *ipa)
|
2020-03-05 21:28:19 -07:00
|
|
|
{
|
2022-03-09 12:20:33 -07:00
|
|
|
struct ipa_power *power = ipa->power;
|
2020-03-05 21:28:19 -07:00
|
|
|
int ret;
|
|
|
|
|
2022-03-09 12:20:33 -07:00
|
|
|
ret = icc_bulk_enable(power->interconnect_count, power->interconnect);
|
2020-03-05 21:28:19 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2022-03-09 12:20:33 -07:00
|
|
|
ret = clk_prepare_enable(power->core);
|
2021-08-04 08:36:23 -07:00
|
|
|
if (ret) {
|
2022-03-09 12:20:37 -07:00
|
|
|
dev_err(power->dev, "error %d enabling core clock\n", ret);
|
2022-03-09 12:20:33 -07:00
|
|
|
icc_bulk_disable(power->interconnect_count,
|
|
|
|
power->interconnect);
|
2021-08-04 08:36:23 -07:00
|
|
|
}
|
2020-03-05 21:28:19 -07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:01:28 -07:00
|
|
|
/* Inverse of ipa_power_enable() */
|
2022-03-09 12:20:33 -07:00
|
|
|
static void ipa_power_disable(struct ipa *ipa)
|
2020-03-05 21:28:19 -07:00
|
|
|
{
|
2022-03-09 12:20:33 -07:00
|
|
|
struct ipa_power *power = ipa->power;
|
|
|
|
|
|
|
|
clk_disable_unprepare(power->core);
|
net: ipa: have ipa_clock_get() return a value
We currently assume no errors occur when enabling or disabling the
IPA core clock and interconnects. And although this commit exposes
errors that could occur, we generally assume this won't happen in
practice.
This commit changes ipa_clock_get() and ipa_clock_put() so each
returns a value. The values returned are meant to mimic what the
runtime power management functions return, so we can set up error
handling here before we make the switch. Have ipa_clock_get()
increment the reference count even if it returns an error, to match
the behavior of pm_runtime_get().
More details follow.
When taking a reference in ipa_clock_get(), return 0 for the first
reference, 1 for subsequent references, or a negative error code if
an error occurs. Note that if ipa_clock_get() returns an error, we
must not touch hardware; in some cases such errors now cause entire
blocks of code to be skipped.
When dropping a reference in ipa_clock_put(), we return 0 or an
error code. The error would come from ipa_clock_disable(), which
now returns what ipa_interconnect_disable() returns (either 0 or a
negative error code). For now, callers ignore the return value;
if an error occurs, a message will have already been logged, and
little more can actually be done to improve the situation.
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2021-08-10 12:26:58 -07:00
|
|
|
|
2022-03-09 12:20:33 -07:00
|
|
|
icc_bulk_disable(power->interconnect_count, power->interconnect);
|
2020-03-05 21:28:19 -07:00
|
|
|
}
|
|
|
|
|
2021-08-10 12:27:00 -07:00
|
|
|
static int ipa_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ipa *ipa = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
/* Endpoints aren't usable until setup is complete */
|
|
|
|
if (ipa->setup_complete) {
|
|
|
|
ipa_endpoint_suspend(ipa);
|
|
|
|
gsi_suspend(&ipa->gsi);
|
|
|
|
}
|
|
|
|
|
2022-03-09 12:20:33 -07:00
|
|
|
ipa_power_disable(ipa);
|
|
|
|
|
|
|
|
return 0;
|
2021-08-10 12:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ipa_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ipa *ipa = dev_get_drvdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
2021-08-20 09:01:28 -07:00
|
|
|
ret = ipa_power_enable(ipa);
|
2021-08-10 12:27:00 -07:00
|
|
|
if (WARN_ON(ret < 0))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Endpoints aren't usable until setup is complete */
|
|
|
|
if (ipa->setup_complete) {
|
|
|
|
gsi_resume(&ipa->gsi);
|
|
|
|
ipa_endpoint_resume(ipa);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-12 12:50:31 -07:00
|
|
|
static int ipa_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ipa *ipa = dev_get_drvdata(dev);
|
|
|
|
|
2023-01-15 10:59:24 -07:00
|
|
|
/* Increment the disable depth to ensure that the IRQ won't
|
|
|
|
* be re-enabled until the matching _enable call in
|
|
|
|
* ipa_resume(). We do this to ensure that the interrupt
|
|
|
|
* handler won't run whilst PM runtime is disabled.
|
|
|
|
*
|
|
|
|
* Note that disabling the IRQ is NOT the same as disabling
|
|
|
|
* irq wake. If wakeup is enabled for the IPA then the IRQ
|
|
|
|
* will still cause the system to wake up, see irq_set_irq_wake().
|
|
|
|
*/
|
|
|
|
ipa_interrupt_irq_disable(ipa);
|
|
|
|
|
2021-08-12 12:50:31 -07:00
|
|
|
return pm_runtime_force_suspend(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipa_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ipa *ipa = dev_get_drvdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pm_runtime_force_resume(dev);
|
|
|
|
|
2023-01-15 10:59:24 -07:00
|
|
|
/* Now that PM runtime is enabled again it's safe
|
|
|
|
* to turn the IRQ back on and process any data
|
|
|
|
* that was received during suspend.
|
|
|
|
*/
|
|
|
|
ipa_interrupt_irq_enable(ipa);
|
|
|
|
|
2021-08-12 12:50:31 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:23:34 -07:00
|
|
|
/* Return the current IPA core clock rate */
|
2021-08-20 09:01:28 -07:00
|
|
|
u32 ipa_core_clock_rate(struct ipa *ipa)
|
2020-07-03 14:23:34 -07:00
|
|
|
{
|
2021-08-20 09:01:28 -07:00
|
|
|
return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
|
2020-07-03 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
2022-02-01 08:02:05 -07:00
|
|
|
static int ipa_power_retention_init(struct ipa_power *power)
|
|
|
|
{
|
|
|
|
struct qmp *qmp = qmp_get(power->dev);
|
|
|
|
|
|
|
|
if (IS_ERR(qmp)) {
|
|
|
|
if (PTR_ERR(qmp) == -EPROBE_DEFER)
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
|
|
|
|
/* We assume any other error means it's not defined/needed */
|
|
|
|
qmp = NULL;
|
|
|
|
}
|
|
|
|
power->qmp = qmp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ipa_power_retention_exit(struct ipa_power *power)
|
|
|
|
{
|
|
|
|
qmp_put(power->qmp);
|
|
|
|
power->qmp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Control register retention on power collapse */
|
|
|
|
void ipa_power_retention(struct ipa *ipa, bool enable)
|
|
|
|
{
|
|
|
|
static const char fmt[] = "{ class: bcm, res: ipa_pc, val: %c }";
|
|
|
|
struct ipa_power *power = ipa->power;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!power->qmp)
|
|
|
|
return; /* Not needed on this platform */
|
|
|
|
|
2023-08-11 13:58:39 -07:00
|
|
|
ret = qmp_send(power->qmp, fmt, enable ? '1' : '0');
|
2022-02-01 08:02:05 -07:00
|
|
|
if (ret)
|
|
|
|
dev_err(power->dev, "error %d sending QMP %sable request\n",
|
|
|
|
ret, enable ? "en" : "dis");
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:01:28 -07:00
|
|
|
/* Initialize IPA power management */
|
|
|
|
struct ipa_power *
|
|
|
|
ipa_power_init(struct device *dev, const struct ipa_power_data *data)
|
2020-03-05 21:28:19 -07:00
|
|
|
{
|
2021-08-20 09:01:28 -07:00
|
|
|
struct ipa_power *power;
|
2020-03-05 21:28:19 -07:00
|
|
|
struct clk *clk;
|
2022-03-09 12:20:36 -07:00
|
|
|
size_t size;
|
2020-03-05 21:28:19 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
clk = clk_get(dev, "core");
|
2024-08-28 05:15:51 -07:00
|
|
|
if (IS_ERR(clk))
|
|
|
|
return dev_err_cast_probe(dev, clk, "error getting core clock\n");
|
2020-03-05 21:28:19 -07:00
|
|
|
|
2020-11-19 15:40:41 -07:00
|
|
|
ret = clk_set_rate(clk, data->core_clock_rate);
|
2020-03-05 21:28:19 -07:00
|
|
|
if (ret) {
|
2020-11-19 15:40:41 -07:00
|
|
|
dev_err(dev, "error %d setting core clock rate to %u\n",
|
|
|
|
ret, data->core_clock_rate);
|
2020-03-05 21:28:19 -07:00
|
|
|
goto err_clk_put;
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:24:23 -07:00
|
|
|
size = struct_size(power, interconnect, data->interconnect_count);
|
|
|
|
power = kzalloc(size, GFP_KERNEL);
|
2021-08-20 09:01:28 -07:00
|
|
|
if (!power) {
|
2020-03-05 21:28:19 -07:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_clk_put;
|
|
|
|
}
|
2021-08-20 09:01:28 -07:00
|
|
|
power->dev = dev;
|
|
|
|
power->core = clk;
|
|
|
|
power->interconnect_count = data->interconnect_count;
|
2020-03-05 21:28:19 -07:00
|
|
|
|
2022-03-09 12:20:37 -07:00
|
|
|
ret = ipa_interconnect_init(power, data->interconnect_data);
|
2020-03-05 21:28:19 -07:00
|
|
|
if (ret)
|
|
|
|
goto err_kfree;
|
|
|
|
|
2022-02-01 08:02:05 -07:00
|
|
|
ret = ipa_power_retention_init(power);
|
|
|
|
if (ret)
|
|
|
|
goto err_interconnect_exit;
|
|
|
|
|
2021-08-20 09:01:27 -07:00
|
|
|
pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
|
|
|
|
pm_runtime_use_autosuspend(dev);
|
2021-08-10 12:27:01 -07:00
|
|
|
pm_runtime_enable(dev);
|
|
|
|
|
2021-08-20 09:01:28 -07:00
|
|
|
return power;
|
2020-03-05 21:28:19 -07:00
|
|
|
|
2022-02-01 08:02:05 -07:00
|
|
|
err_interconnect_exit:
|
|
|
|
ipa_interconnect_exit(power);
|
2020-03-05 21:28:19 -07:00
|
|
|
err_kfree:
|
2021-08-20 09:01:28 -07:00
|
|
|
kfree(power);
|
2020-03-05 21:28:19 -07:00
|
|
|
err_clk_put:
|
|
|
|
clk_put(clk);
|
|
|
|
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:01:28 -07:00
|
|
|
/* Inverse of ipa_power_init() */
|
|
|
|
void ipa_power_exit(struct ipa_power *power)
|
2020-03-05 21:28:19 -07:00
|
|
|
{
|
2021-08-20 09:01:28 -07:00
|
|
|
struct device *dev = power->dev;
|
|
|
|
struct clk *clk = power->core;
|
2020-03-05 21:28:19 -07:00
|
|
|
|
2021-08-20 09:01:27 -07:00
|
|
|
pm_runtime_disable(dev);
|
|
|
|
pm_runtime_dont_use_autosuspend(dev);
|
2022-02-01 08:02:05 -07:00
|
|
|
ipa_power_retention_exit(power);
|
2021-08-20 09:01:28 -07:00
|
|
|
ipa_interconnect_exit(power);
|
|
|
|
kfree(power);
|
2020-03-05 21:28:19 -07:00
|
|
|
clk_put(clk);
|
|
|
|
}
|
2021-08-04 08:36:24 -07:00
|
|
|
|
|
|
|
const struct dev_pm_ops ipa_pm_ops = {
|
2021-08-12 12:50:31 -07:00
|
|
|
.suspend = ipa_suspend,
|
|
|
|
.resume = ipa_resume,
|
2021-08-10 12:27:01 -07:00
|
|
|
.runtime_suspend = ipa_runtime_suspend,
|
|
|
|
.runtime_resume = ipa_runtime_resume,
|
2021-08-04 08:36:24 -07:00
|
|
|
};
|