5eef2643ab15349587dcd9e32be9ecb29d3625fc
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / tools / gator / daemon / MaliVideoDriver.cpp
1 /**
2  * Copyright (C) ARM Limited 2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include "MaliVideoDriver.h"
10
11 #include <unistd.h>
12
13 #include "Buffer.h"
14 #include "Counter.h"
15 #include "Logging.h"
16 #include "SessionData.h"
17
18 // From instr/src/mve_instr_comm_protocol.h
19 typedef enum mve_instr_configuration_type {
20         MVE_INSTR_RAW         = 1 << 0,
21         MVE_INSTR_COUNTERS    = 1 << 1,
22         MVE_INSTR_EVENTS      = 1 << 2,
23         MVE_INSTR_ACTIVITIES  = 1 << 3,
24
25         // Raw always pushed regardless
26         MVE_INSTR_PULL        = 1 << 12,
27         // Raw always unpacked regardless
28         MVE_INSTR_PACKED_COMM = 1 << 13,
29         // Don’t send ACKt response
30         MVE_INSTR_NO_AUTO_ACK   = 1 << 14,
31 } mve_instr_configuration_type_t;
32
33 static const char COUNTER[] = "ARM_Mali-V500_cnt";
34 static const char EVENT[] = "ARM_Mali-V500_evn";
35 static const char ACTIVITY[] = "ARM_Mali-V500_act";
36
37 class MaliVideoCounter : public DriverCounter {
38 public:
39         MaliVideoCounter(DriverCounter *next, const char *name, const MaliVideoCounterType type, const int id) : DriverCounter(next, name), mType(type), mId(id) {
40         }
41
42         ~MaliVideoCounter() {
43         }
44
45         MaliVideoCounterType getType() const { return mType; }
46         int getId() const { return mId; }
47
48 private:
49         const MaliVideoCounterType mType;
50         // Mali Video id
51         const int mId;
52 };
53
54 MaliVideoDriver::MaliVideoDriver() {
55 }
56
57 MaliVideoDriver::~MaliVideoDriver() {
58 }
59
60 void MaliVideoDriver::readEvents(mxml_node_t *const xml) {
61         mxml_node_t *node = xml;
62         while (true) {
63                 node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND);
64                 if (node == NULL) {
65                         break;
66                 }
67                 const char *counter = mxmlElementGetAttr(node, "counter");
68                 if (counter == NULL) {
69                         // Ignore
70                 } else if (strncmp(counter, COUNTER, sizeof(COUNTER) - 1) == 0) {
71                         const int i = strtol(counter + sizeof(COUNTER) - 1, NULL, 10);
72                         setCounters(new MaliVideoCounter(getCounters(), strdup(counter), MVCT_COUNTER, i));
73                 } else if (strncmp(counter, EVENT, sizeof(EVENT) - 1) == 0) {
74                         const int i = strtol(counter + sizeof(EVENT) - 1, NULL, 10);
75                         setCounters(new MaliVideoCounter(getCounters(), strdup(counter), MVCT_EVENT, i));
76                 } else if (strncmp(counter, ACTIVITY, sizeof(ACTIVITY) - 1) == 0) {
77                         const int i = strtol(counter + sizeof(ACTIVITY) - 1, NULL, 10);
78                         setCounters(new MaliVideoCounter(getCounters(), strdup(counter), MVCT_ACTIVITY, i));
79                 }
80         }
81 }
82
83 int MaliVideoDriver::writeCounters(mxml_node_t *root) const {
84         if (access("/dev/mv500", F_OK) != 0) {
85                 return 0;
86         }
87
88         return super::writeCounters(root);
89 }
90
91 void MaliVideoDriver::marshalEnable(const MaliVideoCounterType type, char *const buf, const size_t bufsize, int &pos) {
92         // size
93         int numEnabled = 0;
94         for (MaliVideoCounter *counter = static_cast<MaliVideoCounter *>(getCounters()); counter != NULL; counter = static_cast<MaliVideoCounter *>(counter->getNext())) {
95                 if (counter->isEnabled() && (counter->getType() == type)) {
96                         ++numEnabled;
97                 }
98         }
99         Buffer::packInt(buf, bufsize, pos, numEnabled*sizeof(uint32_t));
100         for (MaliVideoCounter *counter = static_cast<MaliVideoCounter *>(getCounters()); counter != NULL; counter = static_cast<MaliVideoCounter *>(counter->getNext())) {
101                 if (counter->isEnabled() && (counter->getType() == type)) {
102                         Buffer::packInt(buf, bufsize, pos, counter->getId());
103                 }
104         }
105 }
106
107 static bool writeAll(const int mveUds, const char *const buf, const int pos) {
108         int written = 0;
109         while (written < pos) {
110                 size_t bytes = ::write(mveUds, buf + written, pos - written);
111                 if (bytes <= 0) {
112                         logg->logMessage("%s(%s:%i): write failed", __FUNCTION__, __FILE__, __LINE__);
113                         return false;
114                 }
115                 written += bytes;
116         }
117
118         return true;
119 }
120
121 bool MaliVideoDriver::start(const int mveUds) {
122         char buf[256];
123         int pos = 0;
124
125         // code - MVE_INSTR_STARTUP
126         buf[pos++] = 'C';
127         buf[pos++] = 'L';
128         buf[pos++] = 'N';
129         buf[pos++] = 'T';
130         // size
131         Buffer::packInt(buf, sizeof(buf), pos, sizeof(uint32_t));
132         // client_version_number
133         Buffer::packInt(buf, sizeof(buf), pos, 1);
134
135         // code - MVE_INSTR_CONFIGURE
136         buf[pos++] = 'C';
137         buf[pos++] = 'N';
138         buf[pos++] = 'F';
139         buf[pos++] = 'G';
140         // size
141         Buffer::packInt(buf, sizeof(buf), pos, 5*sizeof(uint32_t));
142         // configuration
143         Buffer::packInt(buf, sizeof(buf), pos, MVE_INSTR_COUNTERS | MVE_INSTR_EVENTS | MVE_INSTR_ACTIVITIES | MVE_INSTR_PACKED_COMM);
144         // communication_protocol_version
145         Buffer::packInt(buf, sizeof(buf), pos, 1);
146         // data_protocol_version
147         Buffer::packInt(buf, sizeof(buf), pos, 1);
148         // sample_rate - convert samples/second to ms/sample
149         Buffer::packInt(buf, sizeof(buf), pos, 1000/gSessionData->mSampleRate);
150         // live_rate - convert ns/flush to ms/flush
151         Buffer::packInt(buf, sizeof(buf), pos, gSessionData->mLiveRate/1000000);
152
153         // code - MVE_INSTR_ENABLE_COUNTERS
154         buf[pos++] = 'C';
155         buf[pos++] = 'F';
156         buf[pos++] = 'G';
157         buf[pos++] = 'c';
158         marshalEnable(MVCT_COUNTER, buf, sizeof(buf), pos);
159
160         // code - MVE_INSTR_ENABLE_EVENTS
161         buf[pos++] = 'C';
162         buf[pos++] = 'F';
163         buf[pos++] = 'G';
164         buf[pos++] = 'e';
165         marshalEnable(MVCT_EVENT, buf, sizeof(buf), pos);
166
167         // code - MVE_INSTR_ENABLE_ACTIVITIES
168         buf[pos++] = 'C';
169         buf[pos++] = 'F';
170         buf[pos++] = 'G';
171         buf[pos++] = 'a';
172         marshalEnable(MVCT_ACTIVITY, buf, sizeof(buf), pos);
173
174         return writeAll(mveUds, buf, pos);
175 }
176
177 void MaliVideoDriver::stop(const int mveUds) {
178         char buf[8];
179         int pos = 0;
180
181         // code - MVE_INSTR_STOP
182         buf[pos++] = 'S';
183         buf[pos++] = 'T';
184         buf[pos++] = 'O';
185         buf[pos++] = 'P';
186         marshalEnable(MVCT_COUNTER, buf, sizeof(buf), pos);
187
188         writeAll(mveUds, buf, pos);
189
190         close(mveUds);
191 }