1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2019-2020 Linaro Ltd. 5 */ 6 7 #include <linux/types.h> 8 #include <linux/device.h> 9 #include <linux/interrupt.h> 10 #include <linux/notifier.h> 11 #include <linux/panic_notifier.h> 12 #include <linux/soc/qcom/smem.h> 13 #include <linux/soc/qcom/smem_state.h> 14 15 #include "ipa_smp2p.h" 16 #include "ipa.h" 17 #include "ipa_uc.h" 18 #include "ipa_clock.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 the IPA clock 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 the IPA 34 * clock by trigging another SMP2P interrupt to the AP. We communicate 35 * whether the clock is enabled using two SMP2P state bits--one to 36 * indicate the clock state (on or off), and a second to indicate the 37 * clock state bit is valid. The modem will poll the valid bit until it 38 * is set, and at that time records whether the AP has the IPA clock 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 clock 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 clock query 53 * @setup_ready_irq: IPA interrupt triggered by modem to signal GSI ready 54 * @clock_on: Whether IPA clock is on 55 * @notified: Whether modem has been notified of clock state 56 * @disabled: Whether setup ready interrupt handling 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 clock_on; 69 bool notified; 70 bool 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 clock state 77 * @smp2p: SMP2P information 78 * 79 * This is called either when the modem has requested it (by triggering 80 * the modem clock 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 clock is running, and the other indicating the first bit 83 * is valid. 84 */ 85 static void ipa_smp2p_notify(struct ipa_smp2p *smp2p) 86 { 87 u32 value; 88 u32 mask; 89 90 if (smp2p->notified) 91 return; 92 93 smp2p->clock_on = ipa_clock_get_additional(smp2p->ipa); 94 95 /* Signal whether the clock is enabled */ 96 mask = BIT(smp2p->enabled_bit); 97 value = smp2p->clock_on ? mask : 0; 98 qcom_smem_state_update_bits(smp2p->enabled_state, mask, value); 99 100 /* Now indicate that the enabled flag is valid */ 101 mask = BIT(smp2p->valid_bit); 102 value = mask; 103 qcom_smem_state_update_bits(smp2p->valid_state, mask, value); 104 105 smp2p->notified = true; 106 } 107 108 /* Threaded IRQ handler for modem "ipa-clock-query" SMP2P interrupt */ 109 static irqreturn_t ipa_smp2p_modem_clk_query_isr(int irq, void *dev_id) 110 { 111 struct ipa_smp2p *smp2p = dev_id; 112 113 ipa_smp2p_notify(smp2p); 114 115 return IRQ_HANDLED; 116 } 117 118 static int ipa_smp2p_panic_notifier(struct notifier_block *nb, 119 unsigned long action, void *data) 120 { 121 struct ipa_smp2p *smp2p; 122 123 smp2p = container_of(nb, struct ipa_smp2p, panic_notifier); 124 125 ipa_smp2p_notify(smp2p); 126 127 if (smp2p->clock_on) 128 ipa_uc_panic_notifier(smp2p->ipa); 129 130 return NOTIFY_DONE; 131 } 132 133 static int ipa_smp2p_panic_notifier_register(struct ipa_smp2p *smp2p) 134 { 135 /* IPA panic handler needs to run before modem shuts down */ 136 smp2p->panic_notifier.notifier_call = ipa_smp2p_panic_notifier; 137 smp2p->panic_notifier.priority = INT_MAX; /* Do it early */ 138 139 return atomic_notifier_chain_register(&panic_notifier_list, 140 &smp2p->panic_notifier); 141 } 142 143 static void ipa_smp2p_panic_notifier_unregister(struct ipa_smp2p *smp2p) 144 { 145 atomic_notifier_chain_unregister(&panic_notifier_list, 146 &smp2p->panic_notifier); 147 } 148 149 /* Threaded IRQ handler for modem "ipa-setup-ready" SMP2P interrupt */ 150 static irqreturn_t ipa_smp2p_modem_setup_ready_isr(int irq, void *dev_id) 151 { 152 struct ipa_smp2p *smp2p = dev_id; 153 154 mutex_lock(&smp2p->mutex); 155 156 if (!smp2p->disabled) { 157 int ret; 158 159 ret = ipa_setup(smp2p->ipa); 160 if (ret) 161 dev_err(&smp2p->ipa->pdev->dev, 162 "error %d from ipa_setup()\n", ret); 163 smp2p->disabled = true; 164 } 165 166 mutex_unlock(&smp2p->mutex); 167 168 return IRQ_HANDLED; 169 } 170 171 /* Initialize SMP2P interrupts */ 172 static int ipa_smp2p_irq_init(struct ipa_smp2p *smp2p, const char *name, 173 irq_handler_t handler) 174 { 175 struct device *dev = &smp2p->ipa->pdev->dev; 176 unsigned int irq; 177 int ret; 178 179 ret = platform_get_irq_byname(smp2p->ipa->pdev, name); 180 if (ret <= 0) { 181 dev_err(dev, "DT error %d getting \"%s\" IRQ property\n", 182 ret, name); 183 return ret ? : -EINVAL; 184 } 185 irq = ret; 186 187 ret = request_threaded_irq(irq, NULL, handler, 0, name, smp2p); 188 if (ret) { 189 dev_err(dev, "error %d requesting \"%s\" IRQ\n", ret, name); 190 return ret; 191 } 192 193 return irq; 194 } 195 196 static void ipa_smp2p_irq_exit(struct ipa_smp2p *smp2p, u32 irq) 197 { 198 free_irq(irq, smp2p); 199 } 200 201 /* Drop the clock reference if it was taken in ipa_smp2p_notify() */ 202 static void ipa_smp2p_clock_release(struct ipa *ipa) 203 { 204 if (!ipa->smp2p->clock_on) 205 return; 206 207 ipa_clock_put(ipa); 208 ipa->smp2p->clock_on = false; 209 } 210 211 /* Initialize the IPA SMP2P subsystem */ 212 int ipa_smp2p_init(struct ipa *ipa, bool modem_init) 213 { 214 struct qcom_smem_state *enabled_state; 215 struct device *dev = &ipa->pdev->dev; 216 struct qcom_smem_state *valid_state; 217 struct ipa_smp2p *smp2p; 218 u32 enabled_bit; 219 u32 valid_bit; 220 int ret; 221 222 valid_state = qcom_smem_state_get(dev, "ipa-clock-enabled-valid", 223 &valid_bit); 224 if (IS_ERR(valid_state)) 225 return PTR_ERR(valid_state); 226 if (valid_bit >= 32) /* BITS_PER_U32 */ 227 return -EINVAL; 228 229 enabled_state = qcom_smem_state_get(dev, "ipa-clock-enabled", 230 &enabled_bit); 231 if (IS_ERR(enabled_state)) 232 return PTR_ERR(enabled_state); 233 if (enabled_bit >= 32) /* BITS_PER_U32 */ 234 return -EINVAL; 235 236 smp2p = kzalloc(sizeof(*smp2p), GFP_KERNEL); 237 if (!smp2p) 238 return -ENOMEM; 239 240 smp2p->ipa = ipa; 241 242 /* These fields are needed by the clock query interrupt 243 * handler, so initialize them now. 244 */ 245 mutex_init(&smp2p->mutex); 246 smp2p->valid_state = valid_state; 247 smp2p->valid_bit = valid_bit; 248 smp2p->enabled_state = enabled_state; 249 smp2p->enabled_bit = enabled_bit; 250 251 /* We have enough information saved to handle notifications */ 252 ipa->smp2p = smp2p; 253 254 ret = ipa_smp2p_irq_init(smp2p, "ipa-clock-query", 255 ipa_smp2p_modem_clk_query_isr); 256 if (ret < 0) 257 goto err_null_smp2p; 258 smp2p->clock_query_irq = ret; 259 260 ret = ipa_smp2p_panic_notifier_register(smp2p); 261 if (ret) 262 goto err_irq_exit; 263 264 if (modem_init) { 265 /* Result will be non-zero (negative for error) */ 266 ret = ipa_smp2p_irq_init(smp2p, "ipa-setup-ready", 267 ipa_smp2p_modem_setup_ready_isr); 268 if (ret < 0) 269 goto err_notifier_unregister; 270 smp2p->setup_ready_irq = ret; 271 } 272 273 return 0; 274 275 err_notifier_unregister: 276 ipa_smp2p_panic_notifier_unregister(smp2p); 277 err_irq_exit: 278 ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq); 279 err_null_smp2p: 280 ipa->smp2p = NULL; 281 mutex_destroy(&smp2p->mutex); 282 kfree(smp2p); 283 284 return ret; 285 } 286 287 void ipa_smp2p_exit(struct ipa *ipa) 288 { 289 struct ipa_smp2p *smp2p = ipa->smp2p; 290 291 if (smp2p->setup_ready_irq) 292 ipa_smp2p_irq_exit(smp2p, smp2p->setup_ready_irq); 293 ipa_smp2p_panic_notifier_unregister(smp2p); 294 ipa_smp2p_irq_exit(smp2p, smp2p->clock_query_irq); 295 /* We won't get notified any more; drop clock reference (if any) */ 296 ipa_smp2p_clock_release(ipa); 297 ipa->smp2p = NULL; 298 mutex_destroy(&smp2p->mutex); 299 kfree(smp2p); 300 } 301 302 void ipa_smp2p_disable(struct ipa *ipa) 303 { 304 struct ipa_smp2p *smp2p = ipa->smp2p; 305 306 if (!smp2p->setup_ready_irq) 307 return; 308 309 mutex_lock(&smp2p->mutex); 310 311 smp2p->disabled = true; 312 313 mutex_unlock(&smp2p->mutex); 314 } 315 316 /* Reset state tracking whether we have notified the modem */ 317 void ipa_smp2p_notify_reset(struct ipa *ipa) 318 { 319 struct ipa_smp2p *smp2p = ipa->smp2p; 320 u32 mask; 321 322 if (!smp2p->notified) 323 return; 324 325 ipa_smp2p_clock_release(ipa); 326 327 /* Reset the clock enabled valid flag */ 328 mask = BIT(smp2p->valid_bit); 329 qcom_smem_state_update_bits(smp2p->valid_state, mask, 0); 330 331 /* Mark the clock disabled for good measure... */ 332 mask = BIT(smp2p->enabled_bit); 333 qcom_smem_state_update_bits(smp2p->enabled_state, mask, 0); 334 335 smp2p->notified = false; 336 } 337