LCOV - code coverage report
Current view: top level - drivers/staging/vc04_services/interface/vchiq_arm - vchiq_arm.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 235 1008 23.3 %
Date: 2020-09-30 20:25:40 Functions: 23 57 40.4 %
Branches: 95 788 12.1 %

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

Generated by: LCOV version 1.14