#include <linux/types.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <linux/panic_notifier.h>
#include <linux/pm_runtime.h>
#include <linux/soc/qcom/smem.h>
#include <linux/soc/qcom/smem_state.h>
#include "ipa_smp2p.h"
#include "ipa.h"
#include "ipa_uc.h"
struct ipa_smp2p {
struct ipa *ipa;
struct qcom_smem_state *valid_state;
struct qcom_smem_state *enabled_state;
u32 valid_bit;
u32 enabled_bit;
u32 clock_query_irq;
u32 setup_ready_irq;
bool power_on;
bool notified;
bool setup_disabled;
struct mutex mutex;
struct notifier_block panic_notifier;
};
static void ipa_smp2p_notify(struct ipa_smp2p *smp2p)
{
struct device *dev;
u32 value;
u32 mask;
if (smp2p->notified)
return;
dev = &smp2p->ipa->pdev->dev;
smp2p->power_on = pm_runtime_get_if_active(dev, true) > 0;
mask = BIT(smp2p->enabled_bit);
value = smp2p->power_on ? mask : 0;
qcom_smem_state_update_bits(smp2p->enabled_state, mask, value);
mask = BIT(smp2p->valid_bit);
value = mask;
qcom_smem_state_update_bits(smp2p->valid_state, mask, value);
smp2p->notified = true;
}
static irqreturn_t ipa_smp2p_modem_clk_query_isr(int irq, void *dev_id)
{
struct ipa_smp2p *smp2p = dev_id;
ipa_smp2p_notify(smp2p);
return IRQ_HANDLED;
}
static int ipa_smp2p_panic_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{
struct ipa_smp2p *smp2p;
smp2p = container_of(nb, struct ipa_smp2p, panic_notifier);
ipa_smp2p_notify(smp2p);
if (smp2p->power_on)
ipa_uc_panic_notifier(smp2p->ipa);
return NOTIFY_DONE;
}
static int ipa_smp2p_panic_notifier_register(struct ipa_smp2p *smp2p)
{
smp2p->panic_notifier.notifier_call = ipa_smp2p_panic_notifier;
smp2p->panic_notifier.priority = INT_MAX;
return atomic_notifier_chain_register(&panic_notifier_list,
&smp2p->panic_notifier);
}
static void ipa_smp2p_panic_notifier_unregister(struct ipa_smp2p *smp2p)
{
atomic_notifier_chain_unregister(&panic_notifier_list,
&smp2p->panic_notifier);
}
static irqreturn_t ipa_smp2p_modem_setup_ready_isr(int irq, void *dev_id)
{
struct ipa_smp2p *smp2p = dev_id;
struct device *dev;
int ret;
if (smp2p->ipa->setup_complete)
return IRQ_HANDLED;
dev = &smp2p->ipa->pdev->dev;
ret = pm_runtime_get_sync(dev);
if (ret < 0) {
dev_err(dev, "error %d getting power for setup\n", ret);
goto out_power_put;
}
ret = ipa_setup(smp2p->ipa);
WARN(ret != 0, "error %d from ipa_setup()\n", ret);
out_power_put:
pm_runtime_mark_last_busy(dev);
(void)pm_runtime_put_autosuspend(dev);
return IRQ_HANDLED;
}
static int ipa_smp2p_irq_init(struct ipa_smp2p *smp2p, const char *name,
irq_handler_t handler)
{
struct device *dev = &smp2p->ipa->pdev->dev;
unsigned int irq;
int ret;
ret = platform_get_irq_byname(smp2p->ipa->pdev, name);
if (ret <= 0)
return ret ? : -EINVAL;
irq = ret;
ret = request_threaded_irq(irq, NULL, handler, 0, name, smp2p);
if (ret) {
dev_err(dev, "error %d requesting \"%s\" IRQ\n", ret, name);
return ret;
}
return irq;
}
static void ipa_smp2p_irq_exit(struct ipa_smp2p *smp2p, u32 irq)
{
free_irq(irq, smp2p);
}
static void ipa_smp2p_power_release(struct ipa *ipa)
{
struct device *dev = &ipa->pdev->dev;
if (!ipa->smp2p->power_on)
return;
pm_runtime_mark_last_busy(dev);
(void)pm_runtime_put_autosuspend(dev);
ipa->smp2p->power_on = false;
}
int ipa_smp2p_init(struct ipa *ipa, bool modem_init)
{
struct qcom_smem_state *enabled_state;
struct device *dev = &ipa->pdev->dev;
struct qcom_smem_state *valid_state;
struct ipa_smp2p *smp2p;
u32 enabled_bit;
u32 valid_bit;
int ret;
valid_state = qcom_smem_state_get(dev, "ipa-clock-enabled-valid",
&valid_bit);
if (IS_ERR(valid_state))
return PTR_ERR(valid_state);
if (valid_bit >= 32)
return -EINVAL;
enabled_state = qcom_smem_state_get(dev, "ipa-clock-enabled",
&enabled_bit);
if (IS_ERR(enabled_state))
return PTR_ERR(enabled_state);
if (enabled_bit >= 32)
return -EINVAL;
smp2p = kzalloc(sizeof(*smp2p), GFP_KERNEL);
if (!smp2p)
return -ENOMEM;
smp2p->ipa = ipa;
mutex_init(&smp2p->mutex);
smp2p->valid_state = valid_state;
smp2p->valid_bit = valid_bit;
smp2p->enabled_state = enabled_state;
smp2p->enabled_bit = enabled_bit;
ipa->smp2p = smp2p;
ret = ipa_smp2p_irq_init(smp2p, "ipa-clock-query",
ipa_smp2p_modem_clk_query_isr);
if (ret < 0)
goto err_null_smp2p;
smp2p->clock_query_irq = ret;
ret = ipa_smp2p_panic_notifier_register(smp2p);
if (ret)
goto err_irq_exit;
if (modem_init) {
ret = ipa_smp2p_irq_init(smp2p, "ipa-setup-ready",
ipa_smp2p_modem_setup_ready_isr);
if (ret < 0)
goto err_notifier_unregister;
smp2p->setup_ready_irq = ret;
}
return 0;
err_notifier_unregister:
ipa_smp2p_panic_notifier_unregister(smp2p);
err_irq_exit:
ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq);
err_null_smp2p:
ipa->smp2p = NULL;
mutex_destroy(&smp2p->mutex);
kfree(smp2p);
return ret;
}
void ipa_smp2p_exit(struct ipa *ipa)
{
struct ipa_smp2p *smp2p = ipa->smp2p;
if (smp2p->setup_ready_irq)
ipa_smp2p_irq_exit(smp2p, smp2p->setup_ready_irq);
ipa_smp2p_panic_notifier_unregister(smp2p);
ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq);
ipa_smp2p_power_release(ipa);
ipa->smp2p = NULL;
mutex_destroy(&smp2p->mutex);
kfree(smp2p);
}
void ipa_smp2p_irq_disable_setup(struct ipa *ipa)
{
struct ipa_smp2p *smp2p = ipa->smp2p;
if (!smp2p->setup_ready_irq)
return;
mutex_lock(&smp2p->mutex);
if (!smp2p->setup_disabled) {
disable_irq(smp2p->setup_ready_irq);
smp2p->setup_disabled = true;
}
mutex_unlock(&smp2p->mutex);
}
void ipa_smp2p_notify_reset(struct ipa *ipa)
{
struct ipa_smp2p *smp2p = ipa->smp2p;
u32 mask;
if (!smp2p->notified)
return;
ipa_smp2p_power_release(ipa);
mask = BIT(smp2p->valid_bit);
qcom_smem_state_update_bits(smp2p->valid_state, mask, 0);
mask = BIT(smp2p->enabled_bit);
qcom_smem_state_update_bits(smp2p->enabled_state, mask, 0);
smp2p->notified = false;
}