Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ipa / ipa_interrupt.c
blob245a06997055699e5dc2e29b49cb96ed91d8a1c9
1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2024 Linaro Ltd.
5 */
7 /* DOC: IPA Interrupts
9 * The IPA has an interrupt line distinct from the interrupt used by the GSI
10 * code. Whereas GSI interrupts are generally related to channel events (like
11 * transfer completions), IPA interrupts are related to other events related
12 * to the IPA. Some of the IPA interrupts come from a microcontroller
13 * embedded in the IPA. Each IPA interrupt type can be both masked and
14 * acknowledged independent of the others.
16 * Two of the IPA interrupts are initiated by the microcontroller. A third
17 * can be generated to signal the need for a wakeup/resume when an IPA
18 * endpoint has been suspended. There are other IPA events, but at this
19 * time only these three are supported.
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pm_wakeirq.h>
26 #include <linux/types.h>
28 #include "ipa.h"
29 #include "ipa_endpoint.h"
30 #include "ipa_interrupt.h"
31 #include "ipa_power.h"
32 #include "ipa_reg.h"
33 #include "ipa_uc.h"
35 /**
36 * struct ipa_interrupt - IPA interrupt information
37 * @ipa: IPA pointer
38 * @irq: Linux IRQ number used for IPA interrupts
39 * @enabled: Mask indicating which interrupts are enabled
40 * @suspend_enabled: Bitmap of endpoints with the SUSPEND interrupt enabled
42 struct ipa_interrupt {
43 struct ipa *ipa;
44 u32 irq;
45 u32 enabled;
46 unsigned long *suspend_enabled;
49 /* Clear the suspend interrupt for all endpoints that signaled it */
50 static void ipa_interrupt_suspend_clear_all(struct ipa_interrupt *interrupt)
52 struct ipa *ipa = interrupt->ipa;
53 u32 unit_count;
54 u32 unit;
56 unit_count = DIV_ROUND_UP(ipa->endpoint_count, 32);
57 for (unit = 0; unit < unit_count; unit++) {
58 const struct reg *reg;
59 u32 val;
61 reg = ipa_reg(ipa, IRQ_SUSPEND_INFO);
62 val = ioread32(ipa->reg_virt + reg_n_offset(reg, unit));
64 /* SUSPEND interrupt status isn't cleared on IPA version 3.0 */
65 if (!val || ipa->version == IPA_VERSION_3_0)
66 continue;
68 reg = ipa_reg(ipa, IRQ_SUSPEND_CLR);
69 iowrite32(val, ipa->reg_virt + reg_n_offset(reg, unit));
73 /* Process a particular interrupt type that has been received */
74 static void ipa_interrupt_process(struct ipa_interrupt *interrupt, u32 irq_id)
76 struct ipa *ipa = interrupt->ipa;
77 const struct reg *reg;
78 u32 mask = BIT(irq_id);
79 u32 offset;
81 reg = ipa_reg(ipa, IPA_IRQ_CLR);
82 offset = reg_offset(reg);
84 switch (irq_id) {
85 case IPA_IRQ_UC_0:
86 case IPA_IRQ_UC_1:
87 /* For microcontroller interrupts, clear the interrupt right
88 * away, "to avoid clearing unhandled interrupts."
90 iowrite32(mask, ipa->reg_virt + offset);
91 ipa_uc_interrupt_handler(ipa, irq_id);
92 break;
94 case IPA_IRQ_TX_SUSPEND:
95 /* Clearing the SUSPEND_TX interrupt also clears the
96 * register that tells us which suspended endpoint(s)
97 * caused the interrupt, so defer clearing until after
98 * the handler has been called.
100 ipa_interrupt_suspend_clear_all(interrupt);
101 fallthrough;
103 default: /* Silently ignore (and clear) any other condition */
104 iowrite32(mask, ipa->reg_virt + offset);
105 break;
109 /* IPA IRQ handler is threaded */
110 static irqreturn_t ipa_isr_thread(int irq, void *dev_id)
112 struct ipa_interrupt *interrupt = dev_id;
113 struct ipa *ipa = interrupt->ipa;
114 u32 enabled = interrupt->enabled;
115 struct device *dev = ipa->dev;
116 const struct reg *reg;
117 u32 pending;
118 u32 offset;
119 u32 mask;
120 int ret;
122 ret = pm_runtime_get_sync(dev);
123 if (WARN_ON(ret < 0))
124 goto out_power_put;
126 /* The status register indicates which conditions are present,
127 * including conditions whose interrupt is not enabled. Handle
128 * only the enabled ones.
130 reg = ipa_reg(ipa, IPA_IRQ_STTS);
131 offset = reg_offset(reg);
132 pending = ioread32(ipa->reg_virt + offset);
133 while ((mask = pending & enabled)) {
134 do {
135 u32 irq_id = __ffs(mask);
137 mask ^= BIT(irq_id);
139 ipa_interrupt_process(interrupt, irq_id);
140 } while (mask);
141 pending = ioread32(ipa->reg_virt + offset);
144 /* If any disabled interrupts are pending, clear them */
145 if (pending) {
146 dev_dbg(dev, "clearing disabled IPA interrupts 0x%08x\n",
147 pending);
148 reg = ipa_reg(ipa, IPA_IRQ_CLR);
149 iowrite32(pending, ipa->reg_virt + reg_offset(reg));
151 out_power_put:
152 pm_runtime_mark_last_busy(dev);
153 (void)pm_runtime_put_autosuspend(dev);
155 return IRQ_HANDLED;
158 static void ipa_interrupt_enabled_update(struct ipa *ipa)
160 const struct reg *reg = ipa_reg(ipa, IPA_IRQ_EN);
162 iowrite32(ipa->interrupt->enabled, ipa->reg_virt + reg_offset(reg));
165 /* Enable an IPA interrupt type */
166 void ipa_interrupt_enable(struct ipa *ipa, enum ipa_irq_id ipa_irq)
168 /* Update the IPA interrupt mask to enable it */
169 ipa->interrupt->enabled |= BIT(ipa_irq);
170 ipa_interrupt_enabled_update(ipa);
173 /* Disable an IPA interrupt type */
174 void ipa_interrupt_disable(struct ipa *ipa, enum ipa_irq_id ipa_irq)
176 /* Update the IPA interrupt mask to disable it */
177 ipa->interrupt->enabled &= ~BIT(ipa_irq);
178 ipa_interrupt_enabled_update(ipa);
181 void ipa_interrupt_irq_disable(struct ipa *ipa)
183 disable_irq(ipa->interrupt->irq);
186 void ipa_interrupt_irq_enable(struct ipa *ipa)
188 enable_irq(ipa->interrupt->irq);
191 /* Common function used to enable/disable TX_SUSPEND for an endpoint */
192 static void ipa_interrupt_suspend_control(struct ipa_interrupt *interrupt,
193 u32 endpoint_id, bool enable)
195 struct ipa *ipa = interrupt->ipa;
196 u32 mask = BIT(endpoint_id % 32);
197 u32 unit = endpoint_id / 32;
198 const struct reg *reg;
199 unsigned long weight;
200 u32 offset;
201 u32 val;
203 WARN_ON(!test_bit(endpoint_id, ipa->available));
205 /* IPA version 3.0 does not support TX_SUSPEND interrupt control */
206 if (ipa->version == IPA_VERSION_3_0)
207 return;
209 weight = bitmap_weight(interrupt->suspend_enabled, ipa->endpoint_count);
210 if (weight == 1 && !enable)
211 ipa_interrupt_disable(ipa, IPA_IRQ_TX_SUSPEND);
213 reg = ipa_reg(ipa, IRQ_SUSPEND_EN);
214 offset = reg_n_offset(reg, unit);
215 val = ioread32(ipa->reg_virt + offset);
217 if (enable)
218 val |= mask;
219 else
220 val &= ~mask;
221 __change_bit(endpoint_id, interrupt->suspend_enabled);
223 iowrite32(val, ipa->reg_virt + offset);
225 if (!weight && enable)
226 ipa_interrupt_enable(ipa, IPA_IRQ_TX_SUSPEND);
229 /* Enable TX_SUSPEND for an endpoint */
230 void
231 ipa_interrupt_suspend_enable(struct ipa_interrupt *interrupt, u32 endpoint_id)
233 ipa_interrupt_suspend_control(interrupt, endpoint_id, true);
236 /* Disable TX_SUSPEND for an endpoint */
237 void
238 ipa_interrupt_suspend_disable(struct ipa_interrupt *interrupt, u32 endpoint_id)
240 ipa_interrupt_suspend_control(interrupt, endpoint_id, false);
243 /* Simulate arrival of an IPA TX_SUSPEND interrupt */
244 void ipa_interrupt_simulate_suspend(struct ipa_interrupt *interrupt)
246 ipa_interrupt_process(interrupt, IPA_IRQ_TX_SUSPEND);
249 /* Configure the IPA interrupt framework */
250 int ipa_interrupt_config(struct ipa *ipa)
252 struct ipa_interrupt *interrupt = ipa->interrupt;
253 unsigned int irq = interrupt->irq;
254 struct device *dev = ipa->dev;
255 const struct reg *reg;
256 int ret;
258 interrupt->ipa = ipa;
260 /* Initially all IPA interrupt types are disabled */
261 interrupt->enabled = 0;
262 interrupt->suspend_enabled = bitmap_zalloc(ipa->endpoint_count,
263 GFP_KERNEL);
264 if (!interrupt->suspend_enabled) {
265 ret = -ENOMEM;
266 goto err_kfree;
269 /* Disable IPA interrupt types */
270 reg = ipa_reg(ipa, IPA_IRQ_EN);
271 iowrite32(0, ipa->reg_virt + reg_offset(reg));
273 ret = request_threaded_irq(irq, NULL, ipa_isr_thread, IRQF_ONESHOT,
274 "ipa", interrupt);
275 if (ret) {
276 dev_err(dev, "error %d requesting \"ipa\" IRQ\n", ret);
277 goto err_free_bitmap;
280 ret = device_init_wakeup(dev, true);
281 if (ret) {
282 dev_err(dev, "error %d enabling wakeup\n", ret);
283 goto err_free_irq;
286 ret = dev_pm_set_wake_irq(dev, irq);
287 if (ret) {
288 dev_err(dev, "error %d registering \"ipa\" IRQ as wakeirq\n",
289 ret);
290 goto err_disable_wakeup;
293 ipa->interrupt = interrupt;
295 return 0;
297 err_disable_wakeup:
298 (void)device_init_wakeup(dev, false);
299 err_free_irq:
300 free_irq(interrupt->irq, interrupt);
301 err_free_bitmap:
302 bitmap_free(interrupt->suspend_enabled);
303 err_kfree:
304 kfree(interrupt);
306 return ret;
309 /* Inverse of ipa_interrupt_config() */
310 void ipa_interrupt_deconfig(struct ipa *ipa)
312 struct ipa_interrupt *interrupt = ipa->interrupt;
313 struct device *dev = ipa->dev;
315 ipa->interrupt = NULL;
317 dev_pm_clear_wake_irq(dev);
318 (void)device_init_wakeup(dev, false);
319 free_irq(interrupt->irq, interrupt);
320 bitmap_free(interrupt->suspend_enabled);
323 /* Initialize the IPA interrupt structure */
324 struct ipa_interrupt *ipa_interrupt_init(struct platform_device *pdev)
326 struct ipa_interrupt *interrupt;
327 int irq;
329 irq = platform_get_irq_byname(pdev, "ipa");
330 if (irq <= 0)
331 return ERR_PTR(irq ? : -EINVAL);
333 interrupt = kzalloc(sizeof(*interrupt), GFP_KERNEL);
334 if (!interrupt)
335 return ERR_PTR(-ENOMEM);
336 interrupt->irq = irq;
338 return interrupt;
341 /* Inverse of ipa_interrupt_init() */
342 void ipa_interrupt_exit(struct ipa_interrupt *interrupt)
344 kfree(interrupt);