Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2019-2020 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/soc/qcom/smem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/soc/qcom/smem_state.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "ipa_smp2p.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "ipa.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "ipa_uc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "ipa_clock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * DOC: IPA SMP2P communication with the modem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * SMP2P is a primitive communication mechanism available between the AP and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * the modem.  The IPA driver uses this for two purposes:  to enable the modem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * to state that the GSI hardware is ready to use; and to communicate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * state of the IPA clock in the event of a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * GSI needs to have early initialization completed before it can be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * This initialization is done either by Trust Zone or by the modem.  In the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * latter case, the modem uses an SMP2P interrupt to tell the AP IPA driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * when the GSI is ready to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * The modem is also able to inquire about the current state of the IPA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * clock by trigging another SMP2P interrupt to the AP.  We communicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * whether the clock is enabled using two SMP2P state bits--one to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * indicate the clock state (on or off), and a second to indicate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * clock state bit is valid.  The modem will poll the valid bit until it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * is set, and at that time records whether the AP has the IPA clock enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * Finally, if the AP kernel panics, we update the SMP2P state bits even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * we never receive an interrupt from the modem requesting this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * struct ipa_smp2p - IPA SMP2P information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * @ipa:		IPA pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * @valid_state:	SMEM state indicating enabled state is valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * @enabled_state:	SMEM state to indicate clock is enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * @valid_bit:		Valid bit in 32-bit SMEM state mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * @enabled_bit:	Enabled bit in 32-bit SMEM state mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * @enabled_bit:	Enabled bit in 32-bit SMEM state mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * @clock_query_irq:	IPA interrupt triggered by modem for clock query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * @setup_ready_irq:	IPA interrupt triggered by modem to signal GSI ready
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * @clock_on:		Whether IPA clock is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * @notified:		Whether modem has been notified of clock state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * @disabled:		Whether setup ready interrupt handling is disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * @mutex:		Mutex protecting ready-interrupt/shutdown interlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * @panic_notifier:	Panic notifier structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) struct ipa_smp2p {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct ipa *ipa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct qcom_smem_state *valid_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct qcom_smem_state *enabled_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	u32 valid_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u32 enabled_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	u32 clock_query_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	u32 setup_ready_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	bool clock_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	bool notified;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	bool disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct notifier_block panic_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * ipa_smp2p_notify() - use SMP2P to tell modem about IPA clock state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * @smp2p:	SMP2P information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * This is called either when the modem has requested it (by triggering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * the modem clock query IPA interrupt) or whenever the AP is shutting down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * (via a panic notifier).  It sets the two SMP2P state bits--one saying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * whether the IPA clock is running, and the other indicating the first bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static void ipa_smp2p_notify(struct ipa_smp2p *smp2p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (smp2p->notified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	smp2p->clock_on = ipa_clock_get_additional(smp2p->ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* Signal whether the clock is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	mask = BIT(smp2p->enabled_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	value = smp2p->clock_on ? mask : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	qcom_smem_state_update_bits(smp2p->enabled_state, mask, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	/* Now indicate that the enabled flag is valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	mask = BIT(smp2p->valid_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	value = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	qcom_smem_state_update_bits(smp2p->valid_state, mask, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	smp2p->notified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Threaded IRQ handler for modem "ipa-clock-query" SMP2P interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static irqreturn_t ipa_smp2p_modem_clk_query_isr(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct ipa_smp2p *smp2p = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ipa_smp2p_notify(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int ipa_smp2p_panic_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				    unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct ipa_smp2p *smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	smp2p = container_of(nb, struct ipa_smp2p, panic_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	ipa_smp2p_notify(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (smp2p->clock_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ipa_uc_panic_notifier(smp2p->ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int ipa_smp2p_panic_notifier_register(struct ipa_smp2p *smp2p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/* IPA panic handler needs to run before modem shuts down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	smp2p->panic_notifier.notifier_call = ipa_smp2p_panic_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	smp2p->panic_notifier.priority = INT_MAX;	/* Do it early */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return atomic_notifier_chain_register(&panic_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 					      &smp2p->panic_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void ipa_smp2p_panic_notifier_unregister(struct ipa_smp2p *smp2p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	atomic_notifier_chain_unregister(&panic_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 					 &smp2p->panic_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Threaded IRQ handler for modem "ipa-setup-ready" SMP2P interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static irqreturn_t ipa_smp2p_modem_setup_ready_isr(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct ipa_smp2p *smp2p = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	mutex_lock(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (!smp2p->disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		ret = ipa_setup(smp2p->ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			dev_err(&smp2p->ipa->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				"error %d from ipa_setup()\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		smp2p->disabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	mutex_unlock(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* Initialize SMP2P interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int ipa_smp2p_irq_init(struct ipa_smp2p *smp2p, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			      irq_handler_t handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct device *dev = &smp2p->ipa->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	ret = platform_get_irq_byname(smp2p->ipa->pdev, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		dev_err(dev, "DT error %d getting \"%s\" IRQ property\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			ret, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return ret ? : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	ret = request_threaded_irq(irq, NULL, handler, 0, name, smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		dev_err(dev, "error %d requesting \"%s\" IRQ\n", ret, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static void ipa_smp2p_irq_exit(struct ipa_smp2p *smp2p, u32 irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	free_irq(irq, smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* Drop the clock reference if it was taken in ipa_smp2p_notify() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void ipa_smp2p_clock_release(struct ipa *ipa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (!ipa->smp2p->clock_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ipa_clock_put(ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	ipa->smp2p->clock_on = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* Initialize the IPA SMP2P subsystem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int ipa_smp2p_init(struct ipa *ipa, bool modem_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct qcom_smem_state *enabled_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct device *dev = &ipa->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct qcom_smem_state *valid_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct ipa_smp2p *smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	u32 enabled_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	u32 valid_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	valid_state = qcom_smem_state_get(dev, "ipa-clock-enabled-valid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 					  &valid_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (IS_ERR(valid_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return PTR_ERR(valid_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (valid_bit >= 32)		/* BITS_PER_U32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	enabled_state = qcom_smem_state_get(dev, "ipa-clock-enabled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					    &enabled_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (IS_ERR(enabled_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return PTR_ERR(enabled_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (enabled_bit >= 32)		/* BITS_PER_U32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	smp2p = kzalloc(sizeof(*smp2p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (!smp2p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	smp2p->ipa = ipa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	/* These fields are needed by the clock query interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * handler, so initialize them now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	mutex_init(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	smp2p->valid_state = valid_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	smp2p->valid_bit = valid_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	smp2p->enabled_state = enabled_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	smp2p->enabled_bit = enabled_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* We have enough information saved to handle notifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	ipa->smp2p = smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	ret = ipa_smp2p_irq_init(smp2p, "ipa-clock-query",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				 ipa_smp2p_modem_clk_query_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		goto err_null_smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	smp2p->clock_query_irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	ret = ipa_smp2p_panic_notifier_register(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		goto err_irq_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (modem_init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		/* Result will be non-zero (negative for error) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		ret = ipa_smp2p_irq_init(smp2p, "ipa-setup-ready",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 					 ipa_smp2p_modem_setup_ready_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			goto err_notifier_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		smp2p->setup_ready_irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) err_notifier_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	ipa_smp2p_panic_notifier_unregister(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err_irq_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err_null_smp2p:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	ipa->smp2p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	mutex_destroy(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	kfree(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) void ipa_smp2p_exit(struct ipa *ipa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct ipa_smp2p *smp2p = ipa->smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (smp2p->setup_ready_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		ipa_smp2p_irq_exit(smp2p, smp2p->setup_ready_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	ipa_smp2p_panic_notifier_unregister(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	/* We won't get notified any more; drop clock reference (if any) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	ipa_smp2p_clock_release(ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	ipa->smp2p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	mutex_destroy(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	kfree(smp2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) void ipa_smp2p_disable(struct ipa *ipa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct ipa_smp2p *smp2p = ipa->smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (!smp2p->setup_ready_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	mutex_lock(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	smp2p->disabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	mutex_unlock(&smp2p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* Reset state tracking whether we have notified the modem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) void ipa_smp2p_notify_reset(struct ipa *ipa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct ipa_smp2p *smp2p = ipa->smp2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (!smp2p->notified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	ipa_smp2p_clock_release(ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	/* Reset the clock enabled valid flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	mask = BIT(smp2p->valid_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	qcom_smem_state_update_bits(smp2p->valid_state, mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	/* Mark the clock disabled for good measure... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	mask = BIT(smp2p->enabled_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	qcom_smem_state_update_bits(smp2p->enabled_state, mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	smp2p->notified = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }