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