Home | History | Annotate | Download | only in fb
      1 /*
      2 * Copyright (c) 2015 - 2017, The Linux Foundation. All rights reserved.
      3 *
      4 * Redistribution and use in source and binary forms, with or without
      5 * modification, are permitted provided that the following conditions are
      6 * met:
      7 *     * Redistributions of source code must retain the above copyright
      8 *       notice, this list of conditions and the following disclaimer.
      9 *     * Redistributions in binary form must reproduce the above
     10 *       copyright notice, this list of conditions and the following
     11 *       disclaimer in the documentation and/or other materials provided
     12 *       with the distribution.
     13 *     * Neither the name of The Linux Foundation nor the names of its
     14 *       contributors may be used to endorse or promote products derived
     15 *       from this software without specific prior written permission.
     16 *
     17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 #include <errno.h>
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <math.h>
     34 #include <fcntl.h>
     35 #include <sys/types.h>
     36 #include <sys/resource.h>
     37 #include <sys/prctl.h>
     38 #include <utils/debug.h>
     39 #include <utils/sys.h>
     40 #include <pthread.h>
     41 #include <algorithm>
     42 #include <vector>
     43 #include <map>
     44 #include <utility>
     45 
     46 #include "hw_events.h"
     47 
     48 #define __CLASS__ "HWEvents"
     49 
     50 namespace sdm {
     51 
     52 pollfd HWEvents::InitializePollFd(HWEventData *event_data) {
     53   char node_path[kMaxStringLength] = {0};
     54   char data[kMaxStringLength] = {0};
     55   pollfd poll_fd = {0};
     56   poll_fd.fd = -1;
     57 
     58   if (event_data->event_type == HWEvent::EXIT) {
     59     // Create an eventfd to be used to unblock the poll system call when
     60     // a thread is exiting.
     61     poll_fd.fd = Sys::eventfd_(0, 0);
     62     poll_fd.events |= POLLIN;
     63     exit_fd_ = poll_fd.fd;
     64   } else {
     65     snprintf(node_path, sizeof(node_path), "%s%d/%s", fb_path_, fb_num_,
     66              map_event_to_node_[event_data->event_type]);
     67     poll_fd.fd = Sys::open_(node_path, O_RDONLY);
     68     poll_fd.events |= POLLPRI | POLLERR;
     69   }
     70 
     71   if (poll_fd.fd < 0) {
     72     DLOGW("open failed for display=%d event=%s, error=%s", fb_num_,
     73           map_event_to_node_[event_data->event_type], strerror(errno));
     74     return poll_fd;
     75   }
     76 
     77   // Read once on all fds to clear data on all fds.
     78   Sys::pread_(poll_fd.fd, data , kMaxStringLength, 0);
     79 
     80   return poll_fd;
     81 }
     82 
     83 DisplayError HWEvents::SetEventParser(HWEvent event_type, HWEventData *event_data) {
     84   DisplayError error = kErrorNone;
     85   switch (event_type) {
     86     case HWEvent::VSYNC:
     87       event_data->event_parser = &HWEvents::HandleVSync;
     88       break;
     89     case HWEvent::IDLE_NOTIFY:
     90       event_data->event_parser = &HWEvents::HandleIdleTimeout;
     91       break;
     92     case HWEvent::CEC_READ_MESSAGE:
     93       event_data->event_parser = &HWEvents::HandleCECMessage;
     94       break;
     95     case HWEvent::EXIT:
     96       event_data->event_parser = &HWEvents::HandleThreadExit;
     97       break;
     98     case HWEvent::SHOW_BLANK_EVENT:
     99       event_data->event_parser = &HWEvents::HandleBlank;
    100       break;
    101     case HWEvent::THERMAL_LEVEL:
    102       event_data->event_parser = &HWEvents::HandleThermal;
    103       break;
    104     case HWEvent::IDLE_POWER_COLLAPSE:
    105       event_data->event_parser = &HWEvents::HandleIdlePowerCollapse;
    106       break;
    107     default:
    108       error = kErrorParameters;
    109       break;
    110   }
    111 
    112   return error;
    113 }
    114 
    115 void HWEvents::PopulateHWEventData() {
    116   for (uint32_t i = 0; i < event_list_.size(); i++) {
    117     HWEventData event_data;
    118     event_data.event_type = event_list_[i];
    119     SetEventParser(event_list_[i], &event_data);
    120     poll_fds_[i] = InitializePollFd(&event_data);
    121     event_data_list_.push_back(event_data);
    122   }
    123 }
    124 
    125 DisplayError HWEvents::Init(int fb_num, HWEventHandler *event_handler,
    126                             const vector<HWEvent> &event_list) {
    127   if (!event_handler)
    128     return kErrorParameters;
    129 
    130   event_handler_ = event_handler;
    131   fb_num_ = fb_num;
    132   event_list_ = event_list;
    133   poll_fds_.resize(event_list_.size());
    134   event_thread_name_ += " - " + std::to_string(fb_num_);
    135   map_event_to_node_ = {{HWEvent::VSYNC, "vsync_event"}, {HWEvent::EXIT, "thread_exit"},
    136     {HWEvent::IDLE_NOTIFY, "idle_notify"}, {HWEvent::SHOW_BLANK_EVENT, "show_blank_event"},
    137     {HWEvent::CEC_READ_MESSAGE, "cec/rd_msg"}, {HWEvent::THERMAL_LEVEL, "msm_fb_thermal_level"},
    138     {HWEvent::IDLE_POWER_COLLAPSE, "idle_power_collapse"}};
    139 
    140   PopulateHWEventData();
    141 
    142   if (pthread_create(&event_thread_, NULL, &DisplayEventThread, this) < 0) {
    143     DLOGE("Failed to start %s, error = %s", event_thread_name_.c_str());
    144     return kErrorResources;
    145   }
    146 
    147   return kErrorNone;
    148 }
    149 
    150 DisplayError HWEvents::Deinit() {
    151   exit_threads_ = true;
    152   Sys::pthread_cancel_(event_thread_);
    153 
    154   uint64_t exit_value = 1;
    155   ssize_t write_size = Sys::write_(exit_fd_, &exit_value, sizeof(uint64_t));
    156   if (write_size != sizeof(uint64_t))
    157     DLOGW("Error triggering exit_fd_ (%d). write size = %d, error = %s", exit_fd_, write_size,
    158           strerror(errno));
    159 
    160   pthread_join(event_thread_, NULL);
    161 
    162   for (uint32_t i = 0; i < event_list_.size(); i++) {
    163     Sys::close_(poll_fds_[i].fd);
    164     poll_fds_[i].fd = -1;
    165   }
    166 
    167   return kErrorNone;
    168 }
    169 
    170 void* HWEvents::DisplayEventThread(void *context) {
    171   if (context) {
    172     return reinterpret_cast<HWEvents *>(context)->DisplayEventHandler();
    173   }
    174 
    175   return NULL;
    176 }
    177 
    178 void* HWEvents::DisplayEventHandler() {
    179   char data[kMaxStringLength] = {0};
    180 
    181   prctl(PR_SET_NAME, event_thread_name_.c_str(), 0, 0, 0);
    182   setpriority(PRIO_PROCESS, 0, kThreadPriorityUrgent);
    183 
    184   while (!exit_threads_) {
    185     int error = Sys::poll_(poll_fds_.data(), UINT32(event_list_.size()), -1);
    186 
    187     if (error <= 0) {
    188       DLOGW("poll failed. error = %s", strerror(errno));
    189       continue;
    190     }
    191 
    192     for (uint32_t event = 0; event < event_list_.size(); event++) {
    193       pollfd &poll_fd = poll_fds_[event];
    194 
    195       if (event_list_.at(event) == HWEvent::EXIT) {
    196         if ((poll_fd.revents & POLLIN) && (Sys::read_(poll_fd.fd, data, kMaxStringLength) > 0)) {
    197           (this->*(event_data_list_[event]).event_parser)(data);
    198         }
    199       } else {
    200         if ((poll_fd.revents & POLLPRI) &&
    201                 (Sys::pread_(poll_fd.fd, data, kMaxStringLength, 0) > 0)) {
    202           (this->*(event_data_list_[event]).event_parser)(data);
    203         }
    204       }
    205     }
    206   }
    207 
    208   pthread_exit(0);
    209 
    210   return NULL;
    211 }
    212 
    213 void HWEvents::HandleVSync(char *data) {
    214   int64_t timestamp = 0;
    215   if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
    216     timestamp = strtoll(data + strlen("VSYNC="), NULL, 0);
    217   }
    218 
    219   event_handler_->VSync(timestamp);
    220 }
    221 
    222 void HWEvents::HandleIdleTimeout(char *data) {
    223   event_handler_->IdleTimeout();
    224 }
    225 
    226 void HWEvents::HandleThermal(char *data) {
    227   int64_t thermal_level = 0;
    228   if (!strncmp(data, "thermal_level=", strlen("thermal_level="))) {
    229     thermal_level = strtoll(data + strlen("thermal_level="), NULL, 0);
    230   }
    231 
    232   DLOGI("Received thermal notification with thermal level = %d", thermal_level);
    233 
    234   event_handler_->ThermalEvent(thermal_level);
    235 }
    236 
    237 void HWEvents::HandleCECMessage(char *data) {
    238   event_handler_->CECMessage(data);
    239 }
    240 
    241 void HWEvents::HandleIdlePowerCollapse(char *data) {
    242   event_handler_->IdlePowerCollapse();
    243 }
    244 
    245 }  // namespace sdm
    246