1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2018-2022 Linaro Ltd. 5 */ 6 7 /* DOC: IPA Interrupts 8 * 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. 15 * 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. 20 */ 21 22 #include <linux/types.h> 23 #include <linux/interrupt.h> 24 #include <linux/pm_runtime.h> 25 26 #include "ipa.h" 27 #include "ipa_reg.h" 28 #include "ipa_endpoint.h" 29 #include "ipa_interrupt.h" 30 31 /** 32 * struct ipa_interrupt - IPA interrupt information 33 * @ipa: IPA pointer 34 * @irq: Linux IRQ number used for IPA interrupts 35 * @enabled: Mask indicating which interrupts are enabled 36 * @handler: Array of handlers indexed by IPA interrupt ID 37 */ 38 struct ipa_interrupt { 39 struct ipa *ipa; 40 u32 irq; 41 u32 enabled; 42 ipa_irq_handler_t handler[IPA_IRQ_COUNT]; 43 }; 44 45 /* Returns true if the interrupt type is associated with the microcontroller */ 46 static bool ipa_interrupt_uc(struct ipa_interrupt *interrupt, u32 irq_id) 47 { 48 return irq_id == IPA_IRQ_UC_0 || irq_id == IPA_IRQ_UC_1; 49 } 50 51 /* Process a particular interrupt type that has been received */ 52 static void ipa_interrupt_process(struct ipa_interrupt *interrupt, u32 irq_id) 53 { 54 bool uc_irq = ipa_interrupt_uc(interrupt, irq_id); 55 struct ipa *ipa = interrupt->ipa; 56 const struct ipa_reg *reg; 57 u32 mask = BIT(irq_id); 58 u32 offset; 59 60 /* For microcontroller interrupts, clear the interrupt right away, 61 * "to avoid clearing unhandled interrupts." 62 */ 63 reg = ipa_reg(ipa, IPA_IRQ_CLR); 64 offset = ipa_reg_offset(reg); 65 if (uc_irq) 66 iowrite32(mask, ipa->reg_virt + offset); 67 68 if (irq_id < IPA_IRQ_COUNT && interrupt->handler[irq_id]) 69 interrupt->handler[irq_id](interrupt->ipa, irq_id); 70 71 /* Clearing the SUSPEND_TX interrupt also clears the register 72 * that tells us which suspended endpoint(s) caused the interrupt, 73 * so defer clearing until after the handler has been called. 74 */ 75 if (!uc_irq) 76 iowrite32(mask, ipa->reg_virt + offset); 77 } 78 79 /* IPA IRQ handler is threaded */ 80 static irqreturn_t ipa_isr_thread(int irq, void *dev_id) 81 { 82 struct ipa_interrupt *interrupt = dev_id; 83 struct ipa *ipa = interrupt->ipa; 84 u32 enabled = interrupt->enabled; 85 const struct ipa_reg *reg; 86 struct device *dev; 87 u32 pending; 88 u32 offset; 89 u32 mask; 90 int ret; 91 92 dev = &ipa->pdev->dev; 93 ret = pm_runtime_get_sync(dev); 94 if (WARN_ON(ret < 0)) 95 goto out_power_put; 96 97 /* The status register indicates which conditions are present, 98 * including conditions whose interrupt is not enabled. Handle 99 * only the enabled ones. 100 */ 101 reg = ipa_reg(ipa, IPA_IRQ_STTS); 102 offset = ipa_reg_offset(reg); 103 pending = ioread32(ipa->reg_virt + offset); 104 while ((mask = pending & enabled)) { 105 do { 106 u32 irq_id = __ffs(mask); 107 108 mask ^= BIT(irq_id); 109 110 ipa_interrupt_process(interrupt, irq_id); 111 } while (mask); 112 pending = ioread32(ipa->reg_virt + offset); 113 } 114 115 /* If any disabled interrupts are pending, clear them */ 116 if (pending) { 117 dev_dbg(dev, "clearing disabled IPA interrupts 0x%08x\n", 118 pending); 119 reg = ipa_reg(ipa, IPA_IRQ_CLR); 120 offset = ipa_reg_offset(reg); 121 iowrite32(pending, ipa->reg_virt + offset); 122 } 123 out_power_put: 124 pm_runtime_mark_last_busy(dev); 125 (void)pm_runtime_put_autosuspend(dev); 126 127 return IRQ_HANDLED; 128 } 129 130 /* Common function used to enable/disable TX_SUSPEND for an endpoint */ 131 static void ipa_interrupt_suspend_control(struct ipa_interrupt *interrupt, 132 u32 endpoint_id, bool enable) 133 { 134 struct ipa *ipa = interrupt->ipa; 135 u32 mask = BIT(endpoint_id % 32); 136 u32 unit = endpoint_id / 32; 137 const struct ipa_reg *reg; 138 u32 offset; 139 u32 val; 140 141 WARN_ON(!test_bit(endpoint_id, ipa->available)); 142 143 /* IPA version 3.0 does not support TX_SUSPEND interrupt control */ 144 if (ipa->version == IPA_VERSION_3_0) 145 return; 146 147 reg = ipa_reg(ipa, IRQ_SUSPEND_EN); 148 offset = ipa_reg_n_offset(reg, unit); 149 val = ioread32(ipa->reg_virt + offset); 150 151 if (enable) 152 val |= mask; 153 else 154 val &= ~mask; 155 156 iowrite32(val, ipa->reg_virt + offset); 157 } 158 159 /* Enable TX_SUSPEND for an endpoint */ 160 void 161 ipa_interrupt_suspend_enable(struct ipa_interrupt *interrupt, u32 endpoint_id) 162 { 163 ipa_interrupt_suspend_control(interrupt, endpoint_id, true); 164 } 165 166 /* Disable TX_SUSPEND for an endpoint */ 167 void 168 ipa_interrupt_suspend_disable(struct ipa_interrupt *interrupt, u32 endpoint_id) 169 { 170 ipa_interrupt_suspend_control(interrupt, endpoint_id, false); 171 } 172 173 /* Clear the suspend interrupt for all endpoints that signaled it */ 174 void ipa_interrupt_suspend_clear_all(struct ipa_interrupt *interrupt) 175 { 176 struct ipa *ipa = interrupt->ipa; 177 u32 unit_count; 178 u32 unit; 179 180 unit_count = roundup(ipa->endpoint_count, 32); 181 for (unit = 0; unit < unit_count; unit++) { 182 const struct ipa_reg *reg; 183 u32 val; 184 185 reg = ipa_reg(ipa, IRQ_SUSPEND_INFO); 186 val = ioread32(ipa->reg_virt + ipa_reg_n_offset(reg, unit)); 187 188 /* SUSPEND interrupt status isn't cleared on IPA version 3.0 */ 189 if (ipa->version == IPA_VERSION_3_0) 190 continue; 191 192 reg = ipa_reg(ipa, IRQ_SUSPEND_CLR); 193 iowrite32(val, ipa->reg_virt + ipa_reg_n_offset(reg, unit)); 194 } 195 } 196 197 /* Simulate arrival of an IPA TX_SUSPEND interrupt */ 198 void ipa_interrupt_simulate_suspend(struct ipa_interrupt *interrupt) 199 { 200 ipa_interrupt_process(interrupt, IPA_IRQ_TX_SUSPEND); 201 } 202 203 /* Add a handler for an IPA interrupt */ 204 void ipa_interrupt_add(struct ipa_interrupt *interrupt, 205 enum ipa_irq_id ipa_irq, ipa_irq_handler_t handler) 206 { 207 struct ipa *ipa = interrupt->ipa; 208 const struct ipa_reg *reg; 209 210 if (WARN_ON(ipa_irq >= IPA_IRQ_COUNT)) 211 return; 212 213 interrupt->handler[ipa_irq] = handler; 214 215 /* Update the IPA interrupt mask to enable it */ 216 interrupt->enabled |= BIT(ipa_irq); 217 218 reg = ipa_reg(ipa, IPA_IRQ_EN); 219 iowrite32(interrupt->enabled, ipa->reg_virt + ipa_reg_offset(reg)); 220 } 221 222 /* Remove the handler for an IPA interrupt type */ 223 void 224 ipa_interrupt_remove(struct ipa_interrupt *interrupt, enum ipa_irq_id ipa_irq) 225 { 226 struct ipa *ipa = interrupt->ipa; 227 const struct ipa_reg *reg; 228 229 if (WARN_ON(ipa_irq >= IPA_IRQ_COUNT)) 230 return; 231 232 /* Update the IPA interrupt mask to disable it */ 233 interrupt->enabled &= ~BIT(ipa_irq); 234 235 reg = ipa_reg(ipa, IPA_IRQ_EN); 236 iowrite32(interrupt->enabled, ipa->reg_virt + ipa_reg_offset(reg)); 237 238 interrupt->handler[ipa_irq] = NULL; 239 } 240 241 /* Configure the IPA interrupt framework */ 242 struct ipa_interrupt *ipa_interrupt_config(struct ipa *ipa) 243 { 244 struct device *dev = &ipa->pdev->dev; 245 struct ipa_interrupt *interrupt; 246 const struct ipa_reg *reg; 247 unsigned int irq; 248 int ret; 249 250 ret = platform_get_irq_byname(ipa->pdev, "ipa"); 251 if (ret <= 0) { 252 dev_err(dev, "DT error %d getting \"ipa\" IRQ property\n", 253 ret); 254 return ERR_PTR(ret ? : -EINVAL); 255 } 256 irq = ret; 257 258 interrupt = kzalloc(sizeof(*interrupt), GFP_KERNEL); 259 if (!interrupt) 260 return ERR_PTR(-ENOMEM); 261 interrupt->ipa = ipa; 262 interrupt->irq = irq; 263 264 /* Start with all IPA interrupts disabled */ 265 reg = ipa_reg(ipa, IPA_IRQ_EN); 266 iowrite32(0, ipa->reg_virt + ipa_reg_offset(reg)); 267 268 ret = request_threaded_irq(irq, NULL, ipa_isr_thread, IRQF_ONESHOT, 269 "ipa", interrupt); 270 if (ret) { 271 dev_err(dev, "error %d requesting \"ipa\" IRQ\n", ret); 272 goto err_kfree; 273 } 274 275 ret = enable_irq_wake(irq); 276 if (ret) { 277 dev_err(dev, "error %d enabling wakeup for \"ipa\" IRQ\n", ret); 278 goto err_free_irq; 279 } 280 281 return interrupt; 282 283 err_free_irq: 284 free_irq(interrupt->irq, interrupt); 285 err_kfree: 286 kfree(interrupt); 287 288 return ERR_PTR(ret); 289 } 290 291 /* Inverse of ipa_interrupt_config() */ 292 void ipa_interrupt_deconfig(struct ipa_interrupt *interrupt) 293 { 294 struct device *dev = &interrupt->ipa->pdev->dev; 295 int ret; 296 297 ret = disable_irq_wake(interrupt->irq); 298 if (ret) 299 dev_err(dev, "error %d disabling \"ipa\" IRQ wakeup\n", ret); 300 free_irq(interrupt->irq, interrupt); 301 kfree(interrupt); 302 } 303