1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2019-2022 Linaro Ltd. 5 */ 6 7 #include <linux/types.h> 8 #include <linux/platform_device.h> 9 #include <linux/interrupt.h> 10 #include <linux/notifier.h> 11 #include <linux/panic_notifier.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/soc/qcom/smem.h> 14 #include <linux/soc/qcom/smem_state.h> 15 16 #include "ipa_smp2p.h" 17 #include "ipa.h" 18 #include "ipa_uc.h" 19 20 /** 21 * DOC: IPA SMP2P communication with the modem 22 * 23 * SMP2P is a primitive communication mechanism available between the AP and 24 * the modem. The IPA driver uses this for two purposes: to enable the modem 25 * to state that the GSI hardware is ready to use; and to communicate the 26 * state of IPA power in the event of a crash. 27 * 28 * GSI needs to have early initialization completed before it can be used. 29 * This initialization is done either by Trust Zone or by the modem. In the 30 * latter case, the modem uses an SMP2P interrupt to tell the AP IPA driver 31 * when the GSI is ready to use. 32 * 33 * The modem is also able to inquire about the current state of IPA 34 * power by trigging another SMP2P interrupt to the AP. We communicate 35 * whether power is enabled using two SMP2P state bits--one to indicate 36 * the power state (on or off), and a second to indicate the power state 37 * bit is valid. The modem will poll the valid bit until it is set, and 38 * at that time records whether the AP has IPA power enabled. 39 * 40 * Finally, if the AP kernel panics, we update the SMP2P state bits even if 41 * we never receive an interrupt from the modem requesting this. 42 */ 43 44 /** 45 * struct ipa_smp2p - IPA SMP2P information 46 * @ipa: IPA pointer 47 * @valid_state: SMEM state indicating enabled state is valid 48 * @enabled_state: SMEM state to indicate power is enabled 49 * @valid_bit: Valid bit in 32-bit SMEM state mask 50 * @enabled_bit: Enabled bit in 32-bit SMEM state mask 51 * @enabled_bit: Enabled bit in 32-bit SMEM state mask 52 * @clock_query_irq: IPA interrupt triggered by modem for power query 53 * @setup_ready_irq: IPA interrupt triggered by modem to signal GSI ready 54 * @power_on: Whether IPA power is on 55 * @notified: Whether modem has been notified of power state 56 * @setup_disabled: Whether setup ready interrupt handler is disabled 57 * @mutex: Mutex protecting ready-interrupt/shutdown interlock 58 * @panic_notifier: Panic notifier structure 59 */ 60 struct ipa_smp2p { 61 struct ipa *ipa; 62 struct qcom_smem_state *valid_state; 63 struct qcom_smem_state *enabled_state; 64 u32 valid_bit; 65 u32 enabled_bit; 66 u32 clock_query_irq; 67 u32 setup_ready_irq; 68 bool power_on; 69 bool notified; 70 bool setup_disabled; 71 struct mutex mutex; 72 struct notifier_block panic_notifier; 73 }; 74 75 /** 76 * ipa_smp2p_notify() - use SMP2P to tell modem about IPA power state 77 * @smp2p: SMP2P information 78 * 79 * This is called either when the modem has requested it (by triggering 80 * the modem power query IPA interrupt) or whenever the AP is shutting down 81 * (via a panic notifier). It sets the two SMP2P state bits--one saying 82 * whether the IPA power is on, and the other indicating the first bit 83 * is valid. 84 */ 85 static void ipa_smp2p_notify(struct ipa_smp2p *smp2p) 86 { 87 struct device *dev; 88 u32 value; 89 u32 mask; 90 91 if (smp2p->notified) 92 return; 93 94 dev = &smp2p->ipa->pdev->dev; 95 smp2p->power_on = pm_runtime_get_if_active(dev, true) > 0; 96 97 /* Signal whether the IPA power is enabled */ 98 mask = BIT(smp2p->enabled_bit); 99 value = smp2p->power_on ? mask : 0; 100 qcom_smem_state_update_bits(smp2p->enabled_state, mask, value); 101 102 /* Now indicate that the enabled flag is valid */ 103 mask = BIT(smp2p->valid_bit); 104 value = mask; 105 qcom_smem_state_update_bits(smp2p->valid_state, mask, value); 106 107 smp2p->notified = true; 108 } 109 110 /* Threaded IRQ handler for modem "ipa-clock-query" SMP2P interrupt */ 111 static irqreturn_t ipa_smp2p_modem_clk_query_isr(int irq, void *dev_id) 112 { 113 struct ipa_smp2p *smp2p = dev_id; 114 115 ipa_smp2p_notify(smp2p); 116 117 return IRQ_HANDLED; 118 } 119 120 static int ipa_smp2p_panic_notifier(struct notifier_block *nb, 121 unsigned long action, void *data) 122 { 123 struct ipa_smp2p *smp2p; 124 125 smp2p = container_of(nb, struct ipa_smp2p, panic_notifier); 126 127 ipa_smp2p_notify(smp2p); 128 129 if (smp2p->power_on) 130 ipa_uc_panic_notifier(smp2p->ipa); 131 132 return NOTIFY_DONE; 133 } 134 135 static int ipa_smp2p_panic_notifier_register(struct ipa_smp2p *smp2p) 136 { 137 /* IPA panic handler needs to run before modem shuts down */ 138 smp2p->panic_notifier.notifier_call = ipa_smp2p_panic_notifier; 139 smp2p->panic_notifier.priority = INT_MAX; /* Do it early */ 140 141 return atomic_notifier_chain_register(&panic_notifier_list, 142 &smp2p->panic_notifier); 143 } 144 145 static void ipa_smp2p_panic_notifier_unregister(struct ipa_smp2p *smp2p) 146 { 147 atomic_notifier_chain_unregister(&panic_notifier_list, 148 &smp2p->panic_notifier); 149 } 150 151 /* Threaded IRQ handler for modem "ipa-setup-ready" SMP2P interrupt */ 152 static irqreturn_t ipa_smp2p_modem_setup_ready_isr(int irq, void *dev_id) 153 { 154 struct ipa_smp2p *smp2p = dev_id; 155 struct device *dev; 156 int ret; 157 158 /* Ignore any (spurious) interrupts received after the first */ 159 if (smp2p->ipa->setup_complete) 160 return IRQ_HANDLED; 161 162 /* Power needs to be active for setup */ 163 dev = &smp2p->ipa->pdev->dev; 164 ret = pm_runtime_get_sync(dev); 165 if (ret < 0) { 166 dev_err(dev, "error %d getting power for setup\n", ret); 167 goto out_power_put; 168 } 169 170 /* An error here won't cause driver shutdown, so warn if one occurs */ 171 ret = ipa_setup(smp2p->ipa); 172 WARN(ret != 0, "error %d from ipa_setup()\n", ret); 173 174 out_power_put: 175 pm_runtime_mark_last_busy(dev); 176 (void)pm_runtime_put_autosuspend(dev); 177 178 return IRQ_HANDLED; 179 } 180 181 /* Initialize SMP2P interrupts */ 182 static int ipa_smp2p_irq_init(struct ipa_smp2p *smp2p, 183 struct platform_device *pdev, 184 const char *name, irq_handler_t handler) 185 { 186 struct device *dev = &pdev->dev; 187 unsigned int irq; 188 int ret; 189 190 ret = platform_get_irq_byname(pdev, name); 191 if (ret <= 0) 192 return ret ? : -EINVAL; 193 irq = ret; 194 195 ret = request_threaded_irq(irq, NULL, handler, 0, name, smp2p); 196 if (ret) { 197 dev_err(dev, "error %d requesting \"%s\" IRQ\n", ret, name); 198 return ret; 199 } 200 201 return irq; 202 } 203 204 static void ipa_smp2p_irq_exit(struct ipa_smp2p *smp2p, u32 irq) 205 { 206 free_irq(irq, smp2p); 207 } 208 209 /* Drop the power reference if it was taken in ipa_smp2p_notify() */ 210 static void ipa_smp2p_power_release(struct ipa *ipa) 211 { 212 struct device *dev = &ipa->pdev->dev; 213 214 if (!ipa->smp2p->power_on) 215 return; 216 217 pm_runtime_mark_last_busy(dev); 218 (void)pm_runtime_put_autosuspend(dev); 219 ipa->smp2p->power_on = false; 220 } 221 222 /* Initialize the IPA SMP2P subsystem */ 223 int ipa_smp2p_init(struct ipa *ipa, bool modem_init) 224 { 225 struct qcom_smem_state *enabled_state; 226 struct device *dev = &ipa->pdev->dev; 227 struct qcom_smem_state *valid_state; 228 struct ipa_smp2p *smp2p; 229 u32 enabled_bit; 230 u32 valid_bit; 231 int ret; 232 233 valid_state = qcom_smem_state_get(dev, "ipa-clock-enabled-valid", 234 &valid_bit); 235 if (IS_ERR(valid_state)) 236 return PTR_ERR(valid_state); 237 if (valid_bit >= 32) /* BITS_PER_U32 */ 238 return -EINVAL; 239 240 enabled_state = qcom_smem_state_get(dev, "ipa-clock-enabled", 241 &enabled_bit); 242 if (IS_ERR(enabled_state)) 243 return PTR_ERR(enabled_state); 244 if (enabled_bit >= 32) /* BITS_PER_U32 */ 245 return -EINVAL; 246 247 smp2p = kzalloc(sizeof(*smp2p), GFP_KERNEL); 248 if (!smp2p) 249 return -ENOMEM; 250 251 smp2p->ipa = ipa; 252 253 /* These fields are needed by the power query interrupt 254 * handler, so initialize them now. 255 */ 256 mutex_init(&smp2p->mutex); 257 smp2p->valid_state = valid_state; 258 smp2p->valid_bit = valid_bit; 259 smp2p->enabled_state = enabled_state; 260 smp2p->enabled_bit = enabled_bit; 261 262 /* We have enough information saved to handle notifications */ 263 ipa->smp2p = smp2p; 264 265 ret = ipa_smp2p_irq_init(smp2p, smp2p->ipa->pdev, "ipa-clock-query", 266 ipa_smp2p_modem_clk_query_isr); 267 if (ret < 0) 268 goto err_null_smp2p; 269 smp2p->clock_query_irq = ret; 270 271 ret = ipa_smp2p_panic_notifier_register(smp2p); 272 if (ret) 273 goto err_irq_exit; 274 275 if (modem_init) { 276 /* Result will be non-zero (negative for error) */ 277 ret = ipa_smp2p_irq_init(smp2p, smp2p->ipa->pdev, 278 "ipa-setup-ready", 279 ipa_smp2p_modem_setup_ready_isr); 280 if (ret < 0) 281 goto err_notifier_unregister; 282 smp2p->setup_ready_irq = ret; 283 } 284 285 return 0; 286 287 err_notifier_unregister: 288 ipa_smp2p_panic_notifier_unregister(smp2p); 289 err_irq_exit: 290 ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq); 291 err_null_smp2p: 292 ipa->smp2p = NULL; 293 mutex_destroy(&smp2p->mutex); 294 kfree(smp2p); 295 296 return ret; 297 } 298 299 void ipa_smp2p_exit(struct ipa *ipa) 300 { 301 struct ipa_smp2p *smp2p = ipa->smp2p; 302 303 if (smp2p->setup_ready_irq) 304 ipa_smp2p_irq_exit(smp2p, smp2p->setup_ready_irq); 305 ipa_smp2p_panic_notifier_unregister(smp2p); 306 ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq); 307 /* We won't get notified any more; drop power reference (if any) */ 308 ipa_smp2p_power_release(ipa); 309 ipa->smp2p = NULL; 310 mutex_destroy(&smp2p->mutex); 311 kfree(smp2p); 312 } 313 314 void ipa_smp2p_irq_disable_setup(struct ipa *ipa) 315 { 316 struct ipa_smp2p *smp2p = ipa->smp2p; 317 318 if (!smp2p->setup_ready_irq) 319 return; 320 321 mutex_lock(&smp2p->mutex); 322 323 if (!smp2p->setup_disabled) { 324 disable_irq(smp2p->setup_ready_irq); 325 smp2p->setup_disabled = true; 326 } 327 328 mutex_unlock(&smp2p->mutex); 329 } 330 331 /* Reset state tracking whether we have notified the modem */ 332 void ipa_smp2p_notify_reset(struct ipa *ipa) 333 { 334 struct ipa_smp2p *smp2p = ipa->smp2p; 335 u32 mask; 336 337 if (!smp2p->notified) 338 return; 339 340 ipa_smp2p_power_release(ipa); 341 342 /* Reset the power enabled valid flag */ 343 mask = BIT(smp2p->valid_bit); 344 qcom_smem_state_update_bits(smp2p->valid_state, mask, 0); 345 346 /* Mark the power disabled for good measure... */ 347 mask = BIT(smp2p->enabled_bit); 348 qcom_smem_state_update_bits(smp2p->enabled_state, mask, 0); 349 350 smp2p->notified = false; 351 } 352