1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2021 Linaro Ltd.
8 #include <linux/device.h>
9 #include <linux/interconnect.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/bitops.h>
14 #include "linux/soc/qcom/qcom_aoss.h"
17 #include "ipa_power.h"
18 #include "ipa_endpoint.h"
19 #include "ipa_modem.h"
23 * DOC: IPA Power Management
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
31 * The core clock currently runs at a fixed clock rate when enabled,
32 * an all interconnects use a fixed average and peak bandwidth.
35 #define IPA_AUTOSUSPEND_DELAY 500 /* milliseconds */
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)
43 struct ipa_interconnect {
44 struct icc_path *path;
45 u32 average_bandwidth;
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
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 */
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
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;
85 static int ipa_interconnect_init_one(struct device *dev,
86 struct ipa_interconnect *interconnect,
87 const struct ipa_interconnect_data *data)
89 struct icc_path *path;
91 path = of_icc_get(dev, data->name);
93 int ret = PTR_ERR(path);
95 dev_err_probe(dev, ret, "error getting %s interconnect\n",
101 interconnect->path = path;
102 interconnect->average_bandwidth = data->average_bandwidth;
103 interconnect->peak_bandwidth = data->peak_bandwidth;
108 static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect)
110 icc_put(interconnect->path);
111 memset(interconnect, 0, sizeof(*interconnect));
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)
118 struct ipa_interconnect *interconnect;
122 count = power->interconnect_count;
123 interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL);
126 power->interconnect = interconnect;
129 ret = ipa_interconnect_init_one(dev, interconnect, data++);
138 while (interconnect-- > power->interconnect)
139 ipa_interconnect_exit_one(interconnect);
140 kfree(power->interconnect);
141 power->interconnect = NULL;
146 /* Inverse of ipa_interconnect_init() */
147 static void ipa_interconnect_exit(struct ipa_power *power)
149 struct ipa_interconnect *interconnect;
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;
158 /* Currently we only use one bandwidth level, so just "enable" interconnects */
159 static int ipa_interconnect_enable(struct ipa *ipa)
161 struct ipa_interconnect *interconnect;
162 struct ipa_power *power = ipa->power;
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);
172 dev_err(&ipa->pdev->dev,
173 "error %d enabling %s interconnect\n",
174 ret, icc_get_name(interconnect->path));
183 while (interconnect-- > power->interconnect)
184 (void)icc_set_bw(interconnect->path, 0, 0);
189 /* To disable an interconnect, we just its bandwidth to 0 */
190 static int ipa_interconnect_disable(struct ipa *ipa)
192 struct ipa_interconnect *interconnect;
193 struct ipa_power *power = ipa->power;
194 struct device *dev = &ipa->pdev->dev;
199 count = power->interconnect_count;
200 interconnect = power->interconnect + count;
203 ret = icc_set_bw(interconnect->path, 0, 0);
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 */
216 /* Enable IPA power, enabling interconnects and the core clock */
217 static int ipa_power_enable(struct ipa *ipa)
221 ret = ipa_interconnect_enable(ipa);
225 ret = clk_prepare_enable(ipa->power->core);
227 dev_err(&ipa->pdev->dev, "error %d enabling core clock\n", ret);
228 (void)ipa_interconnect_disable(ipa);
234 /* Inverse of ipa_power_enable() */
235 static int ipa_power_disable(struct ipa *ipa)
237 clk_disable_unprepare(ipa->power->core);
239 return ipa_interconnect_disable(ipa);
242 static int ipa_runtime_suspend(struct device *dev)
244 struct ipa *ipa = dev_get_drvdata(dev);
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);
253 return ipa_power_disable(ipa);
256 static int ipa_runtime_resume(struct device *dev)
258 struct ipa *ipa = dev_get_drvdata(dev);
261 ret = ipa_power_enable(ipa);
262 if (WARN_ON(ret < 0))
265 /* Endpoints aren't usable until setup is complete */
266 if (ipa->setup_complete) {
267 gsi_resume(&ipa->gsi);
268 ipa_endpoint_resume(ipa);
274 static int ipa_suspend(struct device *dev)
276 struct ipa *ipa = dev_get_drvdata(dev);
278 __set_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
280 return pm_runtime_force_suspend(dev);
283 static int ipa_resume(struct device *dev)
285 struct ipa *ipa = dev_get_drvdata(dev);
288 ret = pm_runtime_force_resume(dev);
290 __clear_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
295 /* Return the current IPA core clock rate */
296 u32 ipa_core_clock_rate(struct ipa *ipa)
298 return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
302 * ipa_suspend_handler() - Handle the suspend IPA interrupt
304 * @irq_id: IPA interrupt type (unused)
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.
311 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
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.
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);
321 /* Acknowledge/clear the suspend interrupt on all endpoints */
322 ipa_interrupt_suspend_clear_all(ipa->interrupt);
325 /* The next few functions coordinate stopping and starting the modem
326 * network device transmit queue.
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.
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.
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.
348 void ipa_power_modem_queue_stop(struct ipa *ipa)
350 struct ipa_power *power = ipa->power;
353 spin_lock_irqsave(&power->spinlock, flags);
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);
360 spin_unlock_irqrestore(&power->spinlock, flags);
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.
368 void ipa_power_modem_queue_wake(struct ipa *ipa)
370 struct ipa_power *power = ipa->power;
373 spin_lock_irqsave(&power->spinlock, flags);
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);
380 spin_unlock_irqrestore(&power->spinlock, flags);
383 /* This function clears the STARTED flag once the TX queue is operating */
384 void ipa_power_modem_queue_active(struct ipa *ipa)
386 clear_bit(IPA_POWER_FLAG_STARTED, ipa->power->flags);
389 static int ipa_power_retention_init(struct ipa_power *power)
391 struct qmp *qmp = qmp_get(power->dev);
394 if (PTR_ERR(qmp) == -EPROBE_DEFER)
395 return -EPROBE_DEFER;
397 /* We assume any other error means it's not defined/needed */
405 static void ipa_power_retention_exit(struct ipa_power *power)
411 /* Control register retention on power collapse */
412 void ipa_power_retention(struct ipa *ipa, bool enable)
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 */
420 return; /* Not needed on this platform */
422 (void)snprintf(buf, sizeof(buf), fmt, enable ? '1' : '0');
424 ret = qmp_send(power->qmp, buf, sizeof(buf));
426 dev_err(power->dev, "error %d sending QMP %sable request\n",
427 ret, enable ? "en" : "dis");
430 int ipa_power_setup(struct ipa *ipa)
434 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND,
435 ipa_suspend_handler);
437 ret = device_init_wakeup(&ipa->pdev->dev, true);
439 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
444 void ipa_power_teardown(struct ipa *ipa)
446 (void)device_init_wakeup(&ipa->pdev->dev, false);
447 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
450 /* Initialize IPA power management */
452 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
454 struct ipa_power *power;
458 clk = clk_get(dev, "core");
460 dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
462 return ERR_CAST(clk);
465 ret = clk_set_rate(clk, data->core_clock_rate);
467 dev_err(dev, "error %d setting core clock rate to %u\n",
468 ret, data->core_clock_rate);
472 power = kzalloc(sizeof(*power), GFP_KERNEL);
479 spin_lock_init(&power->spinlock);
480 power->interconnect_count = data->interconnect_count;
482 ret = ipa_interconnect_init(power, dev, data->interconnect_data);
486 ret = ipa_power_retention_init(power);
488 goto err_interconnect_exit;
490 pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
491 pm_runtime_use_autosuspend(dev);
492 pm_runtime_enable(dev);
496 err_interconnect_exit:
497 ipa_interconnect_exit(power);
506 /* Inverse of ipa_power_init() */
507 void ipa_power_exit(struct ipa_power *power)
509 struct device *dev = power->dev;
510 struct clk *clk = power->core;
512 pm_runtime_disable(dev);
513 pm_runtime_dont_use_autosuspend(dev);
514 ipa_power_retention_exit(power);
515 ipa_interconnect_exit(power);
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,