xref: /linux/drivers/net/ipa/ipa_smp2p.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2024 Linaro Ltd.
5  */
6 
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>
13 
14 #include <linux/soc/qcom/smem_state.h>
15 
16 #include "ipa.h"
17 #include "ipa_smp2p.h"
18 #include "ipa_uc.h"
19 
20 /**
21  * DOC: IPA SMP2P communication with the modem
22  *
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.
27  *
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.
32  *
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.
39  *
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.
42  */
43 
44 /**
45  * struct ipa_smp2p - IPA SMP2P information
46  * @ipa:		IPA pointer
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
59 */
60 struct ipa_smp2p {
61 	struct ipa *ipa;
62 	struct qcom_smem_state *valid_state;
63 	struct qcom_smem_state *enabled_state;
64 	u32 valid_bit;
65 	u32 enabled_bit;
66 	u32 clock_query_irq;
67 	u32 setup_ready_irq;
68 	bool power_on;
69 	bool notified;
70 	bool setup_disabled;
71 	struct mutex mutex;
72 	struct notifier_block panic_notifier;
73 };
74 
75 /**
76  * ipa_smp2p_notify() - use SMP2P to tell modem about IPA power state
77  * @smp2p:	SMP2P information
78  *
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
83  * is valid.
84  */
85 static void ipa_smp2p_notify(struct ipa_smp2p *smp2p)
86 {
87 	u32 value;
88 	u32 mask;
89 
90 	if (smp2p->notified)
91 		return;
92 
93 	smp2p->power_on = pm_runtime_get_if_active(smp2p->ipa->dev) > 0;
94 
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);
99 
100 	/* Now indicate that the enabled flag is valid */
101 	mask = BIT(smp2p->valid_bit);
102 	value = mask;
103 	qcom_smem_state_update_bits(smp2p->valid_state, mask, value);
104 
105 	smp2p->notified = true;
106 }
107 
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)
110 {
111 	struct ipa_smp2p *smp2p = dev_id;
112 
113 	ipa_smp2p_notify(smp2p);
114 
115 	return IRQ_HANDLED;
116 }
117 
118 static int ipa_smp2p_panic_notifier(struct notifier_block *nb,
119 				    unsigned long action, void *data)
120 {
121 	struct ipa_smp2p *smp2p;
122 
123 	smp2p = container_of(nb, struct ipa_smp2p, panic_notifier);
124 
125 	ipa_smp2p_notify(smp2p);
126 
127 	if (smp2p->power_on)
128 		ipa_uc_panic_notifier(smp2p->ipa);
129 
130 	return NOTIFY_DONE;
131 }
132 
133 static int ipa_smp2p_panic_notifier_register(struct ipa_smp2p *smp2p)
134 {
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 */
138 
139 	return atomic_notifier_chain_register(&panic_notifier_list,
140 					      &smp2p->panic_notifier);
141 }
142 
143 static void ipa_smp2p_panic_notifier_unregister(struct ipa_smp2p *smp2p)
144 {
145 	atomic_notifier_chain_unregister(&panic_notifier_list,
146 					 &smp2p->panic_notifier);
147 }
148 
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)
151 {
152 	struct ipa_smp2p *smp2p = dev_id;
153 	struct ipa *ipa = smp2p->ipa;
154 	struct device *dev;
155 	int ret;
156 
157 	/* Ignore any (spurious) interrupts received after the first */
158 	if (ipa->setup_complete)
159 		return IRQ_HANDLED;
160 
161 	/* Power needs to be active for setup */
162 	dev = ipa->dev;
163 	ret = pm_runtime_get_sync(dev);
164 	if (ret < 0) {
165 		dev_err(dev, "error %d getting power for setup\n", ret);
166 		goto out_power_put;
167 	}
168 
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);
172 
173 out_power_put:
174 	(void)pm_runtime_put_autosuspend(dev);
175 
176 	return IRQ_HANDLED;
177 }
178 
179 /* Initialize SMP2P interrupts */
180 static int ipa_smp2p_irq_init(struct ipa_smp2p *smp2p,
181 			      struct platform_device *pdev,
182 			      const char *name, irq_handler_t handler)
183 {
184 	struct device *dev = &pdev->dev;
185 	unsigned int irq;
186 	int ret;
187 
188 	ret = platform_get_irq_byname(pdev, name);
189 	if (ret <= 0)
190 		return ret ? : -EINVAL;
191 	irq = ret;
192 
193 	ret = request_threaded_irq(irq, NULL, handler, 0, name, smp2p);
194 	if (ret) {
195 		dev_err(dev, "error %d requesting \"%s\" IRQ\n", ret, name);
196 		return ret;
197 	}
198 
199 	return irq;
200 }
201 
202 static void ipa_smp2p_irq_exit(struct ipa_smp2p *smp2p, u32 irq)
203 {
204 	free_irq(irq, smp2p);
205 }
206 
207 /* Drop the power reference if it was taken in ipa_smp2p_notify() */
208 static void ipa_smp2p_power_release(struct ipa *ipa)
209 {
210 	struct device *dev = ipa->dev;
211 
212 	if (!ipa->smp2p->power_on)
213 		return;
214 
215 	(void)pm_runtime_put_autosuspend(dev);
216 	ipa->smp2p->power_on = false;
217 }
218 
219 /* Initialize the IPA SMP2P subsystem */
220 int
221 ipa_smp2p_init(struct ipa *ipa, struct platform_device *pdev, bool modem_init)
222 {
223 	struct qcom_smem_state *enabled_state;
224 	struct device *dev = &pdev->dev;
225 	struct qcom_smem_state *valid_state;
226 	struct ipa_smp2p *smp2p;
227 	u32 enabled_bit;
228 	u32 valid_bit;
229 	int ret;
230 
231 	valid_state = qcom_smem_state_get(dev, "ipa-clock-enabled-valid",
232 					  &valid_bit);
233 	if (IS_ERR(valid_state))
234 		return PTR_ERR(valid_state);
235 	if (valid_bit >= 32) {		/* BITS_PER_U32 */
236 		ret = -EINVAL;
237 		goto err_valid_state_put;
238 	}
239 
240 	enabled_state = qcom_smem_state_get(dev, "ipa-clock-enabled",
241 					    &enabled_bit);
242 	if (IS_ERR(enabled_state)) {
243 		ret = PTR_ERR(enabled_state);
244 		goto err_valid_state_put;
245 	}
246 	if (enabled_bit >= 32) {		/* BITS_PER_U32 */
247 		ret = -EINVAL;
248 		goto err_enabled_state_put;
249 	}
250 
251 	smp2p = kzalloc_obj(*smp2p);
252 	if (!smp2p) {
253 		ret = -ENOMEM;
254 		goto err_enabled_state_put;
255 	}
256 
257 	smp2p->ipa = ipa;
258 
259 	/* These fields are needed by the power query interrupt
260 	 * handler, so initialize them now.
261 	 */
262 	mutex_init(&smp2p->mutex);
263 	smp2p->valid_state = valid_state;
264 	smp2p->valid_bit = valid_bit;
265 	smp2p->enabled_state = enabled_state;
266 	smp2p->enabled_bit = enabled_bit;
267 
268 	/* We have enough information saved to handle notifications */
269 	ipa->smp2p = smp2p;
270 
271 	ret = ipa_smp2p_irq_init(smp2p, pdev, "ipa-clock-query",
272 				 ipa_smp2p_modem_clk_query_isr);
273 	if (ret < 0)
274 		goto err_null_smp2p;
275 	smp2p->clock_query_irq = ret;
276 
277 	ret = ipa_smp2p_panic_notifier_register(smp2p);
278 	if (ret)
279 		goto err_irq_exit;
280 
281 	if (modem_init) {
282 		/* Result will be non-zero (negative for error) */
283 		ret = ipa_smp2p_irq_init(smp2p, pdev, "ipa-setup-ready",
284 					 ipa_smp2p_modem_setup_ready_isr);
285 		if (ret < 0)
286 			goto err_notifier_unregister;
287 		smp2p->setup_ready_irq = ret;
288 	}
289 
290 	return 0;
291 
292 err_notifier_unregister:
293 	ipa_smp2p_panic_notifier_unregister(smp2p);
294 err_irq_exit:
295 	ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq);
296 err_null_smp2p:
297 	ipa->smp2p = NULL;
298 	mutex_destroy(&smp2p->mutex);
299 	kfree(smp2p);
300 err_enabled_state_put:
301 	qcom_smem_state_put(enabled_state);
302 err_valid_state_put:
303 	qcom_smem_state_put(valid_state);
304 
305 	return ret;
306 }
307 
308 void ipa_smp2p_exit(struct ipa *ipa)
309 {
310 	struct ipa_smp2p *smp2p = ipa->smp2p;
311 
312 	if (smp2p->setup_ready_irq)
313 		ipa_smp2p_irq_exit(smp2p, smp2p->setup_ready_irq);
314 	ipa_smp2p_panic_notifier_unregister(smp2p);
315 	ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq);
316 	/* We won't get notified any more; drop power reference (if any) */
317 	ipa_smp2p_power_release(ipa);
318 	ipa->smp2p = NULL;
319 	mutex_destroy(&smp2p->mutex);
320 	qcom_smem_state_put(smp2p->enabled_state);
321 	qcom_smem_state_put(smp2p->valid_state);
322 	kfree(smp2p);
323 }
324 
325 void ipa_smp2p_irq_disable_setup(struct ipa *ipa)
326 {
327 	struct ipa_smp2p *smp2p = ipa->smp2p;
328 
329 	if (!smp2p->setup_ready_irq)
330 		return;
331 
332 	mutex_lock(&smp2p->mutex);
333 
334 	if (!smp2p->setup_disabled) {
335 		disable_irq(smp2p->setup_ready_irq);
336 		smp2p->setup_disabled = true;
337 	}
338 
339 	mutex_unlock(&smp2p->mutex);
340 }
341 
342 /* Reset state tracking whether we have notified the modem */
343 void ipa_smp2p_notify_reset(struct ipa *ipa)
344 {
345 	struct ipa_smp2p *smp2p = ipa->smp2p;
346 	u32 mask;
347 
348 	if (!smp2p->notified)
349 		return;
350 
351 	ipa_smp2p_power_release(ipa);
352 
353 	/* Reset the power enabled valid flag */
354 	mask = BIT(smp2p->valid_bit);
355 	qcom_smem_state_update_bits(smp2p->valid_state, mask, 0);
356 
357 	/* Mark the power disabled for good measure... */
358 	mask = BIT(smp2p->enabled_bit);
359 	qcom_smem_state_update_bits(smp2p->enabled_state, mask, 0);
360 
361 	smp2p->notified = false;
362 }
363