1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2024 Linaro Ltd.
7 #include <linux/interrupt.h>
8 #include <linux/notifier.h>
9 #include <linux/panic_notifier.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/types.h>
14 #include <linux/soc/qcom/smem_state.h>
17 #include "ipa_smp2p.h"
21 * DOC: IPA SMP2P communication with the modem
23 * SMP2P is a primitive communication mechanism available between the AP and
24 * the modem. The IPA driver uses this for two purposes: to enable the modem
25 * to state that the GSI hardware is ready to use; and to communicate the
26 * state of IPA power in the event of a crash.
28 * GSI needs to have early initialization completed before it can be used.
29 * This initialization is done either by Trust Zone or by the modem. In the
30 * latter case, the modem uses an SMP2P interrupt to tell the AP IPA driver
31 * when the GSI is ready to use.
33 * The modem is also able to inquire about the current state of IPA
34 * power by trigging another SMP2P interrupt to the AP. We communicate
35 * whether power is enabled using two SMP2P state bits--one to indicate
36 * the power state (on or off), and a second to indicate the power state
37 * bit is valid. The modem will poll the valid bit until it is set, and
38 * at that time records whether the AP has IPA power enabled.
40 * Finally, if the AP kernel panics, we update the SMP2P state bits even if
41 * we never receive an interrupt from the modem requesting this.
45 * struct ipa_smp2p - IPA SMP2P information
47 * @valid_state: SMEM state indicating enabled state is valid
48 * @enabled_state: SMEM state to indicate power is enabled
49 * @valid_bit: Valid bit in 32-bit SMEM state mask
50 * @enabled_bit: Enabled bit in 32-bit SMEM state mask
51 * @enabled_bit: Enabled bit in 32-bit SMEM state mask
52 * @clock_query_irq: IPA interrupt triggered by modem for power query
53 * @setup_ready_irq: IPA interrupt triggered by modem to signal GSI ready
54 * @power_on: Whether IPA power is on
55 * @notified: Whether modem has been notified of power state
56 * @setup_disabled: Whether setup ready interrupt handler is disabled
57 * @mutex: Mutex protecting ready-interrupt/shutdown interlock
58 * @panic_notifier: Panic notifier structure
62 struct qcom_smem_state
*valid_state
;
63 struct qcom_smem_state
*enabled_state
;
72 struct notifier_block panic_notifier
;
76 * ipa_smp2p_notify() - use SMP2P to tell modem about IPA power state
77 * @smp2p: SMP2P information
79 * This is called either when the modem has requested it (by triggering
80 * the modem power query IPA interrupt) or whenever the AP is shutting down
81 * (via a panic notifier). It sets the two SMP2P state bits--one saying
82 * whether the IPA power is on, and the other indicating the first bit
85 static void ipa_smp2p_notify(struct ipa_smp2p
*smp2p
)
93 smp2p
->power_on
= pm_runtime_get_if_active(smp2p
->ipa
->dev
) > 0;
95 /* Signal whether the IPA power is enabled */
96 mask
= BIT(smp2p
->enabled_bit
);
97 value
= smp2p
->power_on
? mask
: 0;
98 qcom_smem_state_update_bits(smp2p
->enabled_state
, mask
, value
);
100 /* Now indicate that the enabled flag is valid */
101 mask
= BIT(smp2p
->valid_bit
);
103 qcom_smem_state_update_bits(smp2p
->valid_state
, mask
, value
);
105 smp2p
->notified
= true;
108 /* Threaded IRQ handler for modem "ipa-clock-query" SMP2P interrupt */
109 static irqreturn_t
ipa_smp2p_modem_clk_query_isr(int irq
, void *dev_id
)
111 struct ipa_smp2p
*smp2p
= dev_id
;
113 ipa_smp2p_notify(smp2p
);
118 static int ipa_smp2p_panic_notifier(struct notifier_block
*nb
,
119 unsigned long action
, void *data
)
121 struct ipa_smp2p
*smp2p
;
123 smp2p
= container_of(nb
, struct ipa_smp2p
, panic_notifier
);
125 ipa_smp2p_notify(smp2p
);
128 ipa_uc_panic_notifier(smp2p
->ipa
);
133 static int ipa_smp2p_panic_notifier_register(struct ipa_smp2p
*smp2p
)
135 /* IPA panic handler needs to run before modem shuts down */
136 smp2p
->panic_notifier
.notifier_call
= ipa_smp2p_panic_notifier
;
137 smp2p
->panic_notifier
.priority
= INT_MAX
; /* Do it early */
139 return atomic_notifier_chain_register(&panic_notifier_list
,
140 &smp2p
->panic_notifier
);
143 static void ipa_smp2p_panic_notifier_unregister(struct ipa_smp2p
*smp2p
)
145 atomic_notifier_chain_unregister(&panic_notifier_list
,
146 &smp2p
->panic_notifier
);
149 /* Threaded IRQ handler for modem "ipa-setup-ready" SMP2P interrupt */
150 static irqreturn_t
ipa_smp2p_modem_setup_ready_isr(int irq
, void *dev_id
)
152 struct ipa_smp2p
*smp2p
= dev_id
;
153 struct ipa
*ipa
= smp2p
->ipa
;
157 /* Ignore any (spurious) interrupts received after the first */
158 if (ipa
->setup_complete
)
161 /* Power needs to be active for setup */
163 ret
= pm_runtime_get_sync(dev
);
165 dev_err(dev
, "error %d getting power for setup\n", ret
);
169 /* An error here won't cause driver shutdown, so warn if one occurs */
170 ret
= ipa_setup(ipa
);
171 WARN(ret
!= 0, "error %d from ipa_setup()\n", ret
);
174 pm_runtime_mark_last_busy(dev
);
175 (void)pm_runtime_put_autosuspend(dev
);
180 /* Initialize SMP2P interrupts */
181 static int ipa_smp2p_irq_init(struct ipa_smp2p
*smp2p
,
182 struct platform_device
*pdev
,
183 const char *name
, irq_handler_t handler
)
185 struct device
*dev
= &pdev
->dev
;
189 ret
= platform_get_irq_byname(pdev
, name
);
191 return ret
? : -EINVAL
;
194 ret
= request_threaded_irq(irq
, NULL
, handler
, 0, name
, smp2p
);
196 dev_err(dev
, "error %d requesting \"%s\" IRQ\n", ret
, name
);
203 static void ipa_smp2p_irq_exit(struct ipa_smp2p
*smp2p
, u32 irq
)
205 free_irq(irq
, smp2p
);
208 /* Drop the power reference if it was taken in ipa_smp2p_notify() */
209 static void ipa_smp2p_power_release(struct ipa
*ipa
)
211 struct device
*dev
= ipa
->dev
;
213 if (!ipa
->smp2p
->power_on
)
216 pm_runtime_mark_last_busy(dev
);
217 (void)pm_runtime_put_autosuspend(dev
);
218 ipa
->smp2p
->power_on
= false;
221 /* Initialize the IPA SMP2P subsystem */
223 ipa_smp2p_init(struct ipa
*ipa
, struct platform_device
*pdev
, bool modem_init
)
225 struct qcom_smem_state
*enabled_state
;
226 struct device
*dev
= &pdev
->dev
;
227 struct qcom_smem_state
*valid_state
;
228 struct ipa_smp2p
*smp2p
;
233 valid_state
= qcom_smem_state_get(dev
, "ipa-clock-enabled-valid",
235 if (IS_ERR(valid_state
))
236 return PTR_ERR(valid_state
);
237 if (valid_bit
>= 32) /* BITS_PER_U32 */
240 enabled_state
= qcom_smem_state_get(dev
, "ipa-clock-enabled",
242 if (IS_ERR(enabled_state
))
243 return PTR_ERR(enabled_state
);
244 if (enabled_bit
>= 32) /* BITS_PER_U32 */
247 smp2p
= kzalloc(sizeof(*smp2p
), GFP_KERNEL
);
253 /* These fields are needed by the power query interrupt
254 * handler, so initialize them now.
256 mutex_init(&smp2p
->mutex
);
257 smp2p
->valid_state
= valid_state
;
258 smp2p
->valid_bit
= valid_bit
;
259 smp2p
->enabled_state
= enabled_state
;
260 smp2p
->enabled_bit
= enabled_bit
;
262 /* We have enough information saved to handle notifications */
265 ret
= ipa_smp2p_irq_init(smp2p
, pdev
, "ipa-clock-query",
266 ipa_smp2p_modem_clk_query_isr
);
269 smp2p
->clock_query_irq
= ret
;
271 ret
= ipa_smp2p_panic_notifier_register(smp2p
);
276 /* Result will be non-zero (negative for error) */
277 ret
= ipa_smp2p_irq_init(smp2p
, pdev
, "ipa-setup-ready",
278 ipa_smp2p_modem_setup_ready_isr
);
280 goto err_notifier_unregister
;
281 smp2p
->setup_ready_irq
= ret
;
286 err_notifier_unregister
:
287 ipa_smp2p_panic_notifier_unregister(smp2p
);
289 ipa_smp2p_irq_exit(smp2p
, smp2p
->clock_query_irq
);
292 mutex_destroy(&smp2p
->mutex
);
298 void ipa_smp2p_exit(struct ipa
*ipa
)
300 struct ipa_smp2p
*smp2p
= ipa
->smp2p
;
302 if (smp2p
->setup_ready_irq
)
303 ipa_smp2p_irq_exit(smp2p
, smp2p
->setup_ready_irq
);
304 ipa_smp2p_panic_notifier_unregister(smp2p
);
305 ipa_smp2p_irq_exit(smp2p
, smp2p
->clock_query_irq
);
306 /* We won't get notified any more; drop power reference (if any) */
307 ipa_smp2p_power_release(ipa
);
309 mutex_destroy(&smp2p
->mutex
);
313 void ipa_smp2p_irq_disable_setup(struct ipa
*ipa
)
315 struct ipa_smp2p
*smp2p
= ipa
->smp2p
;
317 if (!smp2p
->setup_ready_irq
)
320 mutex_lock(&smp2p
->mutex
);
322 if (!smp2p
->setup_disabled
) {
323 disable_irq(smp2p
->setup_ready_irq
);
324 smp2p
->setup_disabled
= true;
327 mutex_unlock(&smp2p
->mutex
);
330 /* Reset state tracking whether we have notified the modem */
331 void ipa_smp2p_notify_reset(struct ipa
*ipa
)
333 struct ipa_smp2p
*smp2p
= ipa
->smp2p
;
336 if (!smp2p
->notified
)
339 ipa_smp2p_power_release(ipa
);
341 /* Reset the power enabled valid flag */
342 mask
= BIT(smp2p
->valid_bit
);
343 qcom_smem_state_update_bits(smp2p
->valid_state
, mask
, 0);
345 /* Mark the power disabled for good measure... */
346 mask
= BIT(smp2p
->enabled_bit
);
347 qcom_smem_state_update_bits(smp2p
->enabled_state
, mask
, 0);
349 smp2p
->notified
= false;