blob: 5850ce5025a87fc149afa8280e2aff4110b1a13f [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Duy Truong73d36df2013-02-09 20:33:23 -08002 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07003
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.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Naseer Ahmed29a26812012-06-14 00:56:20 -070014 * 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
Naseer Ahmed7c958d42012-07-31 18:57:03 -070030#include "idle_invalidator.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070031#include <unistd.h>
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080032#include <poll.h>
33#include <string.h>
34#include <fcntl.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070035
Naseer Ahmed7c958d42012-07-31 18:57:03 -070036#define II_DEBUG 0
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080037#define IDLE_NOTIFY_PATH "/sys/devices/virtual/graphics/fb0/idle_notify"
38#define IDLE_TIME_PATH "/sys/devices/virtual/graphics/fb0/idle_time"
Naseer Ahmed29a26812012-06-14 00:56:20 -070039
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080040
41static const char *threadName = "IdleInvalidator";
Naseer Ahmed29a26812012-06-14 00:56:20 -070042InvalidatorHandler IdleInvalidator::mHandler = NULL;
43android::sp<IdleInvalidator> IdleInvalidator::sInstance(0);
44
45IdleInvalidator::IdleInvalidator(): Thread(false), mHwcContext(0),
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080046 mTimeoutEventFd(-1) {
47 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s", __FUNCTION__);
48}
Naseer Ahmed29a26812012-06-14 00:56:20 -070049
Saurabh Shah59562ff2014-09-30 16:13:12 -070050IdleInvalidator::~IdleInvalidator() {
51 if(mTimeoutEventFd >= 0) {
52 close(mTimeoutEventFd);
53 }
54}
55
56int IdleInvalidator::init(InvalidatorHandler reg_handler, void* user_data) {
Naseer Ahmed29a26812012-06-14 00:56:20 -070057 mHandler = reg_handler;
58 mHwcContext = user_data;
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080059
60 // Open a sysfs node to receive the timeout notification from driver.
61 mTimeoutEventFd = open(IDLE_NOTIFY_PATH, O_RDONLY);
62 if (mTimeoutEventFd < 0) {
63 ALOGE ("%s:not able to open %s node %s",
64 __FUNCTION__, IDLE_NOTIFY_PATH, strerror(errno));
65 return -1;
66 }
67
Saurabh Shah59562ff2014-09-30 16:13:12 -070068 enum {DEFAULT_IDLE_TIME = 70}; //ms
69 if(not setIdleTimeout(DEFAULT_IDLE_TIME)) {
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080070 close(mTimeoutEventFd);
71 mTimeoutEventFd = -1;
72 return -1;
73 }
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -080074
75 //Triggers the threadLoop to run, if not already running.
76 run(threadName, android::PRIORITY_LOWEST);
Naseer Ahmed29a26812012-06-14 00:56:20 -070077 return 0;
78}
79
Saurabh Shah59562ff2014-09-30 16:13:12 -070080bool IdleInvalidator::setIdleTimeout(const uint32_t& timeout) {
81 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s timeout %d",
82 __FUNCTION__, timeout);
83
84 // Open a sysfs node to send the timeout value to driver.
85 int fd = open(IDLE_TIME_PATH, O_WRONLY);
86
87 if (fd < 0) {
88 ALOGE ("%s:Unable to open %s node %s",
89 __FUNCTION__, IDLE_TIME_PATH, strerror(errno));
90 return false;
91 }
92
93 char strSleepTime[64];
94 snprintf(strSleepTime, sizeof(strSleepTime), "%d", timeout);
95
96 // Notify driver about the timeout value
97 ssize_t len = pwrite(fd, strSleepTime, strlen(strSleepTime), 0);
98 if(len < -1) {
99 ALOGE ("%s:Unable to write into %s node %s",
100 __FUNCTION__, IDLE_TIME_PATH, strerror(errno));
101 close(fd);
102 return false;
103 }
104
105 close(fd);
106 return true;
107}
108
Naseer Ahmed29a26812012-06-14 00:56:20 -0700109bool IdleInvalidator::threadLoop() {
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -0800110 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s", __FUNCTION__);
111 struct pollfd pFd;
112 pFd.fd = mTimeoutEventFd;
113 if (pFd.fd >= 0)
114 pFd.events = POLLPRI | POLLERR;
115 // Poll for an timeout event from driver
116 int err = poll(&pFd, 1, -1);
117 if(err > 0) {
118 if (pFd.revents & POLLPRI) {
119 char data[64];
120 // Consume the node by reading it
121 ssize_t len = pread(pFd.fd, data, 64, 0);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700122 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s Idle Timeout fired len %zd",
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -0800123 __FUNCTION__, len);
124 mHandler((void*)mHwcContext);
125 }
Saurabh Shahb2117fe2014-01-23 18:39:01 -0800126 }
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -0800127 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700128}
129
130int IdleInvalidator::readyToRun() {
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -0800131 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700132 return 0; /*NO_ERROR*/
133}
134
135void IdleInvalidator::onFirstRef() {
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -0800136 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700137}
138
139IdleInvalidator *IdleInvalidator::getInstance() {
Ramkumar Radhakrishnan92abb4f2014-02-06 21:31:29 -0800140 ALOGD_IF(II_DEBUG, "IdleInvalidator::%s", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700141 if(sInstance.get() == NULL)
142 sInstance = new IdleInvalidator();
143 return sInstance.get();
144}