• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ipa_interconnect_init_one(struct device * dev,struct ipa_interconnect * interconnect,const struct ipa_interconnect_data * data)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 
ipa_interconnect_exit_one(struct ipa_interconnect * interconnect)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 */
ipa_interconnect_init(struct ipa_power * power,struct device * dev,const struct ipa_interconnect_data * data)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() */
ipa_interconnect_exit(struct ipa_power * power)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 */
ipa_interconnect_enable(struct ipa * ipa)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 */
ipa_interconnect_disable(struct ipa * ipa)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 */
ipa_power_enable(struct ipa * ipa)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() */
ipa_power_disable(struct ipa * ipa)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 
ipa_runtime_suspend(struct device * dev)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 
ipa_runtime_resume(struct device * dev)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 
ipa_suspend(struct device * dev)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 	/* Increment the disable depth to ensure that the IRQ won't
281 	 * be re-enabled until the matching _enable call in
282 	 * ipa_resume(). We do this to ensure that the interrupt
283 	 * handler won't run whilst PM runtime is disabled.
284 	 *
285 	 * Note that disabling the IRQ is NOT the same as disabling
286 	 * irq wake. If wakeup is enabled for the IPA then the IRQ
287 	 * will still cause the system to wake up, see irq_set_irq_wake().
288 	 */
289 	ipa_interrupt_irq_disable(ipa);
290 
291 	return pm_runtime_force_suspend(dev);
292 }
293 
ipa_resume(struct device * dev)294 static int ipa_resume(struct device *dev)
295 {
296 	struct ipa *ipa = dev_get_drvdata(dev);
297 	int ret;
298 
299 	ret = pm_runtime_force_resume(dev);
300 
301 	__clear_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
302 
303 	/* Now that PM runtime is enabled again it's safe
304 	 * to turn the IRQ back on and process any data
305 	 * that was received during suspend.
306 	 */
307 	ipa_interrupt_irq_enable(ipa);
308 
309 	return ret;
310 }
311 
312 /* Return the current IPA core clock rate */
ipa_core_clock_rate(struct ipa * ipa)313 u32 ipa_core_clock_rate(struct ipa *ipa)
314 {
315 	return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
316 }
317 
318 /**
319  * ipa_suspend_handler() - Handle the suspend IPA interrupt
320  * @ipa:	IPA pointer
321  * @irq_id:	IPA interrupt type (unused)
322  *
323  * If an RX endpoint is suspended, and the IPA has a packet destined for
324  * that endpoint, the IPA generates a SUSPEND interrupt to inform the AP
325  * that it should resume the endpoint.  If we get one of these interrupts
326  * we just wake up the system.
327  */
ipa_suspend_handler(struct ipa * ipa,enum ipa_irq_id irq_id)328 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
329 {
330 	/* To handle an IPA interrupt we will have resumed the hardware
331 	 * just to handle the interrupt, so we're done.  If we are in a
332 	 * system suspend, trigger a system resume.
333 	 */
334 	if (!__test_and_set_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags))
335 		if (test_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags))
336 			pm_wakeup_dev_event(&ipa->pdev->dev, 0, true);
337 
338 	/* Acknowledge/clear the suspend interrupt on all endpoints */
339 	ipa_interrupt_suspend_clear_all(ipa->interrupt);
340 }
341 
342 /* The next few functions coordinate stopping and starting the modem
343  * network device transmit queue.
344  *
345  * Transmit can be running concurrent with power resume, and there's a
346  * chance the resume completes before the transmit path stops the queue,
347  * leaving the queue in a stopped state.  The next two functions are used
348  * to avoid this: ipa_power_modem_queue_stop() is used by ipa_start_xmit()
349  * to conditionally stop the TX queue; and ipa_power_modem_queue_start()
350  * is used by ipa_runtime_resume() to conditionally restart it.
351  *
352  * Two flags and a spinlock are used.  If the queue is stopped, the STOPPED
353  * power flag is set.  And if the queue is started, the STARTED flag is set.
354  * The queue is only started on resume if the STOPPED flag is set.  And the
355  * queue is only started in ipa_start_xmit() if the STARTED flag is *not*
356  * set.  As a result, the queue remains operational if the two activites
357  * happen concurrently regardless of the order they complete.  The spinlock
358  * ensures the flag and TX queue operations are done atomically.
359  *
360  * The first function stops the modem netdev transmit queue, but only if
361  * the STARTED flag is *not* set.  That flag is cleared if it was set.
362  * If the queue is stopped, the STOPPED flag is set.  This is called only
363  * from the power ->runtime_resume operation.
364  */
ipa_power_modem_queue_stop(struct ipa * ipa)365 void ipa_power_modem_queue_stop(struct ipa *ipa)
366 {
367 	struct ipa_power *power = ipa->power;
368 	unsigned long flags;
369 
370 	spin_lock_irqsave(&power->spinlock, flags);
371 
372 	if (!__test_and_clear_bit(IPA_POWER_FLAG_STARTED, power->flags)) {
373 		netif_stop_queue(ipa->modem_netdev);
374 		__set_bit(IPA_POWER_FLAG_STOPPED, power->flags);
375 	}
376 
377 	spin_unlock_irqrestore(&power->spinlock, flags);
378 }
379 
380 /* This function starts the modem netdev transmit queue, but only if the
381  * STOPPED flag is set.  That flag is cleared if it was set.  If the queue
382  * was restarted, the STARTED flag is set; this allows ipa_start_xmit()
383  * to skip stopping the queue in the event of a race.
384  */
ipa_power_modem_queue_wake(struct ipa * ipa)385 void ipa_power_modem_queue_wake(struct ipa *ipa)
386 {
387 	struct ipa_power *power = ipa->power;
388 	unsigned long flags;
389 
390 	spin_lock_irqsave(&power->spinlock, flags);
391 
392 	if (__test_and_clear_bit(IPA_POWER_FLAG_STOPPED, power->flags)) {
393 		__set_bit(IPA_POWER_FLAG_STARTED, power->flags);
394 		netif_wake_queue(ipa->modem_netdev);
395 	}
396 
397 	spin_unlock_irqrestore(&power->spinlock, flags);
398 }
399 
400 /* This function clears the STARTED flag once the TX queue is operating */
ipa_power_modem_queue_active(struct ipa * ipa)401 void ipa_power_modem_queue_active(struct ipa *ipa)
402 {
403 	clear_bit(IPA_POWER_FLAG_STARTED, ipa->power->flags);
404 }
405 
ipa_power_retention_init(struct ipa_power * power)406 static int ipa_power_retention_init(struct ipa_power *power)
407 {
408 	struct qmp *qmp = qmp_get(power->dev);
409 
410 	if (IS_ERR(qmp)) {
411 		if (PTR_ERR(qmp) == -EPROBE_DEFER)
412 			return -EPROBE_DEFER;
413 
414 		/* We assume any other error means it's not defined/needed */
415 		qmp = NULL;
416 	}
417 	power->qmp = qmp;
418 
419 	return 0;
420 }
421 
ipa_power_retention_exit(struct ipa_power * power)422 static void ipa_power_retention_exit(struct ipa_power *power)
423 {
424 	qmp_put(power->qmp);
425 	power->qmp = NULL;
426 }
427 
428 /* Control register retention on power collapse */
ipa_power_retention(struct ipa * ipa,bool enable)429 void ipa_power_retention(struct ipa *ipa, bool enable)
430 {
431 	static const char fmt[] = "{ class: bcm, res: ipa_pc, val: %c }";
432 	struct ipa_power *power = ipa->power;
433 	char buf[36];	/* Exactly enough for fmt[]; size a multiple of 4 */
434 	int ret;
435 
436 	if (!power->qmp)
437 		return;		/* Not needed on this platform */
438 
439 	(void)snprintf(buf, sizeof(buf), fmt, enable ? '1' : '0');
440 
441 	ret = qmp_send(power->qmp, buf, sizeof(buf));
442 	if (ret)
443 		dev_err(power->dev, "error %d sending QMP %sable request\n",
444 			ret, enable ? "en" : "dis");
445 }
446 
ipa_power_setup(struct ipa * ipa)447 int ipa_power_setup(struct ipa *ipa)
448 {
449 	int ret;
450 
451 	ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND,
452 			  ipa_suspend_handler);
453 
454 	ret = device_init_wakeup(&ipa->pdev->dev, true);
455 	if (ret)
456 		ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
457 
458 	return ret;
459 }
460 
ipa_power_teardown(struct ipa * ipa)461 void ipa_power_teardown(struct ipa *ipa)
462 {
463 	(void)device_init_wakeup(&ipa->pdev->dev, false);
464 	ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
465 }
466 
467 /* Initialize IPA power management */
468 struct ipa_power *
ipa_power_init(struct device * dev,const struct ipa_power_data * data)469 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
470 {
471 	struct ipa_power *power;
472 	struct clk *clk;
473 	int ret;
474 
475 	clk = clk_get(dev, "core");
476 	if (IS_ERR(clk)) {
477 		dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
478 
479 		return ERR_CAST(clk);
480 	}
481 
482 	ret = clk_set_rate(clk, data->core_clock_rate);
483 	if (ret) {
484 		dev_err(dev, "error %d setting core clock rate to %u\n",
485 			ret, data->core_clock_rate);
486 		goto err_clk_put;
487 	}
488 
489 	power = kzalloc(sizeof(*power), GFP_KERNEL);
490 	if (!power) {
491 		ret = -ENOMEM;
492 		goto err_clk_put;
493 	}
494 	power->dev = dev;
495 	power->core = clk;
496 	spin_lock_init(&power->spinlock);
497 	power->interconnect_count = data->interconnect_count;
498 
499 	ret = ipa_interconnect_init(power, dev, data->interconnect_data);
500 	if (ret)
501 		goto err_kfree;
502 
503 	ret = ipa_power_retention_init(power);
504 	if (ret)
505 		goto err_interconnect_exit;
506 
507 	pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
508 	pm_runtime_use_autosuspend(dev);
509 	pm_runtime_enable(dev);
510 
511 	return power;
512 
513 err_interconnect_exit:
514 	ipa_interconnect_exit(power);
515 err_kfree:
516 	kfree(power);
517 err_clk_put:
518 	clk_put(clk);
519 
520 	return ERR_PTR(ret);
521 }
522 
523 /* Inverse of ipa_power_init() */
ipa_power_exit(struct ipa_power * power)524 void ipa_power_exit(struct ipa_power *power)
525 {
526 	struct device *dev = power->dev;
527 	struct clk *clk = power->core;
528 
529 	pm_runtime_disable(dev);
530 	pm_runtime_dont_use_autosuspend(dev);
531 	ipa_power_retention_exit(power);
532 	ipa_interconnect_exit(power);
533 	kfree(power);
534 	clk_put(clk);
535 }
536 
537 const struct dev_pm_ops ipa_pm_ops = {
538 	.suspend		= ipa_suspend,
539 	.resume			= ipa_resume,
540 	.runtime_suspend	= ipa_runtime_suspend,
541 	.runtime_resume		= ipa_runtime_resume,
542 };
543