1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved. 4 * 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/device.h> 9 #include <linux/dma-direction.h> 10 #include <linux/dma-mapping.h> 11 #include <linux/interrupt.h> 12 #include <linux/list.h> 13 #include <linux/mhi.h> 14 #include <linux/module.h> 15 #include <linux/slab.h> 16 #include <linux/wait.h> 17 #include "internal.h" 18 #include "trace.h" 19 20 /* 21 * Not all MHI state transitions are synchronous. Transitions like Linkdown, 22 * SYS_ERR, and shutdown can happen anytime asynchronously. This function will 23 * transition to a new state only if we're allowed to. 24 * 25 * Priority increases as we go down. For instance, from any state in L0, the 26 * transition can be made to states in L1, L2 and L3. A notable exception to 27 * this rule is state DISABLE. From DISABLE state we can only transition to 28 * POR state. Also, while in L2 state, user cannot jump back to previous 29 * L1 or L0 states. 30 * 31 * Valid transitions: 32 * L0: DISABLE <--> POR 33 * POR <--> POR 34 * POR -> M0 -> M2 --> M0 35 * POR -> FW_DL_ERR 36 * FW_DL_ERR <--> FW_DL_ERR 37 * M0 <--> M0 38 * M0 -> FW_DL_ERR 39 * M0 -> M3_ENTER -> M3 -> M3_EXIT --> M0 40 * L1: SYS_ERR_DETECT -> SYS_ERR_PROCESS 41 * SYS_ERR_PROCESS -> SYS_ERR_FAIL 42 * SYS_ERR_FAIL -> SYS_ERR_DETECT 43 * SYS_ERR_PROCESS --> POR 44 * L2: SHUTDOWN_PROCESS -> LD_ERR_FATAL_DETECT 45 * SHUTDOWN_PROCESS -> DISABLE 46 * L3: LD_ERR_FATAL_DETECT <--> LD_ERR_FATAL_DETECT 47 * LD_ERR_FATAL_DETECT -> DISABLE 48 */ 49 static const struct mhi_pm_transitions dev_state_transitions[] = { 50 /* L0 States */ 51 { 52 MHI_PM_DISABLE, 53 MHI_PM_POR 54 }, 55 { 56 MHI_PM_POR, 57 MHI_PM_POR | MHI_PM_DISABLE | MHI_PM_M0 | 58 MHI_PM_SYS_ERR_DETECT | MHI_PM_SHUTDOWN_PROCESS | 59 MHI_PM_LD_ERR_FATAL_DETECT | MHI_PM_FW_DL_ERR 60 }, 61 { 62 MHI_PM_M0, 63 MHI_PM_M0 | MHI_PM_M2 | MHI_PM_M3_ENTER | 64 MHI_PM_SYS_ERR_DETECT | MHI_PM_SHUTDOWN_PROCESS | 65 MHI_PM_LD_ERR_FATAL_DETECT | MHI_PM_FW_DL_ERR 66 }, 67 { 68 MHI_PM_M2, 69 MHI_PM_M0 | MHI_PM_SYS_ERR_DETECT | MHI_PM_SHUTDOWN_PROCESS | 70 MHI_PM_LD_ERR_FATAL_DETECT 71 }, 72 { 73 MHI_PM_M3_ENTER, 74 MHI_PM_M3 | MHI_PM_SYS_ERR_DETECT | MHI_PM_SHUTDOWN_PROCESS | 75 MHI_PM_LD_ERR_FATAL_DETECT 76 }, 77 { 78 MHI_PM_M3, 79 MHI_PM_M3_EXIT | MHI_PM_SYS_ERR_DETECT | 80 MHI_PM_LD_ERR_FATAL_DETECT 81 }, 82 { 83 MHI_PM_M3_EXIT, 84 MHI_PM_M0 | MHI_PM_SYS_ERR_DETECT | MHI_PM_SHUTDOWN_PROCESS | 85 MHI_PM_LD_ERR_FATAL_DETECT 86 }, 87 { 88 MHI_PM_FW_DL_ERR, 89 MHI_PM_FW_DL_ERR | MHI_PM_SYS_ERR_DETECT | 90 MHI_PM_SHUTDOWN_PROCESS | MHI_PM_LD_ERR_FATAL_DETECT 91 }, 92 /* L1 States */ 93 { 94 MHI_PM_SYS_ERR_DETECT, 95 MHI_PM_SYS_ERR_PROCESS | MHI_PM_SHUTDOWN_PROCESS | 96 MHI_PM_LD_ERR_FATAL_DETECT 97 }, 98 { 99 MHI_PM_SYS_ERR_PROCESS, 100 MHI_PM_POR | MHI_PM_SYS_ERR_FAIL | MHI_PM_SHUTDOWN_PROCESS | 101 MHI_PM_LD_ERR_FATAL_DETECT 102 }, 103 { 104 MHI_PM_SYS_ERR_FAIL, 105 MHI_PM_SYS_ERR_DETECT | MHI_PM_SHUTDOWN_PROCESS | 106 MHI_PM_LD_ERR_FATAL_DETECT 107 }, 108 /* L2 States */ 109 { 110 MHI_PM_SHUTDOWN_PROCESS, 111 MHI_PM_DISABLE | MHI_PM_LD_ERR_FATAL_DETECT 112 }, 113 /* L3 States */ 114 { 115 MHI_PM_LD_ERR_FATAL_DETECT, 116 MHI_PM_LD_ERR_FATAL_DETECT | MHI_PM_DISABLE 117 }, 118 }; 119 120 enum mhi_pm_state __must_check mhi_tryset_pm_state(struct mhi_controller *mhi_cntrl, 121 enum mhi_pm_state state) 122 { 123 unsigned long cur_state = mhi_cntrl->pm_state; 124 int index = find_last_bit(&cur_state, 32); 125 126 if (unlikely(index >= ARRAY_SIZE(dev_state_transitions))) 127 return cur_state; 128 129 if (unlikely(dev_state_transitions[index].from_state != cur_state)) 130 return cur_state; 131 132 if (unlikely(!(dev_state_transitions[index].to_states & state))) 133 return cur_state; 134 135 trace_mhi_tryset_pm_state(mhi_cntrl, state); 136 mhi_cntrl->pm_state = state; 137 return mhi_cntrl->pm_state; 138 } 139 140 void mhi_set_mhi_state(struct mhi_controller *mhi_cntrl, enum mhi_state state) 141 { 142 struct device *dev = &mhi_cntrl->mhi_dev->dev; 143 int ret; 144 145 if (state == MHI_STATE_RESET) { 146 ret = mhi_write_reg_field(mhi_cntrl, mhi_cntrl->regs, MHICTRL, 147 MHICTRL_RESET_MASK, 1); 148 } else { 149 ret = mhi_write_reg_field(mhi_cntrl, mhi_cntrl->regs, MHICTRL, 150 MHICTRL_MHISTATE_MASK, state); 151 } 152 153 if (ret) 154 dev_err(dev, "Failed to set MHI state to: %s\n", 155 mhi_state_str(state)); 156 } 157 158 /* NOP for backward compatibility, host allowed to ring DB in M2 state */ 159 static void mhi_toggle_dev_wake_nop(struct mhi_controller *mhi_cntrl) 160 { 161 } 162 163 static void mhi_toggle_dev_wake(struct mhi_controller *mhi_cntrl) 164 { 165 mhi_cntrl->wake_get(mhi_cntrl, false); 166 mhi_cntrl->wake_put(mhi_cntrl, true); 167 } 168 169 /* Handle device ready state transition */ 170 int mhi_ready_state_transition(struct mhi_controller *mhi_cntrl) 171 { 172 struct mhi_event *mhi_event; 173 enum mhi_pm_state cur_state; 174 struct device *dev = &mhi_cntrl->mhi_dev->dev; 175 u32 interval_us = 25000; /* poll register field every 25 milliseconds */ 176 u32 timeout_ms; 177 int ret, i; 178 179 /* Check if device entered error state */ 180 if (MHI_PM_IN_FATAL_STATE(mhi_cntrl->pm_state)) { 181 dev_err(dev, "Device link is not accessible\n"); 182 return -EIO; 183 } 184 185 /* Wait for RESET to be cleared and READY bit to be set by the device */ 186 ret = mhi_poll_reg_field(mhi_cntrl, mhi_cntrl->regs, MHICTRL, 187 MHICTRL_RESET_MASK, 0, interval_us, 188 mhi_cntrl->timeout_ms); 189 if (ret) { 190 dev_err(dev, "Device failed to clear MHI Reset\n"); 191 return ret; 192 } 193 194 timeout_ms = mhi_cntrl->ready_timeout_ms ? 195 mhi_cntrl->ready_timeout_ms : mhi_cntrl->timeout_ms; 196 ret = mhi_poll_reg_field(mhi_cntrl, mhi_cntrl->regs, MHISTATUS, 197 MHISTATUS_READY_MASK, 1, interval_us, 198 timeout_ms); 199 if (ret) { 200 dev_err(dev, "Device failed to enter MHI Ready\n"); 201 return ret; 202 } 203 204 dev_dbg(dev, "Device in READY State\n"); 205 write_lock_irq(&mhi_cntrl->pm_lock); 206 cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_POR); 207 mhi_cntrl->dev_state = MHI_STATE_READY; 208 write_unlock_irq(&mhi_cntrl->pm_lock); 209 210 if (cur_state != MHI_PM_POR) { 211 dev_err(dev, "Error moving to state %s from %s\n", 212 to_mhi_pm_state_str(MHI_PM_POR), 213 to_mhi_pm_state_str(cur_state)); 214 return -EIO; 215 } 216 217 read_lock_bh(&mhi_cntrl->pm_lock); 218 if (!MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state)) { 219 dev_err(dev, "Device registers not accessible\n"); 220 goto error_mmio; 221 } 222 223 /* Configure MMIO registers */ 224 ret = mhi_init_mmio(mhi_cntrl); 225 if (ret) { 226 dev_err(dev, "Error configuring MMIO registers\n"); 227 goto error_mmio; 228 } 229 230 /* Add elements to all SW event rings */ 231 mhi_event = mhi_cntrl->mhi_event; 232 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, mhi_event++) { 233 struct mhi_ring *ring = &mhi_event->ring; 234 235 /* Skip if this is an offload or HW event */ 236 if (mhi_event->offload_ev || mhi_event->hw_ring) 237 continue; 238 239 ring->wp = ring->base + ring->len - ring->el_size; 240 *ring->ctxt_wp = cpu_to_le64(ring->iommu_base + ring->len - ring->el_size); 241 /* Update all cores */ 242 smp_wmb(); 243 244 /* Ring the event ring db */ 245 spin_lock_irq(&mhi_event->lock); 246 mhi_ring_er_db(mhi_event); 247 spin_unlock_irq(&mhi_event->lock); 248 } 249 250 /* Set MHI to M0 state */ 251 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_M0); 252 read_unlock_bh(&mhi_cntrl->pm_lock); 253 254 return 0; 255 256 error_mmio: 257 read_unlock_bh(&mhi_cntrl->pm_lock); 258 259 return -EIO; 260 } 261 262 int mhi_pm_m0_transition(struct mhi_controller *mhi_cntrl) 263 { 264 enum mhi_pm_state cur_state; 265 struct mhi_chan *mhi_chan; 266 struct device *dev = &mhi_cntrl->mhi_dev->dev; 267 int i; 268 269 write_lock_irq(&mhi_cntrl->pm_lock); 270 mhi_cntrl->dev_state = MHI_STATE_M0; 271 cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_M0); 272 write_unlock_irq(&mhi_cntrl->pm_lock); 273 if (unlikely(cur_state != MHI_PM_M0)) { 274 dev_err(dev, "Unable to transition to M0 state\n"); 275 return -EIO; 276 } 277 mhi_cntrl->M0++; 278 279 /* Wake up the device */ 280 read_lock_bh(&mhi_cntrl->pm_lock); 281 mhi_cntrl->wake_get(mhi_cntrl, true); 282 283 /* Ring all event rings and CMD ring only if we're in mission mode */ 284 if (MHI_IN_MISSION_MODE(mhi_cntrl->ee)) { 285 struct mhi_event *mhi_event = mhi_cntrl->mhi_event; 286 struct mhi_cmd *mhi_cmd = 287 &mhi_cntrl->mhi_cmd[PRIMARY_CMD_RING]; 288 289 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, mhi_event++) { 290 if (mhi_event->offload_ev) 291 continue; 292 293 spin_lock_irq(&mhi_event->lock); 294 mhi_ring_er_db(mhi_event); 295 spin_unlock_irq(&mhi_event->lock); 296 } 297 298 /* Only ring primary cmd ring if ring is not empty */ 299 spin_lock_irq(&mhi_cmd->lock); 300 if (mhi_cmd->ring.rp != mhi_cmd->ring.wp) 301 mhi_ring_cmd_db(mhi_cntrl, mhi_cmd); 302 spin_unlock_irq(&mhi_cmd->lock); 303 } 304 305 /* Ring channel DB registers */ 306 mhi_chan = mhi_cntrl->mhi_chan; 307 for (i = 0; i < mhi_cntrl->max_chan; i++, mhi_chan++) { 308 struct mhi_ring *tre_ring = &mhi_chan->tre_ring; 309 310 if (mhi_chan->db_cfg.reset_req) { 311 write_lock_irq(&mhi_chan->lock); 312 mhi_chan->db_cfg.db_mode = true; 313 write_unlock_irq(&mhi_chan->lock); 314 } 315 316 read_lock_irq(&mhi_chan->lock); 317 318 /* Only ring DB if ring is not empty */ 319 if (tre_ring->base && tre_ring->wp != tre_ring->rp && 320 mhi_chan->ch_state == MHI_CH_STATE_ENABLED) 321 mhi_ring_chan_db(mhi_cntrl, mhi_chan); 322 read_unlock_irq(&mhi_chan->lock); 323 } 324 325 mhi_cntrl->wake_put(mhi_cntrl, false); 326 read_unlock_bh(&mhi_cntrl->pm_lock); 327 wake_up_all(&mhi_cntrl->state_event); 328 329 return 0; 330 } 331 332 /* 333 * After receiving the MHI state change event from the device indicating the 334 * transition to M1 state, the host can transition the device to M2 state 335 * for keeping it in low power state. 336 */ 337 void mhi_pm_m1_transition(struct mhi_controller *mhi_cntrl) 338 { 339 enum mhi_pm_state state; 340 struct device *dev = &mhi_cntrl->mhi_dev->dev; 341 342 write_lock_irq(&mhi_cntrl->pm_lock); 343 state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_M2); 344 if (state == MHI_PM_M2) { 345 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_M2); 346 mhi_cntrl->dev_state = MHI_STATE_M2; 347 348 write_unlock_irq(&mhi_cntrl->pm_lock); 349 350 mhi_cntrl->M2++; 351 wake_up_all(&mhi_cntrl->state_event); 352 353 /* If there are any pending resources, exit M2 immediately */ 354 if (unlikely(atomic_read(&mhi_cntrl->pending_pkts) || 355 atomic_read(&mhi_cntrl->dev_wake))) { 356 dev_dbg(dev, 357 "Exiting M2, pending_pkts: %d dev_wake: %d\n", 358 atomic_read(&mhi_cntrl->pending_pkts), 359 atomic_read(&mhi_cntrl->dev_wake)); 360 read_lock_bh(&mhi_cntrl->pm_lock); 361 mhi_cntrl->wake_get(mhi_cntrl, true); 362 mhi_cntrl->wake_put(mhi_cntrl, true); 363 read_unlock_bh(&mhi_cntrl->pm_lock); 364 } else { 365 mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_IDLE); 366 } 367 } else { 368 write_unlock_irq(&mhi_cntrl->pm_lock); 369 } 370 } 371 372 /* MHI M3 completion handler */ 373 int mhi_pm_m3_transition(struct mhi_controller *mhi_cntrl) 374 { 375 enum mhi_pm_state state; 376 struct device *dev = &mhi_cntrl->mhi_dev->dev; 377 378 write_lock_irq(&mhi_cntrl->pm_lock); 379 mhi_cntrl->dev_state = MHI_STATE_M3; 380 state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_M3); 381 write_unlock_irq(&mhi_cntrl->pm_lock); 382 if (state != MHI_PM_M3) { 383 dev_err(dev, "Unable to transition to M3 state\n"); 384 return -EIO; 385 } 386 387 mhi_cntrl->M3++; 388 wake_up_all(&mhi_cntrl->state_event); 389 390 return 0; 391 } 392 393 /* Handle device Mission Mode transition */ 394 static int mhi_pm_mission_mode_transition(struct mhi_controller *mhi_cntrl) 395 { 396 struct mhi_event *mhi_event; 397 struct device *dev = &mhi_cntrl->mhi_dev->dev; 398 enum mhi_ee_type ee = MHI_EE_MAX, current_ee = mhi_cntrl->ee; 399 int i, ret; 400 401 dev_dbg(dev, "Processing Mission Mode transition\n"); 402 403 write_lock_irq(&mhi_cntrl->pm_lock); 404 if (MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state)) 405 ee = mhi_get_exec_env(mhi_cntrl); 406 407 if (!MHI_IN_MISSION_MODE(ee)) { 408 mhi_cntrl->pm_state = MHI_PM_LD_ERR_FATAL_DETECT; 409 write_unlock_irq(&mhi_cntrl->pm_lock); 410 wake_up_all(&mhi_cntrl->state_event); 411 return -EIO; 412 } 413 mhi_cntrl->ee = ee; 414 write_unlock_irq(&mhi_cntrl->pm_lock); 415 416 wake_up_all(&mhi_cntrl->state_event); 417 418 device_for_each_child(&mhi_cntrl->mhi_dev->dev, ¤t_ee, 419 mhi_destroy_device); 420 mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_EE_MISSION_MODE); 421 mhi_uevent_notify(mhi_cntrl, mhi_cntrl->ee); 422 423 /* Force MHI to be in M0 state before continuing */ 424 ret = __mhi_device_get_sync(mhi_cntrl); 425 if (ret) 426 return ret; 427 428 read_lock_bh(&mhi_cntrl->pm_lock); 429 430 if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { 431 ret = -EIO; 432 goto error_mission_mode; 433 } 434 435 /* Add elements to all HW event rings */ 436 mhi_event = mhi_cntrl->mhi_event; 437 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, mhi_event++) { 438 struct mhi_ring *ring = &mhi_event->ring; 439 440 if (mhi_event->offload_ev || !mhi_event->hw_ring) 441 continue; 442 443 ring->wp = ring->base + ring->len - ring->el_size; 444 *ring->ctxt_wp = cpu_to_le64(ring->iommu_base + ring->len - ring->el_size); 445 /* Update to all cores */ 446 smp_wmb(); 447 448 spin_lock_irq(&mhi_event->lock); 449 if (MHI_DB_ACCESS_VALID(mhi_cntrl)) 450 mhi_ring_er_db(mhi_event); 451 spin_unlock_irq(&mhi_event->lock); 452 } 453 454 read_unlock_bh(&mhi_cntrl->pm_lock); 455 456 /* 457 * The MHI devices are only created when the client device switches its 458 * Execution Environment (EE) to either SBL or AMSS states 459 */ 460 mhi_create_devices(mhi_cntrl); 461 462 read_lock_bh(&mhi_cntrl->pm_lock); 463 464 error_mission_mode: 465 mhi_cntrl->wake_put(mhi_cntrl, false); 466 read_unlock_bh(&mhi_cntrl->pm_lock); 467 468 return ret; 469 } 470 471 /* Handle shutdown transitions */ 472 static void mhi_pm_disable_transition(struct mhi_controller *mhi_cntrl, 473 bool destroy_device) 474 { 475 enum mhi_pm_state cur_state; 476 struct mhi_event *mhi_event; 477 struct mhi_cmd_ctxt *cmd_ctxt; 478 struct mhi_cmd *mhi_cmd; 479 struct mhi_event_ctxt *er_ctxt; 480 struct device *dev = &mhi_cntrl->mhi_dev->dev; 481 int ret, i; 482 483 dev_dbg(dev, "Processing disable transition with PM state: %s\n", 484 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 485 486 mutex_lock(&mhi_cntrl->pm_mutex); 487 488 /* Trigger MHI RESET so that the device will not access host memory */ 489 if (!MHI_PM_IN_FATAL_STATE(mhi_cntrl->pm_state)) { 490 /* Skip MHI RESET if in RDDM state */ 491 if (mhi_cntrl->rddm_image && mhi_get_exec_env(mhi_cntrl) == MHI_EE_RDDM) 492 goto skip_mhi_reset; 493 494 dev_dbg(dev, "Triggering MHI Reset in device\n"); 495 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_RESET); 496 497 /* Wait for the reset bit to be cleared by the device */ 498 ret = mhi_poll_reg_field(mhi_cntrl, mhi_cntrl->regs, MHICTRL, 499 MHICTRL_RESET_MASK, 0, 25000, mhi_cntrl->timeout_ms); 500 if (ret) 501 dev_err(dev, "Device failed to clear MHI Reset\n"); 502 503 /* 504 * Device will clear BHI_INTVEC as a part of RESET processing, 505 * hence re-program it 506 */ 507 mhi_write_reg(mhi_cntrl, mhi_cntrl->bhi, BHI_INTVEC, 0); 508 509 if (!MHI_IN_PBL(mhi_get_exec_env(mhi_cntrl))) { 510 /* wait for ready to be set */ 511 ret = mhi_poll_reg_field(mhi_cntrl, mhi_cntrl->regs, 512 MHISTATUS, MHISTATUS_READY_MASK, 513 1, 25000, mhi_cntrl->timeout_ms); 514 if (ret) 515 dev_err(dev, "Device failed to enter READY state\n"); 516 } 517 } 518 519 skip_mhi_reset: 520 dev_dbg(dev, 521 "Waiting for all pending event ring processing to complete\n"); 522 mhi_event = mhi_cntrl->mhi_event; 523 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, mhi_event++) { 524 if (mhi_event->offload_ev) 525 continue; 526 disable_irq(mhi_cntrl->irq[mhi_event->irq]); 527 tasklet_kill(&mhi_event->task); 528 } 529 530 /* Release lock and wait for all pending threads to complete */ 531 mutex_unlock(&mhi_cntrl->pm_mutex); 532 dev_dbg(dev, "Waiting for all pending threads to complete\n"); 533 wake_up_all(&mhi_cntrl->state_event); 534 535 /* 536 * Only destroy the 'struct device' for channels if indicated by the 537 * 'destroy_device' flag. Because, during system suspend or hibernation 538 * state, there is no need to destroy the 'struct device' as the endpoint 539 * device would still be physically attached to the machine. 540 */ 541 if (destroy_device) { 542 dev_dbg(dev, "Reset all active channels and remove MHI devices\n"); 543 device_for_each_child(&mhi_cntrl->mhi_dev->dev, NULL, mhi_destroy_device); 544 } 545 546 mutex_lock(&mhi_cntrl->pm_mutex); 547 548 WARN_ON(atomic_read(&mhi_cntrl->dev_wake)); 549 WARN_ON(atomic_read(&mhi_cntrl->pending_pkts)); 550 551 /* Reset the ev rings and cmd rings */ 552 dev_dbg(dev, "Resetting EV CTXT and CMD CTXT\n"); 553 mhi_cmd = mhi_cntrl->mhi_cmd; 554 cmd_ctxt = mhi_cntrl->mhi_ctxt->cmd_ctxt; 555 for (i = 0; i < NR_OF_CMD_RINGS; i++, mhi_cmd++, cmd_ctxt++) { 556 struct mhi_ring *ring = &mhi_cmd->ring; 557 558 ring->rp = ring->base; 559 ring->wp = ring->base; 560 cmd_ctxt->rp = cmd_ctxt->rbase; 561 cmd_ctxt->wp = cmd_ctxt->rbase; 562 } 563 564 mhi_event = mhi_cntrl->mhi_event; 565 er_ctxt = mhi_cntrl->mhi_ctxt->er_ctxt; 566 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, er_ctxt++, 567 mhi_event++) { 568 struct mhi_ring *ring = &mhi_event->ring; 569 570 /* Skip offload events */ 571 if (mhi_event->offload_ev) 572 continue; 573 574 ring->rp = ring->base; 575 ring->wp = ring->base; 576 er_ctxt->rp = er_ctxt->rbase; 577 er_ctxt->wp = er_ctxt->rbase; 578 } 579 580 /* Move to disable state */ 581 write_lock_irq(&mhi_cntrl->pm_lock); 582 cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_DISABLE); 583 write_unlock_irq(&mhi_cntrl->pm_lock); 584 if (unlikely(cur_state != MHI_PM_DISABLE)) 585 dev_err(dev, "Error moving from PM state: %s to: %s\n", 586 to_mhi_pm_state_str(cur_state), 587 to_mhi_pm_state_str(MHI_PM_DISABLE)); 588 589 dev_dbg(dev, "Exiting with PM state: %s, MHI state: %s\n", 590 to_mhi_pm_state_str(mhi_cntrl->pm_state), 591 mhi_state_str(mhi_cntrl->dev_state)); 592 593 mutex_unlock(&mhi_cntrl->pm_mutex); 594 } 595 596 /* Handle system error transitions */ 597 static void mhi_pm_sys_error_transition(struct mhi_controller *mhi_cntrl) 598 { 599 enum mhi_pm_state cur_state, prev_state; 600 enum dev_st_transition next_state; 601 struct mhi_event *mhi_event; 602 struct mhi_cmd_ctxt *cmd_ctxt; 603 struct mhi_cmd *mhi_cmd; 604 struct mhi_event_ctxt *er_ctxt; 605 struct device *dev = &mhi_cntrl->mhi_dev->dev; 606 bool reset_device = false; 607 int ret, i; 608 609 dev_dbg(dev, "Transitioning from PM state: %s to: %s\n", 610 to_mhi_pm_state_str(mhi_cntrl->pm_state), 611 to_mhi_pm_state_str(MHI_PM_SYS_ERR_PROCESS)); 612 613 /* We must notify MHI control driver so it can clean up first */ 614 mhi_cntrl->status_cb(mhi_cntrl, MHI_CB_SYS_ERROR); 615 616 mutex_lock(&mhi_cntrl->pm_mutex); 617 write_lock_irq(&mhi_cntrl->pm_lock); 618 prev_state = mhi_cntrl->pm_state; 619 cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_SYS_ERR_PROCESS); 620 write_unlock_irq(&mhi_cntrl->pm_lock); 621 622 if (cur_state != MHI_PM_SYS_ERR_PROCESS) { 623 dev_err(dev, "Failed to transition from PM state: %s to: %s\n", 624 to_mhi_pm_state_str(cur_state), 625 to_mhi_pm_state_str(MHI_PM_SYS_ERR_PROCESS)); 626 goto exit_sys_error_transition; 627 } 628 629 mhi_cntrl->ee = MHI_EE_DISABLE_TRANSITION; 630 mhi_cntrl->dev_state = MHI_STATE_RESET; 631 632 /* Wake up threads waiting for state transition */ 633 wake_up_all(&mhi_cntrl->state_event); 634 635 mhi_uevent_notify(mhi_cntrl, mhi_cntrl->ee); 636 637 if (MHI_REG_ACCESS_VALID(prev_state)) { 638 /* 639 * If the device is in PBL or SBL, it will only respond to 640 * RESET if the device is in SYSERR state. SYSERR might 641 * already be cleared at this point. 642 */ 643 enum mhi_state cur_state = mhi_get_mhi_state(mhi_cntrl); 644 enum mhi_ee_type cur_ee = mhi_get_exec_env(mhi_cntrl); 645 646 if (cur_state == MHI_STATE_SYS_ERR) 647 reset_device = true; 648 else if (cur_ee != MHI_EE_PBL && cur_ee != MHI_EE_SBL) 649 reset_device = true; 650 } 651 652 /* Trigger MHI RESET so that the device will not access host memory */ 653 if (reset_device) { 654 dev_dbg(dev, "Triggering MHI Reset in device\n"); 655 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_RESET); 656 657 /* Wait for the reset bit to be cleared by the device */ 658 ret = mhi_poll_reg_field(mhi_cntrl, mhi_cntrl->regs, MHICTRL, 659 MHICTRL_RESET_MASK, 0, 25000, mhi_cntrl->timeout_ms); 660 if (ret) { 661 dev_err(dev, "Device failed to exit MHI Reset state\n"); 662 write_lock_irq(&mhi_cntrl->pm_lock); 663 cur_state = mhi_tryset_pm_state(mhi_cntrl, 664 MHI_PM_SYS_ERR_FAIL); 665 write_unlock_irq(&mhi_cntrl->pm_lock); 666 /* Shutdown may have occurred, otherwise cleanup now */ 667 if (cur_state != MHI_PM_SYS_ERR_FAIL) 668 goto exit_sys_error_transition; 669 } 670 671 /* 672 * Device will clear BHI_INTVEC as a part of RESET processing, 673 * hence re-program it 674 */ 675 mhi_write_reg(mhi_cntrl, mhi_cntrl->bhi, BHI_INTVEC, 0); 676 } 677 678 dev_dbg(dev, 679 "Waiting for all pending event ring processing to complete\n"); 680 mhi_event = mhi_cntrl->mhi_event; 681 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, mhi_event++) { 682 if (mhi_event->offload_ev) 683 continue; 684 tasklet_kill(&mhi_event->task); 685 } 686 687 /* Release lock and wait for all pending threads to complete */ 688 mutex_unlock(&mhi_cntrl->pm_mutex); 689 dev_dbg(dev, "Waiting for all pending threads to complete\n"); 690 wake_up_all(&mhi_cntrl->state_event); 691 692 dev_dbg(dev, "Reset all active channels and remove MHI devices\n"); 693 device_for_each_child(&mhi_cntrl->mhi_dev->dev, NULL, mhi_destroy_device); 694 695 mutex_lock(&mhi_cntrl->pm_mutex); 696 697 WARN_ON(atomic_read(&mhi_cntrl->dev_wake)); 698 WARN_ON(atomic_read(&mhi_cntrl->pending_pkts)); 699 700 /* Reset the ev rings and cmd rings */ 701 dev_dbg(dev, "Resetting EV CTXT and CMD CTXT\n"); 702 mhi_cmd = mhi_cntrl->mhi_cmd; 703 cmd_ctxt = mhi_cntrl->mhi_ctxt->cmd_ctxt; 704 for (i = 0; i < NR_OF_CMD_RINGS; i++, mhi_cmd++, cmd_ctxt++) { 705 struct mhi_ring *ring = &mhi_cmd->ring; 706 707 ring->rp = ring->base; 708 ring->wp = ring->base; 709 cmd_ctxt->rp = cmd_ctxt->rbase; 710 cmd_ctxt->wp = cmd_ctxt->rbase; 711 } 712 713 mhi_event = mhi_cntrl->mhi_event; 714 er_ctxt = mhi_cntrl->mhi_ctxt->er_ctxt; 715 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, er_ctxt++, 716 mhi_event++) { 717 struct mhi_ring *ring = &mhi_event->ring; 718 719 /* Skip offload events */ 720 if (mhi_event->offload_ev) 721 continue; 722 723 ring->rp = ring->base; 724 ring->wp = ring->base; 725 er_ctxt->rp = er_ctxt->rbase; 726 er_ctxt->wp = er_ctxt->rbase; 727 } 728 729 /* Transition to next state */ 730 if (MHI_IN_PBL(mhi_get_exec_env(mhi_cntrl))) { 731 write_lock_irq(&mhi_cntrl->pm_lock); 732 cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_POR); 733 write_unlock_irq(&mhi_cntrl->pm_lock); 734 if (cur_state != MHI_PM_POR) { 735 dev_err(dev, "Error moving to state %s from %s\n", 736 to_mhi_pm_state_str(MHI_PM_POR), 737 to_mhi_pm_state_str(cur_state)); 738 goto exit_sys_error_transition; 739 } 740 next_state = DEV_ST_TRANSITION_PBL; 741 } else { 742 next_state = DEV_ST_TRANSITION_READY; 743 } 744 745 mhi_queue_state_transition(mhi_cntrl, next_state); 746 747 exit_sys_error_transition: 748 dev_dbg(dev, "Exiting with PM state: %s, MHI state: %s\n", 749 to_mhi_pm_state_str(mhi_cntrl->pm_state), 750 mhi_state_str(mhi_cntrl->dev_state)); 751 752 mutex_unlock(&mhi_cntrl->pm_mutex); 753 } 754 755 /* Queue a new work item and schedule work */ 756 int mhi_queue_state_transition(struct mhi_controller *mhi_cntrl, 757 enum dev_st_transition state) 758 { 759 struct state_transition *item = kmalloc_obj(*item, GFP_ATOMIC); 760 unsigned long flags; 761 762 if (!item) 763 return -ENOMEM; 764 765 item->state = state; 766 spin_lock_irqsave(&mhi_cntrl->transition_lock, flags); 767 list_add_tail(&item->node, &mhi_cntrl->transition_list); 768 spin_unlock_irqrestore(&mhi_cntrl->transition_lock, flags); 769 770 queue_work(mhi_cntrl->hiprio_wq, &mhi_cntrl->st_worker); 771 772 return 0; 773 } 774 775 /* SYS_ERR worker */ 776 void mhi_pm_sys_err_handler(struct mhi_controller *mhi_cntrl) 777 { 778 struct device *dev = &mhi_cntrl->mhi_dev->dev; 779 780 /* skip if controller supports RDDM */ 781 if (mhi_cntrl->rddm_image) { 782 dev_dbg(dev, "Controller supports RDDM, skip SYS_ERROR\n"); 783 return; 784 } 785 786 mhi_queue_state_transition(mhi_cntrl, DEV_ST_TRANSITION_SYS_ERR); 787 } 788 789 /* Device State Transition worker */ 790 void mhi_pm_st_worker(struct work_struct *work) 791 { 792 struct state_transition *itr, *tmp; 793 LIST_HEAD(head); 794 struct mhi_controller *mhi_cntrl = container_of(work, 795 struct mhi_controller, 796 st_worker); 797 798 spin_lock_irq(&mhi_cntrl->transition_lock); 799 list_splice_tail_init(&mhi_cntrl->transition_list, &head); 800 spin_unlock_irq(&mhi_cntrl->transition_lock); 801 802 list_for_each_entry_safe(itr, tmp, &head, node) { 803 list_del(&itr->node); 804 805 trace_mhi_pm_st_transition(mhi_cntrl, itr->state); 806 807 switch (itr->state) { 808 case DEV_ST_TRANSITION_PBL: 809 write_lock_irq(&mhi_cntrl->pm_lock); 810 if (MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state)) 811 mhi_cntrl->ee = mhi_get_exec_env(mhi_cntrl); 812 write_unlock_irq(&mhi_cntrl->pm_lock); 813 mhi_fw_load_handler(mhi_cntrl); 814 break; 815 case DEV_ST_TRANSITION_SBL: 816 write_lock_irq(&mhi_cntrl->pm_lock); 817 mhi_cntrl->ee = MHI_EE_SBL; 818 write_unlock_irq(&mhi_cntrl->pm_lock); 819 /* 820 * The MHI devices are only created when the client 821 * device switches its Execution Environment (EE) to 822 * either SBL or AMSS states 823 */ 824 mhi_create_devices(mhi_cntrl); 825 if (mhi_cntrl->fbc_download) 826 mhi_download_amss_image(mhi_cntrl); 827 828 mhi_uevent_notify(mhi_cntrl, mhi_cntrl->ee); 829 break; 830 case DEV_ST_TRANSITION_MISSION_MODE: 831 mhi_pm_mission_mode_transition(mhi_cntrl); 832 break; 833 case DEV_ST_TRANSITION_FP: 834 write_lock_irq(&mhi_cntrl->pm_lock); 835 mhi_cntrl->ee = MHI_EE_FP; 836 write_unlock_irq(&mhi_cntrl->pm_lock); 837 mhi_create_devices(mhi_cntrl); 838 mhi_uevent_notify(mhi_cntrl, mhi_cntrl->ee); 839 break; 840 case DEV_ST_TRANSITION_READY: 841 mhi_ready_state_transition(mhi_cntrl); 842 break; 843 case DEV_ST_TRANSITION_SYS_ERR: 844 mhi_pm_sys_error_transition(mhi_cntrl); 845 break; 846 case DEV_ST_TRANSITION_DISABLE: 847 mhi_pm_disable_transition(mhi_cntrl, false); 848 break; 849 case DEV_ST_TRANSITION_DISABLE_DESTROY_DEVICE: 850 mhi_pm_disable_transition(mhi_cntrl, true); 851 break; 852 default: 853 break; 854 } 855 kfree(itr); 856 } 857 } 858 859 int mhi_pm_suspend(struct mhi_controller *mhi_cntrl) 860 { 861 struct mhi_chan *itr, *tmp; 862 struct device *dev = &mhi_cntrl->mhi_dev->dev; 863 enum mhi_pm_state new_state; 864 int ret; 865 866 if (mhi_cntrl->pm_state == MHI_PM_DISABLE) 867 return -EINVAL; 868 869 if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) 870 return -EIO; 871 872 /* Return busy if there are any pending resources */ 873 if (atomic_read(&mhi_cntrl->dev_wake) || 874 atomic_read(&mhi_cntrl->pending_pkts)) 875 return -EBUSY; 876 877 /* Take MHI out of M2 state */ 878 read_lock_bh(&mhi_cntrl->pm_lock); 879 mhi_cntrl->wake_get(mhi_cntrl, false); 880 read_unlock_bh(&mhi_cntrl->pm_lock); 881 882 ret = wait_event_timeout(mhi_cntrl->state_event, 883 mhi_cntrl->dev_state == MHI_STATE_M0 || 884 mhi_cntrl->dev_state == MHI_STATE_M1 || 885 MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state), 886 msecs_to_jiffies(mhi_cntrl->timeout_ms)); 887 888 read_lock_bh(&mhi_cntrl->pm_lock); 889 mhi_cntrl->wake_put(mhi_cntrl, false); 890 read_unlock_bh(&mhi_cntrl->pm_lock); 891 892 if (!ret || MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { 893 dev_err(dev, 894 "Could not enter M0/M1 state"); 895 return -EIO; 896 } 897 898 write_lock_irq(&mhi_cntrl->pm_lock); 899 900 if (atomic_read(&mhi_cntrl->dev_wake) || 901 atomic_read(&mhi_cntrl->pending_pkts)) { 902 write_unlock_irq(&mhi_cntrl->pm_lock); 903 return -EBUSY; 904 } 905 906 dev_dbg(dev, "Allowing M3 transition\n"); 907 new_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_M3_ENTER); 908 if (new_state != MHI_PM_M3_ENTER) { 909 write_unlock_irq(&mhi_cntrl->pm_lock); 910 dev_err(dev, 911 "Error setting to PM state: %s from: %s\n", 912 to_mhi_pm_state_str(MHI_PM_M3_ENTER), 913 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 914 return -EIO; 915 } 916 917 /* 918 * For devices without M3 support, just set the host state to M3. This 919 * host transition is needed to prevent the client drivers from 920 * accessing the device during suspend. 921 */ 922 if (mhi_cntrl->no_m3) { 923 new_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_M3); 924 write_unlock_irq(&mhi_cntrl->pm_lock); 925 if (new_state != MHI_PM_M3) { 926 dev_err(dev, 927 "Error setting to PM state: %s from: %s\n", 928 to_mhi_pm_state_str(MHI_PM_M3), 929 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 930 return -EIO; 931 } 932 } else { 933 /* Set MHI to M3 and wait for completion */ 934 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_M3); 935 write_unlock_irq(&mhi_cntrl->pm_lock); 936 dev_dbg(dev, "Waiting for M3 completion\n"); 937 938 ret = wait_event_timeout(mhi_cntrl->state_event, 939 mhi_cntrl->dev_state == MHI_STATE_M3 || 940 MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state), 941 msecs_to_jiffies(mhi_cntrl->timeout_ms)); 942 943 if (!ret || MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { 944 dev_err(dev, 945 "Did not enter M3 state, MHI state: %s, PM state: %s\n", 946 mhi_state_str(mhi_cntrl->dev_state), 947 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 948 return -EIO; 949 } 950 } 951 952 /* Notify clients about entering LPM */ 953 list_for_each_entry_safe(itr, tmp, &mhi_cntrl->lpm_chans, node) { 954 mutex_lock(&itr->mutex); 955 if (itr->mhi_dev) 956 mhi_notify(itr->mhi_dev, MHI_CB_LPM_ENTER); 957 mutex_unlock(&itr->mutex); 958 } 959 960 return 0; 961 } 962 EXPORT_SYMBOL_GPL(mhi_pm_suspend); 963 964 static int __mhi_pm_resume(struct mhi_controller *mhi_cntrl, bool force) 965 { 966 struct mhi_chan *itr, *tmp; 967 struct device *dev = &mhi_cntrl->mhi_dev->dev; 968 enum mhi_pm_state cur_state; 969 int ret; 970 971 dev_dbg(dev, "Entered with PM state: %s, MHI state: %s\n", 972 to_mhi_pm_state_str(mhi_cntrl->pm_state), 973 mhi_state_str(mhi_cntrl->dev_state)); 974 975 if (mhi_cntrl->pm_state == MHI_PM_DISABLE) 976 return 0; 977 978 if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) 979 return -EIO; 980 981 if (!mhi_cntrl->no_m3 && 982 mhi_get_mhi_state(mhi_cntrl) != MHI_STATE_M3) { 983 dev_warn(dev, "Resuming from non M3 state (%s)\n", 984 mhi_state_str(mhi_get_mhi_state(mhi_cntrl))); 985 if (!force) 986 return -EINVAL; 987 } 988 989 /* Notify clients about exiting LPM */ 990 list_for_each_entry_safe(itr, tmp, &mhi_cntrl->lpm_chans, node) { 991 mutex_lock(&itr->mutex); 992 if (itr->mhi_dev) 993 mhi_notify(itr->mhi_dev, MHI_CB_LPM_EXIT); 994 mutex_unlock(&itr->mutex); 995 } 996 997 write_lock_irq(&mhi_cntrl->pm_lock); 998 cur_state = mhi_tryset_pm_state(mhi_cntrl, MHI_PM_M3_EXIT); 999 if (cur_state != MHI_PM_M3_EXIT) { 1000 write_unlock_irq(&mhi_cntrl->pm_lock); 1001 dev_info(dev, 1002 "Error setting to PM state: %s from: %s\n", 1003 to_mhi_pm_state_str(MHI_PM_M3_EXIT), 1004 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 1005 return -EIO; 1006 } 1007 1008 /* 1009 * For devices without M3 support, just move the host back to M0 1010 * directly. 1011 */ 1012 if (mhi_cntrl->no_m3) { 1013 write_unlock_irq(&mhi_cntrl->pm_lock); 1014 return mhi_pm_m0_transition(mhi_cntrl); 1015 } 1016 1017 /* Set MHI to M0 and wait for completion */ 1018 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_M0); 1019 write_unlock_irq(&mhi_cntrl->pm_lock); 1020 1021 ret = wait_event_timeout(mhi_cntrl->state_event, 1022 mhi_cntrl->dev_state == MHI_STATE_M0 || 1023 mhi_cntrl->dev_state == MHI_STATE_M2 || 1024 MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state), 1025 msecs_to_jiffies(mhi_cntrl->timeout_ms)); 1026 1027 if (!ret || MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { 1028 dev_err(dev, 1029 "Did not enter M0 state, MHI state: %s, PM state: %s\n", 1030 mhi_state_str(mhi_cntrl->dev_state), 1031 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 1032 return -EIO; 1033 } 1034 1035 return 0; 1036 } 1037 1038 int mhi_pm_resume(struct mhi_controller *mhi_cntrl) 1039 { 1040 return __mhi_pm_resume(mhi_cntrl, false); 1041 } 1042 EXPORT_SYMBOL_GPL(mhi_pm_resume); 1043 1044 int mhi_pm_resume_force(struct mhi_controller *mhi_cntrl) 1045 { 1046 return __mhi_pm_resume(mhi_cntrl, true); 1047 } 1048 EXPORT_SYMBOL_GPL(mhi_pm_resume_force); 1049 1050 int __mhi_device_get_sync(struct mhi_controller *mhi_cntrl) 1051 { 1052 int ret; 1053 1054 /* Wake up the device */ 1055 read_lock_bh(&mhi_cntrl->pm_lock); 1056 if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { 1057 read_unlock_bh(&mhi_cntrl->pm_lock); 1058 return -EIO; 1059 } 1060 mhi_cntrl->wake_get(mhi_cntrl, true); 1061 if (MHI_PM_IN_SUSPEND_STATE(mhi_cntrl->pm_state)) 1062 mhi_trigger_resume(mhi_cntrl); 1063 read_unlock_bh(&mhi_cntrl->pm_lock); 1064 1065 ret = wait_event_timeout(mhi_cntrl->state_event, 1066 mhi_cntrl->pm_state == MHI_PM_M0 || 1067 MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state), 1068 msecs_to_jiffies(mhi_cntrl->timeout_ms)); 1069 1070 if (!ret || MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { 1071 read_lock_bh(&mhi_cntrl->pm_lock); 1072 mhi_cntrl->wake_put(mhi_cntrl, false); 1073 read_unlock_bh(&mhi_cntrl->pm_lock); 1074 return -EIO; 1075 } 1076 1077 return 0; 1078 } 1079 1080 /* Assert device wake db */ 1081 static void mhi_assert_dev_wake(struct mhi_controller *mhi_cntrl, bool force) 1082 { 1083 unsigned long flags; 1084 1085 /* 1086 * If force flag is set, then increment the wake count value and 1087 * ring wake db 1088 */ 1089 if (unlikely(force)) { 1090 spin_lock_irqsave(&mhi_cntrl->wlock, flags); 1091 atomic_inc(&mhi_cntrl->dev_wake); 1092 if (MHI_WAKE_DB_FORCE_SET_VALID(mhi_cntrl->pm_state) && 1093 !mhi_cntrl->wake_set) { 1094 mhi_write_db(mhi_cntrl, mhi_cntrl->wake_db, 1); 1095 mhi_cntrl->wake_set = true; 1096 } 1097 spin_unlock_irqrestore(&mhi_cntrl->wlock, flags); 1098 } else { 1099 /* 1100 * If resources are already requested, then just increment 1101 * the wake count value and return 1102 */ 1103 if (likely(atomic_add_unless(&mhi_cntrl->dev_wake, 1, 0))) 1104 return; 1105 1106 spin_lock_irqsave(&mhi_cntrl->wlock, flags); 1107 if ((atomic_inc_return(&mhi_cntrl->dev_wake) == 1) && 1108 MHI_WAKE_DB_SET_VALID(mhi_cntrl->pm_state) && 1109 !mhi_cntrl->wake_set) { 1110 mhi_write_db(mhi_cntrl, mhi_cntrl->wake_db, 1); 1111 mhi_cntrl->wake_set = true; 1112 } 1113 spin_unlock_irqrestore(&mhi_cntrl->wlock, flags); 1114 } 1115 } 1116 1117 /* De-assert device wake db */ 1118 static void mhi_deassert_dev_wake(struct mhi_controller *mhi_cntrl, 1119 bool override) 1120 { 1121 unsigned long flags; 1122 1123 /* 1124 * Only continue if there is a single resource, else just decrement 1125 * and return 1126 */ 1127 if (likely(atomic_add_unless(&mhi_cntrl->dev_wake, -1, 1))) 1128 return; 1129 1130 spin_lock_irqsave(&mhi_cntrl->wlock, flags); 1131 if ((atomic_dec_return(&mhi_cntrl->dev_wake) == 0) && 1132 MHI_WAKE_DB_CLEAR_VALID(mhi_cntrl->pm_state) && !override && 1133 mhi_cntrl->wake_set) { 1134 mhi_write_db(mhi_cntrl, mhi_cntrl->wake_db, 0); 1135 mhi_cntrl->wake_set = false; 1136 } 1137 spin_unlock_irqrestore(&mhi_cntrl->wlock, flags); 1138 } 1139 1140 int mhi_async_power_up(struct mhi_controller *mhi_cntrl) 1141 { 1142 struct mhi_event *mhi_event = mhi_cntrl->mhi_event; 1143 enum mhi_state state; 1144 enum mhi_ee_type current_ee; 1145 enum dev_st_transition next_state; 1146 struct device *dev = &mhi_cntrl->mhi_dev->dev; 1147 u32 interval_us = 25000; /* poll register field every 25 milliseconds */ 1148 int ret, i; 1149 1150 dev_info(dev, "Requested to power ON\n"); 1151 1152 /* Supply default wake routines if not provided by controller driver */ 1153 if (!mhi_cntrl->wake_get || !mhi_cntrl->wake_put || 1154 !mhi_cntrl->wake_toggle) { 1155 mhi_cntrl->wake_get = mhi_assert_dev_wake; 1156 mhi_cntrl->wake_put = mhi_deassert_dev_wake; 1157 mhi_cntrl->wake_toggle = (mhi_cntrl->db_access & MHI_PM_M2) ? 1158 mhi_toggle_dev_wake_nop : mhi_toggle_dev_wake; 1159 } 1160 1161 mutex_lock(&mhi_cntrl->pm_mutex); 1162 mhi_cntrl->pm_state = MHI_PM_DISABLE; 1163 1164 /* Setup BHI INTVEC */ 1165 write_lock_irq(&mhi_cntrl->pm_lock); 1166 mhi_write_reg(mhi_cntrl, mhi_cntrl->bhi, BHI_INTVEC, 0); 1167 mhi_cntrl->pm_state = MHI_PM_POR; 1168 mhi_cntrl->ee = MHI_EE_MAX; 1169 current_ee = mhi_get_exec_env(mhi_cntrl); 1170 write_unlock_irq(&mhi_cntrl->pm_lock); 1171 1172 /* Confirm that the device is in valid exec env */ 1173 if (!MHI_POWER_UP_CAPABLE(current_ee)) { 1174 dev_err(dev, "%s is not a valid EE for power on\n", 1175 TO_MHI_EXEC_STR(current_ee)); 1176 ret = -EIO; 1177 goto error_exit; 1178 } 1179 1180 state = mhi_get_mhi_state(mhi_cntrl); 1181 dev_dbg(dev, "Attempting power on with EE: %s, state: %s\n", 1182 TO_MHI_EXEC_STR(current_ee), mhi_state_str(state)); 1183 1184 if (state == MHI_STATE_SYS_ERR) { 1185 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_RESET); 1186 ret = mhi_poll_reg_field(mhi_cntrl, mhi_cntrl->regs, MHICTRL, 1187 MHICTRL_RESET_MASK, 0, interval_us, 1188 mhi_cntrl->timeout_ms); 1189 if (ret) { 1190 dev_info(dev, "Failed to reset MHI due to syserr state\n"); 1191 goto error_exit; 1192 } 1193 1194 /* 1195 * device cleares INTVEC as part of RESET processing, 1196 * re-program it 1197 */ 1198 mhi_write_reg(mhi_cntrl, mhi_cntrl->bhi, BHI_INTVEC, 0); 1199 } 1200 1201 /* IRQs have been requested during probe, so we just need to enable them. */ 1202 enable_irq(mhi_cntrl->irq[0]); 1203 1204 for (i = 0; i < mhi_cntrl->total_ev_rings; i++, mhi_event++) { 1205 if (mhi_event->offload_ev) 1206 continue; 1207 1208 enable_irq(mhi_cntrl->irq[mhi_event->irq]); 1209 } 1210 1211 /* Transition to next state */ 1212 next_state = MHI_IN_PBL(current_ee) ? 1213 DEV_ST_TRANSITION_PBL : DEV_ST_TRANSITION_READY; 1214 1215 mhi_queue_state_transition(mhi_cntrl, next_state); 1216 1217 mutex_unlock(&mhi_cntrl->pm_mutex); 1218 1219 dev_info(dev, "Power on setup success\n"); 1220 1221 return 0; 1222 1223 error_exit: 1224 mhi_cntrl->pm_state = MHI_PM_DISABLE; 1225 mutex_unlock(&mhi_cntrl->pm_mutex); 1226 1227 return ret; 1228 } 1229 EXPORT_SYMBOL_GPL(mhi_async_power_up); 1230 1231 static void __mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful, 1232 bool destroy_device) 1233 { 1234 enum mhi_pm_state cur_state, transition_state; 1235 struct device *dev = &mhi_cntrl->mhi_dev->dev; 1236 1237 mutex_lock(&mhi_cntrl->pm_mutex); 1238 write_lock_irq(&mhi_cntrl->pm_lock); 1239 cur_state = mhi_cntrl->pm_state; 1240 if (cur_state == MHI_PM_DISABLE) { 1241 write_unlock_irq(&mhi_cntrl->pm_lock); 1242 mutex_unlock(&mhi_cntrl->pm_mutex); 1243 return; /* Already powered down */ 1244 } 1245 1246 /* If it's not a graceful shutdown, force MHI to linkdown state */ 1247 transition_state = (graceful) ? MHI_PM_SHUTDOWN_PROCESS : 1248 MHI_PM_LD_ERR_FATAL_DETECT; 1249 1250 cur_state = mhi_tryset_pm_state(mhi_cntrl, transition_state); 1251 if (cur_state != transition_state) { 1252 dev_err(dev, "Failed to move to state: %s from: %s\n", 1253 to_mhi_pm_state_str(transition_state), 1254 to_mhi_pm_state_str(mhi_cntrl->pm_state)); 1255 /* Force link down or error fatal detected state */ 1256 mhi_cntrl->pm_state = MHI_PM_LD_ERR_FATAL_DETECT; 1257 } 1258 1259 /* mark device inactive to avoid any further host processing */ 1260 mhi_cntrl->ee = MHI_EE_DISABLE_TRANSITION; 1261 mhi_cntrl->dev_state = MHI_STATE_RESET; 1262 1263 wake_up_all(&mhi_cntrl->state_event); 1264 1265 write_unlock_irq(&mhi_cntrl->pm_lock); 1266 mutex_unlock(&mhi_cntrl->pm_mutex); 1267 1268 mhi_uevent_notify(mhi_cntrl, mhi_cntrl->ee); 1269 1270 if (destroy_device) 1271 mhi_queue_state_transition(mhi_cntrl, 1272 DEV_ST_TRANSITION_DISABLE_DESTROY_DEVICE); 1273 else 1274 mhi_queue_state_transition(mhi_cntrl, 1275 DEV_ST_TRANSITION_DISABLE); 1276 1277 /* Wait for shutdown to complete */ 1278 flush_work(&mhi_cntrl->st_worker); 1279 1280 disable_irq(mhi_cntrl->irq[0]); 1281 } 1282 1283 void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful) 1284 { 1285 __mhi_power_down(mhi_cntrl, graceful, true); 1286 } 1287 EXPORT_SYMBOL_GPL(mhi_power_down); 1288 1289 void mhi_power_down_keep_dev(struct mhi_controller *mhi_cntrl, 1290 bool graceful) 1291 { 1292 __mhi_power_down(mhi_cntrl, graceful, false); 1293 } 1294 EXPORT_SYMBOL_GPL(mhi_power_down_keep_dev); 1295 1296 int mhi_sync_power_up(struct mhi_controller *mhi_cntrl) 1297 { 1298 int ret = mhi_async_power_up(mhi_cntrl); 1299 u32 timeout_ms; 1300 1301 if (ret) 1302 return ret; 1303 1304 /* Some devices need more time to set ready during power up */ 1305 timeout_ms = mhi_cntrl->ready_timeout_ms ? 1306 mhi_cntrl->ready_timeout_ms : mhi_cntrl->timeout_ms; 1307 wait_event_timeout(mhi_cntrl->state_event, 1308 MHI_IN_MISSION_MODE(mhi_cntrl->ee) || 1309 MHI_PM_FATAL_ERROR(mhi_cntrl->pm_state), 1310 msecs_to_jiffies(timeout_ms)); 1311 1312 ret = (MHI_IN_MISSION_MODE(mhi_cntrl->ee)) ? 0 : -ETIMEDOUT; 1313 if (ret) 1314 mhi_power_down(mhi_cntrl, false); 1315 1316 return ret; 1317 } 1318 EXPORT_SYMBOL(mhi_sync_power_up); 1319 1320 int mhi_force_rddm_mode(struct mhi_controller *mhi_cntrl) 1321 { 1322 struct device *dev = &mhi_cntrl->mhi_dev->dev; 1323 int ret; 1324 1325 /* Check if device is already in RDDM */ 1326 if (mhi_cntrl->ee == MHI_EE_RDDM) 1327 return 0; 1328 1329 dev_dbg(dev, "Triggering SYS_ERR to force RDDM state\n"); 1330 mhi_set_mhi_state(mhi_cntrl, MHI_STATE_SYS_ERR); 1331 1332 /* Wait for RDDM event */ 1333 ret = wait_event_timeout(mhi_cntrl->state_event, 1334 mhi_cntrl->ee == MHI_EE_RDDM, 1335 msecs_to_jiffies(mhi_cntrl->timeout_ms)); 1336 ret = ret ? 0 : -EIO; 1337 1338 return ret; 1339 } 1340 EXPORT_SYMBOL_GPL(mhi_force_rddm_mode); 1341 1342 int mhi_device_get_sync(struct mhi_device *mhi_dev) 1343 { 1344 struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; 1345 int ret; 1346 1347 ret = __mhi_device_get_sync(mhi_cntrl); 1348 if (!ret) 1349 mhi_dev->dev_wake++; 1350 1351 return ret; 1352 } 1353 EXPORT_SYMBOL_GPL(mhi_device_get_sync); 1354 1355 void mhi_device_put(struct mhi_device *mhi_dev) 1356 { 1357 struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; 1358 1359 mhi_dev->dev_wake--; 1360 read_lock_bh(&mhi_cntrl->pm_lock); 1361 if (MHI_PM_IN_SUSPEND_STATE(mhi_cntrl->pm_state)) 1362 mhi_trigger_resume(mhi_cntrl); 1363 1364 mhi_cntrl->wake_put(mhi_cntrl, false); 1365 read_unlock_bh(&mhi_cntrl->pm_lock); 1366 } 1367 EXPORT_SYMBOL_GPL(mhi_device_put); 1368 1369 void mhi_uevent_notify(struct mhi_controller *mhi_cntrl, enum mhi_ee_type ee) 1370 { 1371 struct device *dev = &mhi_cntrl->mhi_dev->dev; 1372 char *buf[2]; 1373 int ret; 1374 1375 buf[0] = kasprintf(GFP_KERNEL, "EXEC_ENV=%s", TO_MHI_EXEC_STR(ee)); 1376 buf[1] = NULL; 1377 1378 if (!buf[0]) 1379 return; 1380 1381 ret = kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, buf); 1382 if (ret) 1383 dev_err(dev, "Failed to send %s uevent\n", TO_MHI_EXEC_STR(ee)); 1384 1385 kfree(buf[0]); 1386 } 1387