1 /**
2  * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
3  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The names of the above-listed copyright holders may not be used
15  *    to endorse or promote products derived from this software without
16  *    specific prior written permission.
17  *
18  * ALTERNATIVELY, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2, as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef VCHIQ_ARM_H
36 #define VCHIQ_ARM_H
37 
38 #include <linux/mutex.h>
39 #include <linux/platform_device.h>
40 #include <linux/semaphore.h>
41 #include <linux/atomic.h>
42 #include "vchiq_core.h"
43 #include "vchiq_debugfs.h"
44 
45 enum vc_suspend_status {
46 	VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
47 	VC_SUSPEND_REJECTED = -2,  /* Videocore rejected suspend request */
48 	VC_SUSPEND_FAILED = -1,    /* Videocore suspend failed */
49 	VC_SUSPEND_IDLE = 0,       /* VC active, no suspend actions */
50 	VC_SUSPEND_REQUESTED,      /* User has requested suspend */
51 	VC_SUSPEND_IN_PROGRESS,    /* Slot handler has recvd suspend request */
52 	VC_SUSPEND_SUSPENDED       /* Videocore suspend succeeded */
53 };
54 
55 enum vc_resume_status {
56 	VC_RESUME_FAILED = -1, /* Videocore resume failed */
57 	VC_RESUME_IDLE = 0,    /* VC suspended, no resume actions */
58 	VC_RESUME_REQUESTED,   /* User has requested resume */
59 	VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
60 	VC_RESUME_RESUMED      /* Videocore resumed successfully (active) */
61 };
62 
63 enum USE_TYPE_E {
64 	USE_TYPE_SERVICE,
65 	USE_TYPE_SERVICE_NO_RESUME,
66 	USE_TYPE_VCHIQ
67 };
68 
69 struct vchiq_arm_state {
70 	/* Keepalive-related data */
71 	struct task_struct *ka_thread;
72 	struct completion ka_evt;
73 	atomic_t ka_use_count;
74 	atomic_t ka_use_ack_count;
75 	atomic_t ka_release_count;
76 
77 	struct completion vc_suspend_complete;
78 	struct completion vc_resume_complete;
79 
80 	rwlock_t susp_res_lock;
81 	enum vc_suspend_status vc_suspend_state;
82 	enum vc_resume_status vc_resume_state;
83 
84 	unsigned int wake_address;
85 
86 	struct vchiq_state *state;
87 	struct timer_list suspend_timer;
88 	int suspend_timer_timeout;
89 	int suspend_timer_running;
90 
91 	/* Global use count for videocore.
92 	** This is equal to the sum of the use counts for all services.  When
93 	** this hits zero the videocore suspend procedure will be initiated.
94 	*/
95 	int videocore_use_count;
96 
97 	/* Use count to track requests from videocore peer.
98 	** This use count is not associated with a service, so needs to be
99 	** tracked separately with the state.
100 	*/
101 	int peer_use_count;
102 
103 	/* Flag to indicate whether resume is blocked.  This happens when the
104 	** ARM is suspending
105 	*/
106 	struct completion resume_blocker;
107 	int resume_blocked;
108 	struct completion blocked_blocker;
109 	int blocked_count;
110 
111 	int autosuspend_override;
112 
113 	/* Flag to indicate that the first vchiq connect has made it through.
114 	** This means that both sides should be fully ready, and we should
115 	** be able to suspend after this point.
116 	*/
117 	int first_connect;
118 
119 	unsigned long long suspend_start_time;
120 	unsigned long long sleep_start_time;
121 	unsigned long long resume_start_time;
122 	unsigned long long last_wake_time;
123 
124 };
125 
126 struct vchiq_drvdata {
127 	const unsigned int cache_line_size;
128 	struct rpi_firmware *fw;
129 };
130 
131 extern int vchiq_arm_log_level;
132 extern int vchiq_susp_log_level;
133 
134 int vchiq_platform_init(struct platform_device *pdev,
135 			struct vchiq_state *state);
136 
137 extern struct vchiq_state *
138 vchiq_get_state(void);
139 
140 extern VCHIQ_STATUS_T
141 vchiq_arm_vcsuspend(struct vchiq_state *state);
142 
143 extern VCHIQ_STATUS_T
144 vchiq_arm_force_suspend(struct vchiq_state *state);
145 
146 extern int
147 vchiq_arm_allow_resume(struct vchiq_state *state);
148 
149 extern VCHIQ_STATUS_T
150 vchiq_arm_vcresume(struct vchiq_state *state);
151 
152 extern VCHIQ_STATUS_T
153 vchiq_arm_init_state(struct vchiq_state *state,
154 		     struct vchiq_arm_state *arm_state);
155 
156 extern int
157 vchiq_check_resume(struct vchiq_state *state);
158 
159 extern void
160 vchiq_check_suspend(struct vchiq_state *state);
161 VCHIQ_STATUS_T
162 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
163 
164 extern VCHIQ_STATUS_T
165 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
166 
167 extern VCHIQ_STATUS_T
168 vchiq_check_service(struct vchiq_service *service);
169 
170 extern VCHIQ_STATUS_T
171 vchiq_platform_suspend(struct vchiq_state *state);
172 
173 extern int
174 vchiq_platform_videocore_wanted(struct vchiq_state *state);
175 
176 extern int
177 vchiq_platform_use_suspend_timer(void);
178 
179 extern void
180 vchiq_dump_platform_use_state(struct vchiq_state *state);
181 
182 extern void
183 vchiq_dump_service_use_state(struct vchiq_state *state);
184 
185 extern struct vchiq_arm_state*
186 vchiq_platform_get_arm_state(struct vchiq_state *state);
187 
188 extern int
189 vchiq_videocore_wanted(struct vchiq_state *state);
190 
191 extern VCHIQ_STATUS_T
192 vchiq_use_internal(struct vchiq_state *state, struct vchiq_service *service,
193 		   enum USE_TYPE_E use_type);
194 extern VCHIQ_STATUS_T
195 vchiq_release_internal(struct vchiq_state *state,
196 		       struct vchiq_service *service);
197 
198 extern struct vchiq_debugfs_node *
199 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
200 
201 extern int
202 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
203 
204 extern int
205 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
206 
207 extern int
208 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
209 
210 extern void
211 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
212 
213 extern void
214 set_suspend_state(struct vchiq_arm_state *arm_state,
215 		  enum vc_suspend_status new_state);
216 
217 extern void
218 set_resume_state(struct vchiq_arm_state *arm_state,
219 		 enum vc_resume_status new_state);
220 
221 extern void
222 start_suspend_timer(struct vchiq_arm_state *arm_state);
223 
224 #endif /* VCHIQ_ARM_H */
225