1 // SPDX-License-Identifier: GPL-2.0
2
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2022 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 * enum ipa_power_flag - IPA power flags
39 * @IPA_POWER_FLAG_RESUMED: Whether resume from suspend has been signaled
40 * @IPA_POWER_FLAG_SYSTEM: Hardware is system (not runtime) suspended
41 * @IPA_POWER_FLAG_STOPPED: Modem TX is disabled by ipa_start_xmit()
42 * @IPA_POWER_FLAG_STARTED: Modem TX was enabled by ipa_runtime_resume()
43 * @IPA_POWER_FLAG_COUNT: Number of defined power flags
44 */
45 enum ipa_power_flag {
46 IPA_POWER_FLAG_RESUMED,
47 IPA_POWER_FLAG_SYSTEM,
48 IPA_POWER_FLAG_STOPPED,
49 IPA_POWER_FLAG_STARTED,
50 IPA_POWER_FLAG_COUNT, /* Last; not a flag */
51 };
52
53 /**
54 * struct ipa_power - IPA power management information
55 * @dev: IPA device pointer
56 * @core: IPA core clock
57 * @qmp: QMP handle for AOSS communication
58 * @spinlock: Protects modem TX queue enable/disable
59 * @flags: Boolean state flags
60 * @interconnect_count: Number of elements in interconnect[]
61 * @interconnect: Interconnect array
62 */
63 struct ipa_power {
64 struct device *dev;
65 struct clk *core;
66 struct qmp *qmp;
67 spinlock_t spinlock; /* used with STOPPED/STARTED power flags */
68 DECLARE_BITMAP(flags, IPA_POWER_FLAG_COUNT);
69 u32 interconnect_count;
70 struct icc_bulk_data interconnect[];
71 };
72
73 /* Initialize interconnects required for IPA operation */
ipa_interconnect_init(struct ipa_power * power,const struct ipa_interconnect_data * data)74 static int ipa_interconnect_init(struct ipa_power *power,
75 const struct ipa_interconnect_data *data)
76 {
77 struct icc_bulk_data *interconnect;
78 int ret;
79 u32 i;
80
81 /* Initialize our interconnect data array for bulk operations */
82 interconnect = &power->interconnect[0];
83 for (i = 0; i < power->interconnect_count; i++) {
84 /* interconnect->path is filled in by of_icc_bulk_get() */
85 interconnect->name = data->name;
86 interconnect->avg_bw = data->average_bandwidth;
87 interconnect->peak_bw = data->peak_bandwidth;
88 data++;
89 interconnect++;
90 }
91
92 ret = of_icc_bulk_get(power->dev, power->interconnect_count,
93 power->interconnect);
94 if (ret)
95 return ret;
96
97 /* All interconnects are initially disabled */
98 icc_bulk_disable(power->interconnect_count, power->interconnect);
99
100 /* Set the bandwidth values to be used when enabled */
101 ret = icc_bulk_set_bw(power->interconnect_count, power->interconnect);
102 if (ret)
103 icc_bulk_put(power->interconnect_count, power->interconnect);
104
105 return ret;
106 }
107
108 /* Inverse of ipa_interconnect_init() */
ipa_interconnect_exit(struct ipa_power * power)109 static void ipa_interconnect_exit(struct ipa_power *power)
110 {
111 icc_bulk_put(power->interconnect_count, power->interconnect);
112 }
113
114 /* Enable IPA power, enabling interconnects and the core clock */
ipa_power_enable(struct ipa * ipa)115 static int ipa_power_enable(struct ipa *ipa)
116 {
117 struct ipa_power *power = ipa->power;
118 int ret;
119
120 ret = icc_bulk_enable(power->interconnect_count, power->interconnect);
121 if (ret)
122 return ret;
123
124 ret = clk_prepare_enable(power->core);
125 if (ret) {
126 dev_err(power->dev, "error %d enabling core clock\n", ret);
127 icc_bulk_disable(power->interconnect_count,
128 power->interconnect);
129 }
130
131 return ret;
132 }
133
134 /* Inverse of ipa_power_enable() */
ipa_power_disable(struct ipa * ipa)135 static void ipa_power_disable(struct ipa *ipa)
136 {
137 struct ipa_power *power = ipa->power;
138
139 clk_disable_unprepare(power->core);
140
141 icc_bulk_disable(power->interconnect_count, power->interconnect);
142 }
143
ipa_runtime_suspend(struct device * dev)144 static int ipa_runtime_suspend(struct device *dev)
145 {
146 struct ipa *ipa = dev_get_drvdata(dev);
147
148 /* Endpoints aren't usable until setup is complete */
149 if (ipa->setup_complete) {
150 __clear_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags);
151 ipa_endpoint_suspend(ipa);
152 gsi_suspend(&ipa->gsi);
153 }
154
155 ipa_power_disable(ipa);
156
157 return 0;
158 }
159
ipa_runtime_resume(struct device * dev)160 static int ipa_runtime_resume(struct device *dev)
161 {
162 struct ipa *ipa = dev_get_drvdata(dev);
163 int ret;
164
165 ret = ipa_power_enable(ipa);
166 if (WARN_ON(ret < 0))
167 return ret;
168
169 /* Endpoints aren't usable until setup is complete */
170 if (ipa->setup_complete) {
171 gsi_resume(&ipa->gsi);
172 ipa_endpoint_resume(ipa);
173 }
174
175 return 0;
176 }
177
ipa_suspend(struct device * dev)178 static int ipa_suspend(struct device *dev)
179 {
180 struct ipa *ipa = dev_get_drvdata(dev);
181
182 __set_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
183
184 /* Increment the disable depth to ensure that the IRQ won't
185 * be re-enabled until the matching _enable call in
186 * ipa_resume(). We do this to ensure that the interrupt
187 * handler won't run whilst PM runtime is disabled.
188 *
189 * Note that disabling the IRQ is NOT the same as disabling
190 * irq wake. If wakeup is enabled for the IPA then the IRQ
191 * will still cause the system to wake up, see irq_set_irq_wake().
192 */
193 ipa_interrupt_irq_disable(ipa);
194
195 return pm_runtime_force_suspend(dev);
196 }
197
ipa_resume(struct device * dev)198 static int ipa_resume(struct device *dev)
199 {
200 struct ipa *ipa = dev_get_drvdata(dev);
201 int ret;
202
203 ret = pm_runtime_force_resume(dev);
204
205 __clear_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
206
207 /* Now that PM runtime is enabled again it's safe
208 * to turn the IRQ back on and process any data
209 * that was received during suspend.
210 */
211 ipa_interrupt_irq_enable(ipa);
212
213 return ret;
214 }
215
216 /* Return the current IPA core clock rate */
ipa_core_clock_rate(struct ipa * ipa)217 u32 ipa_core_clock_rate(struct ipa *ipa)
218 {
219 return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
220 }
221
222 /**
223 * ipa_suspend_handler() - Handle the suspend IPA interrupt
224 * @ipa: IPA pointer
225 * @irq_id: IPA interrupt type (unused)
226 *
227 * If an RX endpoint is suspended, and the IPA has a packet destined for
228 * that endpoint, the IPA generates a SUSPEND interrupt to inform the AP
229 * that it should resume the endpoint. If we get one of these interrupts
230 * we just wake up the system.
231 */
ipa_suspend_handler(struct ipa * ipa,enum ipa_irq_id irq_id)232 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
233 {
234 /* To handle an IPA interrupt we will have resumed the hardware
235 * just to handle the interrupt, so we're done. If we are in a
236 * system suspend, trigger a system resume.
237 */
238 if (!__test_and_set_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags))
239 if (test_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags))
240 pm_wakeup_dev_event(&ipa->pdev->dev, 0, true);
241
242 /* Acknowledge/clear the suspend interrupt on all endpoints */
243 ipa_interrupt_suspend_clear_all(ipa->interrupt);
244 }
245
246 /* The next few functions coordinate stopping and starting the modem
247 * network device transmit queue.
248 *
249 * Transmit can be running concurrent with power resume, and there's a
250 * chance the resume completes before the transmit path stops the queue,
251 * leaving the queue in a stopped state. The next two functions are used
252 * to avoid this: ipa_power_modem_queue_stop() is used by ipa_start_xmit()
253 * to conditionally stop the TX queue; and ipa_power_modem_queue_start()
254 * is used by ipa_runtime_resume() to conditionally restart it.
255 *
256 * Two flags and a spinlock are used. If the queue is stopped, the STOPPED
257 * power flag is set. And if the queue is started, the STARTED flag is set.
258 * The queue is only started on resume if the STOPPED flag is set. And the
259 * queue is only started in ipa_start_xmit() if the STARTED flag is *not*
260 * set. As a result, the queue remains operational if the two activites
261 * happen concurrently regardless of the order they complete. The spinlock
262 * ensures the flag and TX queue operations are done atomically.
263 *
264 * The first function stops the modem netdev transmit queue, but only if
265 * the STARTED flag is *not* set. That flag is cleared if it was set.
266 * If the queue is stopped, the STOPPED flag is set. This is called only
267 * from the power ->runtime_resume operation.
268 */
ipa_power_modem_queue_stop(struct ipa * ipa)269 void ipa_power_modem_queue_stop(struct ipa *ipa)
270 {
271 struct ipa_power *power = ipa->power;
272 unsigned long flags;
273
274 spin_lock_irqsave(&power->spinlock, flags);
275
276 if (!__test_and_clear_bit(IPA_POWER_FLAG_STARTED, power->flags)) {
277 netif_stop_queue(ipa->modem_netdev);
278 __set_bit(IPA_POWER_FLAG_STOPPED, power->flags);
279 }
280
281 spin_unlock_irqrestore(&power->spinlock, flags);
282 }
283
284 /* This function starts the modem netdev transmit queue, but only if the
285 * STOPPED flag is set. That flag is cleared if it was set. If the queue
286 * was restarted, the STARTED flag is set; this allows ipa_start_xmit()
287 * to skip stopping the queue in the event of a race.
288 */
ipa_power_modem_queue_wake(struct ipa * ipa)289 void ipa_power_modem_queue_wake(struct ipa *ipa)
290 {
291 struct ipa_power *power = ipa->power;
292 unsigned long flags;
293
294 spin_lock_irqsave(&power->spinlock, flags);
295
296 if (__test_and_clear_bit(IPA_POWER_FLAG_STOPPED, power->flags)) {
297 __set_bit(IPA_POWER_FLAG_STARTED, power->flags);
298 netif_wake_queue(ipa->modem_netdev);
299 }
300
301 spin_unlock_irqrestore(&power->spinlock, flags);
302 }
303
304 /* This function clears the STARTED flag once the TX queue is operating */
ipa_power_modem_queue_active(struct ipa * ipa)305 void ipa_power_modem_queue_active(struct ipa *ipa)
306 {
307 clear_bit(IPA_POWER_FLAG_STARTED, ipa->power->flags);
308 }
309
ipa_power_retention_init(struct ipa_power * power)310 static int ipa_power_retention_init(struct ipa_power *power)
311 {
312 struct qmp *qmp = qmp_get(power->dev);
313
314 if (IS_ERR(qmp)) {
315 if (PTR_ERR(qmp) == -EPROBE_DEFER)
316 return -EPROBE_DEFER;
317
318 /* We assume any other error means it's not defined/needed */
319 qmp = NULL;
320 }
321 power->qmp = qmp;
322
323 return 0;
324 }
325
ipa_power_retention_exit(struct ipa_power * power)326 static void ipa_power_retention_exit(struct ipa_power *power)
327 {
328 qmp_put(power->qmp);
329 power->qmp = NULL;
330 }
331
332 /* Control register retention on power collapse */
ipa_power_retention(struct ipa * ipa,bool enable)333 void ipa_power_retention(struct ipa *ipa, bool enable)
334 {
335 static const char fmt[] = "{ class: bcm, res: ipa_pc, val: %c }";
336 struct ipa_power *power = ipa->power;
337 char buf[36]; /* Exactly enough for fmt[]; size a multiple of 4 */
338 int ret;
339
340 if (!power->qmp)
341 return; /* Not needed on this platform */
342
343 (void)snprintf(buf, sizeof(buf), fmt, enable ? '1' : '0');
344
345 ret = qmp_send(power->qmp, buf, sizeof(buf));
346 if (ret)
347 dev_err(power->dev, "error %d sending QMP %sable request\n",
348 ret, enable ? "en" : "dis");
349 }
350
ipa_power_setup(struct ipa * ipa)351 int ipa_power_setup(struct ipa *ipa)
352 {
353 int ret;
354
355 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND,
356 ipa_suspend_handler);
357
358 ret = device_init_wakeup(&ipa->pdev->dev, true);
359 if (ret)
360 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
361
362 return ret;
363 }
364
ipa_power_teardown(struct ipa * ipa)365 void ipa_power_teardown(struct ipa *ipa)
366 {
367 (void)device_init_wakeup(&ipa->pdev->dev, false);
368 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
369 }
370
371 /* Initialize IPA power management */
372 struct ipa_power *
ipa_power_init(struct device * dev,const struct ipa_power_data * data)373 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
374 {
375 struct ipa_power *power;
376 struct clk *clk;
377 size_t size;
378 int ret;
379
380 clk = clk_get(dev, "core");
381 if (IS_ERR(clk)) {
382 dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
383
384 return ERR_CAST(clk);
385 }
386
387 ret = clk_set_rate(clk, data->core_clock_rate);
388 if (ret) {
389 dev_err(dev, "error %d setting core clock rate to %u\n",
390 ret, data->core_clock_rate);
391 goto err_clk_put;
392 }
393
394 size = struct_size(power, interconnect, data->interconnect_count);
395 power = kzalloc(size, GFP_KERNEL);
396 if (!power) {
397 ret = -ENOMEM;
398 goto err_clk_put;
399 }
400 power->dev = dev;
401 power->core = clk;
402 spin_lock_init(&power->spinlock);
403 power->interconnect_count = data->interconnect_count;
404
405 ret = ipa_interconnect_init(power, data->interconnect_data);
406 if (ret)
407 goto err_kfree;
408
409 ret = ipa_power_retention_init(power);
410 if (ret)
411 goto err_interconnect_exit;
412
413 pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
414 pm_runtime_use_autosuspend(dev);
415 pm_runtime_enable(dev);
416
417 return power;
418
419 err_interconnect_exit:
420 ipa_interconnect_exit(power);
421 err_kfree:
422 kfree(power);
423 err_clk_put:
424 clk_put(clk);
425
426 return ERR_PTR(ret);
427 }
428
429 /* Inverse of ipa_power_init() */
ipa_power_exit(struct ipa_power * power)430 void ipa_power_exit(struct ipa_power *power)
431 {
432 struct device *dev = power->dev;
433 struct clk *clk = power->core;
434
435 pm_runtime_disable(dev);
436 pm_runtime_dont_use_autosuspend(dev);
437 ipa_power_retention_exit(power);
438 ipa_interconnect_exit(power);
439 kfree(power);
440 clk_put(clk);
441 }
442
443 const struct dev_pm_ops ipa_pm_ops = {
444 .suspend = ipa_suspend,
445 .resume = ipa_resume,
446 .runtime_suspend = ipa_runtime_suspend,
447 .runtime_resume = ipa_runtime_resume,
448 };
449