xref: /openbmc/linux/drivers/net/ipa/ipa_power.c (revision 91b0383f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2018-2021 Linaro Ltd.
5  */
6 
7 #include <linux/clk.h>
8 #include <linux/device.h>
9 #include <linux/interconnect.h>
10 #include <linux/pm.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/bitops.h>
13 
14 #include "linux/soc/qcom/qcom_aoss.h"
15 
16 #include "ipa.h"
17 #include "ipa_power.h"
18 #include "ipa_endpoint.h"
19 #include "ipa_modem.h"
20 #include "ipa_data.h"
21 
22 /**
23  * DOC: IPA Power Management
24  *
25  * The IPA hardware is enabled when the IPA core clock and all the
26  * interconnects (buses) it depends on are enabled.  Runtime power
27  * management is used to determine whether the core clock and
28  * interconnects are enabled, and if not in use to be suspended
29  * automatically.
30  *
31  * The core clock currently runs at a fixed clock rate when enabled,
32  * an all interconnects use a fixed average and peak bandwidth.
33  */
34 
35 #define IPA_AUTOSUSPEND_DELAY	500	/* milliseconds */
36 
37 /**
38  * struct ipa_interconnect - IPA interconnect information
39  * @path:		Interconnect path
40  * @average_bandwidth:	Average interconnect bandwidth (KB/second)
41  * @peak_bandwidth:	Peak interconnect bandwidth (KB/second)
42  */
43 struct ipa_interconnect {
44 	struct icc_path *path;
45 	u32 average_bandwidth;
46 	u32 peak_bandwidth;
47 };
48 
49 /**
50  * enum ipa_power_flag - IPA power flags
51  * @IPA_POWER_FLAG_RESUMED:	Whether resume from suspend has been signaled
52  * @IPA_POWER_FLAG_SYSTEM:	Hardware is system (not runtime) suspended
53  * @IPA_POWER_FLAG_STOPPED:	Modem TX is disabled by ipa_start_xmit()
54  * @IPA_POWER_FLAG_STARTED:	Modem TX was enabled by ipa_runtime_resume()
55  * @IPA_POWER_FLAG_COUNT:	Number of defined power flags
56  */
57 enum ipa_power_flag {
58 	IPA_POWER_FLAG_RESUMED,
59 	IPA_POWER_FLAG_SYSTEM,
60 	IPA_POWER_FLAG_STOPPED,
61 	IPA_POWER_FLAG_STARTED,
62 	IPA_POWER_FLAG_COUNT,		/* Last; not a flag */
63 };
64 
65 /**
66  * struct ipa_power - IPA power management information
67  * @dev:		IPA device pointer
68  * @core:		IPA core clock
69  * @qmp:		QMP handle for AOSS communication
70  * @spinlock:		Protects modem TX queue enable/disable
71  * @flags:		Boolean state flags
72  * @interconnect_count:	Number of elements in interconnect[]
73  * @interconnect:	Interconnect array
74  */
75 struct ipa_power {
76 	struct device *dev;
77 	struct clk *core;
78 	struct qmp *qmp;
79 	spinlock_t spinlock;	/* used with STOPPED/STARTED power flags */
80 	DECLARE_BITMAP(flags, IPA_POWER_FLAG_COUNT);
81 	u32 interconnect_count;
82 	struct ipa_interconnect *interconnect;
83 };
84 
85 static int ipa_interconnect_init_one(struct device *dev,
86 				     struct ipa_interconnect *interconnect,
87 				     const struct ipa_interconnect_data *data)
88 {
89 	struct icc_path *path;
90 
91 	path = of_icc_get(dev, data->name);
92 	if (IS_ERR(path)) {
93 		int ret = PTR_ERR(path);
94 
95 		dev_err_probe(dev, ret, "error getting %s interconnect\n",
96 			      data->name);
97 
98 		return ret;
99 	}
100 
101 	interconnect->path = path;
102 	interconnect->average_bandwidth = data->average_bandwidth;
103 	interconnect->peak_bandwidth = data->peak_bandwidth;
104 
105 	return 0;
106 }
107 
108 static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect)
109 {
110 	icc_put(interconnect->path);
111 	memset(interconnect, 0, sizeof(*interconnect));
112 }
113 
114 /* Initialize interconnects required for IPA operation */
115 static int ipa_interconnect_init(struct ipa_power *power, struct device *dev,
116 				 const struct ipa_interconnect_data *data)
117 {
118 	struct ipa_interconnect *interconnect;
119 	u32 count;
120 	int ret;
121 
122 	count = power->interconnect_count;
123 	interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL);
124 	if (!interconnect)
125 		return -ENOMEM;
126 	power->interconnect = interconnect;
127 
128 	while (count--) {
129 		ret = ipa_interconnect_init_one(dev, interconnect, data++);
130 		if (ret)
131 			goto out_unwind;
132 		interconnect++;
133 	}
134 
135 	return 0;
136 
137 out_unwind:
138 	while (interconnect-- > power->interconnect)
139 		ipa_interconnect_exit_one(interconnect);
140 	kfree(power->interconnect);
141 	power->interconnect = NULL;
142 
143 	return ret;
144 }
145 
146 /* Inverse of ipa_interconnect_init() */
147 static void ipa_interconnect_exit(struct ipa_power *power)
148 {
149 	struct ipa_interconnect *interconnect;
150 
151 	interconnect = power->interconnect + power->interconnect_count;
152 	while (interconnect-- > power->interconnect)
153 		ipa_interconnect_exit_one(interconnect);
154 	kfree(power->interconnect);
155 	power->interconnect = NULL;
156 }
157 
158 /* Currently we only use one bandwidth level, so just "enable" interconnects */
159 static int ipa_interconnect_enable(struct ipa *ipa)
160 {
161 	struct ipa_interconnect *interconnect;
162 	struct ipa_power *power = ipa->power;
163 	int ret;
164 	u32 i;
165 
166 	interconnect = power->interconnect;
167 	for (i = 0; i < power->interconnect_count; i++) {
168 		ret = icc_set_bw(interconnect->path,
169 				 interconnect->average_bandwidth,
170 				 interconnect->peak_bandwidth);
171 		if (ret) {
172 			dev_err(&ipa->pdev->dev,
173 				"error %d enabling %s interconnect\n",
174 				ret, icc_get_name(interconnect->path));
175 			goto out_unwind;
176 		}
177 		interconnect++;
178 	}
179 
180 	return 0;
181 
182 out_unwind:
183 	while (interconnect-- > power->interconnect)
184 		(void)icc_set_bw(interconnect->path, 0, 0);
185 
186 	return ret;
187 }
188 
189 /* To disable an interconnect, we just its bandwidth to 0 */
190 static int ipa_interconnect_disable(struct ipa *ipa)
191 {
192 	struct ipa_interconnect *interconnect;
193 	struct ipa_power *power = ipa->power;
194 	struct device *dev = &ipa->pdev->dev;
195 	int result = 0;
196 	u32 count;
197 	int ret;
198 
199 	count = power->interconnect_count;
200 	interconnect = power->interconnect + count;
201 	while (count--) {
202 		interconnect--;
203 		ret = icc_set_bw(interconnect->path, 0, 0);
204 		if (ret) {
205 			dev_err(dev, "error %d disabling %s interconnect\n",
206 				ret, icc_get_name(interconnect->path));
207 			/* Try to disable all; record only the first error */
208 			if (!result)
209 				result = ret;
210 		}
211 	}
212 
213 	return result;
214 }
215 
216 /* Enable IPA power, enabling interconnects and the core clock */
217 static int ipa_power_enable(struct ipa *ipa)
218 {
219 	int ret;
220 
221 	ret = ipa_interconnect_enable(ipa);
222 	if (ret)
223 		return ret;
224 
225 	ret = clk_prepare_enable(ipa->power->core);
226 	if (ret) {
227 		dev_err(&ipa->pdev->dev, "error %d enabling core clock\n", ret);
228 		(void)ipa_interconnect_disable(ipa);
229 	}
230 
231 	return ret;
232 }
233 
234 /* Inverse of ipa_power_enable() */
235 static int ipa_power_disable(struct ipa *ipa)
236 {
237 	clk_disable_unprepare(ipa->power->core);
238 
239 	return ipa_interconnect_disable(ipa);
240 }
241 
242 static int ipa_runtime_suspend(struct device *dev)
243 {
244 	struct ipa *ipa = dev_get_drvdata(dev);
245 
246 	/* Endpoints aren't usable until setup is complete */
247 	if (ipa->setup_complete) {
248 		__clear_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags);
249 		ipa_endpoint_suspend(ipa);
250 		gsi_suspend(&ipa->gsi);
251 	}
252 
253 	return ipa_power_disable(ipa);
254 }
255 
256 static int ipa_runtime_resume(struct device *dev)
257 {
258 	struct ipa *ipa = dev_get_drvdata(dev);
259 	int ret;
260 
261 	ret = ipa_power_enable(ipa);
262 	if (WARN_ON(ret < 0))
263 		return ret;
264 
265 	/* Endpoints aren't usable until setup is complete */
266 	if (ipa->setup_complete) {
267 		gsi_resume(&ipa->gsi);
268 		ipa_endpoint_resume(ipa);
269 	}
270 
271 	return 0;
272 }
273 
274 static int ipa_suspend(struct device *dev)
275 {
276 	struct ipa *ipa = dev_get_drvdata(dev);
277 
278 	__set_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
279 
280 	return pm_runtime_force_suspend(dev);
281 }
282 
283 static int ipa_resume(struct device *dev)
284 {
285 	struct ipa *ipa = dev_get_drvdata(dev);
286 	int ret;
287 
288 	ret = pm_runtime_force_resume(dev);
289 
290 	__clear_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
291 
292 	return ret;
293 }
294 
295 /* Return the current IPA core clock rate */
296 u32 ipa_core_clock_rate(struct ipa *ipa)
297 {
298 	return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
299 }
300 
301 /**
302  * ipa_suspend_handler() - Handle the suspend IPA interrupt
303  * @ipa:	IPA pointer
304  * @irq_id:	IPA interrupt type (unused)
305  *
306  * If an RX endpoint is suspended, and the IPA has a packet destined for
307  * that endpoint, the IPA generates a SUSPEND interrupt to inform the AP
308  * that it should resume the endpoint.  If we get one of these interrupts
309  * we just wake up the system.
310  */
311 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
312 {
313 	/* To handle an IPA interrupt we will have resumed the hardware
314 	 * just to handle the interrupt, so we're done.  If we are in a
315 	 * system suspend, trigger a system resume.
316 	 */
317 	if (!__test_and_set_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags))
318 		if (test_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags))
319 			pm_wakeup_dev_event(&ipa->pdev->dev, 0, true);
320 
321 	/* Acknowledge/clear the suspend interrupt on all endpoints */
322 	ipa_interrupt_suspend_clear_all(ipa->interrupt);
323 }
324 
325 /* The next few functions coordinate stopping and starting the modem
326  * network device transmit queue.
327  *
328  * Transmit can be running concurrent with power resume, and there's a
329  * chance the resume completes before the transmit path stops the queue,
330  * leaving the queue in a stopped state.  The next two functions are used
331  * to avoid this: ipa_power_modem_queue_stop() is used by ipa_start_xmit()
332  * to conditionally stop the TX queue; and ipa_power_modem_queue_start()
333  * is used by ipa_runtime_resume() to conditionally restart it.
334  *
335  * Two flags and a spinlock are used.  If the queue is stopped, the STOPPED
336  * power flag is set.  And if the queue is started, the STARTED flag is set.
337  * The queue is only started on resume if the STOPPED flag is set.  And the
338  * queue is only started in ipa_start_xmit() if the STARTED flag is *not*
339  * set.  As a result, the queue remains operational if the two activites
340  * happen concurrently regardless of the order they complete.  The spinlock
341  * ensures the flag and TX queue operations are done atomically.
342  *
343  * The first function stops the modem netdev transmit queue, but only if
344  * the STARTED flag is *not* set.  That flag is cleared if it was set.
345  * If the queue is stopped, the STOPPED flag is set.  This is called only
346  * from the power ->runtime_resume operation.
347  */
348 void ipa_power_modem_queue_stop(struct ipa *ipa)
349 {
350 	struct ipa_power *power = ipa->power;
351 	unsigned long flags;
352 
353 	spin_lock_irqsave(&power->spinlock, flags);
354 
355 	if (!__test_and_clear_bit(IPA_POWER_FLAG_STARTED, power->flags)) {
356 		netif_stop_queue(ipa->modem_netdev);
357 		__set_bit(IPA_POWER_FLAG_STOPPED, power->flags);
358 	}
359 
360 	spin_unlock_irqrestore(&power->spinlock, flags);
361 }
362 
363 /* This function starts the modem netdev transmit queue, but only if the
364  * STOPPED flag is set.  That flag is cleared if it was set.  If the queue
365  * was restarted, the STARTED flag is set; this allows ipa_start_xmit()
366  * to skip stopping the queue in the event of a race.
367  */
368 void ipa_power_modem_queue_wake(struct ipa *ipa)
369 {
370 	struct ipa_power *power = ipa->power;
371 	unsigned long flags;
372 
373 	spin_lock_irqsave(&power->spinlock, flags);
374 
375 	if (__test_and_clear_bit(IPA_POWER_FLAG_STOPPED, power->flags)) {
376 		__set_bit(IPA_POWER_FLAG_STARTED, power->flags);
377 		netif_wake_queue(ipa->modem_netdev);
378 	}
379 
380 	spin_unlock_irqrestore(&power->spinlock, flags);
381 }
382 
383 /* This function clears the STARTED flag once the TX queue is operating */
384 void ipa_power_modem_queue_active(struct ipa *ipa)
385 {
386 	clear_bit(IPA_POWER_FLAG_STARTED, ipa->power->flags);
387 }
388 
389 static int ipa_power_retention_init(struct ipa_power *power)
390 {
391 	struct qmp *qmp = qmp_get(power->dev);
392 
393 	if (IS_ERR(qmp)) {
394 		if (PTR_ERR(qmp) == -EPROBE_DEFER)
395 			return -EPROBE_DEFER;
396 
397 		/* We assume any other error means it's not defined/needed */
398 		qmp = NULL;
399 	}
400 	power->qmp = qmp;
401 
402 	return 0;
403 }
404 
405 static void ipa_power_retention_exit(struct ipa_power *power)
406 {
407 	qmp_put(power->qmp);
408 	power->qmp = NULL;
409 }
410 
411 /* Control register retention on power collapse */
412 void ipa_power_retention(struct ipa *ipa, bool enable)
413 {
414 	static const char fmt[] = "{ class: bcm, res: ipa_pc, val: %c }";
415 	struct ipa_power *power = ipa->power;
416 	char buf[36];	/* Exactly enough for fmt[]; size a multiple of 4 */
417 	int ret;
418 
419 	if (!power->qmp)
420 		return;		/* Not needed on this platform */
421 
422 	(void)snprintf(buf, sizeof(buf), fmt, enable ? '1' : '0');
423 
424 	ret = qmp_send(power->qmp, buf, sizeof(buf));
425 	if (ret)
426 		dev_err(power->dev, "error %d sending QMP %sable request\n",
427 			ret, enable ? "en" : "dis");
428 }
429 
430 int ipa_power_setup(struct ipa *ipa)
431 {
432 	int ret;
433 
434 	ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND,
435 			  ipa_suspend_handler);
436 
437 	ret = device_init_wakeup(&ipa->pdev->dev, true);
438 	if (ret)
439 		ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
440 
441 	return ret;
442 }
443 
444 void ipa_power_teardown(struct ipa *ipa)
445 {
446 	(void)device_init_wakeup(&ipa->pdev->dev, false);
447 	ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
448 }
449 
450 /* Initialize IPA power management */
451 struct ipa_power *
452 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
453 {
454 	struct ipa_power *power;
455 	struct clk *clk;
456 	int ret;
457 
458 	clk = clk_get(dev, "core");
459 	if (IS_ERR(clk)) {
460 		dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
461 
462 		return ERR_CAST(clk);
463 	}
464 
465 	ret = clk_set_rate(clk, data->core_clock_rate);
466 	if (ret) {
467 		dev_err(dev, "error %d setting core clock rate to %u\n",
468 			ret, data->core_clock_rate);
469 		goto err_clk_put;
470 	}
471 
472 	power = kzalloc(sizeof(*power), GFP_KERNEL);
473 	if (!power) {
474 		ret = -ENOMEM;
475 		goto err_clk_put;
476 	}
477 	power->dev = dev;
478 	power->core = clk;
479 	spin_lock_init(&power->spinlock);
480 	power->interconnect_count = data->interconnect_count;
481 
482 	ret = ipa_interconnect_init(power, dev, data->interconnect_data);
483 	if (ret)
484 		goto err_kfree;
485 
486 	ret = ipa_power_retention_init(power);
487 	if (ret)
488 		goto err_interconnect_exit;
489 
490 	pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
491 	pm_runtime_use_autosuspend(dev);
492 	pm_runtime_enable(dev);
493 
494 	return power;
495 
496 err_interconnect_exit:
497 	ipa_interconnect_exit(power);
498 err_kfree:
499 	kfree(power);
500 err_clk_put:
501 	clk_put(clk);
502 
503 	return ERR_PTR(ret);
504 }
505 
506 /* Inverse of ipa_power_init() */
507 void ipa_power_exit(struct ipa_power *power)
508 {
509 	struct device *dev = power->dev;
510 	struct clk *clk = power->core;
511 
512 	pm_runtime_disable(dev);
513 	pm_runtime_dont_use_autosuspend(dev);
514 	ipa_power_retention_exit(power);
515 	ipa_interconnect_exit(power);
516 	kfree(power);
517 	clk_put(clk);
518 }
519 
520 const struct dev_pm_ops ipa_pm_ops = {
521 	.suspend		= ipa_suspend,
522 	.resume			= ipa_resume,
523 	.runtime_suspend	= ipa_runtime_suspend,
524 	.runtime_resume		= ipa_runtime_resume,
525 };
526