]>
Commit | Line | Data |
---|---|---|
71bad7f0 | 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 | #include <linux/kernel.h> | |
36 | #include <linux/module.h> | |
37 | #include <linux/types.h> | |
38 | #include <linux/errno.h> | |
39 | #include <linux/cdev.h> | |
40 | #include <linux/fs.h> | |
41 | #include <linux/device.h> | |
42 | #include <linux/mm.h> | |
43 | #include <linux/highmem.h> | |
44 | #include <linux/pagemap.h> | |
45 | #include <linux/bug.h> | |
46 | #include <linux/semaphore.h> | |
47 | #include <linux/list.h> | |
48 | #include <linux/of.h> | |
49 | #include <linux/platform_device.h> | |
50 | #include <soc/bcm2835/raspberrypi-firmware.h> | |
51 | ||
52 | #include "vchiq_core.h" | |
53 | #include "vchiq_ioctl.h" | |
54 | #include "vchiq_arm.h" | |
55 | #include "vchiq_debugfs.h" | |
56 | #include "vchiq_killable.h" | |
57 | ||
58 | #define DEVICE_NAME "vchiq" | |
59 | ||
60 | /* Override the default prefix, which would be vchiq_arm (from the filename) */ | |
61 | #undef MODULE_PARAM_PREFIX | |
62 | #define MODULE_PARAM_PREFIX DEVICE_NAME "." | |
63 | ||
64 | #define VCHIQ_MINOR 0 | |
65 | ||
66 | /* Some per-instance constants */ | |
67 | #define MAX_COMPLETIONS 16 | |
68 | #define MAX_SERVICES 64 | |
69 | #define MAX_ELEMENTS 8 | |
70 | #define MSG_QUEUE_SIZE 64 | |
71 | ||
72 | #define KEEPALIVE_VER 1 | |
73 | #define KEEPALIVE_VER_MIN KEEPALIVE_VER | |
74 | ||
75 | /* Run time control of log level, based on KERN_XXX level. */ | |
76 | int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT; | |
77 | int vchiq_susp_log_level = VCHIQ_LOG_ERROR; | |
78 | ||
79 | #define SUSPEND_TIMER_TIMEOUT_MS 100 | |
80 | #define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000 | |
81 | ||
82 | #define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */ | |
83 | static const char *const suspend_state_names[] = { | |
84 | "VC_SUSPEND_FORCE_CANCELED", | |
85 | "VC_SUSPEND_REJECTED", | |
86 | "VC_SUSPEND_FAILED", | |
87 | "VC_SUSPEND_IDLE", | |
88 | "VC_SUSPEND_REQUESTED", | |
89 | "VC_SUSPEND_IN_PROGRESS", | |
90 | "VC_SUSPEND_SUSPENDED" | |
91 | }; | |
92 | #define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */ | |
93 | static const char *const resume_state_names[] = { | |
94 | "VC_RESUME_FAILED", | |
95 | "VC_RESUME_IDLE", | |
96 | "VC_RESUME_REQUESTED", | |
97 | "VC_RESUME_IN_PROGRESS", | |
98 | "VC_RESUME_RESUMED" | |
99 | }; | |
100 | /* The number of times we allow force suspend to timeout before actually | |
101 | ** _forcing_ suspend. This is to cater for SW which fails to release vchiq | |
102 | ** correctly - we don't want to prevent ARM suspend indefinitely in this case. | |
103 | */ | |
104 | #define FORCE_SUSPEND_FAIL_MAX 8 | |
105 | ||
106 | /* The time in ms allowed for videocore to go idle when force suspend has been | |
107 | * requested */ | |
108 | #define FORCE_SUSPEND_TIMEOUT_MS 200 | |
109 | ||
110 | ||
111 | static void suspend_timer_callback(unsigned long context); | |
112 | ||
113 | ||
114 | typedef struct user_service_struct { | |
115 | VCHIQ_SERVICE_T *service; | |
116 | void *userdata; | |
117 | VCHIQ_INSTANCE_T instance; | |
118 | char is_vchi; | |
119 | char dequeue_pending; | |
120 | char close_pending; | |
121 | int message_available_pos; | |
122 | int msg_insert; | |
123 | int msg_remove; | |
124 | struct semaphore insert_event; | |
125 | struct semaphore remove_event; | |
126 | struct semaphore close_event; | |
127 | VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE]; | |
128 | } USER_SERVICE_T; | |
129 | ||
130 | struct bulk_waiter_node { | |
131 | struct bulk_waiter bulk_waiter; | |
132 | int pid; | |
133 | struct list_head list; | |
134 | }; | |
135 | ||
136 | struct vchiq_instance_struct { | |
137 | VCHIQ_STATE_T *state; | |
138 | VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS]; | |
139 | int completion_insert; | |
140 | int completion_remove; | |
141 | struct semaphore insert_event; | |
142 | struct semaphore remove_event; | |
143 | struct mutex completion_mutex; | |
144 | ||
145 | int connected; | |
146 | int closing; | |
147 | int pid; | |
148 | int mark; | |
149 | int use_close_delivered; | |
150 | int trace; | |
151 | ||
152 | struct list_head bulk_waiter_list; | |
153 | struct mutex bulk_waiter_list_mutex; | |
154 | ||
155 | VCHIQ_DEBUGFS_NODE_T debugfs_node; | |
156 | }; | |
157 | ||
158 | typedef struct dump_context_struct { | |
159 | char __user *buf; | |
160 | size_t actual; | |
161 | size_t space; | |
162 | loff_t offset; | |
163 | } DUMP_CONTEXT_T; | |
164 | ||
165 | static struct cdev vchiq_cdev; | |
166 | static dev_t vchiq_devid; | |
167 | static VCHIQ_STATE_T g_state; | |
168 | static struct class *vchiq_class; | |
169 | static struct device *vchiq_dev; | |
170 | static DEFINE_SPINLOCK(msg_queue_spinlock); | |
171 | ||
172 | static const char *const ioctl_names[] = { | |
173 | "CONNECT", | |
174 | "SHUTDOWN", | |
175 | "CREATE_SERVICE", | |
176 | "REMOVE_SERVICE", | |
177 | "QUEUE_MESSAGE", | |
178 | "QUEUE_BULK_TRANSMIT", | |
179 | "QUEUE_BULK_RECEIVE", | |
180 | "AWAIT_COMPLETION", | |
181 | "DEQUEUE_MESSAGE", | |
182 | "GET_CLIENT_ID", | |
183 | "GET_CONFIG", | |
184 | "CLOSE_SERVICE", | |
185 | "USE_SERVICE", | |
186 | "RELEASE_SERVICE", | |
187 | "SET_SERVICE_OPTION", | |
188 | "DUMP_PHYS_MEM", | |
189 | "LIB_VERSION", | |
190 | "CLOSE_DELIVERED" | |
191 | }; | |
192 | ||
5209f93b | 193 | vchiq_static_assert(ARRAY_SIZE(ioctl_names) == |
194 | (VCHIQ_IOC_MAX + 1)); | |
71bad7f0 | 195 | |
196 | static void | |
197 | dump_phys_mem(void *virt_addr, uint32_t num_bytes); | |
198 | ||
199 | /**************************************************************************** | |
200 | * | |
201 | * add_completion | |
202 | * | |
203 | ***************************************************************************/ | |
204 | ||
205 | static VCHIQ_STATUS_T | |
206 | add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason, | |
207 | VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service, | |
208 | void *bulk_userdata) | |
209 | { | |
210 | VCHIQ_COMPLETION_DATA_T *completion; | |
211 | DEBUG_INITIALISE(g_state.local) | |
212 | ||
213 | while (instance->completion_insert == | |
214 | (instance->completion_remove + MAX_COMPLETIONS)) { | |
215 | /* Out of space - wait for the client */ | |
216 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
217 | vchiq_log_trace(vchiq_arm_log_level, | |
218 | "add_completion - completion queue full"); | |
219 | DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT); | |
220 | if (down_interruptible(&instance->remove_event) != 0) { | |
221 | vchiq_log_info(vchiq_arm_log_level, | |
222 | "service_callback interrupted"); | |
223 | return VCHIQ_RETRY; | |
224 | } else if (instance->closing) { | |
225 | vchiq_log_info(vchiq_arm_log_level, | |
226 | "service_callback closing"); | |
227 | return VCHIQ_ERROR; | |
228 | } | |
229 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
230 | } | |
231 | ||
232 | completion = | |
233 | &instance->completions[instance->completion_insert & | |
234 | (MAX_COMPLETIONS - 1)]; | |
235 | ||
236 | completion->header = header; | |
237 | completion->reason = reason; | |
238 | /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */ | |
239 | completion->service_userdata = user_service->service; | |
240 | completion->bulk_userdata = bulk_userdata; | |
241 | ||
242 | if (reason == VCHIQ_SERVICE_CLOSED) { | |
243 | /* Take an extra reference, to be held until | |
244 | this CLOSED notification is delivered. */ | |
245 | lock_service(user_service->service); | |
246 | if (instance->use_close_delivered) | |
247 | user_service->close_pending = 1; | |
248 | } | |
249 | ||
250 | /* A write barrier is needed here to ensure that the entire completion | |
251 | record is written out before the insert point. */ | |
252 | wmb(); | |
253 | ||
254 | if (reason == VCHIQ_MESSAGE_AVAILABLE) | |
255 | user_service->message_available_pos = | |
256 | instance->completion_insert; | |
257 | instance->completion_insert++; | |
258 | ||
259 | up(&instance->insert_event); | |
260 | ||
261 | return VCHIQ_SUCCESS; | |
262 | } | |
263 | ||
264 | /**************************************************************************** | |
265 | * | |
266 | * service_callback | |
267 | * | |
268 | ***************************************************************************/ | |
269 | ||
270 | static VCHIQ_STATUS_T | |
271 | service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header, | |
272 | VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata) | |
273 | { | |
274 | /* How do we ensure the callback goes to the right client? | |
275 | ** The service_user data points to a USER_SERVICE_T record containing | |
276 | ** the original callback and the user state structure, which contains a | |
277 | ** circular buffer for completion records. | |
278 | */ | |
279 | USER_SERVICE_T *user_service; | |
280 | VCHIQ_SERVICE_T *service; | |
281 | VCHIQ_INSTANCE_T instance; | |
282 | DEBUG_INITIALISE(g_state.local) | |
283 | ||
284 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
285 | ||
286 | service = handle_to_service(handle); | |
287 | BUG_ON(!service); | |
288 | user_service = (USER_SERVICE_T *)service->base.userdata; | |
289 | instance = user_service->instance; | |
290 | ||
291 | if (!instance || instance->closing) | |
292 | return VCHIQ_SUCCESS; | |
293 | ||
294 | vchiq_log_trace(vchiq_arm_log_level, | |
295 | "service_callback - service %lx(%d,%p), reason %d, header %lx, " | |
296 | "instance %lx, bulk_userdata %lx", | |
297 | (unsigned long)user_service, | |
298 | service->localport, user_service->userdata, | |
299 | reason, (unsigned long)header, | |
300 | (unsigned long)instance, (unsigned long)bulk_userdata); | |
301 | ||
302 | if (header && user_service->is_vchi) { | |
303 | spin_lock(&msg_queue_spinlock); | |
304 | while (user_service->msg_insert == | |
305 | (user_service->msg_remove + MSG_QUEUE_SIZE)) { | |
306 | spin_unlock(&msg_queue_spinlock); | |
307 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
308 | DEBUG_COUNT(MSG_QUEUE_FULL_COUNT); | |
309 | vchiq_log_trace(vchiq_arm_log_level, | |
310 | "service_callback - msg queue full"); | |
311 | /* If there is no MESSAGE_AVAILABLE in the completion | |
312 | ** queue, add one | |
313 | */ | |
314 | if ((user_service->message_available_pos - | |
315 | instance->completion_remove) < 0) { | |
316 | VCHIQ_STATUS_T status; | |
317 | vchiq_log_info(vchiq_arm_log_level, | |
318 | "Inserting extra MESSAGE_AVAILABLE"); | |
319 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
320 | status = add_completion(instance, reason, | |
321 | NULL, user_service, bulk_userdata); | |
322 | if (status != VCHIQ_SUCCESS) { | |
323 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
324 | return status; | |
325 | } | |
326 | } | |
327 | ||
328 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
329 | if (down_interruptible(&user_service->remove_event) | |
330 | != 0) { | |
331 | vchiq_log_info(vchiq_arm_log_level, | |
332 | "service_callback interrupted"); | |
333 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
334 | return VCHIQ_RETRY; | |
335 | } else if (instance->closing) { | |
336 | vchiq_log_info(vchiq_arm_log_level, | |
337 | "service_callback closing"); | |
338 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
339 | return VCHIQ_ERROR; | |
340 | } | |
341 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
342 | spin_lock(&msg_queue_spinlock); | |
343 | } | |
344 | ||
345 | user_service->msg_queue[user_service->msg_insert & | |
346 | (MSG_QUEUE_SIZE - 1)] = header; | |
347 | user_service->msg_insert++; | |
348 | spin_unlock(&msg_queue_spinlock); | |
349 | ||
350 | up(&user_service->insert_event); | |
351 | ||
352 | /* If there is a thread waiting in DEQUEUE_MESSAGE, or if | |
353 | ** there is a MESSAGE_AVAILABLE in the completion queue then | |
354 | ** bypass the completion queue. | |
355 | */ | |
356 | if (((user_service->message_available_pos - | |
357 | instance->completion_remove) >= 0) || | |
358 | user_service->dequeue_pending) { | |
359 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
360 | user_service->dequeue_pending = 0; | |
361 | return VCHIQ_SUCCESS; | |
362 | } | |
363 | ||
364 | header = NULL; | |
365 | } | |
366 | DEBUG_TRACE(SERVICE_CALLBACK_LINE); | |
367 | ||
368 | return add_completion(instance, reason, header, user_service, | |
369 | bulk_userdata); | |
370 | } | |
371 | ||
372 | /**************************************************************************** | |
373 | * | |
374 | * user_service_free | |
375 | * | |
376 | ***************************************************************************/ | |
377 | static void | |
378 | user_service_free(void *userdata) | |
379 | { | |
380 | kfree(userdata); | |
381 | } | |
382 | ||
383 | /**************************************************************************** | |
384 | * | |
385 | * close_delivered | |
386 | * | |
387 | ***************************************************************************/ | |
388 | static void close_delivered(USER_SERVICE_T *user_service) | |
389 | { | |
390 | vchiq_log_info(vchiq_arm_log_level, | |
391 | "close_delivered(handle=%x)", | |
392 | user_service->service->handle); | |
393 | ||
394 | if (user_service->close_pending) { | |
395 | /* Allow the underlying service to be culled */ | |
396 | unlock_service(user_service->service); | |
397 | ||
398 | /* Wake the user-thread blocked in close_ or remove_service */ | |
399 | up(&user_service->close_event); | |
400 | ||
401 | user_service->close_pending = 0; | |
402 | } | |
403 | } | |
404 | ||
405 | /**************************************************************************** | |
406 | * | |
407 | * vchiq_ioctl | |
408 | * | |
409 | ***************************************************************************/ | |
410 | static long | |
411 | vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
412 | { | |
413 | VCHIQ_INSTANCE_T instance = file->private_data; | |
414 | VCHIQ_STATUS_T status = VCHIQ_SUCCESS; | |
415 | VCHIQ_SERVICE_T *service = NULL; | |
416 | long ret = 0; | |
417 | int i, rc; | |
418 | DEBUG_INITIALISE(g_state.local) | |
419 | ||
420 | vchiq_log_trace(vchiq_arm_log_level, | |
df044ebf GKH |
421 | "vchiq_ioctl - instance %pK, cmd %s, arg %lx", |
422 | instance, | |
71bad7f0 | 423 | ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) && |
424 | (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ? | |
425 | ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg); | |
426 | ||
427 | switch (cmd) { | |
428 | case VCHIQ_IOC_SHUTDOWN: | |
429 | if (!instance->connected) | |
430 | break; | |
431 | ||
432 | /* Remove all services */ | |
433 | i = 0; | |
434 | while ((service = next_service_by_instance(instance->state, | |
435 | instance, &i)) != NULL) { | |
436 | status = vchiq_remove_service(service->handle); | |
437 | unlock_service(service); | |
438 | if (status != VCHIQ_SUCCESS) | |
439 | break; | |
440 | } | |
441 | service = NULL; | |
442 | ||
443 | if (status == VCHIQ_SUCCESS) { | |
444 | /* Wake the completion thread and ask it to exit */ | |
445 | instance->closing = 1; | |
446 | up(&instance->insert_event); | |
447 | } | |
448 | ||
449 | break; | |
450 | ||
451 | case VCHIQ_IOC_CONNECT: | |
452 | if (instance->connected) { | |
453 | ret = -EINVAL; | |
454 | break; | |
455 | } | |
b826d73b | 456 | rc = mutex_lock_killable(&instance->state->mutex); |
71bad7f0 | 457 | if (rc != 0) { |
458 | vchiq_log_error(vchiq_arm_log_level, | |
459 | "vchiq: connect: could not lock mutex for " | |
460 | "state %d: %d", | |
461 | instance->state->id, rc); | |
462 | ret = -EINTR; | |
463 | break; | |
464 | } | |
465 | status = vchiq_connect_internal(instance->state, instance); | |
466 | mutex_unlock(&instance->state->mutex); | |
467 | ||
468 | if (status == VCHIQ_SUCCESS) | |
469 | instance->connected = 1; | |
470 | else | |
471 | vchiq_log_error(vchiq_arm_log_level, | |
472 | "vchiq: could not connect: %d", status); | |
473 | break; | |
474 | ||
475 | case VCHIQ_IOC_CREATE_SERVICE: { | |
476 | VCHIQ_CREATE_SERVICE_T args; | |
477 | USER_SERVICE_T *user_service = NULL; | |
478 | void *userdata; | |
479 | int srvstate; | |
480 | ||
481 | if (copy_from_user | |
482 | (&args, (const void __user *)arg, | |
483 | sizeof(args)) != 0) { | |
484 | ret = -EFAULT; | |
485 | break; | |
486 | } | |
487 | ||
488 | user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL); | |
489 | if (!user_service) { | |
490 | ret = -ENOMEM; | |
491 | break; | |
492 | } | |
493 | ||
494 | if (args.is_open) { | |
495 | if (!instance->connected) { | |
496 | ret = -ENOTCONN; | |
497 | kfree(user_service); | |
498 | break; | |
499 | } | |
500 | srvstate = VCHIQ_SRVSTATE_OPENING; | |
501 | } else { | |
502 | srvstate = | |
503 | instance->connected ? | |
504 | VCHIQ_SRVSTATE_LISTENING : | |
505 | VCHIQ_SRVSTATE_HIDDEN; | |
506 | } | |
507 | ||
508 | userdata = args.params.userdata; | |
509 | args.params.callback = service_callback; | |
510 | args.params.userdata = user_service; | |
511 | service = vchiq_add_service_internal( | |
512 | instance->state, | |
513 | &args.params, srvstate, | |
514 | instance, user_service_free); | |
515 | ||
516 | if (service != NULL) { | |
517 | user_service->service = service; | |
518 | user_service->userdata = userdata; | |
519 | user_service->instance = instance; | |
520 | user_service->is_vchi = (args.is_vchi != 0); | |
521 | user_service->dequeue_pending = 0; | |
522 | user_service->close_pending = 0; | |
523 | user_service->message_available_pos = | |
524 | instance->completion_remove - 1; | |
525 | user_service->msg_insert = 0; | |
526 | user_service->msg_remove = 0; | |
527 | sema_init(&user_service->insert_event, 0); | |
528 | sema_init(&user_service->remove_event, 0); | |
529 | sema_init(&user_service->close_event, 0); | |
530 | ||
531 | if (args.is_open) { | |
532 | status = vchiq_open_service_internal | |
533 | (service, instance->pid); | |
534 | if (status != VCHIQ_SUCCESS) { | |
535 | vchiq_remove_service(service->handle); | |
536 | service = NULL; | |
537 | ret = (status == VCHIQ_RETRY) ? | |
538 | -EINTR : -EIO; | |
539 | break; | |
540 | } | |
541 | } | |
542 | ||
543 | if (copy_to_user((void __user *) | |
544 | &(((VCHIQ_CREATE_SERVICE_T __user *) | |
545 | arg)->handle), | |
546 | (const void *)&service->handle, | |
547 | sizeof(service->handle)) != 0) { | |
548 | ret = -EFAULT; | |
549 | vchiq_remove_service(service->handle); | |
550 | } | |
551 | ||
552 | service = NULL; | |
553 | } else { | |
554 | ret = -EEXIST; | |
555 | kfree(user_service); | |
556 | } | |
557 | } break; | |
558 | ||
559 | case VCHIQ_IOC_CLOSE_SERVICE: { | |
560 | VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg; | |
561 | ||
562 | service = find_service_for_instance(instance, handle); | |
563 | if (service != NULL) { | |
564 | USER_SERVICE_T *user_service = | |
565 | (USER_SERVICE_T *)service->base.userdata; | |
566 | /* close_pending is false on first entry, and when the | |
567 | wait in vchiq_close_service has been interrupted. */ | |
568 | if (!user_service->close_pending) { | |
569 | status = vchiq_close_service(service->handle); | |
570 | if (status != VCHIQ_SUCCESS) | |
571 | break; | |
572 | } | |
573 | ||
574 | /* close_pending is true once the underlying service | |
575 | has been closed until the client library calls the | |
576 | CLOSE_DELIVERED ioctl, signalling close_event. */ | |
577 | if (user_service->close_pending && | |
578 | down_interruptible(&user_service->close_event)) | |
579 | status = VCHIQ_RETRY; | |
580 | } | |
581 | else | |
582 | ret = -EINVAL; | |
583 | } break; | |
584 | ||
585 | case VCHIQ_IOC_REMOVE_SERVICE: { | |
586 | VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg; | |
587 | ||
588 | service = find_service_for_instance(instance, handle); | |
589 | if (service != NULL) { | |
590 | USER_SERVICE_T *user_service = | |
591 | (USER_SERVICE_T *)service->base.userdata; | |
592 | /* close_pending is false on first entry, and when the | |
593 | wait in vchiq_close_service has been interrupted. */ | |
594 | if (!user_service->close_pending) { | |
595 | status = vchiq_remove_service(service->handle); | |
596 | if (status != VCHIQ_SUCCESS) | |
597 | break; | |
598 | } | |
599 | ||
600 | /* close_pending is true once the underlying service | |
601 | has been closed until the client library calls the | |
602 | CLOSE_DELIVERED ioctl, signalling close_event. */ | |
603 | if (user_service->close_pending && | |
604 | down_interruptible(&user_service->close_event)) | |
605 | status = VCHIQ_RETRY; | |
606 | } | |
607 | else | |
608 | ret = -EINVAL; | |
609 | } break; | |
610 | ||
611 | case VCHIQ_IOC_USE_SERVICE: | |
612 | case VCHIQ_IOC_RELEASE_SERVICE: { | |
613 | VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg; | |
614 | ||
615 | service = find_service_for_instance(instance, handle); | |
616 | if (service != NULL) { | |
617 | status = (cmd == VCHIQ_IOC_USE_SERVICE) ? | |
618 | vchiq_use_service_internal(service) : | |
619 | vchiq_release_service_internal(service); | |
620 | if (status != VCHIQ_SUCCESS) { | |
621 | vchiq_log_error(vchiq_susp_log_level, | |
622 | "%s: cmd %s returned error %d for " | |
623 | "service %c%c%c%c:%03d", | |
624 | __func__, | |
625 | (cmd == VCHIQ_IOC_USE_SERVICE) ? | |
626 | "VCHIQ_IOC_USE_SERVICE" : | |
627 | "VCHIQ_IOC_RELEASE_SERVICE", | |
628 | status, | |
629 | VCHIQ_FOURCC_AS_4CHARS( | |
630 | service->base.fourcc), | |
631 | service->client_id); | |
632 | ret = -EINVAL; | |
633 | } | |
634 | } else | |
635 | ret = -EINVAL; | |
636 | } break; | |
637 | ||
638 | case VCHIQ_IOC_QUEUE_MESSAGE: { | |
639 | VCHIQ_QUEUE_MESSAGE_T args; | |
640 | if (copy_from_user | |
641 | (&args, (const void __user *)arg, | |
642 | sizeof(args)) != 0) { | |
643 | ret = -EFAULT; | |
644 | break; | |
645 | } | |
646 | ||
647 | service = find_service_for_instance(instance, args.handle); | |
648 | ||
649 | if ((service != NULL) && (args.count <= MAX_ELEMENTS)) { | |
650 | /* Copy elements into kernel space */ | |
651 | VCHIQ_ELEMENT_T elements[MAX_ELEMENTS]; | |
652 | if (copy_from_user(elements, args.elements, | |
653 | args.count * sizeof(VCHIQ_ELEMENT_T)) == 0) | |
c04feb11 | 654 | status = vchiq_queue_message |
71bad7f0 | 655 | (args.handle, |
656 | elements, args.count); | |
657 | else | |
658 | ret = -EFAULT; | |
659 | } else { | |
660 | ret = -EINVAL; | |
661 | } | |
662 | } break; | |
663 | ||
664 | case VCHIQ_IOC_QUEUE_BULK_TRANSMIT: | |
665 | case VCHIQ_IOC_QUEUE_BULK_RECEIVE: { | |
666 | VCHIQ_QUEUE_BULK_TRANSFER_T args; | |
667 | struct bulk_waiter_node *waiter = NULL; | |
668 | VCHIQ_BULK_DIR_T dir = | |
669 | (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ? | |
670 | VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE; | |
671 | ||
672 | if (copy_from_user | |
673 | (&args, (const void __user *)arg, | |
674 | sizeof(args)) != 0) { | |
675 | ret = -EFAULT; | |
676 | break; | |
677 | } | |
678 | ||
679 | service = find_service_for_instance(instance, args.handle); | |
680 | if (!service) { | |
681 | ret = -EINVAL; | |
682 | break; | |
683 | } | |
684 | ||
685 | if (args.mode == VCHIQ_BULK_MODE_BLOCKING) { | |
686 | waiter = kzalloc(sizeof(struct bulk_waiter_node), | |
687 | GFP_KERNEL); | |
688 | if (!waiter) { | |
689 | ret = -ENOMEM; | |
690 | break; | |
691 | } | |
692 | args.userdata = &waiter->bulk_waiter; | |
693 | } else if (args.mode == VCHIQ_BULK_MODE_WAITING) { | |
694 | struct list_head *pos; | |
695 | mutex_lock(&instance->bulk_waiter_list_mutex); | |
696 | list_for_each(pos, &instance->bulk_waiter_list) { | |
697 | if (list_entry(pos, struct bulk_waiter_node, | |
698 | list)->pid == current->pid) { | |
699 | waiter = list_entry(pos, | |
700 | struct bulk_waiter_node, | |
701 | list); | |
702 | list_del(pos); | |
703 | break; | |
704 | } | |
705 | ||
706 | } | |
707 | mutex_unlock(&instance->bulk_waiter_list_mutex); | |
708 | if (!waiter) { | |
709 | vchiq_log_error(vchiq_arm_log_level, | |
710 | "no bulk_waiter found for pid %d", | |
711 | current->pid); | |
712 | ret = -ESRCH; | |
713 | break; | |
714 | } | |
715 | vchiq_log_info(vchiq_arm_log_level, | |
df044ebf GKH |
716 | "found bulk_waiter %pK for pid %d", waiter, |
717 | current->pid); | |
71bad7f0 | 718 | args.userdata = &waiter->bulk_waiter; |
719 | } | |
720 | status = vchiq_bulk_transfer | |
721 | (args.handle, | |
722 | VCHI_MEM_HANDLE_INVALID, | |
723 | args.data, args.size, | |
724 | args.userdata, args.mode, | |
725 | dir); | |
726 | if (!waiter) | |
727 | break; | |
728 | if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) || | |
729 | !waiter->bulk_waiter.bulk) { | |
730 | if (waiter->bulk_waiter.bulk) { | |
731 | /* Cancel the signal when the transfer | |
732 | ** completes. */ | |
733 | spin_lock(&bulk_waiter_spinlock); | |
734 | waiter->bulk_waiter.bulk->userdata = NULL; | |
735 | spin_unlock(&bulk_waiter_spinlock); | |
736 | } | |
737 | kfree(waiter); | |
738 | } else { | |
739 | const VCHIQ_BULK_MODE_T mode_waiting = | |
740 | VCHIQ_BULK_MODE_WAITING; | |
741 | waiter->pid = current->pid; | |
742 | mutex_lock(&instance->bulk_waiter_list_mutex); | |
743 | list_add(&waiter->list, &instance->bulk_waiter_list); | |
744 | mutex_unlock(&instance->bulk_waiter_list_mutex); | |
745 | vchiq_log_info(vchiq_arm_log_level, | |
df044ebf GKH |
746 | "saved bulk_waiter %pK for pid %d", |
747 | waiter, current->pid); | |
71bad7f0 | 748 | |
749 | if (copy_to_user((void __user *) | |
750 | &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *) | |
751 | arg)->mode), | |
752 | (const void *)&mode_waiting, | |
753 | sizeof(mode_waiting)) != 0) | |
754 | ret = -EFAULT; | |
755 | } | |
756 | } break; | |
757 | ||
758 | case VCHIQ_IOC_AWAIT_COMPLETION: { | |
759 | VCHIQ_AWAIT_COMPLETION_T args; | |
760 | ||
761 | DEBUG_TRACE(AWAIT_COMPLETION_LINE); | |
762 | if (!instance->connected) { | |
763 | ret = -ENOTCONN; | |
764 | break; | |
765 | } | |
766 | ||
767 | if (copy_from_user(&args, (const void __user *)arg, | |
768 | sizeof(args)) != 0) { | |
769 | ret = -EFAULT; | |
770 | break; | |
771 | } | |
772 | ||
773 | mutex_lock(&instance->completion_mutex); | |
774 | ||
775 | DEBUG_TRACE(AWAIT_COMPLETION_LINE); | |
776 | while ((instance->completion_remove == | |
777 | instance->completion_insert) | |
778 | && !instance->closing) { | |
779 | int rc; | |
780 | DEBUG_TRACE(AWAIT_COMPLETION_LINE); | |
781 | mutex_unlock(&instance->completion_mutex); | |
782 | rc = down_interruptible(&instance->insert_event); | |
783 | mutex_lock(&instance->completion_mutex); | |
784 | if (rc != 0) { | |
785 | DEBUG_TRACE(AWAIT_COMPLETION_LINE); | |
786 | vchiq_log_info(vchiq_arm_log_level, | |
787 | "AWAIT_COMPLETION interrupted"); | |
788 | ret = -EINTR; | |
789 | break; | |
790 | } | |
791 | } | |
792 | DEBUG_TRACE(AWAIT_COMPLETION_LINE); | |
793 | ||
794 | /* A read memory barrier is needed to stop prefetch of a stale | |
795 | ** completion record | |
796 | */ | |
797 | rmb(); | |
798 | ||
799 | if (ret == 0) { | |
800 | int msgbufcount = args.msgbufcount; | |
801 | for (ret = 0; ret < args.count; ret++) { | |
802 | VCHIQ_COMPLETION_DATA_T *completion; | |
803 | VCHIQ_SERVICE_T *service; | |
804 | USER_SERVICE_T *user_service; | |
805 | VCHIQ_HEADER_T *header; | |
806 | if (instance->completion_remove == | |
807 | instance->completion_insert) | |
808 | break; | |
809 | completion = &instance->completions[ | |
810 | instance->completion_remove & | |
811 | (MAX_COMPLETIONS - 1)]; | |
812 | ||
813 | service = completion->service_userdata; | |
814 | user_service = service->base.userdata; | |
815 | completion->service_userdata = | |
816 | user_service->userdata; | |
817 | ||
818 | header = completion->header; | |
819 | if (header) { | |
820 | void __user *msgbuf; | |
821 | int msglen; | |
822 | ||
823 | msglen = header->size + | |
824 | sizeof(VCHIQ_HEADER_T); | |
825 | /* This must be a VCHIQ-style service */ | |
826 | if (args.msgbufsize < msglen) { | |
827 | vchiq_log_error( | |
828 | vchiq_arm_log_level, | |
df044ebf GKH |
829 | "header %pK: msgbufsize %x < msglen %x", |
830 | header, args.msgbufsize, | |
71bad7f0 | 831 | msglen); |
832 | WARN(1, "invalid message " | |
833 | "size\n"); | |
834 | if (ret == 0) | |
835 | ret = -EMSGSIZE; | |
836 | break; | |
837 | } | |
838 | if (msgbufcount <= 0) | |
839 | /* Stall here for lack of a | |
840 | ** buffer for the message. */ | |
841 | break; | |
842 | /* Get the pointer from user space */ | |
843 | msgbufcount--; | |
844 | if (copy_from_user(&msgbuf, | |
845 | (const void __user *) | |
846 | &args.msgbufs[msgbufcount], | |
847 | sizeof(msgbuf)) != 0) { | |
848 | if (ret == 0) | |
849 | ret = -EFAULT; | |
850 | break; | |
851 | } | |
852 | ||
853 | /* Copy the message to user space */ | |
854 | if (copy_to_user(msgbuf, header, | |
855 | msglen) != 0) { | |
856 | if (ret == 0) | |
857 | ret = -EFAULT; | |
858 | break; | |
859 | } | |
860 | ||
861 | /* Now it has been copied, the message | |
862 | ** can be released. */ | |
863 | vchiq_release_message(service->handle, | |
864 | header); | |
865 | ||
866 | /* The completion must point to the | |
867 | ** msgbuf. */ | |
868 | completion->header = msgbuf; | |
869 | } | |
870 | ||
871 | if ((completion->reason == | |
872 | VCHIQ_SERVICE_CLOSED) && | |
873 | !instance->use_close_delivered) | |
874 | unlock_service(service); | |
875 | ||
876 | if (copy_to_user((void __user *)( | |
877 | (size_t)args.buf + | |
878 | ret * sizeof(VCHIQ_COMPLETION_DATA_T)), | |
879 | completion, | |
880 | sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) { | |
881 | if (ret == 0) | |
882 | ret = -EFAULT; | |
883 | break; | |
884 | } | |
885 | ||
886 | instance->completion_remove++; | |
887 | } | |
888 | ||
889 | if (msgbufcount != args.msgbufcount) { | |
890 | if (copy_to_user((void __user *) | |
891 | &((VCHIQ_AWAIT_COMPLETION_T *)arg)-> | |
892 | msgbufcount, | |
893 | &msgbufcount, | |
894 | sizeof(msgbufcount)) != 0) { | |
895 | ret = -EFAULT; | |
896 | } | |
897 | } | |
898 | } | |
899 | ||
900 | if (ret != 0) | |
901 | up(&instance->remove_event); | |
902 | mutex_unlock(&instance->completion_mutex); | |
903 | DEBUG_TRACE(AWAIT_COMPLETION_LINE); | |
904 | } break; | |
905 | ||
906 | case VCHIQ_IOC_DEQUEUE_MESSAGE: { | |
907 | VCHIQ_DEQUEUE_MESSAGE_T args; | |
908 | USER_SERVICE_T *user_service; | |
909 | VCHIQ_HEADER_T *header; | |
910 | ||
911 | DEBUG_TRACE(DEQUEUE_MESSAGE_LINE); | |
912 | if (copy_from_user | |
913 | (&args, (const void __user *)arg, | |
914 | sizeof(args)) != 0) { | |
915 | ret = -EFAULT; | |
916 | break; | |
917 | } | |
918 | service = find_service_for_instance(instance, args.handle); | |
919 | if (!service) { | |
920 | ret = -EINVAL; | |
921 | break; | |
922 | } | |
923 | user_service = (USER_SERVICE_T *)service->base.userdata; | |
924 | if (user_service->is_vchi == 0) { | |
925 | ret = -EINVAL; | |
926 | break; | |
927 | } | |
928 | ||
929 | spin_lock(&msg_queue_spinlock); | |
930 | if (user_service->msg_remove == user_service->msg_insert) { | |
931 | if (!args.blocking) { | |
932 | spin_unlock(&msg_queue_spinlock); | |
933 | DEBUG_TRACE(DEQUEUE_MESSAGE_LINE); | |
934 | ret = -EWOULDBLOCK; | |
935 | break; | |
936 | } | |
937 | user_service->dequeue_pending = 1; | |
938 | do { | |
939 | spin_unlock(&msg_queue_spinlock); | |
940 | DEBUG_TRACE(DEQUEUE_MESSAGE_LINE); | |
941 | if (down_interruptible( | |
942 | &user_service->insert_event) != 0) { | |
943 | vchiq_log_info(vchiq_arm_log_level, | |
944 | "DEQUEUE_MESSAGE interrupted"); | |
945 | ret = -EINTR; | |
946 | break; | |
947 | } | |
948 | spin_lock(&msg_queue_spinlock); | |
949 | } while (user_service->msg_remove == | |
950 | user_service->msg_insert); | |
951 | ||
952 | if (ret) | |
953 | break; | |
954 | } | |
955 | ||
956 | BUG_ON((int)(user_service->msg_insert - | |
957 | user_service->msg_remove) < 0); | |
958 | ||
959 | header = user_service->msg_queue[user_service->msg_remove & | |
960 | (MSG_QUEUE_SIZE - 1)]; | |
961 | user_service->msg_remove++; | |
962 | spin_unlock(&msg_queue_spinlock); | |
963 | ||
964 | up(&user_service->remove_event); | |
965 | if (header == NULL) | |
966 | ret = -ENOTCONN; | |
967 | else if (header->size <= args.bufsize) { | |
968 | /* Copy to user space if msgbuf is not NULL */ | |
969 | if ((args.buf == NULL) || | |
970 | (copy_to_user((void __user *)args.buf, | |
971 | header->data, | |
972 | header->size) == 0)) { | |
973 | ret = header->size; | |
974 | vchiq_release_message( | |
975 | service->handle, | |
976 | header); | |
977 | } else | |
978 | ret = -EFAULT; | |
979 | } else { | |
980 | vchiq_log_error(vchiq_arm_log_level, | |
df044ebf GKH |
981 | "header %pK: bufsize %x < size %x", |
982 | header, args.bufsize, header->size); | |
71bad7f0 | 983 | WARN(1, "invalid size\n"); |
984 | ret = -EMSGSIZE; | |
985 | } | |
986 | DEBUG_TRACE(DEQUEUE_MESSAGE_LINE); | |
987 | } break; | |
988 | ||
989 | case VCHIQ_IOC_GET_CLIENT_ID: { | |
990 | VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg; | |
991 | ||
992 | ret = vchiq_get_client_id(handle); | |
993 | } break; | |
994 | ||
995 | case VCHIQ_IOC_GET_CONFIG: { | |
996 | VCHIQ_GET_CONFIG_T args; | |
997 | VCHIQ_CONFIG_T config; | |
998 | ||
999 | if (copy_from_user(&args, (const void __user *)arg, | |
1000 | sizeof(args)) != 0) { | |
1001 | ret = -EFAULT; | |
1002 | break; | |
1003 | } | |
1004 | if (args.config_size > sizeof(config)) { | |
1005 | ret = -EINVAL; | |
1006 | break; | |
1007 | } | |
1008 | status = vchiq_get_config(instance, args.config_size, &config); | |
1009 | if (status == VCHIQ_SUCCESS) { | |
1010 | if (copy_to_user((void __user *)args.pconfig, | |
1011 | &config, args.config_size) != 0) { | |
1012 | ret = -EFAULT; | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | } break; | |
1017 | ||
1018 | case VCHIQ_IOC_SET_SERVICE_OPTION: { | |
1019 | VCHIQ_SET_SERVICE_OPTION_T args; | |
1020 | ||
1021 | if (copy_from_user( | |
1022 | &args, (const void __user *)arg, | |
1023 | sizeof(args)) != 0) { | |
1024 | ret = -EFAULT; | |
1025 | break; | |
1026 | } | |
1027 | ||
1028 | service = find_service_for_instance(instance, args.handle); | |
1029 | if (!service) { | |
1030 | ret = -EINVAL; | |
1031 | break; | |
1032 | } | |
1033 | ||
1034 | status = vchiq_set_service_option( | |
1035 | args.handle, args.option, args.value); | |
1036 | } break; | |
1037 | ||
1038 | case VCHIQ_IOC_DUMP_PHYS_MEM: { | |
1039 | VCHIQ_DUMP_MEM_T args; | |
1040 | ||
1041 | if (copy_from_user | |
1042 | (&args, (const void __user *)arg, | |
1043 | sizeof(args)) != 0) { | |
1044 | ret = -EFAULT; | |
1045 | break; | |
1046 | } | |
1047 | dump_phys_mem(args.virt_addr, args.num_bytes); | |
1048 | } break; | |
1049 | ||
1050 | case VCHIQ_IOC_LIB_VERSION: { | |
1051 | unsigned int lib_version = (unsigned int)arg; | |
1052 | ||
1053 | if (lib_version < VCHIQ_VERSION_MIN) | |
1054 | ret = -EINVAL; | |
1055 | else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED) | |
1056 | instance->use_close_delivered = 1; | |
1057 | } break; | |
1058 | ||
1059 | case VCHIQ_IOC_CLOSE_DELIVERED: { | |
1060 | VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg; | |
1061 | ||
1062 | service = find_closed_service_for_instance(instance, handle); | |
1063 | if (service != NULL) { | |
1064 | USER_SERVICE_T *user_service = | |
1065 | (USER_SERVICE_T *)service->base.userdata; | |
1066 | close_delivered(user_service); | |
1067 | } | |
1068 | else | |
1069 | ret = -EINVAL; | |
1070 | } break; | |
1071 | ||
1072 | default: | |
1073 | ret = -ENOTTY; | |
1074 | break; | |
1075 | } | |
1076 | ||
1077 | if (service) | |
1078 | unlock_service(service); | |
1079 | ||
1080 | if (ret == 0) { | |
1081 | if (status == VCHIQ_ERROR) | |
1082 | ret = -EIO; | |
1083 | else if (status == VCHIQ_RETRY) | |
1084 | ret = -EINTR; | |
1085 | } | |
1086 | ||
1087 | if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) && | |
1088 | (ret != -EWOULDBLOCK)) | |
1089 | vchiq_log_info(vchiq_arm_log_level, | |
1090 | " ioctl instance %lx, cmd %s -> status %d, %ld", | |
1091 | (unsigned long)instance, | |
1092 | (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ? | |
1093 | ioctl_names[_IOC_NR(cmd)] : | |
1094 | "<invalid>", | |
1095 | status, ret); | |
1096 | else | |
1097 | vchiq_log_trace(vchiq_arm_log_level, | |
1098 | " ioctl instance %lx, cmd %s -> status %d, %ld", | |
1099 | (unsigned long)instance, | |
1100 | (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ? | |
1101 | ioctl_names[_IOC_NR(cmd)] : | |
1102 | "<invalid>", | |
1103 | status, ret); | |
1104 | ||
1105 | return ret; | |
1106 | } | |
1107 | ||
1108 | /**************************************************************************** | |
1109 | * | |
1110 | * vchiq_open | |
1111 | * | |
1112 | ***************************************************************************/ | |
1113 | ||
1114 | static int | |
1115 | vchiq_open(struct inode *inode, struct file *file) | |
1116 | { | |
1117 | int dev = iminor(inode) & 0x0f; | |
1118 | vchiq_log_info(vchiq_arm_log_level, "vchiq_open"); | |
1119 | switch (dev) { | |
1120 | case VCHIQ_MINOR: { | |
1121 | int ret; | |
1122 | VCHIQ_STATE_T *state = vchiq_get_state(); | |
1123 | VCHIQ_INSTANCE_T instance; | |
1124 | ||
1125 | if (!state) { | |
1126 | vchiq_log_error(vchiq_arm_log_level, | |
1127 | "vchiq has no connection to VideoCore"); | |
1128 | return -ENOTCONN; | |
1129 | } | |
1130 | ||
1131 | instance = kzalloc(sizeof(*instance), GFP_KERNEL); | |
1132 | if (!instance) | |
1133 | return -ENOMEM; | |
1134 | ||
1135 | instance->state = state; | |
1136 | instance->pid = current->tgid; | |
1137 | ||
1138 | ret = vchiq_debugfs_add_instance(instance); | |
1139 | if (ret != 0) { | |
1140 | kfree(instance); | |
1141 | return ret; | |
1142 | } | |
1143 | ||
1144 | sema_init(&instance->insert_event, 0); | |
1145 | sema_init(&instance->remove_event, 0); | |
1146 | mutex_init(&instance->completion_mutex); | |
1147 | mutex_init(&instance->bulk_waiter_list_mutex); | |
1148 | INIT_LIST_HEAD(&instance->bulk_waiter_list); | |
1149 | ||
1150 | file->private_data = instance; | |
1151 | } break; | |
1152 | ||
1153 | default: | |
1154 | vchiq_log_error(vchiq_arm_log_level, | |
1155 | "Unknown minor device: %d", dev); | |
1156 | return -ENXIO; | |
1157 | } | |
1158 | ||
1159 | return 0; | |
1160 | } | |
1161 | ||
1162 | /**************************************************************************** | |
1163 | * | |
1164 | * vchiq_release | |
1165 | * | |
1166 | ***************************************************************************/ | |
1167 | ||
1168 | static int | |
1169 | vchiq_release(struct inode *inode, struct file *file) | |
1170 | { | |
1171 | int dev = iminor(inode) & 0x0f; | |
1172 | int ret = 0; | |
1173 | switch (dev) { | |
1174 | case VCHIQ_MINOR: { | |
1175 | VCHIQ_INSTANCE_T instance = file->private_data; | |
1176 | VCHIQ_STATE_T *state = vchiq_get_state(); | |
1177 | VCHIQ_SERVICE_T *service; | |
1178 | int i; | |
1179 | ||
1180 | vchiq_log_info(vchiq_arm_log_level, | |
1181 | "vchiq_release: instance=%lx", | |
1182 | (unsigned long)instance); | |
1183 | ||
1184 | if (!state) { | |
1185 | ret = -EPERM; | |
1186 | goto out; | |
1187 | } | |
1188 | ||
1189 | /* Ensure videocore is awake to allow termination. */ | |
1190 | vchiq_use_internal(instance->state, NULL, | |
1191 | USE_TYPE_VCHIQ); | |
1192 | ||
1193 | mutex_lock(&instance->completion_mutex); | |
1194 | ||
1195 | /* Wake the completion thread and ask it to exit */ | |
1196 | instance->closing = 1; | |
1197 | up(&instance->insert_event); | |
1198 | ||
1199 | mutex_unlock(&instance->completion_mutex); | |
1200 | ||
1201 | /* Wake the slot handler if the completion queue is full. */ | |
1202 | up(&instance->remove_event); | |
1203 | ||
1204 | /* Mark all services for termination... */ | |
1205 | i = 0; | |
1206 | while ((service = next_service_by_instance(state, instance, | |
1207 | &i)) != NULL) { | |
1208 | USER_SERVICE_T *user_service = service->base.userdata; | |
1209 | ||
1210 | /* Wake the slot handler if the msg queue is full. */ | |
1211 | up(&user_service->remove_event); | |
1212 | ||
1213 | vchiq_terminate_service_internal(service); | |
1214 | unlock_service(service); | |
1215 | } | |
1216 | ||
1217 | /* ...and wait for them to die */ | |
1218 | i = 0; | |
1219 | while ((service = next_service_by_instance(state, instance, &i)) | |
1220 | != NULL) { | |
1221 | USER_SERVICE_T *user_service = service->base.userdata; | |
1222 | ||
1223 | down(&service->remove_event); | |
1224 | ||
1225 | BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE); | |
1226 | ||
1227 | spin_lock(&msg_queue_spinlock); | |
1228 | ||
1229 | while (user_service->msg_remove != | |
1230 | user_service->msg_insert) { | |
1231 | VCHIQ_HEADER_T *header = user_service-> | |
1232 | msg_queue[user_service->msg_remove & | |
1233 | (MSG_QUEUE_SIZE - 1)]; | |
1234 | user_service->msg_remove++; | |
1235 | spin_unlock(&msg_queue_spinlock); | |
1236 | ||
1237 | if (header) | |
1238 | vchiq_release_message( | |
1239 | service->handle, | |
1240 | header); | |
1241 | spin_lock(&msg_queue_spinlock); | |
1242 | } | |
1243 | ||
1244 | spin_unlock(&msg_queue_spinlock); | |
1245 | ||
1246 | unlock_service(service); | |
1247 | } | |
1248 | ||
1249 | /* Release any closed services */ | |
1250 | while (instance->completion_remove != | |
1251 | instance->completion_insert) { | |
1252 | VCHIQ_COMPLETION_DATA_T *completion; | |
1253 | VCHIQ_SERVICE_T *service; | |
1254 | completion = &instance->completions[ | |
1255 | instance->completion_remove & | |
1256 | (MAX_COMPLETIONS - 1)]; | |
1257 | service = completion->service_userdata; | |
1258 | if (completion->reason == VCHIQ_SERVICE_CLOSED) | |
1259 | { | |
1260 | USER_SERVICE_T *user_service = | |
1261 | service->base.userdata; | |
1262 | ||
1263 | /* Wake any blocked user-thread */ | |
1264 | if (instance->use_close_delivered) | |
1265 | up(&user_service->close_event); | |
1266 | unlock_service(service); | |
1267 | } | |
1268 | instance->completion_remove++; | |
1269 | } | |
1270 | ||
1271 | /* Release the PEER service count. */ | |
1272 | vchiq_release_internal(instance->state, NULL); | |
1273 | ||
1274 | { | |
1275 | struct list_head *pos, *next; | |
1276 | list_for_each_safe(pos, next, | |
1277 | &instance->bulk_waiter_list) { | |
1278 | struct bulk_waiter_node *waiter; | |
1279 | waiter = list_entry(pos, | |
1280 | struct bulk_waiter_node, | |
1281 | list); | |
1282 | list_del(pos); | |
1283 | vchiq_log_info(vchiq_arm_log_level, | |
df044ebf GKH |
1284 | "bulk_waiter - cleaned up %pK for pid %d", |
1285 | waiter, waiter->pid); | |
71bad7f0 | 1286 | kfree(waiter); |
1287 | } | |
1288 | } | |
1289 | ||
1290 | vchiq_debugfs_remove_instance(instance); | |
1291 | ||
1292 | kfree(instance); | |
1293 | file->private_data = NULL; | |
1294 | } break; | |
1295 | ||
1296 | default: | |
1297 | vchiq_log_error(vchiq_arm_log_level, | |
1298 | "Unknown minor device: %d", dev); | |
1299 | ret = -ENXIO; | |
1300 | } | |
1301 | ||
1302 | out: | |
1303 | return ret; | |
1304 | } | |
1305 | ||
1306 | /**************************************************************************** | |
1307 | * | |
1308 | * vchiq_dump | |
1309 | * | |
1310 | ***************************************************************************/ | |
1311 | ||
1312 | void | |
1313 | vchiq_dump(void *dump_context, const char *str, int len) | |
1314 | { | |
1315 | DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context; | |
1316 | ||
1317 | if (context->actual < context->space) { | |
1318 | int copy_bytes; | |
1319 | if (context->offset > 0) { | |
1320 | int skip_bytes = min(len, (int)context->offset); | |
1321 | str += skip_bytes; | |
1322 | len -= skip_bytes; | |
1323 | context->offset -= skip_bytes; | |
1324 | if (context->offset > 0) | |
1325 | return; | |
1326 | } | |
1327 | copy_bytes = min(len, (int)(context->space - context->actual)); | |
1328 | if (copy_bytes == 0) | |
1329 | return; | |
1330 | if (copy_to_user(context->buf + context->actual, str, | |
1331 | copy_bytes)) | |
1332 | context->actual = -EFAULT; | |
1333 | context->actual += copy_bytes; | |
1334 | len -= copy_bytes; | |
1335 | ||
1336 | /* If tne terminating NUL is included in the length, then it | |
1337 | ** marks the end of a line and should be replaced with a | |
1338 | ** carriage return. */ | |
1339 | if ((len == 0) && (str[copy_bytes - 1] == '\0')) { | |
1340 | char cr = '\n'; | |
1341 | if (copy_to_user(context->buf + context->actual - 1, | |
1342 | &cr, 1)) | |
1343 | context->actual = -EFAULT; | |
1344 | } | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | /**************************************************************************** | |
1349 | * | |
1350 | * vchiq_dump_platform_instance_state | |
1351 | * | |
1352 | ***************************************************************************/ | |
1353 | ||
1354 | void | |
1355 | vchiq_dump_platform_instances(void *dump_context) | |
1356 | { | |
1357 | VCHIQ_STATE_T *state = vchiq_get_state(); | |
1358 | char buf[80]; | |
1359 | int len; | |
1360 | int i; | |
1361 | ||
1362 | /* There is no list of instances, so instead scan all services, | |
1363 | marking those that have been dumped. */ | |
1364 | ||
1365 | for (i = 0; i < state->unused_service; i++) { | |
1366 | VCHIQ_SERVICE_T *service = state->services[i]; | |
1367 | VCHIQ_INSTANCE_T instance; | |
1368 | ||
1369 | if (service && (service->base.callback == service_callback)) { | |
1370 | instance = service->instance; | |
1371 | if (instance) | |
1372 | instance->mark = 0; | |
1373 | } | |
1374 | } | |
1375 | ||
1376 | for (i = 0; i < state->unused_service; i++) { | |
1377 | VCHIQ_SERVICE_T *service = state->services[i]; | |
1378 | VCHIQ_INSTANCE_T instance; | |
1379 | ||
1380 | if (service && (service->base.callback == service_callback)) { | |
1381 | instance = service->instance; | |
1382 | if (instance && !instance->mark) { | |
1383 | len = snprintf(buf, sizeof(buf), | |
df044ebf GKH |
1384 | "Instance %pK: pid %d,%s completions %d/%d", |
1385 | instance, instance->pid, | |
71bad7f0 | 1386 | instance->connected ? " connected, " : |
1387 | "", | |
1388 | instance->completion_insert - | |
1389 | instance->completion_remove, | |
1390 | MAX_COMPLETIONS); | |
1391 | ||
1392 | vchiq_dump(dump_context, buf, len + 1); | |
1393 | ||
1394 | instance->mark = 1; | |
1395 | } | |
1396 | } | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | /**************************************************************************** | |
1401 | * | |
1402 | * vchiq_dump_platform_service_state | |
1403 | * | |
1404 | ***************************************************************************/ | |
1405 | ||
1406 | void | |
1407 | vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service) | |
1408 | { | |
1409 | USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata; | |
1410 | char buf[80]; | |
1411 | int len; | |
1412 | ||
df044ebf | 1413 | len = snprintf(buf, sizeof(buf), " instance %pK", service->instance); |
71bad7f0 | 1414 | |
1415 | if ((service->base.callback == service_callback) && | |
1416 | user_service->is_vchi) { | |
1417 | len += snprintf(buf + len, sizeof(buf) - len, | |
1418 | ", %d/%d messages", | |
1419 | user_service->msg_insert - user_service->msg_remove, | |
1420 | MSG_QUEUE_SIZE); | |
1421 | ||
1422 | if (user_service->dequeue_pending) | |
1423 | len += snprintf(buf + len, sizeof(buf) - len, | |
1424 | " (dequeue pending)"); | |
1425 | } | |
1426 | ||
1427 | vchiq_dump(dump_context, buf, len + 1); | |
1428 | } | |
1429 | ||
1430 | /**************************************************************************** | |
1431 | * | |
1432 | * dump_user_mem | |
1433 | * | |
1434 | ***************************************************************************/ | |
1435 | ||
1436 | static void | |
1437 | dump_phys_mem(void *virt_addr, uint32_t num_bytes) | |
1438 | { | |
1439 | int rc; | |
1440 | uint8_t *end_virt_addr = virt_addr + num_bytes; | |
1441 | int num_pages; | |
1442 | int offset; | |
1443 | int end_offset; | |
1444 | int page_idx; | |
1445 | int prev_idx; | |
1446 | struct page *page; | |
1447 | struct page **pages; | |
1448 | uint8_t *kmapped_virt_ptr; | |
1449 | ||
1450 | /* Align virtAddr and endVirtAddr to 16 byte boundaries. */ | |
1451 | ||
1452 | virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL); | |
1453 | end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) & | |
1454 | ~0x0fuL); | |
1455 | ||
1456 | offset = (int)(long)virt_addr & (PAGE_SIZE - 1); | |
1457 | end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1); | |
1458 | ||
1459 | num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE; | |
1460 | ||
1461 | pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL); | |
1462 | if (pages == NULL) { | |
1463 | vchiq_log_error(vchiq_arm_log_level, | |
1464 | "Unable to allocation memory for %d pages\n", | |
1465 | num_pages); | |
1466 | return; | |
1467 | } | |
1468 | ||
1469 | down_read(¤t->mm->mmap_sem); | |
166beccd | 1470 | rc = get_user_pages( |
71bad7f0 | 1471 | (unsigned long)virt_addr, /* start */ |
1472 | num_pages, /* len */ | |
768ae309 | 1473 | 0, /* gup_flags */ |
71bad7f0 | 1474 | pages, /* pages (array of page pointers) */ |
1475 | NULL); /* vmas */ | |
1476 | up_read(¤t->mm->mmap_sem); | |
1477 | ||
1478 | prev_idx = -1; | |
1479 | page = NULL; | |
1480 | ||
0feb1ed5 SW |
1481 | if (rc < 0) { |
1482 | vchiq_log_error(vchiq_arm_log_level, | |
1483 | "Failed to get user pages: %d\n", rc); | |
1484 | goto out; | |
1485 | } | |
1486 | ||
71bad7f0 | 1487 | while (offset < end_offset) { |
1488 | ||
1489 | int page_offset = offset % PAGE_SIZE; | |
1490 | page_idx = offset / PAGE_SIZE; | |
1491 | ||
1492 | if (page_idx != prev_idx) { | |
1493 | ||
1494 | if (page != NULL) | |
1495 | kunmap(page); | |
1496 | page = pages[page_idx]; | |
1497 | kmapped_virt_ptr = kmap(page); | |
1498 | ||
1499 | prev_idx = page_idx; | |
1500 | } | |
1501 | ||
1502 | if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE) | |
1503 | vchiq_log_dump_mem("ph", | |
1504 | (uint32_t)(unsigned long)&kmapped_virt_ptr[ | |
1505 | page_offset], | |
1506 | &kmapped_virt_ptr[page_offset], 16); | |
1507 | ||
1508 | offset += 16; | |
1509 | } | |
0feb1ed5 SW |
1510 | |
1511 | out: | |
71bad7f0 | 1512 | if (page != NULL) |
1513 | kunmap(page); | |
1514 | ||
1515 | for (page_idx = 0; page_idx < num_pages; page_idx++) | |
232664b3 | 1516 | put_page(pages[page_idx]); |
71bad7f0 | 1517 | |
1518 | kfree(pages); | |
1519 | } | |
1520 | ||
1521 | /**************************************************************************** | |
1522 | * | |
1523 | * vchiq_read | |
1524 | * | |
1525 | ***************************************************************************/ | |
1526 | ||
1527 | static ssize_t | |
1528 | vchiq_read(struct file *file, char __user *buf, | |
1529 | size_t count, loff_t *ppos) | |
1530 | { | |
1531 | DUMP_CONTEXT_T context; | |
1532 | context.buf = buf; | |
1533 | context.actual = 0; | |
1534 | context.space = count; | |
1535 | context.offset = *ppos; | |
1536 | ||
1537 | vchiq_dump_state(&context, &g_state); | |
1538 | ||
1539 | *ppos += context.actual; | |
1540 | ||
1541 | return context.actual; | |
1542 | } | |
1543 | ||
1544 | VCHIQ_STATE_T * | |
1545 | vchiq_get_state(void) | |
1546 | { | |
1547 | ||
1548 | if (g_state.remote == NULL) | |
1549 | printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__); | |
1550 | else if (g_state.remote->initialised != 1) | |
1551 | printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n", | |
1552 | __func__, g_state.remote->initialised); | |
1553 | ||
1554 | return ((g_state.remote != NULL) && | |
1555 | (g_state.remote->initialised == 1)) ? &g_state : NULL; | |
1556 | } | |
1557 | ||
1558 | static const struct file_operations | |
1559 | vchiq_fops = { | |
1560 | .owner = THIS_MODULE, | |
1561 | .unlocked_ioctl = vchiq_ioctl, | |
1562 | .open = vchiq_open, | |
1563 | .release = vchiq_release, | |
1564 | .read = vchiq_read | |
1565 | }; | |
1566 | ||
1567 | /* | |
1568 | * Autosuspend related functionality | |
1569 | */ | |
1570 | ||
1571 | int | |
1572 | vchiq_videocore_wanted(VCHIQ_STATE_T *state) | |
1573 | { | |
1574 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
1575 | if (!arm_state) | |
1576 | /* autosuspend not supported - always return wanted */ | |
1577 | return 1; | |
1578 | else if (arm_state->blocked_count) | |
1579 | return 1; | |
1580 | else if (!arm_state->videocore_use_count) | |
1581 | /* usage count zero - check for override unless we're forcing */ | |
1582 | if (arm_state->resume_blocked) | |
1583 | return 0; | |
1584 | else | |
1585 | return vchiq_platform_videocore_wanted(state); | |
1586 | else | |
1587 | /* non-zero usage count - videocore still required */ | |
1588 | return 1; | |
1589 | } | |
1590 | ||
1591 | static VCHIQ_STATUS_T | |
1592 | vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason, | |
1593 | VCHIQ_HEADER_T *header, | |
1594 | VCHIQ_SERVICE_HANDLE_T service_user, | |
1595 | void *bulk_user) | |
1596 | { | |
1597 | vchiq_log_error(vchiq_susp_log_level, | |
1598 | "%s callback reason %d", __func__, reason); | |
1599 | return 0; | |
1600 | } | |
1601 | ||
1602 | static int | |
1603 | vchiq_keepalive_thread_func(void *v) | |
1604 | { | |
1605 | VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v; | |
1606 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
1607 | ||
1608 | VCHIQ_STATUS_T status; | |
1609 | VCHIQ_INSTANCE_T instance; | |
1610 | VCHIQ_SERVICE_HANDLE_T ka_handle; | |
1611 | ||
1612 | VCHIQ_SERVICE_PARAMS_T params = { | |
1613 | .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'), | |
1614 | .callback = vchiq_keepalive_vchiq_callback, | |
1615 | .version = KEEPALIVE_VER, | |
1616 | .version_min = KEEPALIVE_VER_MIN | |
1617 | }; | |
1618 | ||
1619 | status = vchiq_initialise(&instance); | |
1620 | if (status != VCHIQ_SUCCESS) { | |
1621 | vchiq_log_error(vchiq_susp_log_level, | |
1622 | "%s vchiq_initialise failed %d", __func__, status); | |
1623 | goto exit; | |
1624 | } | |
1625 | ||
1626 | status = vchiq_connect(instance); | |
1627 | if (status != VCHIQ_SUCCESS) { | |
1628 | vchiq_log_error(vchiq_susp_log_level, | |
1629 | "%s vchiq_connect failed %d", __func__, status); | |
1630 | goto shutdown; | |
1631 | } | |
1632 | ||
1633 | status = vchiq_add_service(instance, ¶ms, &ka_handle); | |
1634 | if (status != VCHIQ_SUCCESS) { | |
1635 | vchiq_log_error(vchiq_susp_log_level, | |
1636 | "%s vchiq_open_service failed %d", __func__, status); | |
1637 | goto shutdown; | |
1638 | } | |
1639 | ||
1640 | while (1) { | |
1641 | long rc = 0, uc = 0; | |
1642 | if (wait_for_completion_interruptible(&arm_state->ka_evt) | |
1643 | != 0) { | |
1644 | vchiq_log_error(vchiq_susp_log_level, | |
1645 | "%s interrupted", __func__); | |
1646 | flush_signals(current); | |
1647 | continue; | |
1648 | } | |
1649 | ||
1650 | /* read and clear counters. Do release_count then use_count to | |
1651 | * prevent getting more releases than uses */ | |
1652 | rc = atomic_xchg(&arm_state->ka_release_count, 0); | |
1653 | uc = atomic_xchg(&arm_state->ka_use_count, 0); | |
1654 | ||
1655 | /* Call use/release service the requisite number of times. | |
1656 | * Process use before release so use counts don't go negative */ | |
1657 | while (uc--) { | |
1658 | atomic_inc(&arm_state->ka_use_ack_count); | |
1659 | status = vchiq_use_service(ka_handle); | |
1660 | if (status != VCHIQ_SUCCESS) { | |
1661 | vchiq_log_error(vchiq_susp_log_level, | |
1662 | "%s vchiq_use_service error %d", | |
1663 | __func__, status); | |
1664 | } | |
1665 | } | |
1666 | while (rc--) { | |
1667 | status = vchiq_release_service(ka_handle); | |
1668 | if (status != VCHIQ_SUCCESS) { | |
1669 | vchiq_log_error(vchiq_susp_log_level, | |
1670 | "%s vchiq_release_service error %d", | |
1671 | __func__, status); | |
1672 | } | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | shutdown: | |
1677 | vchiq_shutdown(instance); | |
1678 | exit: | |
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | ||
1683 | ||
1684 | VCHIQ_STATUS_T | |
1685 | vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state) | |
1686 | { | |
71bad7f0 | 1687 | if (arm_state) { |
1688 | rwlock_init(&arm_state->susp_res_lock); | |
1689 | ||
1690 | init_completion(&arm_state->ka_evt); | |
1691 | atomic_set(&arm_state->ka_use_count, 0); | |
1692 | atomic_set(&arm_state->ka_use_ack_count, 0); | |
1693 | atomic_set(&arm_state->ka_release_count, 0); | |
1694 | ||
1695 | init_completion(&arm_state->vc_suspend_complete); | |
1696 | ||
1697 | init_completion(&arm_state->vc_resume_complete); | |
1698 | /* Initialise to 'done' state. We only want to block on resume | |
1699 | * completion while videocore is suspended. */ | |
1700 | set_resume_state(arm_state, VC_RESUME_RESUMED); | |
1701 | ||
1702 | init_completion(&arm_state->resume_blocker); | |
1703 | /* Initialise to 'done' state. We only want to block on this | |
1704 | * completion while resume is blocked */ | |
1705 | complete_all(&arm_state->resume_blocker); | |
1706 | ||
1707 | init_completion(&arm_state->blocked_blocker); | |
1708 | /* Initialise to 'done' state. We only want to block on this | |
1709 | * completion while things are waiting on the resume blocker */ | |
1710 | complete_all(&arm_state->blocked_blocker); | |
1711 | ||
1712 | arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS; | |
1713 | arm_state->suspend_timer_running = 0; | |
53a6e71f | 1714 | setup_timer(&arm_state->suspend_timer, suspend_timer_callback, |
1715 | (unsigned long)(state)); | |
71bad7f0 | 1716 | |
1717 | arm_state->first_connect = 0; | |
1718 | ||
1719 | } | |
30d84860 | 1720 | return VCHIQ_SUCCESS; |
71bad7f0 | 1721 | } |
1722 | ||
1723 | /* | |
1724 | ** Functions to modify the state variables; | |
1725 | ** set_suspend_state | |
1726 | ** set_resume_state | |
1727 | ** | |
1728 | ** There are more state variables than we might like, so ensure they remain in | |
1729 | ** step. Suspend and resume state are maintained separately, since most of | |
1730 | ** these state machines can operate independently. However, there are a few | |
1731 | ** states where state transitions in one state machine cause a reset to the | |
1732 | ** other state machine. In addition, there are some completion events which | |
1733 | ** need to occur on state machine reset and end-state(s), so these are also | |
1734 | ** dealt with in these functions. | |
1735 | ** | |
1736 | ** In all states we set the state variable according to the input, but in some | |
1737 | ** cases we perform additional steps outlined below; | |
1738 | ** | |
1739 | ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time. | |
1740 | ** The suspend completion is completed after any suspend | |
1741 | ** attempt. When we reset the state machine we also reset | |
1742 | ** the completion. This reset occurs when videocore is | |
1743 | ** resumed, and also if we initiate suspend after a suspend | |
1744 | ** failure. | |
1745 | ** | |
1746 | ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for | |
1747 | ** suspend - ie from this point on we must try to suspend | |
1748 | ** before resuming can occur. We therefore also reset the | |
1749 | ** resume state machine to VC_RESUME_IDLE in this state. | |
1750 | ** | |
1751 | ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call | |
1752 | ** complete_all on the suspend completion to notify | |
1753 | ** anything waiting for suspend to happen. | |
1754 | ** | |
1755 | ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also | |
1756 | ** initiate resume, so no need to alter resume state. | |
1757 | ** We call complete_all on the suspend completion to notify | |
1758 | ** of suspend rejection. | |
1759 | ** | |
1760 | ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the | |
1761 | ** suspend completion and reset the resume state machine. | |
1762 | ** | |
1763 | ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The | |
1764 | ** resume completion is in it's 'done' state whenever | |
fa9b8988 MY |
1765 | ** videcore is running. Therefore, the VC_RESUME_IDLE |
1766 | ** state implies that videocore is suspended. | |
71bad7f0 | 1767 | ** Hence, any thread which needs to wait until videocore is |
1768 | ** running can wait on this completion - it will only block | |
1769 | ** if videocore is suspended. | |
1770 | ** | |
1771 | ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running. | |
1772 | ** Call complete_all on the resume completion to unblock | |
1773 | ** any threads waiting for resume. Also reset the suspend | |
1774 | ** state machine to it's idle state. | |
1775 | ** | |
1776 | ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists. | |
1777 | */ | |
1778 | ||
1779 | void | |
1780 | set_suspend_state(VCHIQ_ARM_STATE_T *arm_state, | |
1781 | enum vc_suspend_status new_state) | |
1782 | { | |
1783 | /* set the state in all cases */ | |
1784 | arm_state->vc_suspend_state = new_state; | |
1785 | ||
1786 | /* state specific additional actions */ | |
1787 | switch (new_state) { | |
1788 | case VC_SUSPEND_FORCE_CANCELED: | |
1789 | complete_all(&arm_state->vc_suspend_complete); | |
1790 | break; | |
1791 | case VC_SUSPEND_REJECTED: | |
1792 | complete_all(&arm_state->vc_suspend_complete); | |
1793 | break; | |
1794 | case VC_SUSPEND_FAILED: | |
1795 | complete_all(&arm_state->vc_suspend_complete); | |
1796 | arm_state->vc_resume_state = VC_RESUME_RESUMED; | |
1797 | complete_all(&arm_state->vc_resume_complete); | |
1798 | break; | |
1799 | case VC_SUSPEND_IDLE: | |
1800 | reinit_completion(&arm_state->vc_suspend_complete); | |
1801 | break; | |
1802 | case VC_SUSPEND_REQUESTED: | |
1803 | break; | |
1804 | case VC_SUSPEND_IN_PROGRESS: | |
1805 | set_resume_state(arm_state, VC_RESUME_IDLE); | |
1806 | break; | |
1807 | case VC_SUSPEND_SUSPENDED: | |
1808 | complete_all(&arm_state->vc_suspend_complete); | |
1809 | break; | |
1810 | default: | |
1811 | BUG(); | |
1812 | break; | |
1813 | } | |
1814 | } | |
1815 | ||
1816 | void | |
1817 | set_resume_state(VCHIQ_ARM_STATE_T *arm_state, | |
1818 | enum vc_resume_status new_state) | |
1819 | { | |
1820 | /* set the state in all cases */ | |
1821 | arm_state->vc_resume_state = new_state; | |
1822 | ||
1823 | /* state specific additional actions */ | |
1824 | switch (new_state) { | |
1825 | case VC_RESUME_FAILED: | |
1826 | break; | |
1827 | case VC_RESUME_IDLE: | |
1828 | reinit_completion(&arm_state->vc_resume_complete); | |
1829 | break; | |
1830 | case VC_RESUME_REQUESTED: | |
1831 | break; | |
1832 | case VC_RESUME_IN_PROGRESS: | |
1833 | break; | |
1834 | case VC_RESUME_RESUMED: | |
1835 | complete_all(&arm_state->vc_resume_complete); | |
1836 | set_suspend_state(arm_state, VC_SUSPEND_IDLE); | |
1837 | break; | |
1838 | default: | |
1839 | BUG(); | |
1840 | break; | |
1841 | } | |
1842 | } | |
1843 | ||
1844 | ||
1845 | /* should be called with the write lock held */ | |
1846 | inline void | |
1847 | start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state) | |
1848 | { | |
1849 | del_timer(&arm_state->suspend_timer); | |
1850 | arm_state->suspend_timer.expires = jiffies + | |
1851 | msecs_to_jiffies(arm_state-> | |
1852 | suspend_timer_timeout); | |
1853 | add_timer(&arm_state->suspend_timer); | |
1854 | arm_state->suspend_timer_running = 1; | |
1855 | } | |
1856 | ||
1857 | /* should be called with the write lock held */ | |
1858 | static inline void | |
1859 | stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state) | |
1860 | { | |
1861 | if (arm_state->suspend_timer_running) { | |
1862 | del_timer(&arm_state->suspend_timer); | |
1863 | arm_state->suspend_timer_running = 0; | |
1864 | } | |
1865 | } | |
1866 | ||
1867 | static inline int | |
1868 | need_resume(VCHIQ_STATE_T *state) | |
1869 | { | |
1870 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
1871 | return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) && | |
1872 | (arm_state->vc_resume_state < VC_RESUME_REQUESTED) && | |
1873 | vchiq_videocore_wanted(state); | |
1874 | } | |
1875 | ||
1876 | static int | |
1877 | block_resume(VCHIQ_ARM_STATE_T *arm_state) | |
1878 | { | |
1879 | int status = VCHIQ_SUCCESS; | |
1880 | const unsigned long timeout_val = | |
1881 | msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS); | |
1882 | int resume_count = 0; | |
1883 | ||
1884 | /* Allow any threads which were blocked by the last force suspend to | |
1885 | * complete if they haven't already. Only give this one shot; if | |
1886 | * blocked_count is incremented after blocked_blocker is completed | |
1887 | * (which only happens when blocked_count hits 0) then those threads | |
1888 | * will have to wait until next time around */ | |
1889 | if (arm_state->blocked_count) { | |
1890 | reinit_completion(&arm_state->blocked_blocker); | |
1891 | write_unlock_bh(&arm_state->susp_res_lock); | |
1892 | vchiq_log_info(vchiq_susp_log_level, "%s wait for previously " | |
1893 | "blocked clients", __func__); | |
1894 | if (wait_for_completion_interruptible_timeout( | |
1895 | &arm_state->blocked_blocker, timeout_val) | |
1896 | <= 0) { | |
1897 | vchiq_log_error(vchiq_susp_log_level, "%s wait for " | |
1898 | "previously blocked clients failed" , __func__); | |
1899 | status = VCHIQ_ERROR; | |
1900 | write_lock_bh(&arm_state->susp_res_lock); | |
1901 | goto out; | |
1902 | } | |
1903 | vchiq_log_info(vchiq_susp_log_level, "%s previously blocked " | |
1904 | "clients resumed", __func__); | |
1905 | write_lock_bh(&arm_state->susp_res_lock); | |
1906 | } | |
1907 | ||
1908 | /* We need to wait for resume to complete if it's in process */ | |
1909 | while (arm_state->vc_resume_state != VC_RESUME_RESUMED && | |
1910 | arm_state->vc_resume_state > VC_RESUME_IDLE) { | |
1911 | if (resume_count > 1) { | |
1912 | status = VCHIQ_ERROR; | |
1913 | vchiq_log_error(vchiq_susp_log_level, "%s waited too " | |
1914 | "many times for resume" , __func__); | |
1915 | goto out; | |
1916 | } | |
1917 | write_unlock_bh(&arm_state->susp_res_lock); | |
1918 | vchiq_log_info(vchiq_susp_log_level, "%s wait for resume", | |
1919 | __func__); | |
1920 | if (wait_for_completion_interruptible_timeout( | |
1921 | &arm_state->vc_resume_complete, timeout_val) | |
1922 | <= 0) { | |
1923 | vchiq_log_error(vchiq_susp_log_level, "%s wait for " | |
1924 | "resume failed (%s)", __func__, | |
1925 | resume_state_names[arm_state->vc_resume_state + | |
1926 | VC_RESUME_NUM_OFFSET]); | |
1927 | status = VCHIQ_ERROR; | |
1928 | write_lock_bh(&arm_state->susp_res_lock); | |
1929 | goto out; | |
1930 | } | |
1931 | vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__); | |
1932 | write_lock_bh(&arm_state->susp_res_lock); | |
1933 | resume_count++; | |
1934 | } | |
1935 | reinit_completion(&arm_state->resume_blocker); | |
1936 | arm_state->resume_blocked = 1; | |
1937 | ||
1938 | out: | |
1939 | return status; | |
1940 | } | |
1941 | ||
1942 | static inline void | |
1943 | unblock_resume(VCHIQ_ARM_STATE_T *arm_state) | |
1944 | { | |
1945 | complete_all(&arm_state->resume_blocker); | |
1946 | arm_state->resume_blocked = 0; | |
1947 | } | |
1948 | ||
1949 | /* Initiate suspend via slot handler. Should be called with the write lock | |
1950 | * held */ | |
1951 | VCHIQ_STATUS_T | |
1952 | vchiq_arm_vcsuspend(VCHIQ_STATE_T *state) | |
1953 | { | |
1954 | VCHIQ_STATUS_T status = VCHIQ_ERROR; | |
1955 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
1956 | ||
1957 | if (!arm_state) | |
1958 | goto out; | |
1959 | ||
1960 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
1961 | status = VCHIQ_SUCCESS; | |
1962 | ||
1963 | ||
1964 | switch (arm_state->vc_suspend_state) { | |
1965 | case VC_SUSPEND_REQUESTED: | |
1966 | vchiq_log_info(vchiq_susp_log_level, "%s: suspend already " | |
1967 | "requested", __func__); | |
1968 | break; | |
1969 | case VC_SUSPEND_IN_PROGRESS: | |
1970 | vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in " | |
1971 | "progress", __func__); | |
1972 | break; | |
1973 | ||
1974 | default: | |
1975 | /* We don't expect to be in other states, so log but continue | |
1976 | * anyway */ | |
1977 | vchiq_log_error(vchiq_susp_log_level, | |
1978 | "%s unexpected suspend state %s", __func__, | |
1979 | suspend_state_names[arm_state->vc_suspend_state + | |
1980 | VC_SUSPEND_NUM_OFFSET]); | |
1981 | /* fall through */ | |
1982 | case VC_SUSPEND_REJECTED: | |
1983 | case VC_SUSPEND_FAILED: | |
1984 | /* Ensure any idle state actions have been run */ | |
1985 | set_suspend_state(arm_state, VC_SUSPEND_IDLE); | |
1986 | /* fall through */ | |
1987 | case VC_SUSPEND_IDLE: | |
1988 | vchiq_log_info(vchiq_susp_log_level, | |
1989 | "%s: suspending", __func__); | |
1990 | set_suspend_state(arm_state, VC_SUSPEND_REQUESTED); | |
1991 | /* kick the slot handler thread to initiate suspend */ | |
1992 | request_poll(state, NULL, 0); | |
1993 | break; | |
1994 | } | |
1995 | ||
1996 | out: | |
1997 | vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status); | |
1998 | return status; | |
1999 | } | |
2000 | ||
2001 | void | |
2002 | vchiq_platform_check_suspend(VCHIQ_STATE_T *state) | |
2003 | { | |
2004 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2005 | int susp = 0; | |
2006 | ||
2007 | if (!arm_state) | |
2008 | goto out; | |
2009 | ||
2010 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2011 | ||
2012 | write_lock_bh(&arm_state->susp_res_lock); | |
2013 | if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED && | |
2014 | arm_state->vc_resume_state == VC_RESUME_RESUMED) { | |
2015 | set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS); | |
2016 | susp = 1; | |
2017 | } | |
2018 | write_unlock_bh(&arm_state->susp_res_lock); | |
2019 | ||
2020 | if (susp) | |
2021 | vchiq_platform_suspend(state); | |
2022 | ||
2023 | out: | |
2024 | vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__); | |
2025 | return; | |
2026 | } | |
2027 | ||
2028 | ||
2029 | static void | |
2030 | output_timeout_error(VCHIQ_STATE_T *state) | |
2031 | { | |
2032 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
6d5f49a9 | 2033 | char err[50] = ""; |
71bad7f0 | 2034 | int vc_use_count = arm_state->videocore_use_count; |
2035 | int active_services = state->unused_service; | |
2036 | int i; | |
2037 | ||
2038 | if (!arm_state->videocore_use_count) { | |
6d5f49a9 | 2039 | snprintf(err, sizeof(err), " Videocore usecount is 0"); |
71bad7f0 | 2040 | goto output_msg; |
2041 | } | |
2042 | for (i = 0; i < active_services; i++) { | |
2043 | VCHIQ_SERVICE_T *service_ptr = state->services[i]; | |
2044 | if (service_ptr && service_ptr->service_use_count && | |
2045 | (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) { | |
6d5f49a9 | 2046 | snprintf(err, sizeof(err), " %c%c%c%c(%d) service has " |
71bad7f0 | 2047 | "use count %d%s", VCHIQ_FOURCC_AS_4CHARS( |
2048 | service_ptr->base.fourcc), | |
2049 | service_ptr->client_id, | |
2050 | service_ptr->service_use_count, | |
2051 | service_ptr->service_use_count == | |
2052 | vc_use_count ? "" : " (+ more)"); | |
2053 | break; | |
2054 | } | |
2055 | } | |
2056 | ||
2057 | output_msg: | |
2058 | vchiq_log_error(vchiq_susp_log_level, | |
2059 | "timed out waiting for vc suspend (%d).%s", | |
6d5f49a9 | 2060 | arm_state->autosuspend_override, err); |
71bad7f0 | 2061 | |
2062 | } | |
2063 | ||
2064 | /* Try to get videocore into suspended state, regardless of autosuspend state. | |
2065 | ** We don't actually force suspend, since videocore may get into a bad state | |
2066 | ** if we force suspend at a bad time. Instead, we wait for autosuspend to | |
2067 | ** determine a good point to suspend. If this doesn't happen within 100ms we | |
2068 | ** report failure. | |
2069 | ** | |
2070 | ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if | |
2071 | ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted. | |
2072 | */ | |
2073 | VCHIQ_STATUS_T | |
2074 | vchiq_arm_force_suspend(VCHIQ_STATE_T *state) | |
2075 | { | |
2076 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2077 | VCHIQ_STATUS_T status = VCHIQ_ERROR; | |
2078 | long rc = 0; | |
2079 | int repeat = -1; | |
2080 | ||
2081 | if (!arm_state) | |
2082 | goto out; | |
2083 | ||
2084 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2085 | ||
2086 | write_lock_bh(&arm_state->susp_res_lock); | |
2087 | ||
2088 | status = block_resume(arm_state); | |
2089 | if (status != VCHIQ_SUCCESS) | |
2090 | goto unlock; | |
2091 | if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) { | |
2092 | /* Already suspended - just block resume and exit */ | |
2093 | vchiq_log_info(vchiq_susp_log_level, "%s already suspended", | |
2094 | __func__); | |
2095 | status = VCHIQ_SUCCESS; | |
2096 | goto unlock; | |
2097 | } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) { | |
2098 | /* initiate suspend immediately in the case that we're waiting | |
2099 | * for the timeout */ | |
2100 | stop_suspend_timer(arm_state); | |
2101 | if (!vchiq_videocore_wanted(state)) { | |
2102 | vchiq_log_info(vchiq_susp_log_level, "%s videocore " | |
2103 | "idle, initiating suspend", __func__); | |
2104 | status = vchiq_arm_vcsuspend(state); | |
2105 | } else if (arm_state->autosuspend_override < | |
2106 | FORCE_SUSPEND_FAIL_MAX) { | |
2107 | vchiq_log_info(vchiq_susp_log_level, "%s letting " | |
2108 | "videocore go idle", __func__); | |
2109 | status = VCHIQ_SUCCESS; | |
2110 | } else { | |
2111 | vchiq_log_warning(vchiq_susp_log_level, "%s failed too " | |
2112 | "many times - attempting suspend", __func__); | |
2113 | status = vchiq_arm_vcsuspend(state); | |
2114 | } | |
2115 | } else { | |
2116 | vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend " | |
2117 | "in progress - wait for completion", __func__); | |
2118 | status = VCHIQ_SUCCESS; | |
2119 | } | |
2120 | ||
2121 | /* Wait for suspend to happen due to system idle (not forced..) */ | |
2122 | if (status != VCHIQ_SUCCESS) | |
2123 | goto unblock_resume; | |
2124 | ||
2125 | do { | |
2126 | write_unlock_bh(&arm_state->susp_res_lock); | |
2127 | ||
2128 | rc = wait_for_completion_interruptible_timeout( | |
2129 | &arm_state->vc_suspend_complete, | |
2130 | msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS)); | |
2131 | ||
2132 | write_lock_bh(&arm_state->susp_res_lock); | |
2133 | if (rc < 0) { | |
2134 | vchiq_log_warning(vchiq_susp_log_level, "%s " | |
2135 | "interrupted waiting for suspend", __func__); | |
2136 | status = VCHIQ_ERROR; | |
2137 | goto unblock_resume; | |
2138 | } else if (rc == 0) { | |
2139 | if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) { | |
2140 | /* Repeat timeout once if in progress */ | |
2141 | if (repeat < 0) { | |
2142 | repeat = 1; | |
2143 | continue; | |
2144 | } | |
2145 | } | |
2146 | arm_state->autosuspend_override++; | |
2147 | output_timeout_error(state); | |
2148 | ||
2149 | status = VCHIQ_RETRY; | |
2150 | goto unblock_resume; | |
2151 | } | |
2152 | } while (0 < (repeat--)); | |
2153 | ||
2154 | /* Check and report state in case we need to abort ARM suspend */ | |
2155 | if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) { | |
2156 | status = VCHIQ_RETRY; | |
2157 | vchiq_log_error(vchiq_susp_log_level, | |
2158 | "%s videocore suspend failed (state %s)", __func__, | |
2159 | suspend_state_names[arm_state->vc_suspend_state + | |
2160 | VC_SUSPEND_NUM_OFFSET]); | |
2161 | /* Reset the state only if it's still in an error state. | |
2162 | * Something could have already initiated another suspend. */ | |
2163 | if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE) | |
2164 | set_suspend_state(arm_state, VC_SUSPEND_IDLE); | |
2165 | ||
2166 | goto unblock_resume; | |
2167 | } | |
2168 | ||
2169 | /* successfully suspended - unlock and exit */ | |
2170 | goto unlock; | |
2171 | ||
2172 | unblock_resume: | |
2173 | /* all error states need to unblock resume before exit */ | |
2174 | unblock_resume(arm_state); | |
2175 | ||
2176 | unlock: | |
2177 | write_unlock_bh(&arm_state->susp_res_lock); | |
2178 | ||
2179 | out: | |
2180 | vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status); | |
2181 | return status; | |
2182 | } | |
2183 | ||
2184 | void | |
2185 | vchiq_check_suspend(VCHIQ_STATE_T *state) | |
2186 | { | |
2187 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2188 | ||
2189 | if (!arm_state) | |
2190 | goto out; | |
2191 | ||
2192 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2193 | ||
2194 | write_lock_bh(&arm_state->susp_res_lock); | |
2195 | if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED && | |
2196 | arm_state->first_connect && | |
2197 | !vchiq_videocore_wanted(state)) { | |
2198 | vchiq_arm_vcsuspend(state); | |
2199 | } | |
2200 | write_unlock_bh(&arm_state->susp_res_lock); | |
2201 | ||
2202 | out: | |
2203 | vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__); | |
2204 | return; | |
2205 | } | |
2206 | ||
2207 | ||
2208 | int | |
2209 | vchiq_arm_allow_resume(VCHIQ_STATE_T *state) | |
2210 | { | |
2211 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2212 | int resume = 0; | |
2213 | int ret = -1; | |
2214 | ||
2215 | if (!arm_state) | |
2216 | goto out; | |
2217 | ||
2218 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2219 | ||
2220 | write_lock_bh(&arm_state->susp_res_lock); | |
2221 | unblock_resume(arm_state); | |
2222 | resume = vchiq_check_resume(state); | |
2223 | write_unlock_bh(&arm_state->susp_res_lock); | |
2224 | ||
2225 | if (resume) { | |
2226 | if (wait_for_completion_interruptible( | |
2227 | &arm_state->vc_resume_complete) < 0) { | |
2228 | vchiq_log_error(vchiq_susp_log_level, | |
2229 | "%s interrupted", __func__); | |
2230 | /* failed, cannot accurately derive suspend | |
2231 | * state, so exit early. */ | |
2232 | goto out; | |
2233 | } | |
2234 | } | |
2235 | ||
2236 | read_lock_bh(&arm_state->susp_res_lock); | |
2237 | if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) { | |
2238 | vchiq_log_info(vchiq_susp_log_level, | |
2239 | "%s: Videocore remains suspended", __func__); | |
2240 | } else { | |
2241 | vchiq_log_info(vchiq_susp_log_level, | |
2242 | "%s: Videocore resumed", __func__); | |
2243 | ret = 0; | |
2244 | } | |
2245 | read_unlock_bh(&arm_state->susp_res_lock); | |
2246 | out: | |
2247 | vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret); | |
2248 | return ret; | |
2249 | } | |
2250 | ||
2251 | /* This function should be called with the write lock held */ | |
2252 | int | |
2253 | vchiq_check_resume(VCHIQ_STATE_T *state) | |
2254 | { | |
2255 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2256 | int resume = 0; | |
2257 | ||
2258 | if (!arm_state) | |
2259 | goto out; | |
2260 | ||
2261 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2262 | ||
2263 | if (need_resume(state)) { | |
2264 | set_resume_state(arm_state, VC_RESUME_REQUESTED); | |
2265 | request_poll(state, NULL, 0); | |
2266 | resume = 1; | |
2267 | } | |
2268 | ||
2269 | out: | |
2270 | vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__); | |
2271 | return resume; | |
2272 | } | |
2273 | ||
2274 | void | |
2275 | vchiq_platform_check_resume(VCHIQ_STATE_T *state) | |
2276 | { | |
2277 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2278 | int res = 0; | |
2279 | ||
2280 | if (!arm_state) | |
2281 | goto out; | |
2282 | ||
2283 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2284 | ||
2285 | write_lock_bh(&arm_state->susp_res_lock); | |
2286 | if (arm_state->wake_address == 0) { | |
2287 | vchiq_log_info(vchiq_susp_log_level, | |
2288 | "%s: already awake", __func__); | |
2289 | goto unlock; | |
2290 | } | |
2291 | if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) { | |
2292 | vchiq_log_info(vchiq_susp_log_level, | |
2293 | "%s: already resuming", __func__); | |
2294 | goto unlock; | |
2295 | } | |
2296 | ||
2297 | if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) { | |
2298 | set_resume_state(arm_state, VC_RESUME_IN_PROGRESS); | |
2299 | res = 1; | |
2300 | } else | |
2301 | vchiq_log_trace(vchiq_susp_log_level, | |
2302 | "%s: not resuming (resume state %s)", __func__, | |
2303 | resume_state_names[arm_state->vc_resume_state + | |
2304 | VC_RESUME_NUM_OFFSET]); | |
2305 | ||
2306 | unlock: | |
2307 | write_unlock_bh(&arm_state->susp_res_lock); | |
2308 | ||
2309 | if (res) | |
2310 | vchiq_platform_resume(state); | |
2311 | ||
2312 | out: | |
2313 | vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__); | |
2314 | return; | |
2315 | ||
2316 | } | |
2317 | ||
2318 | ||
2319 | ||
2320 | VCHIQ_STATUS_T | |
2321 | vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, | |
2322 | enum USE_TYPE_E use_type) | |
2323 | { | |
2324 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2325 | VCHIQ_STATUS_T ret = VCHIQ_SUCCESS; | |
2326 | char entity[16]; | |
2327 | int *entity_uc; | |
2328 | int local_uc, local_entity_uc; | |
2329 | ||
2330 | if (!arm_state) | |
2331 | goto out; | |
2332 | ||
2333 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2334 | ||
2335 | if (use_type == USE_TYPE_VCHIQ) { | |
2336 | sprintf(entity, "VCHIQ: "); | |
2337 | entity_uc = &arm_state->peer_use_count; | |
2338 | } else if (service) { | |
2339 | sprintf(entity, "%c%c%c%c:%03d", | |
2340 | VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc), | |
2341 | service->client_id); | |
2342 | entity_uc = &service->service_use_count; | |
2343 | } else { | |
2344 | vchiq_log_error(vchiq_susp_log_level, "%s null service " | |
2345 | "ptr", __func__); | |
2346 | ret = VCHIQ_ERROR; | |
2347 | goto out; | |
2348 | } | |
2349 | ||
2350 | write_lock_bh(&arm_state->susp_res_lock); | |
2351 | while (arm_state->resume_blocked) { | |
2352 | /* If we call 'use' while force suspend is waiting for suspend, | |
2353 | * then we're about to block the thread which the force is | |
2354 | * waiting to complete, so we're bound to just time out. In this | |
2355 | * case, set the suspend state such that the wait will be | |
2356 | * canceled, so we can complete as quickly as possible. */ | |
2357 | if (arm_state->resume_blocked && arm_state->vc_suspend_state == | |
2358 | VC_SUSPEND_IDLE) { | |
2359 | set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED); | |
2360 | break; | |
2361 | } | |
2362 | /* If suspend is already in progress then we need to block */ | |
2363 | if (!try_wait_for_completion(&arm_state->resume_blocker)) { | |
2364 | /* Indicate that there are threads waiting on the resume | |
2365 | * blocker. These need to be allowed to complete before | |
2366 | * a _second_ call to force suspend can complete, | |
2367 | * otherwise low priority threads might never actually | |
2368 | * continue */ | |
2369 | arm_state->blocked_count++; | |
2370 | write_unlock_bh(&arm_state->susp_res_lock); | |
2371 | vchiq_log_info(vchiq_susp_log_level, "%s %s resume " | |
2372 | "blocked - waiting...", __func__, entity); | |
2373 | if (wait_for_completion_killable( | |
2374 | &arm_state->resume_blocker) != 0) { | |
2375 | vchiq_log_error(vchiq_susp_log_level, "%s %s " | |
2376 | "wait for resume blocker interrupted", | |
2377 | __func__, entity); | |
2378 | ret = VCHIQ_ERROR; | |
2379 | write_lock_bh(&arm_state->susp_res_lock); | |
2380 | arm_state->blocked_count--; | |
2381 | write_unlock_bh(&arm_state->susp_res_lock); | |
2382 | goto out; | |
2383 | } | |
2384 | vchiq_log_info(vchiq_susp_log_level, "%s %s resume " | |
2385 | "unblocked", __func__, entity); | |
2386 | write_lock_bh(&arm_state->susp_res_lock); | |
2387 | if (--arm_state->blocked_count == 0) | |
2388 | complete_all(&arm_state->blocked_blocker); | |
2389 | } | |
2390 | } | |
2391 | ||
2392 | stop_suspend_timer(arm_state); | |
2393 | ||
2394 | local_uc = ++arm_state->videocore_use_count; | |
2395 | local_entity_uc = ++(*entity_uc); | |
2396 | ||
2397 | /* If there's a pending request which hasn't yet been serviced then | |
2398 | * just clear it. If we're past VC_SUSPEND_REQUESTED state then | |
2399 | * vc_resume_complete will block until we either resume or fail to | |
2400 | * suspend */ | |
2401 | if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED) | |
2402 | set_suspend_state(arm_state, VC_SUSPEND_IDLE); | |
2403 | ||
2404 | if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) { | |
2405 | set_resume_state(arm_state, VC_RESUME_REQUESTED); | |
2406 | vchiq_log_info(vchiq_susp_log_level, | |
2407 | "%s %s count %d, state count %d", | |
2408 | __func__, entity, local_entity_uc, local_uc); | |
2409 | request_poll(state, NULL, 0); | |
2410 | } else | |
2411 | vchiq_log_trace(vchiq_susp_log_level, | |
2412 | "%s %s count %d, state count %d", | |
2413 | __func__, entity, *entity_uc, local_uc); | |
2414 | ||
2415 | ||
2416 | write_unlock_bh(&arm_state->susp_res_lock); | |
2417 | ||
2418 | /* Completion is in a done state when we're not suspended, so this won't | |
2419 | * block for the non-suspended case. */ | |
2420 | if (!try_wait_for_completion(&arm_state->vc_resume_complete)) { | |
2421 | vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume", | |
2422 | __func__, entity); | |
2423 | if (wait_for_completion_killable( | |
2424 | &arm_state->vc_resume_complete) != 0) { | |
2425 | vchiq_log_error(vchiq_susp_log_level, "%s %s wait for " | |
2426 | "resume interrupted", __func__, entity); | |
2427 | ret = VCHIQ_ERROR; | |
2428 | goto out; | |
2429 | } | |
2430 | vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__, | |
2431 | entity); | |
2432 | } | |
2433 | ||
2434 | if (ret == VCHIQ_SUCCESS) { | |
2435 | VCHIQ_STATUS_T status = VCHIQ_SUCCESS; | |
2436 | long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0); | |
2437 | while (ack_cnt && (status == VCHIQ_SUCCESS)) { | |
2438 | /* Send the use notify to videocore */ | |
2439 | status = vchiq_send_remote_use_active(state); | |
2440 | if (status == VCHIQ_SUCCESS) | |
2441 | ack_cnt--; | |
2442 | else | |
2443 | atomic_add(ack_cnt, | |
2444 | &arm_state->ka_use_ack_count); | |
2445 | } | |
2446 | } | |
2447 | ||
2448 | out: | |
2449 | vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret); | |
2450 | return ret; | |
2451 | } | |
2452 | ||
2453 | VCHIQ_STATUS_T | |
2454 | vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service) | |
2455 | { | |
2456 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2457 | VCHIQ_STATUS_T ret = VCHIQ_SUCCESS; | |
2458 | char entity[16]; | |
2459 | int *entity_uc; | |
2460 | int local_uc, local_entity_uc; | |
2461 | ||
2462 | if (!arm_state) | |
2463 | goto out; | |
2464 | ||
2465 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2466 | ||
2467 | if (service) { | |
2468 | sprintf(entity, "%c%c%c%c:%03d", | |
2469 | VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc), | |
2470 | service->client_id); | |
2471 | entity_uc = &service->service_use_count; | |
2472 | } else { | |
2473 | sprintf(entity, "PEER: "); | |
2474 | entity_uc = &arm_state->peer_use_count; | |
2475 | } | |
2476 | ||
2477 | write_lock_bh(&arm_state->susp_res_lock); | |
2478 | if (!arm_state->videocore_use_count || !(*entity_uc)) { | |
2479 | /* Don't use BUG_ON - don't allow user thread to crash kernel */ | |
2480 | WARN_ON(!arm_state->videocore_use_count); | |
2481 | WARN_ON(!(*entity_uc)); | |
2482 | ret = VCHIQ_ERROR; | |
2483 | goto unlock; | |
2484 | } | |
2485 | local_uc = --arm_state->videocore_use_count; | |
2486 | local_entity_uc = --(*entity_uc); | |
2487 | ||
2488 | if (!vchiq_videocore_wanted(state)) { | |
2489 | if (vchiq_platform_use_suspend_timer() && | |
2490 | !arm_state->resume_blocked) { | |
2491 | /* Only use the timer if we're not trying to force | |
2492 | * suspend (=> resume_blocked) */ | |
2493 | start_suspend_timer(arm_state); | |
2494 | } else { | |
2495 | vchiq_log_info(vchiq_susp_log_level, | |
2496 | "%s %s count %d, state count %d - suspending", | |
2497 | __func__, entity, *entity_uc, | |
2498 | arm_state->videocore_use_count); | |
2499 | vchiq_arm_vcsuspend(state); | |
2500 | } | |
2501 | } else | |
2502 | vchiq_log_trace(vchiq_susp_log_level, | |
2503 | "%s %s count %d, state count %d", | |
2504 | __func__, entity, *entity_uc, | |
2505 | arm_state->videocore_use_count); | |
2506 | ||
2507 | unlock: | |
2508 | write_unlock_bh(&arm_state->susp_res_lock); | |
2509 | ||
2510 | out: | |
2511 | vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret); | |
2512 | return ret; | |
2513 | } | |
2514 | ||
2515 | void | |
2516 | vchiq_on_remote_use(VCHIQ_STATE_T *state) | |
2517 | { | |
2518 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2519 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2520 | atomic_inc(&arm_state->ka_use_count); | |
2521 | complete(&arm_state->ka_evt); | |
2522 | } | |
2523 | ||
2524 | void | |
2525 | vchiq_on_remote_release(VCHIQ_STATE_T *state) | |
2526 | { | |
2527 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2528 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2529 | atomic_inc(&arm_state->ka_release_count); | |
2530 | complete(&arm_state->ka_evt); | |
2531 | } | |
2532 | ||
2533 | VCHIQ_STATUS_T | |
2534 | vchiq_use_service_internal(VCHIQ_SERVICE_T *service) | |
2535 | { | |
2536 | return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE); | |
2537 | } | |
2538 | ||
2539 | VCHIQ_STATUS_T | |
2540 | vchiq_release_service_internal(VCHIQ_SERVICE_T *service) | |
2541 | { | |
2542 | return vchiq_release_internal(service->state, service); | |
2543 | } | |
2544 | ||
2545 | VCHIQ_DEBUGFS_NODE_T * | |
2546 | vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance) | |
2547 | { | |
2548 | return &instance->debugfs_node; | |
2549 | } | |
2550 | ||
2551 | int | |
2552 | vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance) | |
2553 | { | |
2554 | VCHIQ_SERVICE_T *service; | |
2555 | int use_count = 0, i; | |
2556 | i = 0; | |
2557 | while ((service = next_service_by_instance(instance->state, | |
2558 | instance, &i)) != NULL) { | |
2559 | use_count += service->service_use_count; | |
2560 | unlock_service(service); | |
2561 | } | |
2562 | return use_count; | |
2563 | } | |
2564 | ||
2565 | int | |
2566 | vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance) | |
2567 | { | |
2568 | return instance->pid; | |
2569 | } | |
2570 | ||
2571 | int | |
2572 | vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance) | |
2573 | { | |
2574 | return instance->trace; | |
2575 | } | |
2576 | ||
2577 | void | |
2578 | vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace) | |
2579 | { | |
2580 | VCHIQ_SERVICE_T *service; | |
2581 | int i; | |
2582 | i = 0; | |
2583 | while ((service = next_service_by_instance(instance->state, | |
2584 | instance, &i)) != NULL) { | |
2585 | service->trace = trace; | |
2586 | unlock_service(service); | |
2587 | } | |
2588 | instance->trace = (trace != 0); | |
2589 | } | |
2590 | ||
2591 | static void suspend_timer_callback(unsigned long context) | |
2592 | { | |
2593 | VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context; | |
2594 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2595 | if (!arm_state) | |
2596 | goto out; | |
2597 | vchiq_log_info(vchiq_susp_log_level, | |
2598 | "%s - suspend timer expired - check suspend", __func__); | |
2599 | vchiq_check_suspend(state); | |
2600 | out: | |
2601 | return; | |
2602 | } | |
2603 | ||
2604 | VCHIQ_STATUS_T | |
2605 | vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle) | |
2606 | { | |
2607 | VCHIQ_STATUS_T ret = VCHIQ_ERROR; | |
2608 | VCHIQ_SERVICE_T *service = find_service_by_handle(handle); | |
2609 | if (service) { | |
2610 | ret = vchiq_use_internal(service->state, service, | |
2611 | USE_TYPE_SERVICE_NO_RESUME); | |
2612 | unlock_service(service); | |
2613 | } | |
2614 | return ret; | |
2615 | } | |
2616 | ||
2617 | VCHIQ_STATUS_T | |
2618 | vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle) | |
2619 | { | |
2620 | VCHIQ_STATUS_T ret = VCHIQ_ERROR; | |
2621 | VCHIQ_SERVICE_T *service = find_service_by_handle(handle); | |
2622 | if (service) { | |
2623 | ret = vchiq_use_internal(service->state, service, | |
2624 | USE_TYPE_SERVICE); | |
2625 | unlock_service(service); | |
2626 | } | |
2627 | return ret; | |
2628 | } | |
2629 | ||
2630 | VCHIQ_STATUS_T | |
2631 | vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle) | |
2632 | { | |
2633 | VCHIQ_STATUS_T ret = VCHIQ_ERROR; | |
2634 | VCHIQ_SERVICE_T *service = find_service_by_handle(handle); | |
2635 | if (service) { | |
2636 | ret = vchiq_release_internal(service->state, service); | |
2637 | unlock_service(service); | |
2638 | } | |
2639 | return ret; | |
2640 | } | |
2641 | ||
2642 | void | |
2643 | vchiq_dump_service_use_state(VCHIQ_STATE_T *state) | |
2644 | { | |
2645 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2646 | int i, j = 0; | |
2647 | /* Only dump 64 services */ | |
2648 | static const int local_max_services = 64; | |
2649 | /* If there's more than 64 services, only dump ones with | |
2650 | * non-zero counts */ | |
2651 | int only_nonzero = 0; | |
2652 | static const char *nz = "<-- preventing suspend"; | |
2653 | ||
2654 | enum vc_suspend_status vc_suspend_state; | |
2655 | enum vc_resume_status vc_resume_state; | |
2656 | int peer_count; | |
2657 | int vc_use_count; | |
2658 | int active_services; | |
2659 | struct service_data_struct { | |
2660 | int fourcc; | |
2661 | int clientid; | |
2662 | int use_count; | |
2663 | } service_data[local_max_services]; | |
2664 | ||
2665 | if (!arm_state) | |
2666 | return; | |
2667 | ||
2668 | read_lock_bh(&arm_state->susp_res_lock); | |
2669 | vc_suspend_state = arm_state->vc_suspend_state; | |
2670 | vc_resume_state = arm_state->vc_resume_state; | |
2671 | peer_count = arm_state->peer_use_count; | |
2672 | vc_use_count = arm_state->videocore_use_count; | |
2673 | active_services = state->unused_service; | |
2674 | if (active_services > local_max_services) | |
2675 | only_nonzero = 1; | |
2676 | ||
2677 | for (i = 0; (i < active_services) && (j < local_max_services); i++) { | |
2678 | VCHIQ_SERVICE_T *service_ptr = state->services[i]; | |
2679 | if (!service_ptr) | |
2680 | continue; | |
2681 | ||
2682 | if (only_nonzero && !service_ptr->service_use_count) | |
2683 | continue; | |
2684 | ||
2685 | if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) { | |
2686 | service_data[j].fourcc = service_ptr->base.fourcc; | |
2687 | service_data[j].clientid = service_ptr->client_id; | |
2688 | service_data[j++].use_count = service_ptr-> | |
2689 | service_use_count; | |
2690 | } | |
2691 | } | |
2692 | ||
2693 | read_unlock_bh(&arm_state->susp_res_lock); | |
2694 | ||
2695 | vchiq_log_warning(vchiq_susp_log_level, | |
2696 | "-- Videcore suspend state: %s --", | |
2697 | suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]); | |
2698 | vchiq_log_warning(vchiq_susp_log_level, | |
2699 | "-- Videcore resume state: %s --", | |
2700 | resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]); | |
2701 | ||
2702 | if (only_nonzero) | |
2703 | vchiq_log_warning(vchiq_susp_log_level, "Too many active " | |
2704 | "services (%d). Only dumping up to first %d services " | |
2705 | "with non-zero use-count", active_services, | |
2706 | local_max_services); | |
2707 | ||
2708 | for (i = 0; i < j; i++) { | |
2709 | vchiq_log_warning(vchiq_susp_log_level, | |
2710 | "----- %c%c%c%c:%d service count %d %s", | |
2711 | VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc), | |
2712 | service_data[i].clientid, | |
2713 | service_data[i].use_count, | |
2714 | service_data[i].use_count ? nz : ""); | |
2715 | } | |
2716 | vchiq_log_warning(vchiq_susp_log_level, | |
2717 | "----- VCHIQ use count count %d", peer_count); | |
2718 | vchiq_log_warning(vchiq_susp_log_level, | |
2719 | "--- Overall vchiq instance use count %d", vc_use_count); | |
2720 | ||
2721 | vchiq_dump_platform_use_state(state); | |
2722 | } | |
2723 | ||
2724 | VCHIQ_STATUS_T | |
2725 | vchiq_check_service(VCHIQ_SERVICE_T *service) | |
2726 | { | |
2727 | VCHIQ_ARM_STATE_T *arm_state; | |
2728 | VCHIQ_STATUS_T ret = VCHIQ_ERROR; | |
2729 | ||
2730 | if (!service || !service->state) | |
2731 | goto out; | |
2732 | ||
2733 | vchiq_log_trace(vchiq_susp_log_level, "%s", __func__); | |
2734 | ||
2735 | arm_state = vchiq_platform_get_arm_state(service->state); | |
2736 | ||
2737 | read_lock_bh(&arm_state->susp_res_lock); | |
2738 | if (service->service_use_count) | |
2739 | ret = VCHIQ_SUCCESS; | |
2740 | read_unlock_bh(&arm_state->susp_res_lock); | |
2741 | ||
2742 | if (ret == VCHIQ_ERROR) { | |
2743 | vchiq_log_error(vchiq_susp_log_level, | |
2744 | "%s ERROR - %c%c%c%c:%d service count %d, " | |
2745 | "state count %d, videocore suspend state %s", __func__, | |
2746 | VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc), | |
2747 | service->client_id, service->service_use_count, | |
2748 | arm_state->videocore_use_count, | |
2749 | suspend_state_names[arm_state->vc_suspend_state + | |
2750 | VC_SUSPEND_NUM_OFFSET]); | |
2751 | vchiq_dump_service_use_state(service->state); | |
2752 | } | |
2753 | out: | |
2754 | return ret; | |
2755 | } | |
2756 | ||
2757 | /* stub functions */ | |
2758 | void vchiq_on_remote_use_active(VCHIQ_STATE_T *state) | |
2759 | { | |
2760 | (void)state; | |
2761 | } | |
2762 | ||
2763 | void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state, | |
2764 | VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate) | |
2765 | { | |
2766 | VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state); | |
2767 | vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id, | |
2768 | get_conn_state_name(oldstate), get_conn_state_name(newstate)); | |
2769 | if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) { | |
2770 | write_lock_bh(&arm_state->susp_res_lock); | |
2771 | if (!arm_state->first_connect) { | |
2772 | char threadname[10]; | |
2773 | arm_state->first_connect = 1; | |
2774 | write_unlock_bh(&arm_state->susp_res_lock); | |
2775 | snprintf(threadname, sizeof(threadname), "VCHIQka-%d", | |
2776 | state->id); | |
2777 | arm_state->ka_thread = kthread_create( | |
2778 | &vchiq_keepalive_thread_func, | |
2779 | (void *)state, | |
2780 | threadname); | |
b04451d7 | 2781 | if (IS_ERR(arm_state->ka_thread)) { |
71bad7f0 | 2782 | vchiq_log_error(vchiq_susp_log_level, |
2783 | "vchiq: FATAL: couldn't create thread %s", | |
2784 | threadname); | |
2785 | } else { | |
2786 | wake_up_process(arm_state->ka_thread); | |
2787 | } | |
2788 | } else | |
2789 | write_unlock_bh(&arm_state->susp_res_lock); | |
2790 | } | |
2791 | } | |
2792 | ||
2793 | static int vchiq_probe(struct platform_device *pdev) | |
2794 | { | |
2795 | struct device_node *fw_node; | |
2796 | struct rpi_firmware *fw; | |
2797 | int err; | |
2798 | void *ptr_err; | |
2799 | ||
2800 | fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0); | |
71bad7f0 | 2801 | if (!fw_node) { |
2802 | dev_err(&pdev->dev, "Missing firmware node\n"); | |
2803 | return -ENOENT; | |
2804 | } | |
bea845af | 2805 | |
71bad7f0 | 2806 | fw = rpi_firmware_get(fw_node); |
d10543ec | 2807 | of_node_put(fw_node); |
71bad7f0 | 2808 | if (!fw) |
2809 | return -EPROBE_DEFER; | |
2810 | ||
2811 | platform_set_drvdata(pdev, fw); | |
2812 | ||
9224c15c | 2813 | err = vchiq_platform_init(pdev, &g_state); |
71bad7f0 | 2814 | if (err != 0) |
9224c15c | 2815 | goto failed_platform_init; |
71bad7f0 | 2816 | |
2817 | err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME); | |
2818 | if (err != 0) { | |
2819 | vchiq_log_error(vchiq_arm_log_level, | |
2820 | "Unable to allocate device number"); | |
9224c15c | 2821 | goto failed_platform_init; |
71bad7f0 | 2822 | } |
2823 | cdev_init(&vchiq_cdev, &vchiq_fops); | |
2824 | vchiq_cdev.owner = THIS_MODULE; | |
2825 | err = cdev_add(&vchiq_cdev, vchiq_devid, 1); | |
2826 | if (err != 0) { | |
2827 | vchiq_log_error(vchiq_arm_log_level, | |
2828 | "Unable to register device"); | |
2829 | goto failed_cdev_add; | |
2830 | } | |
2831 | ||
2832 | /* create sysfs entries */ | |
2833 | vchiq_class = class_create(THIS_MODULE, DEVICE_NAME); | |
2834 | ptr_err = vchiq_class; | |
2835 | if (IS_ERR(ptr_err)) | |
2836 | goto failed_class_create; | |
2837 | ||
2838 | vchiq_dev = device_create(vchiq_class, NULL, | |
2839 | vchiq_devid, NULL, "vchiq"); | |
2840 | ptr_err = vchiq_dev; | |
2841 | if (IS_ERR(ptr_err)) | |
2842 | goto failed_device_create; | |
2843 | ||
9224c15c SW |
2844 | /* create debugfs entries */ |
2845 | err = vchiq_debugfs_init(); | |
71bad7f0 | 2846 | if (err != 0) |
9224c15c | 2847 | goto failed_debugfs_init; |
71bad7f0 | 2848 | |
2849 | vchiq_log_info(vchiq_arm_log_level, | |
2850 | "vchiq: initialised - version %d (min %d), device %d.%d", | |
2851 | VCHIQ_VERSION, VCHIQ_VERSION_MIN, | |
2852 | MAJOR(vchiq_devid), MINOR(vchiq_devid)); | |
2853 | ||
2854 | return 0; | |
2855 | ||
9224c15c | 2856 | failed_debugfs_init: |
71bad7f0 | 2857 | device_destroy(vchiq_class, vchiq_devid); |
2858 | failed_device_create: | |
2859 | class_destroy(vchiq_class); | |
2860 | failed_class_create: | |
2861 | cdev_del(&vchiq_cdev); | |
2862 | err = PTR_ERR(ptr_err); | |
2863 | failed_cdev_add: | |
2864 | unregister_chrdev_region(vchiq_devid, 1); | |
9224c15c | 2865 | failed_platform_init: |
71bad7f0 | 2866 | vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq"); |
2867 | return err; | |
2868 | } | |
2869 | ||
2870 | static int vchiq_remove(struct platform_device *pdev) | |
2871 | { | |
0ece01c6 | 2872 | vchiq_debugfs_deinit(); |
71bad7f0 | 2873 | device_destroy(vchiq_class, vchiq_devid); |
2874 | class_destroy(vchiq_class); | |
2875 | cdev_del(&vchiq_cdev); | |
2876 | unregister_chrdev_region(vchiq_devid, 1); | |
2877 | ||
2878 | return 0; | |
2879 | } | |
2880 | ||
2881 | static const struct of_device_id vchiq_of_match[] = { | |
2882 | { .compatible = "brcm,bcm2835-vchiq", }, | |
2883 | {}, | |
2884 | }; | |
2885 | MODULE_DEVICE_TABLE(of, vchiq_of_match); | |
2886 | ||
2887 | static struct platform_driver vchiq_driver = { | |
2888 | .driver = { | |
2889 | .name = "bcm2835_vchiq", | |
71bad7f0 | 2890 | .of_match_table = vchiq_of_match, |
2891 | }, | |
2892 | .probe = vchiq_probe, | |
2893 | .remove = vchiq_remove, | |
2894 | }; | |
2895 | module_platform_driver(vchiq_driver); | |
2896 | ||
2897 | MODULE_LICENSE("GPL"); | |
8f8a3402 | 2898 | MODULE_DESCRIPTION("Videocore VCHIQ driver"); |
71bad7f0 | 2899 | MODULE_AUTHOR("Broadcom Corporation"); |