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>
15 #include "ipa_power.h"
16 #include "ipa_endpoint.h"
17 #include "ipa_modem.h"
21 * DOC: IPA Power Management
23 * The IPA hardware is enabled when the IPA core clock and all the
24 * interconnects (buses) it depends on are enabled. Runtime power
25 * management is used to determine whether the core clock and
26 * interconnects are enabled, and if not in use to be suspended
29 * The core clock currently runs at a fixed clock rate when enabled,
30 * an all interconnects use a fixed average and peak bandwidth.
33 #define IPA_AUTOSUSPEND_DELAY 500 /* milliseconds */
36 * struct ipa_interconnect - IPA interconnect information
37 * @path: Interconnect path
38 * @average_bandwidth: Average interconnect bandwidth (KB/second)
39 * @peak_bandwidth: Peak interconnect bandwidth (KB/second)
41 struct ipa_interconnect {
42 struct icc_path *path;
43 u32 average_bandwidth;
48 * enum ipa_power_flag - IPA power flags
49 * @IPA_POWER_FLAG_RESUMED: Whether resume from suspend has been signaled
50 * @IPA_POWER_FLAG_SYSTEM: Hardware is system (not runtime) suspended
51 * @IPA_POWER_FLAG_STOPPED: Modem TX is disabled by ipa_start_xmit()
52 * @IPA_POWER_FLAG_STARTED: Modem TX was enabled by ipa_runtime_resume()
53 * @IPA_POWER_FLAG_COUNT: Number of defined power flags
56 IPA_POWER_FLAG_RESUMED,
57 IPA_POWER_FLAG_SYSTEM,
58 IPA_POWER_FLAG_STOPPED,
59 IPA_POWER_FLAG_STARTED,
60 IPA_POWER_FLAG_COUNT, /* Last; not a flag */
64 * struct ipa_power - IPA power management information
65 * @dev: IPA device pointer
66 * @core: IPA core clock
67 * @spinlock: Protects modem TX queue enable/disable
68 * @flags: Boolean state flags
69 * @interconnect_count: Number of elements in interconnect[]
70 * @interconnect: Interconnect array
75 spinlock_t spinlock; /* used with STOPPED/STARTED power flags */
76 DECLARE_BITMAP(flags, IPA_POWER_FLAG_COUNT);
77 u32 interconnect_count;
78 struct ipa_interconnect *interconnect;
81 static int ipa_interconnect_init_one(struct device *dev,
82 struct ipa_interconnect *interconnect,
83 const struct ipa_interconnect_data *data)
85 struct icc_path *path;
87 path = of_icc_get(dev, data->name);
89 int ret = PTR_ERR(path);
91 dev_err_probe(dev, ret, "error getting %s interconnect\n",
97 interconnect->path = path;
98 interconnect->average_bandwidth = data->average_bandwidth;
99 interconnect->peak_bandwidth = data->peak_bandwidth;
104 static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect)
106 icc_put(interconnect->path);
107 memset(interconnect, 0, sizeof(*interconnect));
110 /* Initialize interconnects required for IPA operation */
111 static int ipa_interconnect_init(struct ipa_power *power, struct device *dev,
112 const struct ipa_interconnect_data *data)
114 struct ipa_interconnect *interconnect;
118 count = power->interconnect_count;
119 interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL);
122 power->interconnect = interconnect;
125 ret = ipa_interconnect_init_one(dev, interconnect, data++);
134 while (interconnect-- > power->interconnect)
135 ipa_interconnect_exit_one(interconnect);
136 kfree(power->interconnect);
137 power->interconnect = NULL;
142 /* Inverse of ipa_interconnect_init() */
143 static void ipa_interconnect_exit(struct ipa_power *power)
145 struct ipa_interconnect *interconnect;
147 interconnect = power->interconnect + power->interconnect_count;
148 while (interconnect-- > power->interconnect)
149 ipa_interconnect_exit_one(interconnect);
150 kfree(power->interconnect);
151 power->interconnect = NULL;
154 /* Currently we only use one bandwidth level, so just "enable" interconnects */
155 static int ipa_interconnect_enable(struct ipa *ipa)
157 struct ipa_interconnect *interconnect;
158 struct ipa_power *power = ipa->power;
162 interconnect = power->interconnect;
163 for (i = 0; i < power->interconnect_count; i++) {
164 ret = icc_set_bw(interconnect->path,
165 interconnect->average_bandwidth,
166 interconnect->peak_bandwidth);
168 dev_err(&ipa->pdev->dev,
169 "error %d enabling %s interconnect\n",
170 ret, icc_get_name(interconnect->path));
179 while (interconnect-- > power->interconnect)
180 (void)icc_set_bw(interconnect->path, 0, 0);
185 /* To disable an interconnect, we just its bandwidth to 0 */
186 static int ipa_interconnect_disable(struct ipa *ipa)
188 struct ipa_interconnect *interconnect;
189 struct ipa_power *power = ipa->power;
190 struct device *dev = &ipa->pdev->dev;
195 count = power->interconnect_count;
196 interconnect = power->interconnect + count;
199 ret = icc_set_bw(interconnect->path, 0, 0);
201 dev_err(dev, "error %d disabling %s interconnect\n",
202 ret, icc_get_name(interconnect->path));
203 /* Try to disable all; record only the first error */
212 /* Enable IPA power, enabling interconnects and the core clock */
213 static int ipa_power_enable(struct ipa *ipa)
217 ret = ipa_interconnect_enable(ipa);
221 ret = clk_prepare_enable(ipa->power->core);
223 dev_err(&ipa->pdev->dev, "error %d enabling core clock\n", ret);
224 (void)ipa_interconnect_disable(ipa);
230 /* Inverse of ipa_power_enable() */
231 static int ipa_power_disable(struct ipa *ipa)
233 clk_disable_unprepare(ipa->power->core);
235 return ipa_interconnect_disable(ipa);
238 static int ipa_runtime_suspend(struct device *dev)
240 struct ipa *ipa = dev_get_drvdata(dev);
242 /* Endpoints aren't usable until setup is complete */
243 if (ipa->setup_complete) {
244 __clear_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags);
245 ipa_endpoint_suspend(ipa);
246 gsi_suspend(&ipa->gsi);
249 return ipa_power_disable(ipa);
252 static int ipa_runtime_resume(struct device *dev)
254 struct ipa *ipa = dev_get_drvdata(dev);
257 ret = ipa_power_enable(ipa);
258 if (WARN_ON(ret < 0))
261 /* Endpoints aren't usable until setup is complete */
262 if (ipa->setup_complete) {
263 gsi_resume(&ipa->gsi);
264 ipa_endpoint_resume(ipa);
270 static int ipa_suspend(struct device *dev)
272 struct ipa *ipa = dev_get_drvdata(dev);
274 __set_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
276 return pm_runtime_force_suspend(dev);
279 static int ipa_resume(struct device *dev)
281 struct ipa *ipa = dev_get_drvdata(dev);
284 ret = pm_runtime_force_resume(dev);
286 __clear_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
291 /* Return the current IPA core clock rate */
292 u32 ipa_core_clock_rate(struct ipa *ipa)
294 return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
298 * ipa_suspend_handler() - Handle the suspend IPA interrupt
300 * @irq_id: IPA interrupt type (unused)
302 * If an RX endpoint is suspended, and the IPA has a packet destined for
303 * that endpoint, the IPA generates a SUSPEND interrupt to inform the AP
304 * that it should resume the endpoint. If we get one of these interrupts
305 * we just wake up the system.
307 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
309 /* To handle an IPA interrupt we will have resumed the hardware
310 * just to handle the interrupt, so we're done. If we are in a
311 * system suspend, trigger a system resume.
313 if (!__test_and_set_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags))
314 if (test_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags))
315 pm_wakeup_dev_event(&ipa->pdev->dev, 0, true);
317 /* Acknowledge/clear the suspend interrupt on all endpoints */
318 ipa_interrupt_suspend_clear_all(ipa->interrupt);
321 /* The next few functions coordinate stopping and starting the modem
322 * network device transmit queue.
324 * Transmit can be running concurrent with power resume, and there's a
325 * chance the resume completes before the transmit path stops the queue,
326 * leaving the queue in a stopped state. The next two functions are used
327 * to avoid this: ipa_power_modem_queue_stop() is used by ipa_start_xmit()
328 * to conditionally stop the TX queue; and ipa_power_modem_queue_start()
329 * is used by ipa_runtime_resume() to conditionally restart it.
331 * Two flags and a spinlock are used. If the queue is stopped, the STOPPED
332 * power flag is set. And if the queue is started, the STARTED flag is set.
333 * The queue is only started on resume if the STOPPED flag is set. And the
334 * queue is only started in ipa_start_xmit() if the STARTED flag is *not*
335 * set. As a result, the queue remains operational if the two activites
336 * happen concurrently regardless of the order they complete. The spinlock
337 * ensures the flag and TX queue operations are done atomically.
339 * The first function stops the modem netdev transmit queue, but only if
340 * the STARTED flag is *not* set. That flag is cleared if it was set.
341 * If the queue is stopped, the STOPPED flag is set. This is called only
342 * from the power ->runtime_resume operation.
344 void ipa_power_modem_queue_stop(struct ipa *ipa)
346 struct ipa_power *power = ipa->power;
349 spin_lock_irqsave(&power->spinlock, flags);
351 if (!__test_and_clear_bit(IPA_POWER_FLAG_STARTED, power->flags)) {
352 netif_stop_queue(ipa->modem_netdev);
353 __set_bit(IPA_POWER_FLAG_STOPPED, power->flags);
356 spin_unlock_irqrestore(&power->spinlock, flags);
359 /* This function starts the modem netdev transmit queue, but only if the
360 * STOPPED flag is set. That flag is cleared if it was set. If the queue
361 * was restarted, the STARTED flag is set; this allows ipa_start_xmit()
362 * to skip stopping the queue in the event of a race.
364 void ipa_power_modem_queue_wake(struct ipa *ipa)
366 struct ipa_power *power = ipa->power;
369 spin_lock_irqsave(&power->spinlock, flags);
371 if (__test_and_clear_bit(IPA_POWER_FLAG_STOPPED, power->flags)) {
372 __set_bit(IPA_POWER_FLAG_STARTED, power->flags);
373 netif_wake_queue(ipa->modem_netdev);
376 spin_unlock_irqrestore(&power->spinlock, flags);
379 /* This function clears the STARTED flag once the TX queue is operating */
380 void ipa_power_modem_queue_active(struct ipa *ipa)
382 clear_bit(IPA_POWER_FLAG_STARTED, ipa->power->flags);
385 int ipa_power_setup(struct ipa *ipa)
389 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND,
390 ipa_suspend_handler);
392 ret = device_init_wakeup(&ipa->pdev->dev, true);
394 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
399 void ipa_power_teardown(struct ipa *ipa)
401 (void)device_init_wakeup(&ipa->pdev->dev, false);
402 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
405 /* Initialize IPA power management */
407 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
409 struct ipa_power *power;
413 clk = clk_get(dev, "core");
415 dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
417 return ERR_CAST(clk);
420 ret = clk_set_rate(clk, data->core_clock_rate);
422 dev_err(dev, "error %d setting core clock rate to %u\n",
423 ret, data->core_clock_rate);
427 power = kzalloc(sizeof(*power), GFP_KERNEL);
434 spin_lock_init(&power->spinlock);
435 power->interconnect_count = data->interconnect_count;
437 ret = ipa_interconnect_init(power, dev, data->interconnect_data);
441 pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
442 pm_runtime_use_autosuspend(dev);
443 pm_runtime_enable(dev);
455 /* Inverse of ipa_power_init() */
456 void ipa_power_exit(struct ipa_power *power)
458 struct device *dev = power->dev;
459 struct clk *clk = power->core;
461 pm_runtime_disable(dev);
462 pm_runtime_dont_use_autosuspend(dev);
463 ipa_interconnect_exit(power);
468 const struct dev_pm_ops ipa_pm_ops = {
469 .suspend = ipa_suspend,
470 .resume = ipa_resume,
471 .runtime_suspend = ipa_runtime_suspend,
472 .runtime_resume = ipa_runtime_resume,